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 | |
| 36 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 37 | import static org.onosproject.yms.app.ydt.AppType.YOB; |
| 38 | import static org.onosproject.yms.app.yob.YobConstants.ADD_TO; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 39 | import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_INVOKE_METHOD; |
| 40 | 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] | 41 | |
| 42 | /** |
| 43 | * Represents a multi instance leaf node handler in YANG object builder. |
| 44 | */ |
| 45 | class YobMultiInstanceLeafHandler |
| 46 | extends YobHandler { |
| 47 | |
| 48 | private static final Logger log = |
| 49 | LoggerFactory.getLogger(YobMultiInstanceLeafHandler.class); |
| 50 | |
| 51 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 52 | public void createBuilder(YdtExtendedContext curNode, |
| 53 | YdtExtendedContext rootNode, |
| 54 | YangSchemaRegistry registry) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 55 | // For multi instance leaf no need to create an object. |
| 56 | } |
| 57 | |
| 58 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 59 | public void buildObject(YdtExtendedContext ydtNode, |
| 60 | YdtExtendedContext ydtRootNode, |
| 61 | YangSchemaRegistry schemaRegistry) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 62 | // For multi instance leaf no need to build object. |
| 63 | } |
| 64 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 65 | /** |
| 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-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 72 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 73 | public void setInParent(YdtExtendedContext leafListNode, |
| 74 | YangSchemaRegistry schemaRegistry) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 75 | Class<?> parentBuilderClass = null; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 76 | YangSchemaNode yangSchemaNode = leafListNode.getYangSchemaNode(); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 77 | YdtExtendedContext parentYdtNode = |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 78 | (YdtExtendedContext) leafListNode.getParent(); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 79 | YobWorkBench parentYobWorkBench = |
| 80 | (YobWorkBench) parentYdtNode.getAppInfo(YOB); |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 81 | Set<String> valueSet = leafListNode.getValueSet(); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 82 | |
| 83 | for (String value : valueSet) { |
| 84 | try { |
| 85 | String setterInParent = yangSchemaNode.getJavaAttributeName(); |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 86 | Object builderObject = parentYobWorkBench |
| 87 | .getParentBuilder(leafListNode, schemaRegistry); |
| 88 | parentBuilderClass = builderObject.getClass(); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 89 | Field leafName = parentBuilderClass |
| 90 | .getDeclaredField(setterInParent); |
| 91 | ParameterizedType genericListType = |
| 92 | (ParameterizedType) leafName.getGenericType(); |
| 93 | Class<?> genericListClass = |
| 94 | (Class<?>) genericListType.getActualTypeArguments()[0]; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 95 | Method setterMethod = |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 96 | parentBuilderClass.getDeclaredMethod( |
| 97 | ADD_TO + getCapitalCase(setterInParent), |
| 98 | genericListClass); |
| 99 | JavaQualifiedTypeInfoContainer javaQualifiedType = |
| 100 | (JavaQualifiedTypeInfoContainer) yangSchemaNode; |
| 101 | YangType<?> yangType = |
| 102 | ((YangLeafList) javaQualifiedType).getDataType(); |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 103 | YobUtils.setDataFromStringValue(yangType, value, setterMethod, |
| 104 | builderObject, leafListNode); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 105 | } catch (NoSuchMethodException | InvocationTargetException |
| 106 | | IllegalAccessException | NoSuchFieldException e) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame^] | 107 | log.error(L_FAIL_TO_INVOKE_METHOD, |
| 108 | parentBuilderClass.getName()); |
| 109 | throw new YobException(E_FAIL_TO_INVOKE_METHOD + |
| 110 | parentBuilderClass.getName()); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |