blob: 9ad458b110c89a601e18951d46ef4b8f7a9bb1d6 [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
19
20import org.onosproject.yangutils.datamodel.YangLeafList;
21import org.onosproject.yangutils.datamodel.YangSchemaNode;
22import org.onosproject.yangutils.datamodel.YangType;
23import org.onosproject.yangutils.datamodel.javadatamodel.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;
32import java.lang.reflect.ParameterizedType;
33import java.util.Set;
34
35import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
36import static org.onosproject.yms.app.ydt.AppType.YOB;
37import static org.onosproject.yms.app.yob.YobConstants.ADD_TO;
38import static org.onosproject.yms.app.yob.YobConstants.FAIL_TO_INVOKE_METHOD;
39
40/**
41 * Represents a multi instance leaf node handler in YANG object builder.
42 */
43class YobMultiInstanceLeafHandler
44 extends YobHandler {
45
46 private static final Logger log =
47 LoggerFactory.getLogger(YobMultiInstanceLeafHandler.class);
48
49 @Override
50 public void createYangBuilderObject(YdtExtendedContext curYdtNode,
51 YdtExtendedContext rootYdtNode,
52 YangSchemaRegistry registry) {
53 // For multi instance leaf no need to create an object.
54 }
55
56 @Override
57 public void buildObjectFromBuilder(YdtExtendedContext ydtNode,
58 YdtExtendedContext ydtRootNode,
59 YangSchemaRegistry schemaRegistry) {
60 // For multi instance leaf no need to build object.
61 }
62
63 @Override
64 public void setObjectInParent(YdtExtendedContext leafListYdtNode,
65 YangSchemaRegistry schemaRegistry) {
66 Class<?> parentBuilderClass = null;
67 YangSchemaNode yangSchemaNode = leafListYdtNode.getYangSchemaNode();
68 YdtExtendedContext parentYdtNode =
69 (YdtExtendedContext) leafListYdtNode.getParent();
70 YobWorkBench parentYobWorkBench =
71 (YobWorkBench) parentYdtNode.getAppInfo(YOB);
72 Set<String> valueSet = leafListYdtNode.getValueSet();
73
74 for (String value : valueSet) {
75 try {
76 String setterInParent = yangSchemaNode.getJavaAttributeName();
77 Object parentBuilderObject = parentYobWorkBench
78 .getParentBuilder(leafListYdtNode, schemaRegistry);
79 parentBuilderClass = parentBuilderObject.getClass();
80 Field leafName = parentBuilderClass
81 .getDeclaredField(setterInParent);
82 ParameterizedType genericListType =
83 (ParameterizedType) leafName.getGenericType();
84 Class<?> genericListClass =
85 (Class<?>) genericListType.getActualTypeArguments()[0];
86 Method parentSetterMethod =
87 parentBuilderClass.getDeclaredMethod(
88 ADD_TO + getCapitalCase(setterInParent),
89 genericListClass);
90 JavaQualifiedTypeInfoContainer javaQualifiedType =
91 (JavaQualifiedTypeInfoContainer) yangSchemaNode;
92 YangType<?> yangType =
93 ((YangLeafList) javaQualifiedType).getDataType();
94 setDataFromStringValue(yangType, value, parentSetterMethod,
95 parentBuilderObject, leafListYdtNode);
96 } catch (NoSuchMethodException | InvocationTargetException
97 | IllegalAccessException | NoSuchFieldException e) {
98 log.error(FAIL_TO_INVOKE_METHOD + parentBuilderClass.getName());
99 }
100 }
101 }
102}