blob: c59725a55fd3c5fe2728095c956a48b3c93d5829 [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;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053025import org.onosproject.yms.app.yob.exception.YobException;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053026import org.onosproject.yms.app.ysr.YangSchemaRegistry;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.lang.reflect.Field;
31import java.lang.reflect.InvocationTargetException;
32import java.lang.reflect.Method;
33import java.lang.reflect.ParameterizedType;
34import java.util.Set;
35
36import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
37import static org.onosproject.yms.app.ydt.AppType.YOB;
38import static org.onosproject.yms.app.yob.YobConstants.ADD_TO;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053039import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_INVOKE_METHOD;
40import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_INVOKE_METHOD;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053041
42/**
43 * Represents a multi instance leaf node handler in YANG object builder.
44 */
45class YobMultiInstanceLeafHandler
46 extends YobHandler {
47
48 private static final Logger log =
49 LoggerFactory.getLogger(YobMultiInstanceLeafHandler.class);
50
51 @Override
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053052 public void createBuilder(YdtExtendedContext curNode,
53 YdtExtendedContext rootNode,
54 YangSchemaRegistry registry) {
Rama-Huaweib711e5c2016-08-31 07:55:46 +053055 // For multi instance leaf no need to create an object.
56 }
57
58 @Override
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053059 public void buildObject(YdtExtendedContext ydtNode,
60 YdtExtendedContext ydtRootNode,
61 YangSchemaRegistry schemaRegistry) {
Rama-Huaweib711e5c2016-08-31 07:55:46 +053062 // For multi instance leaf no need to build object.
63 }
64
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053065 /**
66 * Set the leaf list values in the YANG object.
67 *
68 * @param leafListNode leaf list YDT node
69 * @param schemaRegistry YANG schema registry
70 * @throws YobException if failed to invoke the leaf list's setter
71 */
Rama-Huaweib711e5c2016-08-31 07:55:46 +053072 @Override
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053073 public void setInParent(YdtExtendedContext leafListNode,
74 YangSchemaRegistry schemaRegistry) {
Rama-Huaweib711e5c2016-08-31 07:55:46 +053075 Class<?> parentBuilderClass = null;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053076 YangSchemaNode yangSchemaNode = leafListNode.getYangSchemaNode();
Rama-Huaweib711e5c2016-08-31 07:55:46 +053077 YdtExtendedContext parentYdtNode =
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053078 (YdtExtendedContext) leafListNode.getParent();
Rama-Huaweib711e5c2016-08-31 07:55:46 +053079 YobWorkBench parentYobWorkBench =
80 (YobWorkBench) parentYdtNode.getAppInfo(YOB);
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053081 Set<String> valueSet = leafListNode.getValueSet();
Rama-Huaweib711e5c2016-08-31 07:55:46 +053082
83 for (String value : valueSet) {
84 try {
85 String setterInParent = yangSchemaNode.getJavaAttributeName();
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053086 Object builderObject = parentYobWorkBench
87 .getParentBuilder(leafListNode, schemaRegistry);
88 parentBuilderClass = builderObject.getClass();
Rama-Huaweib711e5c2016-08-31 07:55:46 +053089 Field leafName = parentBuilderClass
90 .getDeclaredField(setterInParent);
91 ParameterizedType genericListType =
92 (ParameterizedType) leafName.getGenericType();
93 Class<?> genericListClass =
94 (Class<?>) genericListType.getActualTypeArguments()[0];
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053095 Method setterMethod =
Rama-Huaweib711e5c2016-08-31 07:55:46 +053096 parentBuilderClass.getDeclaredMethod(
97 ADD_TO + getCapitalCase(setterInParent),
98 genericListClass);
99 JavaQualifiedTypeInfoContainer javaQualifiedType =
100 (JavaQualifiedTypeInfoContainer) yangSchemaNode;
101 YangType<?> yangType =
102 ((YangLeafList) javaQualifiedType).getDataType();
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530103 YobUtils.setDataFromStringValue(yangType, value, setterMethod,
104 builderObject, leafListNode);
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530105 } catch (NoSuchMethodException | InvocationTargetException
106 | IllegalAccessException | NoSuchFieldException e) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530107 log.error(L_FAIL_TO_INVOKE_METHOD,
108 parentBuilderClass.getName());
109 throw new YobException(E_FAIL_TO_INVOKE_METHOD +
110 parentBuilderClass.getName());
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530111 }
112 }
113 }
114}