blob: 4d8736b93f33aa2370cd436e53b5edff441dd1c3 [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;
Vidyashree Rama6160be12016-11-24 13:43:31 +053022import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053023import org.onosproject.yms.app.ydt.YdtExtendedContext;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053024import org.onosproject.yms.app.yob.exception.YobException;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053025import 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;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053034import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_INVOKE_METHOD;
35import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_INVOKE_METHOD;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053036
37/**
38 * Represents a single instance leaf node handler in YANG object builder.
39 */
40class YobSingleInstanceLeafHandler extends YobHandler {
41
42 private static final Logger log =
43 LoggerFactory.getLogger(YobSingleInstanceLeafHandler.class);
44
45 @Override
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053046 public void createBuilder(YdtExtendedContext curNode,
47 YdtExtendedContext rootNode,
48 YangSchemaRegistry registry) {
Rama-Huaweib711e5c2016-08-31 07:55:46 +053049 // For single instance leaf no need to create an object.
50 }
51
52 @Override
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053053 public void buildObject(YdtExtendedContext ydtNode,
54 YdtExtendedContext ydtRootNode,
55 YangSchemaRegistry schemaRegistry) {
Rama-Huaweib711e5c2016-08-31 07:55:46 +053056 // For single instance leaf no need to build an object.
57 }
58
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053059 /**
60 * Set the leaf's value in the YANG object.
61 *
62 * @param leafNode leaf YDT node
63 * @param schemaRegistry YANG schema registry
64 * @throws YobException if failed to invoke the leaf's setter
65 */
Rama-Huaweib711e5c2016-08-31 07:55:46 +053066 @Override
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053067 public void setInParent(YdtExtendedContext leafNode,
68 YangSchemaRegistry schemaRegistry) {
69 Class<?> builderClass = null;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053070
71 try {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053072 YangSchemaNode schemaNode = leafNode.getYangSchemaNode();
Vidyashree Rama6160be12016-11-24 13:43:31 +053073 while (schemaNode.getReferredSchema() != null) {
74 schemaNode = schemaNode.getReferredSchema();
75 }
76
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053077 String setterInParent = schemaNode.getJavaAttributeName();
78 YdtExtendedContext parentNode =
79 (YdtExtendedContext) leafNode.getParent();
80 YobWorkBench workBench = (YobWorkBench) parentNode.getAppInfo(YOB);
81 Object builderObject = workBench
Rama-Huaweib711e5c2016-08-31 07:55:46 +053082 .getParentBuilder(leafNode, schemaRegistry);
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053083 builderClass = builderObject.getClass();
Vidyashree Rama6160be12016-11-24 13:43:31 +053084 if (leafNode.getValue() != null || ((YangLeaf) schemaNode)
85 .getDataType().getDataType() == YangDataTypes.EMPTY) {
86 Field leafName = builderClass.getDeclaredField(setterInParent);
87 Method setterMethod = builderClass
88 .getDeclaredMethod(setterInParent, leafName.getType());
89 YangType<?> yangType = ((YangLeaf) schemaNode).getDataType();
90 YobUtils.setDataFromStringValue(yangType.getDataType(), leafNode
91 .getValue(),
92 setterMethod, builderObject,
93 leafNode);
94 } else {
95 YobUtils.setSelectLeaf(builderClass, leafNode,
96 schemaRegistry, builderObject);
97 }
Rama-Huaweib711e5c2016-08-31 07:55:46 +053098 } catch (NoSuchMethodException | InvocationTargetException |
99 IllegalAccessException | NoSuchFieldException e) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530100 log.error(L_FAIL_TO_INVOKE_METHOD, builderClass.getName());
101 throw new YobException(E_FAIL_TO_INVOKE_METHOD +
102 builderClass.getName());
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530103 }
104 }
105}