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 | |
| 19 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.ResolvableType; |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 21 | import org.onosproject.yangutils.datamodel.YangAtomicPath; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangAugment; |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangBase; |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 24 | import org.onosproject.yangutils.datamodel.YangEntityToResolveInfoImpl; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangIdentityRef; |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 27 | import org.onosproject.yangutils.datamodel.YangIfFeature; |
| 28 | import org.onosproject.yangutils.datamodel.YangImport; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 30 | import org.onosproject.yangutils.datamodel.YangLeafList; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 33 | import org.onosproject.yangutils.datamodel.YangModule; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
| 36 | import org.onosproject.yangutils.datamodel.YangResolutionInfo; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.datamodel.YangRpc; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.datamodel.YangType; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.datamodel.YangUnion; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 40 | import org.onosproject.yangutils.datamodel.YangUses; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 41 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 42 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 43 | |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 44 | import java.io.FileInputStream; |
| 45 | import java.io.IOException; |
| 46 | import java.io.ObjectInputStream; |
| 47 | import java.util.ArrayList; |
| 48 | import java.util.Iterator; |
| 49 | import java.util.LinkedList; |
| 50 | import java.util.List; |
| 51 | import java.util.Map; |
| 52 | import java.util.Set; |
| 53 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 54 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 55 | * Represents utilities for data model tree. |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 56 | */ |
| 57 | public final class DataModelUtils { |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 58 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 59 | /** |
| 60 | * Creates a new data model tree utility. |
| 61 | */ |
| 62 | private DataModelUtils() { |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Detects the colliding identifier name in a given YANG node and its child. |
| 67 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 68 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 69 | * @param dataType type of YANG node asking for detecting collision |
| 70 | * @param node instance of calling node |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 71 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 72 | */ |
| 73 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 74 | throws DataModelException { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 75 | if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) { |
| 76 | detectCollidingForUsesGrouping(identifierName, dataType, node); |
| 77 | } else { |
| 78 | if (node instanceof YangLeavesHolder) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 79 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 80 | detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName); |
| 81 | detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName); |
| 82 | } |
| 83 | node = node.getChild(); |
| 84 | while (node != null) { |
| 85 | Parsable parsable = (Parsable) node; |
| 86 | if (node instanceof CollisionDetector |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 87 | && parsable.getYangConstructType() != YangConstructType.USES_DATA |
| 88 | && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 89 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 90 | } |
| 91 | node = node.getNextSibling(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 92 | } |
| 93 | } |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Detects colliding of uses and grouping only with uses and grouping respectively. |
| 98 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 99 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 100 | * @param dataType type of YANG node asking for detecting collision |
| 101 | * @param node node instance of calling node |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 102 | * @throws DataModelException a violation of data model rules |
| 103 | */ |
| 104 | public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node) |
| 105 | throws DataModelException { |
| 106 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 107 | node = node.getChild(); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 108 | while (node != null) { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 109 | Parsable parsable = (Parsable) node; |
| 110 | if (node instanceof CollisionDetector |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 111 | && parsable.getYangConstructType() == dataType) { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 112 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 113 | } |
| 114 | node = node.getNextSibling(); |
| 115 | } |
| 116 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Detects the colliding identifier name in a given leaf node. |
| 120 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 121 | * @param listOfLeaf List of leaves to detect collision |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 122 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 123 | * @throws DataModelException a violation of data model rules |
| 124 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 125 | private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 126 | throws DataModelException { |
| 127 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 128 | if (listOfLeaf == null) { |
| 129 | return; |
| 130 | } |
| 131 | for (YangLeaf leaf : listOfLeaf) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 132 | if (leaf.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 133 | 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] | 134 | + leaf.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Detects the colliding identifier name in a given leaf-list node. |
| 141 | * |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 142 | * @param listOfLeafList list of leaf-lists to detect collision |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 143 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 144 | * @throws DataModelException a violation of data model rules |
| 145 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 146 | private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 147 | throws DataModelException { |
| 148 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 149 | if (listOfLeafList == null) { |
| 150 | return; |
| 151 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 152 | for (YangLeafList leafList : listOfLeafList) { |
| 153 | if (leafList.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 154 | 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] | 155 | "list \"" + leafList.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Add a resolution information. |
| 162 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 163 | * @param resolutionInfo information about the YANG construct which has to be resolved |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 164 | * @throws DataModelException a violation of data model rules |
| 165 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 166 | public static void addResolutionInfo(YangResolutionInfo resolutionInfo) |
| 167 | throws DataModelException { |
| 168 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 169 | /* 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] | 170 | YangNode curNode = resolutionInfo.getEntityToResolveInfo() |
| 171 | .getHolderOfEntityToResolve(); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 172 | while (!(curNode instanceof YangReferenceResolver)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 173 | curNode = curNode.getParent(); |
| 174 | if (curNode == null) { |
| 175 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 176 | } |
| 177 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 178 | YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode; |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 179 | |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 180 | if (resolutionInfo.getEntityToResolveInfo() |
| 181 | .getEntityToResolve() instanceof YangType) { |
| 182 | resolutionNode.addToResolutionList(resolutionInfo, |
| 183 | ResolvableType.YANG_DERIVED_DATA_TYPE); |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 184 | } else if (resolutionInfo.getEntityToResolveInfo() |
| 185 | .getEntityToResolve() instanceof YangUses) { |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 186 | resolutionNode.addToResolutionList(resolutionInfo, |
| 187 | ResolvableType.YANG_USES); |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 188 | } else if (resolutionInfo.getEntityToResolveInfo() |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 189 | .getEntityToResolve() instanceof YangAugment) { |
| 190 | resolutionNode.addToResolutionList(resolutionInfo, |
| 191 | ResolvableType.YANG_AUGMENT); |
| 192 | } else if (resolutionInfo.getEntityToResolveInfo() |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 193 | .getEntityToResolve() instanceof YangIfFeature) { |
| 194 | resolutionNode.addToResolutionList(resolutionInfo, |
| 195 | ResolvableType.YANG_IF_FEATURE); |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 196 | } else if (resolutionInfo.getEntityToResolveInfo() |
| 197 | .getEntityToResolve() instanceof YangLeafRef) { |
| 198 | resolutionNode.addToResolutionList(resolutionInfo, |
| 199 | ResolvableType.YANG_LEAFREF); |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 200 | } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangBase) { |
| 201 | resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_BASE); |
| 202 | } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangIdentityRef) { |
| 203 | resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_IDENTITYREF); |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 204 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 205 | } |
| 206 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 207 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 208 | * Resolve linking for a resolution list. |
| 209 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 210 | * @param resolutionList resolution list for which linking to be done |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 211 | * @param dataModelRootNode module/sub-module node |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 212 | * @throws DataModelException a violation of data model rules |
| 213 | */ |
| 214 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 215 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 216 | throws DataModelException { |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 217 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 218 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 219 | resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Links type/uses referring to typedef/uses of inter YANG file. |
| 225 | * |
| 226 | * @param resolutionList resolution list for which linking to be done |
| 227 | * @param dataModelRootNode module/sub-module node |
| 228 | * @throws DataModelException a violation of data model rules |
| 229 | */ |
| 230 | public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList, |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 231 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 232 | throws DataModelException { |
| 233 | /* |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 234 | * Run through the resolution list, find type/uses referring to inter |
| 235 | * file typedef/grouping, ask for linking. |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 236 | */ |
| 237 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
| 238 | resolutionInfo.linkInterFile(dataModelRootNode); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 239 | } |
| 240 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Checks if there is any rpc defined in the module or sub-module. |
| 244 | * |
| 245 | * @param rootNode root node of the data model |
| 246 | * @return status of rpc's existence |
| 247 | */ |
| 248 | public static boolean isRpcChildNodePresent(YangNode rootNode) { |
| 249 | YangNode childNode = rootNode.getChild(); |
| 250 | while (childNode != null) { |
| 251 | if (childNode instanceof YangRpc) { |
| 252 | return true; |
| 253 | } |
| 254 | childNode = childNode.getNextSibling(); |
| 255 | } |
| 256 | return false; |
| 257 | } |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 258 | |
| 259 | /** |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 260 | * Returns referred node in a given set. |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 261 | * |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 262 | * @param yangNodeSet YANG node set |
| 263 | * @param refNodeName name of the node which is referred |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 264 | * @return referred node's reference |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 265 | */ |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 266 | public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 267 | /* |
| 268 | * Run through the YANG files to see which YANG file matches the |
| 269 | * referred node name. |
| 270 | */ |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 271 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 272 | if (yangNode.getName().equals(refNodeName)) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 273 | return yangNode; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 274 | } |
| 275 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 276 | return null; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 277 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 278 | |
| 279 | /** |
| 280 | * Returns the contained data model parent node. |
| 281 | * |
| 282 | * @param currentNode current node which parent contained node is required |
| 283 | * @return parent node in which the current node is an attribute |
| 284 | */ |
| 285 | public static YangNode getParentNodeInGenCode(YangNode currentNode) { |
| 286 | |
| 287 | /* |
| 288 | * TODO: recursive parent lookup to support choice/augment/uses. TODO: |
| 289 | * need to check if this needs to be updated for |
| 290 | * choice/case/augment/grouping |
| 291 | */ |
| 292 | return currentNode.getParent(); |
| 293 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Returns de-serializes YANG data-model nodes. |
| 297 | * |
| 298 | * @param serializableInfoSet YANG file info set |
| 299 | * @return de-serializes YANG data-model nodes |
| 300 | * @throws IOException when fails do IO operations |
| 301 | */ |
| 302 | public static List<YangNode> deSerializeDataModel(List<String> serializableInfoSet) throws IOException { |
| 303 | |
| 304 | List<YangNode> nodes = new ArrayList<>(); |
| 305 | for (String fileInfo : serializableInfoSet) { |
| 306 | YangNode node = null; |
| 307 | try { |
| 308 | FileInputStream fileInputStream = new FileInputStream(fileInfo); |
| 309 | ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); |
| 310 | node = (YangNode) objectInputStream.readObject(); |
| 311 | nodes.add(node); |
| 312 | objectInputStream.close(); |
| 313 | fileInputStream.close(); |
| 314 | } catch (IOException | ClassNotFoundException e) { |
| 315 | throw new IOException(fileInfo + " not found."); |
| 316 | } |
| 317 | } |
| 318 | return nodes; |
| 319 | } |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 320 | |
| 321 | /** |
| 322 | * Clones the list of leaves and list of leaf list in the leaves holder. |
| 323 | * |
| 324 | * @param leavesHolder YANG node potentially containing leaves or leaf lists |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 325 | * @param yangUses instance of YANG uses |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 326 | * @throws CloneNotSupportedException clone is not supported |
| 327 | * @throws DataModelException data model error |
| 328 | */ |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 329 | public static void cloneLeaves(YangLeavesHolder leavesHolder, YangUses yangUses) |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 330 | throws CloneNotSupportedException, DataModelException { |
| 331 | List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf(); |
| 332 | if (currentListOfLeaves != null) { |
| 333 | List<YangLeaf> clonedLeavesList = new LinkedList<YangLeaf>(); |
| 334 | for (YangLeaf leaf : currentListOfLeaves) { |
| 335 | YangLeaf clonedLeaf = leaf.clone(); |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 336 | if (yangUses.getCurrentGroupingDepth() == 0) { |
| 337 | YangEntityToResolveInfoImpl resolveInfo = |
| 338 | resolveLeafrefUnderGroupingForLeaf(clonedLeaf, leavesHolder, yangUses); |
| 339 | if (resolveInfo != null) { |
| 340 | yangUses.addEntityToResolve(resolveInfo); |
| 341 | } |
| 342 | } |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 343 | clonedLeaf.setContainedIn(leavesHolder); |
| 344 | clonedLeavesList.add(clonedLeaf); |
| 345 | } |
| 346 | leavesHolder.setListOfLeaf(clonedLeavesList); |
| 347 | } |
| 348 | |
| 349 | List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList(); |
| 350 | if (currentListOfLeafList != null) { |
| 351 | List<YangLeafList> clonedListOfLeafList = new LinkedList<YangLeafList>(); |
| 352 | for (YangLeafList leafList : currentListOfLeafList) { |
| 353 | YangLeafList clonedLeafList = leafList.clone(); |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 354 | if (yangUses.getCurrentGroupingDepth() == 0) { |
| 355 | YangEntityToResolveInfoImpl resolveInfo = |
| 356 | resolveLeafrefUnderGroupingForLeafList(clonedLeafList, leavesHolder); |
| 357 | if (resolveInfo != null) { |
| 358 | yangUses.addEntityToResolve(resolveInfo); |
| 359 | } |
| 360 | } |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 361 | clonedLeafList.setContainedIn(leavesHolder); |
| 362 | clonedListOfLeafList.add(clonedLeafList); |
| 363 | } |
| 364 | leavesHolder.setListOfLeafList(clonedListOfLeafList); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame^] | 369 | * Resolves leafref in leaf, which are under grouping by adding it to the resolution list. |
| 370 | * |
| 371 | * @param clonedLeaf cloned leaf in uses from grouping |
| 372 | * @param leafParentHolder holder of the leaf from uses |
| 373 | * @return entity of leafref which has to be resolved |
| 374 | * @throws DataModelException data model error |
| 375 | */ |
| 376 | public static YangEntityToResolveInfoImpl resolveLeafrefUnderGroupingForLeaf(YangLeaf clonedLeaf, |
| 377 | YangLeavesHolder leafParentHolder, |
| 378 | YangUses yangUses) throws |
| 379 | DataModelException { |
| 380 | if (clonedLeaf.getDataType().getDataTypeExtendedInfo() instanceof YangLeafRef) { |
| 381 | YangLeafRef leafrefForCloning = (YangLeafRef) clonedLeaf.getDataType().getDataTypeExtendedInfo(); |
| 382 | // Conversion of prefixes in absolute path while cloning them. |
| 383 | convertThePrefixesDuringChange(leafrefForCloning, yangUses); |
| 384 | leafrefForCloning.setParentNodeOfLeafref((YangNode) leafParentHolder); |
| 385 | YangEntityToResolveInfoImpl yangEntityToResolveInfo = new YangEntityToResolveInfoImpl(); |
| 386 | yangEntityToResolveInfo.setEntityToResolve(leafrefForCloning); |
| 387 | yangEntityToResolveInfo.setHolderOfEntityToResolve((YangNode) leafParentHolder); |
| 388 | yangEntityToResolveInfo.setLineNumber(leafrefForCloning.getLineNumber()); |
| 389 | yangEntityToResolveInfo.setCharPosition(leafrefForCloning.getCharPosition()); |
| 390 | return yangEntityToResolveInfo; |
| 391 | } |
| 392 | return null; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Converts the prefixes in all the nodes of the leafref with respect to the uses node. |
| 397 | * |
| 398 | * @param leafrefForCloning leafref that is to be cloned |
| 399 | * @param yangUses instance of YANG uses where cloning is done |
| 400 | * @throws DataModelException data model error |
| 401 | */ |
| 402 | private static void convertThePrefixesDuringChange(YangLeafRef leafrefForCloning, YangUses yangUses) throws |
| 403 | DataModelException { |
| 404 | List<YangAtomicPath> atomicPathList = leafrefForCloning.getAtomicPath(); |
| 405 | if (atomicPathList != null && !atomicPathList.isEmpty()) { |
| 406 | Iterator<YangAtomicPath> atomicPathIterator = atomicPathList.listIterator(); |
| 407 | while (atomicPathIterator.hasNext()) { |
| 408 | YangAtomicPath atomicPath = atomicPathIterator.next(); |
| 409 | Map<String, String> prefixesAndItsImportNameNode = leafrefForCloning.getPrefixAndItsImportedModule(); |
| 410 | if (!prefixesAndItsImportNameNode.isEmpty() || prefixesAndItsImportNameNode != null) { |
| 411 | String prefixInPath = atomicPath.getNodeIdentifier().getPrefix(); |
| 412 | String importedNodeName = prefixesAndItsImportNameNode.get(prefixInPath); |
| 413 | assignCurrentLeafrefWithNewPrefixes(importedNodeName, atomicPath, yangUses); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Assigns leafref with new prefixes while cloning. |
| 421 | * |
| 422 | * @param importedNodeName imported node name from grouping |
| 423 | * @param atomicPath atomic path in leafref |
| 424 | * @param node instance of YANG uses where cloning is done |
| 425 | * @throws DataModelException data model error |
| 426 | */ |
| 427 | private static void assignCurrentLeafrefWithNewPrefixes(String importedNodeName, YangAtomicPath atomicPath, |
| 428 | YangNode node) throws DataModelException { |
| 429 | while (!(node instanceof YangReferenceResolver)) { |
| 430 | node = node.getParent(); |
| 431 | if (node == null) { |
| 432 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 433 | } |
| 434 | } |
| 435 | if (node instanceof YangModule) { |
| 436 | List<YangImport> importInUsesList = ((YangModule) node).getImportList(); |
| 437 | if (importInUsesList != null && !importInUsesList.isEmpty()) { |
| 438 | Iterator<YangImport> importInUsesListIterator = importInUsesList.listIterator(); |
| 439 | while (importInUsesListIterator.hasNext()) { |
| 440 | YangImport importInUsesNode = importInUsesListIterator.next(); |
| 441 | if (importInUsesNode.getModuleName().equals(importedNodeName)) { |
| 442 | atomicPath.getNodeIdentifier().setPrefix(importInUsesNode.getPrefixId()); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Resolves leafref in leaf-list, which are under grouping by adding it to the resolution list. |
| 451 | * |
| 452 | * @param clonedLeafList cloned leaf-list in uses from grouping |
| 453 | * @param leafListParentHolder holder of the leaf-list from uses |
| 454 | * @return entity of leafref which has to be resolved |
| 455 | * @throws DataModelException data model error |
| 456 | */ |
| 457 | public static YangEntityToResolveInfoImpl resolveLeafrefUnderGroupingForLeafList(YangLeafList clonedLeafList, |
| 458 | YangLeavesHolder |
| 459 | leafListParentHolder) |
| 460 | throws DataModelException { |
| 461 | if (clonedLeafList.getDataType().getDataTypeExtendedInfo() instanceof YangLeafRef) { |
| 462 | YangLeafRef leafrefForCloning = (YangLeafRef) clonedLeafList.getDataType().getDataTypeExtendedInfo(); |
| 463 | leafrefForCloning.setParentNodeOfLeafref((YangNode) leafListParentHolder); |
| 464 | YangEntityToResolveInfoImpl yangEntityToResolveInfo = new YangEntityToResolveInfoImpl(); |
| 465 | yangEntityToResolveInfo.setEntityToResolve(leafrefForCloning); |
| 466 | yangEntityToResolveInfo.setHolderOfEntityToResolve((YangNode) leafListParentHolder); |
| 467 | yangEntityToResolveInfo.setLineNumber(leafrefForCloning.getLineNumber()); |
| 468 | yangEntityToResolveInfo.setCharPosition(leafrefForCloning.getCharPosition()); |
| 469 | return yangEntityToResolveInfo; |
| 470 | } |
| 471 | return null; |
| 472 | } |
| 473 | |
| 474 | /** |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 475 | * Clones the union or enum leaves. If there is any cloned leaves whose type is union/enum then the corresponding |
| 476 | * type info needs to be updated to the cloned new type node. |
| 477 | * |
| 478 | * @param leavesHolder cloned leaves holder, for whom the leaves reference needs to be updated |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 479 | * @throws DataModelException when fails to do data model operations |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 480 | */ |
| 481 | public static void updateClonedLeavesUnionEnumRef(YangLeavesHolder leavesHolder) throws DataModelException { |
| 482 | List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf(); |
| 483 | if (currentListOfLeaves != null) { |
| 484 | for (YangLeaf leaf : currentListOfLeaves) { |
| 485 | if (leaf.getDataType().getDataType() == YangDataTypes.ENUMERATION |
| 486 | || leaf.getDataType().getDataType() == YangDataTypes.UNION) { |
| 487 | try { |
| 488 | updateClonedTypeRef(leaf.getDataType(), leavesHolder); |
| 489 | } catch (DataModelException e) { |
| 490 | throw e; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | } |
| 496 | |
| 497 | List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList(); |
| 498 | if (currentListOfLeafList != null) { |
| 499 | for (YangLeafList leafList : currentListOfLeafList) { |
| 500 | if (leafList.getDataType().getDataType() == YangDataTypes.ENUMERATION |
| 501 | || leafList.getDataType().getDataType() == YangDataTypes.UNION) { |
| 502 | try { |
| 503 | updateClonedTypeRef(leafList.getDataType(), leavesHolder); |
| 504 | } catch (DataModelException e) { |
| 505 | throw e; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Updates the types extended info pointer to point to the cloned type node. |
| 514 | * |
| 515 | * @param dataType data type, whose extended info needs to be pointed to the cloned type |
| 516 | * @param leavesHolder the leaves holder having the cloned type |
| 517 | */ |
| 518 | private static void updateClonedTypeRef(YangType dataType, YangLeavesHolder leavesHolder) |
| 519 | throws DataModelException { |
| 520 | if (!(leavesHolder instanceof YangNode)) { |
| 521 | throw new DataModelException("Data model error: cloned leaves holder is not a node"); |
| 522 | } |
| 523 | YangNode potentialTypeNode = ((YangNode) leavesHolder).getChild(); |
| 524 | while (potentialTypeNode != null) { |
| 525 | String dataTypeName = null; |
| 526 | if (dataType.getDataType() == YangDataTypes.ENUMERATION) { |
| 527 | YangEnumeration enumNode = (YangEnumeration) dataType.getDataTypeExtendedInfo(); |
| 528 | dataTypeName = enumNode.getName(); |
| 529 | } else if (dataType.getDataType() == YangDataTypes.UNION) { |
| 530 | YangUnion unionNode = (YangUnion) dataType.getDataTypeExtendedInfo(); |
| 531 | dataTypeName = unionNode.getName(); |
| 532 | } |
| 533 | if (potentialTypeNode.getName().contentEquals(dataTypeName)) { |
| 534 | dataType.setDataTypeExtendedInfo((Object) potentialTypeNode); |
| 535 | return; |
| 536 | } |
| 537 | potentialTypeNode = potentialTypeNode.getNextSibling(); |
| 538 | } |
| 539 | |
| 540 | throw new DataModelException("Data model error: cloned leaves type is not found"); |
| 541 | } |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 542 | } |