Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yms.app.yob; |
| 18 | |
| 19 | |
| 20 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 21 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 22 | import org.onosproject.yangutils.datamodel.YangType; |
| 23 | import org.onosproject.yangutils.datamodel.javadatamodel.JavaQualifiedTypeInfoContainer; |
| 24 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 25 | import org.onosproject.yms.app.yob.exception.YobException; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 26 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.lang.reflect.Field; |
| 31 | import java.lang.reflect.InvocationTargetException; |
| 32 | import java.lang.reflect.Method; |
| 33 | import java.lang.reflect.ParameterizedType; |
| 34 | import java.util.Set; |
| 35 | |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.IDENTITYREF; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 38 | import static org.onosproject.yms.app.ydt.AppType.YOB; |
| 39 | import static org.onosproject.yms.app.yob.YobConstants.ADD_TO; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 40 | import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_INVOKE_METHOD; |
| 41 | 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] | 42 | |
| 43 | /** |
| 44 | * Represents a multi instance leaf node handler in YANG object builder. |
| 45 | */ |
| 46 | class YobMultiInstanceLeafHandler |
| 47 | extends YobHandler { |
| 48 | |
| 49 | private static final Logger log = |
| 50 | LoggerFactory.getLogger(YobMultiInstanceLeafHandler.class); |
| 51 | |
| 52 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 53 | public void createBuilder(YdtExtendedContext curNode, |
| 54 | YdtExtendedContext rootNode, |
| 55 | YangSchemaRegistry registry) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 56 | // For multi instance leaf no need to create an object. |
| 57 | } |
| 58 | |
| 59 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 60 | public void buildObject(YdtExtendedContext ydtNode, |
| 61 | YdtExtendedContext ydtRootNode, |
| 62 | YangSchemaRegistry schemaRegistry) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 63 | // For multi instance leaf no need to build object. |
| 64 | } |
| 65 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 66 | /** |
| 67 | * Set the leaf list values in the YANG object. |
| 68 | * |
| 69 | * @param leafListNode leaf list YDT node |
| 70 | * @param schemaRegistry YANG schema registry |
| 71 | * @throws YobException if failed to invoke the leaf list's setter |
| 72 | */ |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 73 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 74 | public void setInParent(YdtExtendedContext leafListNode, |
| 75 | YangSchemaRegistry schemaRegistry) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 76 | Class<?> parentBuilderClass = null; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 77 | YangSchemaNode yangSchemaNode = leafListNode.getYangSchemaNode(); |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 78 | while (yangSchemaNode.getReferredSchema() != null) { |
| 79 | yangSchemaNode = yangSchemaNode.getReferredSchema(); |
| 80 | } |
| 81 | |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 82 | YdtExtendedContext parentYdtNode = |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 83 | (YdtExtendedContext) leafListNode.getParent(); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 84 | YobWorkBench parentYobWorkBench = |
| 85 | (YobWorkBench) parentYdtNode.getAppInfo(YOB); |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 86 | Set<String> valueSet = leafListNode.getValueSet(); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 87 | |
| 88 | for (String value : valueSet) { |
| 89 | try { |
| 90 | String setterInParent = yangSchemaNode.getJavaAttributeName(); |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 91 | Object builderObject = parentYobWorkBench |
| 92 | .getParentBuilder(leafListNode, schemaRegistry); |
| 93 | parentBuilderClass = builderObject.getClass(); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 94 | Field leafName = parentBuilderClass |
| 95 | .getDeclaredField(setterInParent); |
| 96 | ParameterizedType genericListType = |
| 97 | (ParameterizedType) leafName.getGenericType(); |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 98 | Class<?> genericListClass; |
| 99 | if (((YangLeafList) leafListNode.getYangSchemaNode()) |
| 100 | .getDataType().getDataType() == IDENTITYREF) { |
| 101 | ParameterizedType type = (ParameterizedType) |
| 102 | genericListType.getActualTypeArguments()[0]; |
| 103 | genericListClass = type.getClass().getClass(); |
| 104 | } else { |
| 105 | genericListClass = (Class<?>) genericListType.getActualTypeArguments()[0]; |
| 106 | } |
| 107 | |
| 108 | Method setterMethod = parentBuilderClass.getDeclaredMethod( |
| 109 | ADD_TO + getCapitalCase(setterInParent), genericListClass); |
| 110 | |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 111 | JavaQualifiedTypeInfoContainer javaQualifiedType = |
| 112 | (JavaQualifiedTypeInfoContainer) yangSchemaNode; |
| 113 | YangType<?> yangType = |
| 114 | ((YangLeafList) javaQualifiedType).getDataType(); |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 115 | YobUtils.setDataFromStringValue(yangType.getDataType(), value, |
| 116 | setterMethod, |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 117 | builderObject, leafListNode); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 118 | } catch (NoSuchMethodException | InvocationTargetException |
| 119 | | IllegalAccessException | NoSuchFieldException e) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 120 | log.error(L_FAIL_TO_INVOKE_METHOD, |
| 121 | parentBuilderClass.getName()); |
| 122 | throw new YobException(E_FAIL_TO_INVOKE_METHOD + |
| 123 | parentBuilderClass.getName()); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |