blob: 5b29216846be6780e6db6a85cecd057f4837e786 [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 {
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053058
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053059 /**
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-Huawei2ee9e7e2016-06-01 14:30:22 +053068 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053069 * @param dataType type of YANG node asking for detecting collision
70 * @param node instance of calling node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053071 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053072 */
73 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
74 throws DataModelException {
janani b4e53f9b2016-04-26 18:49:20 +053075 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
76 detectCollidingForUsesGrouping(identifierName, dataType, node);
77 } else {
78 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053079 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053080 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 saraswal33dfa012016-05-17 19:59:16 +053087 && parsable.getYangConstructType() != YangConstructType.USES_DATA
88 && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) {
janani b4e53f9b2016-04-26 18:49:20 +053089 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
90 }
91 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092 }
93 }
janani b4e53f9b2016-04-26 18:49:20 +053094 }
95
96 /**
97 * Detects colliding of uses and grouping only with uses and grouping respectively.
98 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053099 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530100 * @param dataType type of YANG node asking for detecting collision
101 * @param node node instance of calling node
janani b4e53f9b2016-04-26 18:49:20 +0530102 * @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 Agrawal8e8770a2016-02-27 03:57:50 +0530107 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +0530108 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +0530109 Parsable parsable = (Parsable) node;
110 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +0530111 && parsable.getYangConstructType() == dataType) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
113 }
114 node = node.getNextSibling();
115 }
116 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530117
118 /**
119 * Detects the colliding identifier name in a given leaf node.
120 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530121 * @param listOfLeaf List of leaves to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530122 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530123 * @throws DataModelException a violation of data model rules
124 */
janani b4e53f9b2016-04-26 18:49:20 +0530125 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530126 throws DataModelException {
127
janani b4e53f9b2016-04-26 18:49:20 +0530128 if (listOfLeaf == null) {
129 return;
130 }
131 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530132 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530133 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530134 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530135 }
136 }
137 }
138
139 /**
140 * Detects the colliding identifier name in a given leaf-list node.
141 *
janani b4e53f9b2016-04-26 18:49:20 +0530142 * @param listOfLeafList list of leaf-lists to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530143 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530144 * @throws DataModelException a violation of data model rules
145 */
janani b4e53f9b2016-04-26 18:49:20 +0530146 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530147 throws DataModelException {
148
janani b4e53f9b2016-04-26 18:49:20 +0530149 if (listOfLeafList == null) {
150 return;
151 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 for (YangLeafList leafList : listOfLeafList) {
153 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530154 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530155 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530156 }
157 }
158 }
159
160 /**
161 * Add a resolution information.
162 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530163 * @param resolutionInfo information about the YANG construct which has to be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530164 * @throws DataModelException a violation of data model rules
165 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530166 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
167 throws DataModelException {
168
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530169 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530170 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
171 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530172 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530173 curNode = curNode.getParent();
174 if (curNode == null) {
175 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
176 }
177 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530178 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530179
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530180 if (resolutionInfo.getEntityToResolveInfo()
181 .getEntityToResolve() instanceof YangType) {
182 resolutionNode.addToResolutionList(resolutionInfo,
183 ResolvableType.YANG_DERIVED_DATA_TYPE);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530184 } else if (resolutionInfo.getEntityToResolveInfo()
185 .getEntityToResolve() instanceof YangUses) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530186 resolutionNode.addToResolutionList(resolutionInfo,
187 ResolvableType.YANG_USES);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530188 } else if (resolutionInfo.getEntityToResolveInfo()
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530189 .getEntityToResolve() instanceof YangAugment) {
190 resolutionNode.addToResolutionList(resolutionInfo,
191 ResolvableType.YANG_AUGMENT);
192 } else if (resolutionInfo.getEntityToResolveInfo()
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530193 .getEntityToResolve() instanceof YangIfFeature) {
194 resolutionNode.addToResolutionList(resolutionInfo,
195 ResolvableType.YANG_IF_FEATURE);
janani be18b5342016-07-13 21:06:41 +0530196 } else if (resolutionInfo.getEntityToResolveInfo()
197 .getEntityToResolve() instanceof YangLeafRef) {
198 resolutionNode.addToResolutionList(resolutionInfo,
199 ResolvableType.YANG_LEAFREF);
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530200 } 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-Huawei2ee9e7e2016-06-01 14:30:22 +0530204 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530205 }
206
janani b4e53f9b2016-04-26 18:49:20 +0530207 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530208 * Resolve linking for a resolution list.
209 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530210 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530211 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530212 * @throws DataModelException a violation of data model rules
213 */
214 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
janani b23ccc312016-07-14 19:35:22 +0530215 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530216 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530217
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530218 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530219 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 b23ccc312016-07-14 19:35:22 +0530231 YangReferenceResolver dataModelRootNode)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530232 throws DataModelException {
233 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530234 * Run through the resolution list, find type/uses referring to inter
235 * file typedef/grouping, ask for linking.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530236 */
237 for (YangResolutionInfo resolutionInfo : resolutionList) {
238 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530239 }
240 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530241
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 Rama1db15562016-05-17 16:16:15 +0530258
259 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530260 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530261 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530262 * @param yangNodeSet YANG node set
263 * @param refNodeName name of the node which is referred
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530264 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530265 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530266 public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530267 /*
268 * Run through the YANG files to see which YANG file matches the
269 * referred node name.
270 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530271 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530272 if (yangNode.getName().equals(refNodeName)) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530273 return yangNode;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530274 }
275 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530276 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530277 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530278
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 saraswalb1170bd2016-07-14 13:26:18 +0530294
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 Rama405d2e62016-07-08 20:45:41 +0530320
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 b23ccc312016-07-14 19:35:22 +0530325 * @param yangUses instance of YANG uses
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530326 * @throws CloneNotSupportedException clone is not supported
327 * @throws DataModelException data model error
328 */
janani b23ccc312016-07-14 19:35:22 +0530329 public static void cloneLeaves(YangLeavesHolder leavesHolder, YangUses yangUses)
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530330 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 b23ccc312016-07-14 19:35:22 +0530336 if (yangUses.getCurrentGroupingDepth() == 0) {
337 YangEntityToResolveInfoImpl resolveInfo =
338 resolveLeafrefUnderGroupingForLeaf(clonedLeaf, leavesHolder, yangUses);
339 if (resolveInfo != null) {
340 yangUses.addEntityToResolve(resolveInfo);
341 }
342 }
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530343 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 b23ccc312016-07-14 19:35:22 +0530354 if (yangUses.getCurrentGroupingDepth() == 0) {
355 YangEntityToResolveInfoImpl resolveInfo =
356 resolveLeafrefUnderGroupingForLeafList(clonedLeafList, leavesHolder);
357 if (resolveInfo != null) {
358 yangUses.addEntityToResolve(resolveInfo);
359 }
360 }
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530361 clonedLeafList.setContainedIn(leavesHolder);
362 clonedListOfLeafList.add(clonedLeafList);
363 }
364 leavesHolder.setListOfLeafList(clonedListOfLeafList);
365 }
366 }
367
368 /**
janani b23ccc312016-07-14 19:35:22 +0530369 * 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
Bharat saraswald50c6382016-07-14 21:57:13 +0530373 * @param yangUses YANG uses
janani b23ccc312016-07-14 19:35:22 +0530374 * @return entity of leafref which has to be resolved
375 * @throws DataModelException data model error
376 */
377 public static YangEntityToResolveInfoImpl resolveLeafrefUnderGroupingForLeaf(YangLeaf clonedLeaf,
378 YangLeavesHolder leafParentHolder,
379 YangUses yangUses) throws
380 DataModelException {
381 if (clonedLeaf.getDataType().getDataTypeExtendedInfo() instanceof YangLeafRef) {
382 YangLeafRef leafrefForCloning = (YangLeafRef) clonedLeaf.getDataType().getDataTypeExtendedInfo();
383 // Conversion of prefixes in absolute path while cloning them.
384 convertThePrefixesDuringChange(leafrefForCloning, yangUses);
385 leafrefForCloning.setParentNodeOfLeafref((YangNode) leafParentHolder);
386 YangEntityToResolveInfoImpl yangEntityToResolveInfo = new YangEntityToResolveInfoImpl();
387 yangEntityToResolveInfo.setEntityToResolve(leafrefForCloning);
388 yangEntityToResolveInfo.setHolderOfEntityToResolve((YangNode) leafParentHolder);
389 yangEntityToResolveInfo.setLineNumber(leafrefForCloning.getLineNumber());
390 yangEntityToResolveInfo.setCharPosition(leafrefForCloning.getCharPosition());
391 return yangEntityToResolveInfo;
392 }
393 return null;
394 }
395
396 /**
397 * Converts the prefixes in all the nodes of the leafref with respect to the uses node.
398 *
399 * @param leafrefForCloning leafref that is to be cloned
400 * @param yangUses instance of YANG uses where cloning is done
401 * @throws DataModelException data model error
402 */
403 private static void convertThePrefixesDuringChange(YangLeafRef leafrefForCloning, YangUses yangUses) throws
404 DataModelException {
405 List<YangAtomicPath> atomicPathList = leafrefForCloning.getAtomicPath();
406 if (atomicPathList != null && !atomicPathList.isEmpty()) {
407 Iterator<YangAtomicPath> atomicPathIterator = atomicPathList.listIterator();
408 while (atomicPathIterator.hasNext()) {
409 YangAtomicPath atomicPath = atomicPathIterator.next();
410 Map<String, String> prefixesAndItsImportNameNode = leafrefForCloning.getPrefixAndItsImportedModule();
411 if (!prefixesAndItsImportNameNode.isEmpty() || prefixesAndItsImportNameNode != null) {
412 String prefixInPath = atomicPath.getNodeIdentifier().getPrefix();
413 String importedNodeName = prefixesAndItsImportNameNode.get(prefixInPath);
414 assignCurrentLeafrefWithNewPrefixes(importedNodeName, atomicPath, yangUses);
415 }
416 }
417 }
418 }
419
420 /**
421 * Assigns leafref with new prefixes while cloning.
422 *
423 * @param importedNodeName imported node name from grouping
424 * @param atomicPath atomic path in leafref
425 * @param node instance of YANG uses where cloning is done
426 * @throws DataModelException data model error
427 */
428 private static void assignCurrentLeafrefWithNewPrefixes(String importedNodeName, YangAtomicPath atomicPath,
429 YangNode node) throws DataModelException {
430 while (!(node instanceof YangReferenceResolver)) {
431 node = node.getParent();
432 if (node == null) {
433 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
434 }
435 }
436 if (node instanceof YangModule) {
437 List<YangImport> importInUsesList = ((YangModule) node).getImportList();
438 if (importInUsesList != null && !importInUsesList.isEmpty()) {
439 Iterator<YangImport> importInUsesListIterator = importInUsesList.listIterator();
440 while (importInUsesListIterator.hasNext()) {
441 YangImport importInUsesNode = importInUsesListIterator.next();
442 if (importInUsesNode.getModuleName().equals(importedNodeName)) {
443 atomicPath.getNodeIdentifier().setPrefix(importInUsesNode.getPrefixId());
444 }
445 }
446 }
447 }
448 }
449
450 /**
451 * Resolves leafref in leaf-list, which are under grouping by adding it to the resolution list.
452 *
453 * @param clonedLeafList cloned leaf-list in uses from grouping
454 * @param leafListParentHolder holder of the leaf-list from uses
455 * @return entity of leafref which has to be resolved
456 * @throws DataModelException data model error
457 */
458 public static YangEntityToResolveInfoImpl resolveLeafrefUnderGroupingForLeafList(YangLeafList clonedLeafList,
459 YangLeavesHolder
460 leafListParentHolder)
461 throws DataModelException {
462 if (clonedLeafList.getDataType().getDataTypeExtendedInfo() instanceof YangLeafRef) {
463 YangLeafRef leafrefForCloning = (YangLeafRef) clonedLeafList.getDataType().getDataTypeExtendedInfo();
464 leafrefForCloning.setParentNodeOfLeafref((YangNode) leafListParentHolder);
465 YangEntityToResolveInfoImpl yangEntityToResolveInfo = new YangEntityToResolveInfoImpl();
466 yangEntityToResolveInfo.setEntityToResolve(leafrefForCloning);
467 yangEntityToResolveInfo.setHolderOfEntityToResolve((YangNode) leafListParentHolder);
468 yangEntityToResolveInfo.setLineNumber(leafrefForCloning.getLineNumber());
469 yangEntityToResolveInfo.setCharPosition(leafrefForCloning.getCharPosition());
470 return yangEntityToResolveInfo;
471 }
472 return null;
473 }
474
475 /**
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530476 * Clones the union or enum leaves. If there is any cloned leaves whose type is union/enum then the corresponding
477 * type info needs to be updated to the cloned new type node.
478 *
479 * @param leavesHolder cloned leaves holder, for whom the leaves reference needs to be updated
Bharat saraswalb551aae2016-07-14 15:18:20 +0530480 * @throws DataModelException when fails to do data model operations
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530481 */
482 public static void updateClonedLeavesUnionEnumRef(YangLeavesHolder leavesHolder) throws DataModelException {
483 List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf();
484 if (currentListOfLeaves != null) {
485 for (YangLeaf leaf : currentListOfLeaves) {
486 if (leaf.getDataType().getDataType() == YangDataTypes.ENUMERATION
487 || leaf.getDataType().getDataType() == YangDataTypes.UNION) {
488 try {
489 updateClonedTypeRef(leaf.getDataType(), leavesHolder);
490 } catch (DataModelException e) {
491 throw e;
492 }
493 }
494 }
495
496 }
497
498 List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList();
499 if (currentListOfLeafList != null) {
500 for (YangLeafList leafList : currentListOfLeafList) {
501 if (leafList.getDataType().getDataType() == YangDataTypes.ENUMERATION
502 || leafList.getDataType().getDataType() == YangDataTypes.UNION) {
503 try {
504 updateClonedTypeRef(leafList.getDataType(), leavesHolder);
505 } catch (DataModelException e) {
506 throw e;
507 }
508 }
509 }
510 }
511 }
512
513 /**
514 * Updates the types extended info pointer to point to the cloned type node.
515 *
516 * @param dataType data type, whose extended info needs to be pointed to the cloned type
517 * @param leavesHolder the leaves holder having the cloned type
518 */
519 private static void updateClonedTypeRef(YangType dataType, YangLeavesHolder leavesHolder)
520 throws DataModelException {
521 if (!(leavesHolder instanceof YangNode)) {
522 throw new DataModelException("Data model error: cloned leaves holder is not a node");
523 }
524 YangNode potentialTypeNode = ((YangNode) leavesHolder).getChild();
525 while (potentialTypeNode != null) {
526 String dataTypeName = null;
527 if (dataType.getDataType() == YangDataTypes.ENUMERATION) {
528 YangEnumeration enumNode = (YangEnumeration) dataType.getDataTypeExtendedInfo();
529 dataTypeName = enumNode.getName();
530 } else if (dataType.getDataType() == YangDataTypes.UNION) {
531 YangUnion unionNode = (YangUnion) dataType.getDataTypeExtendedInfo();
532 dataTypeName = unionNode.getName();
533 }
534 if (potentialTypeNode.getName().contentEquals(dataTypeName)) {
535 dataType.setDataTypeExtendedInfo((Object) potentialTypeNode);
536 return;
537 }
538 potentialTypeNode = potentialTypeNode.getNextSibling();
539 }
540
541 throw new DataModelException("Data model error: cloned leaves type is not found");
542 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530543}