Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yms.app.yob; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 20 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 21 | import org.onosproject.yangutils.datamodel.YangType; |
| 22 | import org.onosproject.yangutils.datamodel.javadatamodel |
| 23 | .JavaQualifiedTypeInfoContainer; |
| 24 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 25 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.lang.reflect.Field; |
| 30 | import java.lang.reflect.InvocationTargetException; |
| 31 | import java.lang.reflect.Method; |
| 32 | |
| 33 | import static org.onosproject.yms.app.ydt.AppType.YOB; |
| 34 | import static org.onosproject.yms.app.yob.YobConstants.FAIL_TO_INVOKE_METHOD; |
| 35 | |
| 36 | /** |
| 37 | * Represents a single instance leaf node handler in YANG object builder. |
| 38 | */ |
| 39 | class YobSingleInstanceLeafHandler extends YobHandler { |
| 40 | |
| 41 | private static final Logger log = |
| 42 | LoggerFactory.getLogger(YobSingleInstanceLeafHandler.class); |
| 43 | |
| 44 | @Override |
| 45 | public void createYangBuilderObject(YdtExtendedContext curYdtNode, |
| 46 | YdtExtendedContext rootYdtNode, |
| 47 | YangSchemaRegistry registry) { |
| 48 | // For single instance leaf no need to create an object. |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void buildObjectFromBuilder(YdtExtendedContext ydtNode, |
| 53 | YdtExtendedContext ydtRootNode, |
| 54 | YangSchemaRegistry schemaRegistry) { |
| 55 | // For single instance leaf no need to build an object. |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void setObjectInParent(YdtExtendedContext leafNode, |
| 60 | YangSchemaRegistry schemaRegistry) { |
| 61 | Class<?> parentBldrClass = null; |
| 62 | YangSchemaNode yangSchemaNode = leafNode.getYangSchemaNode(); |
| 63 | YdtExtendedContext parentYdtNode = |
| 64 | (YdtExtendedContext) leafNode.getParent(); |
| 65 | YobWorkBench parentYobWorkBench = |
| 66 | (YobWorkBench) parentYdtNode.getAppInfo(YOB); |
| 67 | String value = leafNode.getValue(); |
| 68 | |
| 69 | try { |
| 70 | String setterInParent = yangSchemaNode.getJavaAttributeName(); |
| 71 | Object parentBuilderObject = parentYobWorkBench |
| 72 | .getParentBuilder(leafNode, schemaRegistry); |
| 73 | parentBldrClass = parentBuilderObject.getClass(); |
| 74 | Field leafName = parentBldrClass.getDeclaredField(setterInParent); |
| 75 | Method parentSetterMethod = parentBldrClass |
| 76 | .getDeclaredMethod(setterInParent, leafName.getType()); |
| 77 | JavaQualifiedTypeInfoContainer javaQualifiedType = |
| 78 | (JavaQualifiedTypeInfoContainer) yangSchemaNode; |
| 79 | YangType<?> yangType = ((YangLeaf) javaQualifiedType).getDataType(); |
| 80 | setDataFromStringValue(yangType, value, parentSetterMethod, |
| 81 | parentBuilderObject, leafNode); |
| 82 | } catch (NoSuchMethodException | InvocationTargetException | |
| 83 | IllegalAccessException | NoSuchFieldException e) { |
| 84 | log.error(FAIL_TO_INVOKE_METHOD + parentBldrClass.getName()); |
| 85 | } |
| 86 | } |
| 87 | } |