Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +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.yangutils.datamodel.utils; |
| 18 | |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 19 | import java.io.FileInputStream; |
| 20 | import java.io.IOException; |
| 21 | import java.io.ObjectInputStream; |
| 22 | import java.util.ArrayList; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame^] | 23 | import java.util.LinkedList; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 24 | import java.util.List; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 25 | import java.util.Set; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 26 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.ResolvableType; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangIfFeature; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangAugment; |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangBase; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame^] | 32 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.datamodel.YangIdentityRef; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 35 | import org.onosproject.yangutils.datamodel.YangLeafList; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 38 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
| 40 | import org.onosproject.yangutils.datamodel.YangResolutionInfo; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 41 | import org.onosproject.yangutils.datamodel.YangRpc; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 42 | import org.onosproject.yangutils.datamodel.YangType; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame^] | 43 | import org.onosproject.yangutils.datamodel.YangUnion; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 44 | import org.onosproject.yangutils.datamodel.YangUses; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 45 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame^] | 46 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 47 | |
| 48 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 49 | * Represents utilities for data model tree. |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 50 | */ |
| 51 | public final class DataModelUtils { |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 52 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 53 | /** |
| 54 | * Creates a new data model tree utility. |
| 55 | */ |
| 56 | private DataModelUtils() { |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Detects the colliding identifier name in a given YANG node and its child. |
| 61 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 62 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 63 | * @param dataType type of YANG node asking for detecting collision |
| 64 | * @param node instance of calling node |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 65 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 66 | */ |
| 67 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 68 | throws DataModelException { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 69 | if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) { |
| 70 | detectCollidingForUsesGrouping(identifierName, dataType, node); |
| 71 | } else { |
| 72 | if (node instanceof YangLeavesHolder) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 73 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 74 | detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName); |
| 75 | detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName); |
| 76 | } |
| 77 | node = node.getChild(); |
| 78 | while (node != null) { |
| 79 | Parsable parsable = (Parsable) node; |
| 80 | if (node instanceof CollisionDetector |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 81 | && parsable.getYangConstructType() != YangConstructType.USES_DATA |
| 82 | && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 83 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 84 | } |
| 85 | node = node.getNextSibling(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 86 | } |
| 87 | } |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Detects colliding of uses and grouping only with uses and grouping respectively. |
| 92 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 93 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 94 | * @param dataType type of YANG node asking for detecting collision |
| 95 | * @param node node instance of calling node |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 96 | * @throws DataModelException a violation of data model rules |
| 97 | */ |
| 98 | public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node) |
| 99 | throws DataModelException { |
| 100 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 101 | node = node.getChild(); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 102 | while (node != null) { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 103 | Parsable parsable = (Parsable) node; |
| 104 | if (node instanceof CollisionDetector |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 105 | && parsable.getYangConstructType() == dataType) { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 106 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 107 | } |
| 108 | node = node.getNextSibling(); |
| 109 | } |
| 110 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Detects the colliding identifier name in a given leaf node. |
| 114 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 115 | * @param listOfLeaf List of leaves to detect collision |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 116 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 117 | * @throws DataModelException a violation of data model rules |
| 118 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 119 | private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 120 | throws DataModelException { |
| 121 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 122 | if (listOfLeaf == null) { |
| 123 | return; |
| 124 | } |
| 125 | for (YangLeaf leaf : listOfLeaf) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 126 | if (leaf.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 127 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \"" |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 128 | + leaf.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Detects the colliding identifier name in a given leaf-list node. |
| 135 | * |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 136 | * @param listOfLeafList list of leaf-lists to detect collision |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 137 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 138 | * @throws DataModelException a violation of data model rules |
| 139 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 140 | private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 141 | throws DataModelException { |
| 142 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 143 | if (listOfLeafList == null) { |
| 144 | return; |
| 145 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 146 | for (YangLeafList leafList : listOfLeafList) { |
| 147 | if (leafList.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 148 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " + |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 149 | "list \"" + leafList.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Add a resolution information. |
| 156 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 157 | * @param resolutionInfo information about the YANG construct which has to be resolved |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 158 | * @throws DataModelException a violation of data model rules |
| 159 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 160 | public static void addResolutionInfo(YangResolutionInfo resolutionInfo) |
| 161 | throws DataModelException { |
| 162 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 163 | /* get the module node to add maintain the list of nested reference */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 164 | YangNode curNode = resolutionInfo.getEntityToResolveInfo() |
| 165 | .getHolderOfEntityToResolve(); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 166 | while (!(curNode instanceof YangReferenceResolver)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 167 | curNode = curNode.getParent(); |
| 168 | if (curNode == null) { |
| 169 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 170 | } |
| 171 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 172 | YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode; |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 173 | |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 174 | if (resolutionInfo.getEntityToResolveInfo() |
| 175 | .getEntityToResolve() instanceof YangType) { |
| 176 | resolutionNode.addToResolutionList(resolutionInfo, |
| 177 | ResolvableType.YANG_DERIVED_DATA_TYPE); |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 178 | } else if (resolutionInfo.getEntityToResolveInfo() |
| 179 | .getEntityToResolve() instanceof YangUses) { |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 180 | resolutionNode.addToResolutionList(resolutionInfo, |
| 181 | ResolvableType.YANG_USES); |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 182 | } else if (resolutionInfo.getEntityToResolveInfo() |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 183 | .getEntityToResolve() instanceof YangAugment) { |
| 184 | resolutionNode.addToResolutionList(resolutionInfo, |
| 185 | ResolvableType.YANG_AUGMENT); |
| 186 | } else if (resolutionInfo.getEntityToResolveInfo() |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 187 | .getEntityToResolve() instanceof YangIfFeature) { |
| 188 | resolutionNode.addToResolutionList(resolutionInfo, |
| 189 | ResolvableType.YANG_IF_FEATURE); |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 190 | } else if (resolutionInfo.getEntityToResolveInfo() |
| 191 | .getEntityToResolve() instanceof YangLeafRef) { |
| 192 | resolutionNode.addToResolutionList(resolutionInfo, |
| 193 | ResolvableType.YANG_LEAFREF); |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 194 | } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangBase) { |
| 195 | resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_BASE); |
| 196 | } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangIdentityRef) { |
| 197 | resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_IDENTITYREF); |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 198 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 199 | } |
| 200 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 201 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 202 | * Resolve linking for a resolution list. |
| 203 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 204 | * @param resolutionList resolution list for which linking to be done |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 205 | * @param dataModelRootNode module/sub-module node |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 206 | * @throws DataModelException a violation of data model rules |
| 207 | */ |
| 208 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 209 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 210 | throws DataModelException { |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 211 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 212 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 213 | resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Links type/uses referring to typedef/uses of inter YANG file. |
| 219 | * |
| 220 | * @param resolutionList resolution list for which linking to be done |
| 221 | * @param dataModelRootNode module/sub-module node |
| 222 | * @throws DataModelException a violation of data model rules |
| 223 | */ |
| 224 | public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList, |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 225 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 226 | throws DataModelException { |
| 227 | /* |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 228 | * Run through the resolution list, find type/uses referring to inter |
| 229 | * file typedef/grouping, ask for linking. |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 230 | */ |
| 231 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
| 232 | resolutionInfo.linkInterFile(dataModelRootNode); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 233 | } |
| 234 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 235 | |
| 236 | /** |
| 237 | * Checks if there is any rpc defined in the module or sub-module. |
| 238 | * |
| 239 | * @param rootNode root node of the data model |
| 240 | * @return status of rpc's existence |
| 241 | */ |
| 242 | public static boolean isRpcChildNodePresent(YangNode rootNode) { |
| 243 | YangNode childNode = rootNode.getChild(); |
| 244 | while (childNode != null) { |
| 245 | if (childNode instanceof YangRpc) { |
| 246 | return true; |
| 247 | } |
| 248 | childNode = childNode.getNextSibling(); |
| 249 | } |
| 250 | return false; |
| 251 | } |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 252 | |
| 253 | /** |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 254 | * Returns referred node in a given set. |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 255 | * |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 256 | * @param yangNodeSet YANG node set |
| 257 | * @param refNodeName name of the node which is referred |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 258 | * @return referred node's reference |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 259 | */ |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 260 | public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 261 | /* |
| 262 | * Run through the YANG files to see which YANG file matches the |
| 263 | * referred node name. |
| 264 | */ |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 265 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 266 | if (yangNode.getName().equals(refNodeName)) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 267 | return yangNode; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 268 | } |
| 269 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 270 | return null; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 271 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 272 | |
| 273 | /** |
| 274 | * Returns the contained data model parent node. |
| 275 | * |
| 276 | * @param currentNode current node which parent contained node is required |
| 277 | * @return parent node in which the current node is an attribute |
| 278 | */ |
| 279 | public static YangNode getParentNodeInGenCode(YangNode currentNode) { |
| 280 | |
| 281 | /* |
| 282 | * TODO: recursive parent lookup to support choice/augment/uses. TODO: |
| 283 | * need to check if this needs to be updated for |
| 284 | * choice/case/augment/grouping |
| 285 | */ |
| 286 | return currentNode.getParent(); |
| 287 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * Returns de-serializes YANG data-model nodes. |
| 291 | * |
| 292 | * @param serializableInfoSet YANG file info set |
| 293 | * @return de-serializes YANG data-model nodes |
| 294 | * @throws IOException when fails do IO operations |
| 295 | */ |
| 296 | public static List<YangNode> deSerializeDataModel(List<String> serializableInfoSet) throws IOException { |
| 297 | |
| 298 | List<YangNode> nodes = new ArrayList<>(); |
| 299 | for (String fileInfo : serializableInfoSet) { |
| 300 | YangNode node = null; |
| 301 | try { |
| 302 | FileInputStream fileInputStream = new FileInputStream(fileInfo); |
| 303 | ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); |
| 304 | node = (YangNode) objectInputStream.readObject(); |
| 305 | nodes.add(node); |
| 306 | objectInputStream.close(); |
| 307 | fileInputStream.close(); |
| 308 | } catch (IOException | ClassNotFoundException e) { |
| 309 | throw new IOException(fileInfo + " not found."); |
| 310 | } |
| 311 | } |
| 312 | return nodes; |
| 313 | } |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame^] | 314 | |
| 315 | /** |
| 316 | * Clones the list of leaves and list of leaf list in the leaves holder. |
| 317 | * |
| 318 | * @param leavesHolder YANG node potentially containing leaves or leaf lists |
| 319 | * @throws CloneNotSupportedException clone is not supported |
| 320 | * @throws DataModelException data model error |
| 321 | */ |
| 322 | public static void cloneLeaves(YangLeavesHolder leavesHolder) |
| 323 | throws CloneNotSupportedException, DataModelException { |
| 324 | List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf(); |
| 325 | if (currentListOfLeaves != null) { |
| 326 | List<YangLeaf> clonedLeavesList = new LinkedList<YangLeaf>(); |
| 327 | for (YangLeaf leaf : currentListOfLeaves) { |
| 328 | YangLeaf clonedLeaf = leaf.clone(); |
| 329 | clonedLeaf.setContainedIn(leavesHolder); |
| 330 | clonedLeavesList.add(clonedLeaf); |
| 331 | } |
| 332 | leavesHolder.setListOfLeaf(clonedLeavesList); |
| 333 | } |
| 334 | |
| 335 | List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList(); |
| 336 | if (currentListOfLeafList != null) { |
| 337 | List<YangLeafList> clonedListOfLeafList = new LinkedList<YangLeafList>(); |
| 338 | for (YangLeafList leafList : currentListOfLeafList) { |
| 339 | YangLeafList clonedLeafList = leafList.clone(); |
| 340 | clonedLeafList.setContainedIn(leavesHolder); |
| 341 | clonedListOfLeafList.add(clonedLeafList); |
| 342 | } |
| 343 | leavesHolder.setListOfLeafList(clonedListOfLeafList); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Clones the union or enum leaves. If there is any cloned leaves whose type is union/enum then the corresponding |
| 349 | * type info needs to be updated to the cloned new type node. |
| 350 | * |
| 351 | * @param leavesHolder cloned leaves holder, for whom the leaves reference needs to be updated |
| 352 | */ |
| 353 | public static void updateClonedLeavesUnionEnumRef(YangLeavesHolder leavesHolder) throws DataModelException { |
| 354 | List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf(); |
| 355 | if (currentListOfLeaves != null) { |
| 356 | for (YangLeaf leaf : currentListOfLeaves) { |
| 357 | if (leaf.getDataType().getDataType() == YangDataTypes.ENUMERATION |
| 358 | || leaf.getDataType().getDataType() == YangDataTypes.UNION) { |
| 359 | try { |
| 360 | updateClonedTypeRef(leaf.getDataType(), leavesHolder); |
| 361 | } catch (DataModelException e) { |
| 362 | throw e; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | } |
| 368 | |
| 369 | List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList(); |
| 370 | if (currentListOfLeafList != null) { |
| 371 | for (YangLeafList leafList : currentListOfLeafList) { |
| 372 | if (leafList.getDataType().getDataType() == YangDataTypes.ENUMERATION |
| 373 | || leafList.getDataType().getDataType() == YangDataTypes.UNION) { |
| 374 | try { |
| 375 | updateClonedTypeRef(leafList.getDataType(), leavesHolder); |
| 376 | } catch (DataModelException e) { |
| 377 | throw e; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Updates the types extended info pointer to point to the cloned type node. |
| 386 | * |
| 387 | * @param dataType data type, whose extended info needs to be pointed to the cloned type |
| 388 | * @param leavesHolder the leaves holder having the cloned type |
| 389 | */ |
| 390 | private static void updateClonedTypeRef(YangType dataType, YangLeavesHolder leavesHolder) |
| 391 | throws DataModelException { |
| 392 | if (!(leavesHolder instanceof YangNode)) { |
| 393 | throw new DataModelException("Data model error: cloned leaves holder is not a node"); |
| 394 | } |
| 395 | YangNode potentialTypeNode = ((YangNode) leavesHolder).getChild(); |
| 396 | while (potentialTypeNode != null) { |
| 397 | String dataTypeName = null; |
| 398 | if (dataType.getDataType() == YangDataTypes.ENUMERATION) { |
| 399 | YangEnumeration enumNode = (YangEnumeration) dataType.getDataTypeExtendedInfo(); |
| 400 | dataTypeName = enumNode.getName(); |
| 401 | } else if (dataType.getDataType() == YangDataTypes.UNION) { |
| 402 | YangUnion unionNode = (YangUnion) dataType.getDataTypeExtendedInfo(); |
| 403 | dataTypeName = unionNode.getName(); |
| 404 | } |
| 405 | if (potentialTypeNode.getName().contentEquals(dataTypeName)) { |
| 406 | dataType.setDataTypeExtendedInfo((Object) potentialTypeNode); |
| 407 | return; |
| 408 | } |
| 409 | potentialTypeNode = potentialTypeNode.getNextSibling(); |
| 410 | } |
| 411 | |
| 412 | throw new DataModelException("Data model error: cloned leaves type is not found"); |
| 413 | } |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 414 | } |