blob: d6e7217da9df477ab265fe70ac5bd0af4303f53a [file] [log] [blame]
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +05303 *
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
17package org.onosproject.yangutils.datamodel.utils;
18
19import org.onosproject.yangutils.datamodel.CollisionDetector;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053020import org.onosproject.yangutils.datamodel.ResolvableType;
janani b23ccc312016-07-14 19:35:22 +053021import org.onosproject.yangutils.datamodel.YangAtomicPath;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053022import org.onosproject.yangutils.datamodel.YangAugment;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +053023import org.onosproject.yangutils.datamodel.YangBase;
janani b23ccc312016-07-14 19:35:22 +053024import org.onosproject.yangutils.datamodel.YangEntityToResolveInfoImpl;
Vidyashree Rama405d2e62016-07-08 20:45:41 +053025import org.onosproject.yangutils.datamodel.YangEnumeration;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +053026import org.onosproject.yangutils.datamodel.YangIdentityRef;
janani b23ccc312016-07-14 19:35:22 +053027import org.onosproject.yangutils.datamodel.YangIfFeature;
28import org.onosproject.yangutils.datamodel.YangImport;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053029import org.onosproject.yangutils.datamodel.YangLeaf;
30import org.onosproject.yangutils.datamodel.YangLeafList;
janani be18b5342016-07-13 21:06:41 +053031import org.onosproject.yangutils.datamodel.YangLeafRef;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053032import org.onosproject.yangutils.datamodel.YangLeavesHolder;
janani b23ccc312016-07-14 19:35:22 +053033import org.onosproject.yangutils.datamodel.YangModule;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053034import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053035import org.onosproject.yangutils.datamodel.YangReferenceResolver;
36import org.onosproject.yangutils.datamodel.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import org.onosproject.yangutils.datamodel.YangRpc;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053038import org.onosproject.yangutils.datamodel.YangType;
Vidyashree Rama405d2e62016-07-08 20:45:41 +053039import org.onosproject.yangutils.datamodel.YangUnion;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053040import org.onosproject.yangutils.datamodel.YangUses;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053041import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Rama405d2e62016-07-08 20:45:41 +053042import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053043
janani b23ccc312016-07-14 19:35:22 +053044import java.io.FileInputStream;
45import java.io.IOException;
46import java.io.ObjectInputStream;
47import java.util.ArrayList;
48import java.util.Iterator;
49import java.util.LinkedList;
50import java.util.List;
51import java.util.Map;
52import java.util.Set;
53
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053054/**
Bharat saraswald9822e92016-04-05 15:13:44 +053055 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053056 */
57public final class DataModelUtils {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053058 public static final String TRUE = "true";
59 public static final String FALSE = "false";
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053060
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053061 /**
62 * Creates a new data model tree utility.
63 */
64 private DataModelUtils() {
65 }
66
67 /**
68 * Detects the colliding identifier name in a given YANG node and its child.
69 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053070 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053071 * @param dataType type of YANG node asking for detecting collision
72 * @param node instance of calling node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053073 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053074 */
75 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
76 throws DataModelException {
janani b4e53f9b2016-04-26 18:49:20 +053077 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
78 detectCollidingForUsesGrouping(identifierName, dataType, node);
79 } else {
80 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053081 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053082 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
83 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
84 }
85 node = node.getChild();
86 while (node != null) {
87 Parsable parsable = (Parsable) node;
88 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053089 && parsable.getYangConstructType() != YangConstructType.USES_DATA
90 && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) {
janani b4e53f9b2016-04-26 18:49:20 +053091 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
92 }
93 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053094 }
95 }
janani b4e53f9b2016-04-26 18:49:20 +053096 }
97
98 /**
99 * Detects colliding of uses and grouping only with uses and grouping respectively.
100 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530101 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530102 * @param dataType type of YANG node asking for detecting collision
103 * @param node node instance of calling node
janani b4e53f9b2016-04-26 18:49:20 +0530104 * @throws DataModelException a violation of data model rules
105 */
106 public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node)
107 throws DataModelException {
108
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530109 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +0530111 Parsable parsable = (Parsable) node;
112 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +0530113 && parsable.getYangConstructType() == dataType) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530114 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
115 }
116 node = node.getNextSibling();
117 }
118 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530119
120 /**
121 * Detects the colliding identifier name in a given leaf node.
122 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530123 * @param listOfLeaf List of leaves to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530124 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530125 * @throws DataModelException a violation of data model rules
126 */
janani b4e53f9b2016-04-26 18:49:20 +0530127 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530128 throws DataModelException {
129
janani b4e53f9b2016-04-26 18:49:20 +0530130 if (listOfLeaf == null) {
131 return;
132 }
133 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530134 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530135 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530137 }
138 }
139 }
140
141 /**
142 * Detects the colliding identifier name in a given leaf-list node.
143 *
janani b4e53f9b2016-04-26 18:49:20 +0530144 * @param listOfLeafList list of leaf-lists to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530145 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530146 * @throws DataModelException a violation of data model rules
147 */
janani b4e53f9b2016-04-26 18:49:20 +0530148 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530149 throws DataModelException {
150
janani b4e53f9b2016-04-26 18:49:20 +0530151 if (listOfLeafList == null) {
152 return;
153 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530154 for (YangLeafList leafList : listOfLeafList) {
155 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530156 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530158 }
159 }
160 }
161
162 /**
163 * Add a resolution information.
164 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530165 * @param resolutionInfo information about the YANG construct which has to be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530166 * @throws DataModelException a violation of data model rules
167 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530168 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
169 throws DataModelException {
170
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530171 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530172 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
173 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530174 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530175 curNode = curNode.getParent();
176 if (curNode == null) {
177 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
178 }
179 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530181
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530182 if (resolutionInfo.getEntityToResolveInfo()
183 .getEntityToResolve() instanceof YangType) {
184 resolutionNode.addToResolutionList(resolutionInfo,
185 ResolvableType.YANG_DERIVED_DATA_TYPE);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530186 } else if (resolutionInfo.getEntityToResolveInfo()
187 .getEntityToResolve() instanceof YangUses) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530188 resolutionNode.addToResolutionList(resolutionInfo,
189 ResolvableType.YANG_USES);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530190 } else if (resolutionInfo.getEntityToResolveInfo()
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530191 .getEntityToResolve() instanceof YangAugment) {
192 resolutionNode.addToResolutionList(resolutionInfo,
193 ResolvableType.YANG_AUGMENT);
194 } else if (resolutionInfo.getEntityToResolveInfo()
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530195 .getEntityToResolve() instanceof YangIfFeature) {
196 resolutionNode.addToResolutionList(resolutionInfo,
197 ResolvableType.YANG_IF_FEATURE);
janani be18b5342016-07-13 21:06:41 +0530198 } else if (resolutionInfo.getEntityToResolveInfo()
199 .getEntityToResolve() instanceof YangLeafRef) {
200 resolutionNode.addToResolutionList(resolutionInfo,
201 ResolvableType.YANG_LEAFREF);
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530202 } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangBase) {
203 resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_BASE);
204 } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangIdentityRef) {
205 resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_IDENTITYREF);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530206 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530207 }
208
janani b4e53f9b2016-04-26 18:49:20 +0530209 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530210 * Resolve linking for a resolution list.
211 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530212 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530213 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530214 * @throws DataModelException a violation of data model rules
215 */
216 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
janani b23ccc312016-07-14 19:35:22 +0530217 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530218 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530219
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530220 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530221 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
222 }
223 }
224
225 /**
226 * Links type/uses referring to typedef/uses of inter YANG file.
227 *
228 * @param resolutionList resolution list for which linking to be done
229 * @param dataModelRootNode module/sub-module node
230 * @throws DataModelException a violation of data model rules
231 */
232 public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
janani b23ccc312016-07-14 19:35:22 +0530233 YangReferenceResolver dataModelRootNode)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530234 throws DataModelException {
235 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530236 * Run through the resolution list, find type/uses referring to inter
237 * file typedef/grouping, ask for linking.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530238 */
239 for (YangResolutionInfo resolutionInfo : resolutionList) {
240 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530241 }
242 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530243
244 /**
245 * Checks if there is any rpc defined in the module or sub-module.
246 *
247 * @param rootNode root node of the data model
248 * @return status of rpc's existence
249 */
250 public static boolean isRpcChildNodePresent(YangNode rootNode) {
251 YangNode childNode = rootNode.getChild();
252 while (childNode != null) {
253 if (childNode instanceof YangRpc) {
254 return true;
255 }
256 childNode = childNode.getNextSibling();
257 }
258 return false;
259 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530260
261 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530262 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530263 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530264 * @param yangNodeSet YANG node set
265 * @param refNodeName name of the node which is referred
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530266 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530267 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530268 public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530269 /*
270 * Run through the YANG files to see which YANG file matches the
271 * referred node name.
272 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530273 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530274 if (yangNode.getName().equals(refNodeName)) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530275 return yangNode;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530276 }
277 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530278 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530279 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530280
281 /**
282 * Returns the contained data model parent node.
283 *
284 * @param currentNode current node which parent contained node is required
285 * @return parent node in which the current node is an attribute
286 */
287 public static YangNode getParentNodeInGenCode(YangNode currentNode) {
288
289 /*
290 * TODO: recursive parent lookup to support choice/augment/uses. TODO:
291 * need to check if this needs to be updated for
292 * choice/case/augment/grouping
293 */
294 return currentNode.getParent();
295 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530296
297 /**
298 * Returns de-serializes YANG data-model nodes.
299 *
300 * @param serializableInfoSet YANG file info set
301 * @return de-serializes YANG data-model nodes
302 * @throws IOException when fails do IO operations
303 */
304 public static List<YangNode> deSerializeDataModel(List<String> serializableInfoSet) throws IOException {
305
306 List<YangNode> nodes = new ArrayList<>();
307 for (String fileInfo : serializableInfoSet) {
308 YangNode node = null;
309 try {
310 FileInputStream fileInputStream = new FileInputStream(fileInfo);
311 ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
312 node = (YangNode) objectInputStream.readObject();
313 nodes.add(node);
314 objectInputStream.close();
315 fileInputStream.close();
316 } catch (IOException | ClassNotFoundException e) {
317 throw new IOException(fileInfo + " not found.");
318 }
319 }
320 return nodes;
321 }
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530322
323 /**
324 * Clones the list of leaves and list of leaf list in the leaves holder.
325 *
326 * @param leavesHolder YANG node potentially containing leaves or leaf lists
janani b23ccc312016-07-14 19:35:22 +0530327 * @param yangUses instance of YANG uses
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530328 * @throws CloneNotSupportedException clone is not supported
329 * @throws DataModelException data model error
330 */
janani b23ccc312016-07-14 19:35:22 +0530331 public static void cloneLeaves(YangLeavesHolder leavesHolder, YangUses yangUses)
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530332 throws CloneNotSupportedException, DataModelException {
333 List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf();
334 if (currentListOfLeaves != null) {
335 List<YangLeaf> clonedLeavesList = new LinkedList<YangLeaf>();
336 for (YangLeaf leaf : currentListOfLeaves) {
337 YangLeaf clonedLeaf = leaf.clone();
janani b23ccc312016-07-14 19:35:22 +0530338 if (yangUses.getCurrentGroupingDepth() == 0) {
339 YangEntityToResolveInfoImpl resolveInfo =
340 resolveLeafrefUnderGroupingForLeaf(clonedLeaf, leavesHolder, yangUses);
341 if (resolveInfo != null) {
342 yangUses.addEntityToResolve(resolveInfo);
343 }
344 }
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530345 clonedLeaf.setContainedIn(leavesHolder);
346 clonedLeavesList.add(clonedLeaf);
347 }
348 leavesHolder.setListOfLeaf(clonedLeavesList);
349 }
350
351 List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList();
352 if (currentListOfLeafList != null) {
353 List<YangLeafList> clonedListOfLeafList = new LinkedList<YangLeafList>();
354 for (YangLeafList leafList : currentListOfLeafList) {
355 YangLeafList clonedLeafList = leafList.clone();
janani b23ccc312016-07-14 19:35:22 +0530356 if (yangUses.getCurrentGroupingDepth() == 0) {
357 YangEntityToResolveInfoImpl resolveInfo =
358 resolveLeafrefUnderGroupingForLeafList(clonedLeafList, leavesHolder);
359 if (resolveInfo != null) {
360 yangUses.addEntityToResolve(resolveInfo);
361 }
362 }
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530363 clonedLeafList.setContainedIn(leavesHolder);
364 clonedListOfLeafList.add(clonedLeafList);
365 }
366 leavesHolder.setListOfLeafList(clonedListOfLeafList);
367 }
368 }
369
370 /**
janani b23ccc312016-07-14 19:35:22 +0530371 * Resolves leafref in leaf, which are under grouping by adding it to the resolution list.
372 *
373 * @param clonedLeaf cloned leaf in uses from grouping
374 * @param leafParentHolder holder of the leaf from uses
Bharat saraswald50c6382016-07-14 21:57:13 +0530375 * @param yangUses YANG uses
janani b23ccc312016-07-14 19:35:22 +0530376 * @return entity of leafref which has to be resolved
377 * @throws DataModelException data model error
378 */
379 public static YangEntityToResolveInfoImpl resolveLeafrefUnderGroupingForLeaf(YangLeaf clonedLeaf,
380 YangLeavesHolder leafParentHolder,
381 YangUses yangUses) throws
382 DataModelException {
383 if (clonedLeaf.getDataType().getDataTypeExtendedInfo() instanceof YangLeafRef) {
384 YangLeafRef leafrefForCloning = (YangLeafRef) clonedLeaf.getDataType().getDataTypeExtendedInfo();
385 // Conversion of prefixes in absolute path while cloning them.
386 convertThePrefixesDuringChange(leafrefForCloning, yangUses);
387 leafrefForCloning.setParentNodeOfLeafref((YangNode) leafParentHolder);
388 YangEntityToResolveInfoImpl yangEntityToResolveInfo = new YangEntityToResolveInfoImpl();
389 yangEntityToResolveInfo.setEntityToResolve(leafrefForCloning);
390 yangEntityToResolveInfo.setHolderOfEntityToResolve((YangNode) leafParentHolder);
391 yangEntityToResolveInfo.setLineNumber(leafrefForCloning.getLineNumber());
392 yangEntityToResolveInfo.setCharPosition(leafrefForCloning.getCharPosition());
393 return yangEntityToResolveInfo;
394 }
395 return null;
396 }
397
398 /**
399 * Converts the prefixes in all the nodes of the leafref with respect to the uses node.
400 *
401 * @param leafrefForCloning leafref that is to be cloned
402 * @param yangUses instance of YANG uses where cloning is done
403 * @throws DataModelException data model error
404 */
405 private static void convertThePrefixesDuringChange(YangLeafRef leafrefForCloning, YangUses yangUses) throws
406 DataModelException {
407 List<YangAtomicPath> atomicPathList = leafrefForCloning.getAtomicPath();
408 if (atomicPathList != null && !atomicPathList.isEmpty()) {
409 Iterator<YangAtomicPath> atomicPathIterator = atomicPathList.listIterator();
410 while (atomicPathIterator.hasNext()) {
411 YangAtomicPath atomicPath = atomicPathIterator.next();
412 Map<String, String> prefixesAndItsImportNameNode = leafrefForCloning.getPrefixAndItsImportedModule();
413 if (!prefixesAndItsImportNameNode.isEmpty() || prefixesAndItsImportNameNode != null) {
414 String prefixInPath = atomicPath.getNodeIdentifier().getPrefix();
415 String importedNodeName = prefixesAndItsImportNameNode.get(prefixInPath);
416 assignCurrentLeafrefWithNewPrefixes(importedNodeName, atomicPath, yangUses);
417 }
418 }
419 }
420 }
421
422 /**
423 * Assigns leafref with new prefixes while cloning.
424 *
425 * @param importedNodeName imported node name from grouping
426 * @param atomicPath atomic path in leafref
427 * @param node instance of YANG uses where cloning is done
428 * @throws DataModelException data model error
429 */
430 private static void assignCurrentLeafrefWithNewPrefixes(String importedNodeName, YangAtomicPath atomicPath,
431 YangNode node) throws DataModelException {
432 while (!(node instanceof YangReferenceResolver)) {
433 node = node.getParent();
434 if (node == null) {
435 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
436 }
437 }
438 if (node instanceof YangModule) {
439 List<YangImport> importInUsesList = ((YangModule) node).getImportList();
440 if (importInUsesList != null && !importInUsesList.isEmpty()) {
441 Iterator<YangImport> importInUsesListIterator = importInUsesList.listIterator();
442 while (importInUsesListIterator.hasNext()) {
443 YangImport importInUsesNode = importInUsesListIterator.next();
444 if (importInUsesNode.getModuleName().equals(importedNodeName)) {
445 atomicPath.getNodeIdentifier().setPrefix(importInUsesNode.getPrefixId());
446 }
447 }
448 }
449 }
450 }
451
452 /**
453 * Resolves leafref in leaf-list, which are under grouping by adding it to the resolution list.
454 *
455 * @param clonedLeafList cloned leaf-list in uses from grouping
456 * @param leafListParentHolder holder of the leaf-list from uses
457 * @return entity of leafref which has to be resolved
458 * @throws DataModelException data model error
459 */
460 public static YangEntityToResolveInfoImpl resolveLeafrefUnderGroupingForLeafList(YangLeafList clonedLeafList,
461 YangLeavesHolder
462 leafListParentHolder)
463 throws DataModelException {
464 if (clonedLeafList.getDataType().getDataTypeExtendedInfo() instanceof YangLeafRef) {
465 YangLeafRef leafrefForCloning = (YangLeafRef) clonedLeafList.getDataType().getDataTypeExtendedInfo();
466 leafrefForCloning.setParentNodeOfLeafref((YangNode) leafListParentHolder);
467 YangEntityToResolveInfoImpl yangEntityToResolveInfo = new YangEntityToResolveInfoImpl();
468 yangEntityToResolveInfo.setEntityToResolve(leafrefForCloning);
469 yangEntityToResolveInfo.setHolderOfEntityToResolve((YangNode) leafListParentHolder);
470 yangEntityToResolveInfo.setLineNumber(leafrefForCloning.getLineNumber());
471 yangEntityToResolveInfo.setCharPosition(leafrefForCloning.getCharPosition());
472 return yangEntityToResolveInfo;
473 }
474 return null;
475 }
476
477 /**
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530478 * Clones the union or enum leaves. If there is any cloned leaves whose type is union/enum then the corresponding
479 * type info needs to be updated to the cloned new type node.
480 *
481 * @param leavesHolder cloned leaves holder, for whom the leaves reference needs to be updated
Bharat saraswalb551aae2016-07-14 15:18:20 +0530482 * @throws DataModelException when fails to do data model operations
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530483 */
484 public static void updateClonedLeavesUnionEnumRef(YangLeavesHolder leavesHolder) throws DataModelException {
485 List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf();
486 if (currentListOfLeaves != null) {
487 for (YangLeaf leaf : currentListOfLeaves) {
488 if (leaf.getDataType().getDataType() == YangDataTypes.ENUMERATION
489 || leaf.getDataType().getDataType() == YangDataTypes.UNION) {
490 try {
491 updateClonedTypeRef(leaf.getDataType(), leavesHolder);
492 } catch (DataModelException e) {
493 throw e;
494 }
495 }
496 }
497
498 }
499
500 List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList();
501 if (currentListOfLeafList != null) {
502 for (YangLeafList leafList : currentListOfLeafList) {
503 if (leafList.getDataType().getDataType() == YangDataTypes.ENUMERATION
504 || leafList.getDataType().getDataType() == YangDataTypes.UNION) {
505 try {
506 updateClonedTypeRef(leafList.getDataType(), leavesHolder);
507 } catch (DataModelException e) {
508 throw e;
509 }
510 }
511 }
512 }
513 }
514
515 /**
516 * Updates the types extended info pointer to point to the cloned type node.
517 *
518 * @param dataType data type, whose extended info needs to be pointed to the cloned type
519 * @param leavesHolder the leaves holder having the cloned type
520 */
521 private static void updateClonedTypeRef(YangType dataType, YangLeavesHolder leavesHolder)
522 throws DataModelException {
523 if (!(leavesHolder instanceof YangNode)) {
524 throw new DataModelException("Data model error: cloned leaves holder is not a node");
525 }
526 YangNode potentialTypeNode = ((YangNode) leavesHolder).getChild();
527 while (potentialTypeNode != null) {
528 String dataTypeName = null;
529 if (dataType.getDataType() == YangDataTypes.ENUMERATION) {
530 YangEnumeration enumNode = (YangEnumeration) dataType.getDataTypeExtendedInfo();
531 dataTypeName = enumNode.getName();
532 } else if (dataType.getDataType() == YangDataTypes.UNION) {
533 YangUnion unionNode = (YangUnion) dataType.getDataTypeExtendedInfo();
534 dataTypeName = unionNode.getName();
535 }
536 if (potentialTypeNode.getName().contentEquals(dataTypeName)) {
537 dataType.setDataTypeExtendedInfo((Object) potentialTypeNode);
538 return;
539 }
540 potentialTypeNode = potentialTypeNode.getNextSibling();
541 }
542
543 throw new DataModelException("Data model error: cloned leaves type is not found");
544 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530545}