blob: f6d86422db746384d6dc4c804fd6f44d95d3c220 [file] [log] [blame]
Rama-Huaweib711e5c2016-08-31 07:55:46 +05301/*
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
17package org.onosproject.yms.app.yob;
18
19import org.onosproject.yangutils.datamodel.YangLeaf;
20import org.onosproject.yangutils.datamodel.YangSchemaNode;
21import org.onosproject.yangutils.datamodel.YangType;
22import org.onosproject.yangutils.datamodel.javadatamodel
23 .JavaQualifiedTypeInfoContainer;
24import org.onosproject.yms.app.ydt.YdtExtendedContext;
25import org.onosproject.yms.app.ysr.YangSchemaRegistry;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.lang.reflect.Field;
30import java.lang.reflect.InvocationTargetException;
31import java.lang.reflect.Method;
32
33import static org.onosproject.yms.app.ydt.AppType.YOB;
34import 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 */
39class 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}