Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 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; |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 23 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 24 | import org.onosproject.yms.app.yob.exception.YobException; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 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; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 34 | import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_INVOKE_METHOD; |
| 35 | import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_INVOKE_METHOD; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Represents a single instance leaf node handler in YANG object builder. |
| 39 | */ |
| 40 | class YobSingleInstanceLeafHandler extends YobHandler { |
| 41 | |
| 42 | private static final Logger log = |
| 43 | LoggerFactory.getLogger(YobSingleInstanceLeafHandler.class); |
| 44 | |
| 45 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 46 | public void createBuilder(YdtExtendedContext curNode, |
| 47 | YdtExtendedContext rootNode, |
| 48 | YangSchemaRegistry registry) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 49 | // For single instance leaf no need to create an object. |
| 50 | } |
| 51 | |
| 52 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 53 | public void buildObject(YdtExtendedContext ydtNode, |
| 54 | YdtExtendedContext ydtRootNode, |
| 55 | YangSchemaRegistry schemaRegistry) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 56 | // For single instance leaf no need to build an object. |
| 57 | } |
| 58 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 59 | /** |
| 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-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 66 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 67 | public void setInParent(YdtExtendedContext leafNode, |
| 68 | YangSchemaRegistry schemaRegistry) { |
| 69 | Class<?> builderClass = null; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 70 | |
| 71 | try { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 72 | YangSchemaNode schemaNode = leafNode.getYangSchemaNode(); |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 73 | while (schemaNode.getReferredSchema() != null) { |
| 74 | schemaNode = schemaNode.getReferredSchema(); |
| 75 | } |
| 76 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 77 | String setterInParent = schemaNode.getJavaAttributeName(); |
| 78 | YdtExtendedContext parentNode = |
| 79 | (YdtExtendedContext) leafNode.getParent(); |
| 80 | YobWorkBench workBench = (YobWorkBench) parentNode.getAppInfo(YOB); |
| 81 | Object builderObject = workBench |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 82 | .getParentBuilder(leafNode, schemaRegistry); |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 83 | builderClass = builderObject.getClass(); |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 84 | 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-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 98 | } catch (NoSuchMethodException | InvocationTargetException | |
| 99 | IllegalAccessException | NoSuchFieldException e) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 100 | log.error(L_FAIL_TO_INVOKE_METHOD, builderClass.getName()); |
| 101 | throw new YobException(E_FAIL_TO_INVOKE_METHOD + |
| 102 | builderClass.getName()); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |