blob: 3f4c7bf561bd3256ed41ffb4ee7956cbff2a497c [file] [log] [blame]
Bharat saraswalb1170bd2016-07-14 13:26:18 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.linker.impl;
18
Bharat saraswald50c6382016-07-14 21:57:13 +053019import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.Iterator;
22import java.util.List;
23import java.util.Map;
24import java.util.Stack;
janani b23ccc312016-07-14 19:35:22 +053025import org.onosproject.yangutils.datamodel.YangAtomicPath;
26import org.onosproject.yangutils.datamodel.YangAugment;
27import org.onosproject.yangutils.datamodel.YangCase;
28import org.onosproject.yangutils.datamodel.YangChoice;
29import org.onosproject.yangutils.datamodel.YangGrouping;
30import org.onosproject.yangutils.datamodel.YangImport;
31import org.onosproject.yangutils.datamodel.YangInclude;
32import org.onosproject.yangutils.datamodel.YangInput;
33import org.onosproject.yangutils.datamodel.YangLeaf;
34import org.onosproject.yangutils.datamodel.YangLeafList;
35import org.onosproject.yangutils.datamodel.YangLeafRef;
36import org.onosproject.yangutils.datamodel.YangLeavesHolder;
37import org.onosproject.yangutils.datamodel.YangModule;
38import org.onosproject.yangutils.datamodel.YangNode;
39import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
40import org.onosproject.yangutils.datamodel.YangOutput;
41import org.onosproject.yangutils.datamodel.YangSubModule;
42import org.onosproject.yangutils.datamodel.YangTypeDef;
43import org.onosproject.yangutils.datamodel.YangUses;
44import org.onosproject.yangutils.linker.exceptions.LinkerException;
45
Bharat saraswalb551aae2016-07-14 15:18:20 +053046import static org.onosproject.yangutils.linker.impl.PrefixResolverType.INTER_TO_INTER;
47import static org.onosproject.yangutils.linker.impl.PrefixResolverType.INTER_TO_INTRA;
48import static org.onosproject.yangutils.linker.impl.PrefixResolverType.INTRA_TO_INTER;
49import static org.onosproject.yangutils.linker.impl.PrefixResolverType.NO_PREFIX_CHANGE_FOR_INTER;
50import static org.onosproject.yangutils.linker.impl.PrefixResolverType.NO_PREFIX_CHANGE_FOR_INTRA;
janani b23ccc312016-07-14 19:35:22 +053051import static org.onosproject.yangutils.utils.UtilConstants.INPUT;
52import static org.onosproject.yangutils.utils.UtilConstants.OUTPUT;
Bharat saraswalb551aae2016-07-14 15:18:20 +053053
Bharat saraswalb1170bd2016-07-14 13:26:18 +053054/**
55 * Represents x-path linking.
56 *
57 * @param <T> x-path linking can be done for target node or for target leaf/leaf-list
58 */
59public class YangXpathLinker<T> {
60
Bharat saraswalb1170bd2016-07-14 13:26:18 +053061 private List<YangAtomicPath> absPaths;
62 private YangNode rootNode;
Bharat saraswalb551aae2016-07-14 15:18:20 +053063 private Map<YangAtomicPath, PrefixResolverType> prefixResolverTypes;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053064 private String curPrefix;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053065
66 /**
67 * Creates an instance of x-path linker.
68 */
69 public YangXpathLinker() {
70 absPaths = new ArrayList<>();
Bharat saraswalb1170bd2016-07-14 13:26:18 +053071 }
72
73 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +053074 * Returns prefix resolver list.
75 *
76 * @return prefix resolver list
77 */
78 public Map<YangAtomicPath, PrefixResolverType> getPrefixResolverTypes() {
79 return prefixResolverTypes;
80 }
81
82 /**
83 * Sets prefix resolver list.
84 *
85 * @param prefixResolverTypes prefix resolver list.
86 */
87 public void setPrefixResolverTypes(Map<YangAtomicPath, PrefixResolverType> prefixResolverTypes) {
88 this.prefixResolverTypes = prefixResolverTypes;
89 }
90
91 /**
92 * Adds to the prefix resolver type map.
93 *
94 * @param type resolver type
95 * @param path absolute path
96 */
97 private void addToPrefixResolverList(PrefixResolverType type, YangAtomicPath path) {
98 getPrefixResolverTypes().put(path, type);
99 }
100
101 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530102 * Returns list of target nodes paths.
103 *
104 * @return target nodes paths
105 */
106 private List<YangAtomicPath> getAbsPaths() {
107 return absPaths;
108 }
109
110 /**
111 * Sets target nodes paths.
112 *
113 * @param absPaths target nodes paths
114 */
115 private void setAbsPaths(List<YangAtomicPath> absPaths) {
116 this.absPaths = absPaths;
117 }
118
119 /**
120 * Returns current prefix.
121 *
122 * @return current prefix
123 */
124 private String getCurPrefix() {
125 return curPrefix;
126 }
127
128 /**
129 * Sets current prefix.
130 *
131 * @param curPrefix current prefix
132 */
133 private void setCurPrefix(String curPrefix) {
134 this.curPrefix = curPrefix;
135 }
136
137 /**
138 * Return root node.
139 *
140 * @return root Node
141 */
142 private YangNode getRootNode() {
143 return rootNode;
144 }
145
146 /**
147 * Sets root node.
148 *
149 * @param rootNode root node
150 */
151 private void setRootNode(YangNode rootNode) {
152 this.rootNode = rootNode;
153 }
154
155 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530156 * Adds node to resolved nodes.
157 *
158 * @param path absolute path
159 * @param node resolved node
160 */
161 private void addToResolvedNodes(YangAtomicPath path, YangNode node) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530162 path.setResolvedNode(node);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530163 }
164
165 /**
166 * Returns list of augment nodes.
167 *
168 * @param node root node
169 * @return list of augment nodes
170 */
171 public List<YangAugment> getListOfYangAugment(YangNode node) {
172 node = node.getChild();
173 List<YangAugment> augments = new ArrayList<>();
174 while (node != null) {
175 if (node instanceof YangAugment) {
176 augments.add((YangAugment) node);
177 }
178 node = node.getNextSibling();
179 }
180 return augments;
181 }
182
183 /**
184 * Process absolute node path for target leaf.
185 *
janani b23ccc312016-07-14 19:35:22 +0530186 * @param atomicPaths atomic path node list
Bharat saraswald50c6382016-07-14 21:57:13 +0530187 * @param root root node
188 * @param leafref instance of YANG leafref
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530189 * @return linked target node
190 */
janani b23ccc312016-07-14 19:35:22 +0530191 public T processLeafRefXpathLinking(List<YangAtomicPath> atomicPaths, YangNode root, YangLeafRef leafref) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530192
Bharat saraswald50c6382016-07-14 21:57:13 +0530193 YangNode targetNode;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530194 setRootNode(root);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530195 setPrefixResolverTypes(new HashMap<>());
janani b23ccc312016-07-14 19:35:22 +0530196 parsePrefixResolverList(atomicPaths);
197 YangAtomicPath leafRefPath = atomicPaths.get(atomicPaths.size() - 1);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530198
199 // When leaf-ref path contains only one absolute path.
janani b23ccc312016-07-14 19:35:22 +0530200 if (atomicPaths.size() == 1) {
201 targetNode = getTargetNodewhenSizeIsOne(atomicPaths);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530202 } else {
janani b23ccc312016-07-14 19:35:22 +0530203 atomicPaths.remove(atomicPaths.size() - 1);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530204
janani b23ccc312016-07-14 19:35:22 +0530205 setAbsPaths(atomicPaths);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530206 targetNode = parseData(root);
207 }
208 if (targetNode == null) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530209 targetNode = searchInSubModule(root);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530210 }
211
janani b23ccc312016-07-14 19:35:22 +0530212 // Invalid path presence in the node list is checked.
213 validateInvalidNodesInThePath(leafref);
214
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530215 if (targetNode != null) {
216 YangLeaf targetLeaf = searchReferredLeaf(targetNode, leafRefPath.getNodeIdentifier().getName());
217 if (targetLeaf == null) {
218 YangLeafList targetLeafList = searchReferredLeafList(targetNode,
219 leafRefPath.getNodeIdentifier().getName());
220 if (targetLeafList != null) {
221 return (T) targetLeafList;
222 } else {
janani b23ccc312016-07-14 19:35:22 +0530223 LinkerException linkerException = new LinkerException("YANG file error: Unable to find base " +
224 "leaf/leaf-list for given leafref path "
225 + leafref.getPath());
226 linkerException.setCharPosition(leafref.getCharPosition());
227 linkerException.setLine(leafref.getLineNumber());
228 throw linkerException;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530229 }
230 }
231 return (T) targetLeaf;
232 }
233 return null;
234 }
235
236 /**
janani b23ccc312016-07-14 19:35:22 +0530237 * Validates the nodes in the path for any invalid node.
238 *
239 * @param leafref instance of YANG leafref
240 */
241 private void validateInvalidNodesInThePath(YangLeafRef leafref) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530242 for (YangAtomicPath absolutePath : (Iterable<YangAtomicPath>) leafref.getAtomicPath()) {
243 YangNode nodeInPath = absolutePath.getResolvedNode();
janani b23ccc312016-07-14 19:35:22 +0530244
245 if (nodeInPath instanceof YangGrouping || nodeInPath instanceof YangUses
246 || nodeInPath instanceof YangTypeDef || nodeInPath instanceof YangCase
247 || nodeInPath instanceof YangChoice) {
248 LinkerException linkerException = new LinkerException("YANG file error: The target node, in the " +
249 "leafref path " + leafref.getPath() + ", is invalid.");
250 linkerException.setCharPosition(leafref.getCharPosition());
251 linkerException.setLine(leafref.getLineNumber());
252 throw linkerException;
253 }
254 }
255 }
256
257 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530258 * Returns target node when leaf-ref has only one absolute path in list.
259 *
260 * @param absPaths absolute paths
261 * @return target node
262 */
263 private YangNode getTargetNodewhenSizeIsOne(List<YangAtomicPath> absPaths) {
264 if (absPaths.get(0).getNodeIdentifier().getPrefix() != null
265 && !absPaths.get(0).getNodeIdentifier().getPrefix().equals(getRootsPrefix(getRootNode()))) {
266 return getImportedNode(getRootNode(), absPaths.get(0).getNodeIdentifier());
267 }
268 return getRootNode();
269
270 }
271
272 /**
273 * Process absolute node path linking for augment.
274 *
275 * @param absPaths absolute path node list
Bharat saraswalb551aae2016-07-14 15:18:20 +0530276 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530277 * @return linked target node
278 */
279 public YangNode processAugmentXpathLinking(List<YangAtomicPath> absPaths, YangNode root) {
280
281 setAbsPaths(absPaths);
282 setRootNode(root);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530283 setPrefixResolverTypes(new HashMap<>());
284 parsePrefixResolverList(absPaths);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530285
286 YangNode targetNode = parseData(root);
287
288 if (targetNode == null) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530289 targetNode = searchInSubModule(root);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530290 }
291 return targetNode;
292
293 }
294
295 /**
296 * Searches for the referred leaf in target node.
297 *
298 * @param targetNode target node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530299 * @param leafName leaf name
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530300 * @return target leaf
301 */
302 private YangLeaf searchReferredLeaf(YangNode targetNode, String leafName) {
303 if (!(targetNode instanceof YangLeavesHolder)) {
304 throw new LinkerException("Refered node " + targetNode.getName() +
305 "should be of type leaves holder ");
306 }
307 YangLeavesHolder holder = (YangLeavesHolder) targetNode;
308 List<YangLeaf> leaves = holder.getListOfLeaf();
janani b23ccc312016-07-14 19:35:22 +0530309 if (leaves != null && !leaves.isEmpty()) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530310 for (YangLeaf leaf : leaves) {
janani b23ccc312016-07-14 19:35:22 +0530311 if (leaf.getName().equals(leafName)) {
312 return leaf;
313 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530314 }
315 }
316 return null;
317 }
318
319 /**
320 * Searches for the referred leaf-list in target node.
321 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530322 * @param targetNode target node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530323 * @param leafListName leaf-list name
324 * @return target leaf-list
325 */
326 private YangLeafList searchReferredLeafList(YangNode targetNode, String leafListName) {
327 if (!(targetNode instanceof YangLeavesHolder)) {
328 throw new LinkerException("Refered node " + targetNode.getName() +
329 "should be of type leaves holder ");
330 }
331 YangLeavesHolder holder = (YangLeavesHolder) targetNode;
332 List<YangLeafList> leavesList = holder.getListOfLeafList();
janani b23ccc312016-07-14 19:35:22 +0530333 if (leavesList != null && !leavesList.isEmpty()) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530334 for (YangLeafList leafList : leavesList) {
janani b23ccc312016-07-14 19:35:22 +0530335 if (leafList.getName().equals(leafListName)) {
336 return leafList;
337 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530338 }
339 }
340 return null;
341 }
342
343 /**
344 * Process linking using for node identifier for inter/intra file.
345 *
346 * @param root root node
347 * @return linked target node
348 */
349 private YangNode parseData(YangNode root) {
350 String rootPrefix = getRootsPrefix(root);
351 Iterator<YangAtomicPath> pathIterator = getAbsPaths().iterator();
352 YangAtomicPath path = pathIterator.next();
353 if (path.getNodeIdentifier().getPrefix() != null
354 && !path.getNodeIdentifier().getPrefix().equals(rootPrefix)) {
355 return parsePath(getImportedNode(root, path.getNodeIdentifier()));
356 } else {
357 return parsePath(root);
358 }
359 }
360
361 /**
362 * Process linking of target node in root node.
363 *
364 * @param root root node
365 * @return linked target node
366 */
367 private YangNode parsePath(YangNode root) {
368
369 YangNode tempNode = root;
370 Stack<YangNode> linkerStack = new Stack<>();
371 Iterator<YangAtomicPath> pathIterator = getAbsPaths().iterator();
372 YangAtomicPath tempPath = pathIterator.next();
373 setCurPrefix(tempPath.getNodeIdentifier().getPrefix());
374 int index = 0;
Bharat saraswalb551aae2016-07-14 15:18:20 +0530375 YangNode tempAugment;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530376 do {
377
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530378 if (tempPath.getNodeIdentifier().getPrefix() == null) {
379 tempAugment = resolveIntraFileAugment(tempPath, root);
380 } else {
381 tempAugment = resolveInterFileAugment(tempPath, root);
382 }
383
384 if (tempAugment != null) {
385 linkerStack.push(tempNode);
386 tempNode = tempAugment;
387 }
388
389 tempNode = searchTargetNode(tempNode, tempPath.getNodeIdentifier());
390 if (tempNode == null && linkerStack.size() != 0) {
391 tempNode = linkerStack.peek();
392 linkerStack.pop();
393 tempNode = searchTargetNode(tempNode, tempPath.getNodeIdentifier());
394 }
395
396 if (tempNode != null) {
397 addToResolvedNodes(tempPath, tempNode);
398 }
399
400 if (index == getAbsPaths().size() - 1) {
401 break;
402 }
403 tempPath = pathIterator.next();
404 index++;
405 } while (validate(tempNode, index));
406 return tempNode;
407 }
408
409 /**
410 * Resolves intra file augment linking.
411 *
412 * @param tempPath temporary absolute path
Bharat saraswalb551aae2016-07-14 15:18:20 +0530413 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530414 * @return linked target node
415 */
416 private YangNode resolveIntraFileAugment(YangAtomicPath tempPath, YangNode root) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530417 YangNode tempAugment;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530418 if (getCurPrefix() != tempPath.getNodeIdentifier().getPrefix()) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530419 root = getIncludedNode(getRootNode(), tempPath.getNodeIdentifier().getName());
420 if (root == null) {
421 root = getIncludedNode(getRootNode(), getAugmentNodeIdentifier(tempPath.getNodeIdentifier(), absPaths,
422 getRootNode()));
423 if (root == null) {
424 root = getRootNode();
425 }
426 }
427 } else {
428 if (getCurPrefix() != null) {
429 root = getImportedNode(root, tempPath.getNodeIdentifier());
430 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530431 }
432
433 setCurPrefix(tempPath.getNodeIdentifier().getPrefix());
434 tempAugment = getAugment(tempPath.getNodeIdentifier(), root, getAbsPaths());
435 if (tempAugment == null) {
436 tempAugment = getAugment(tempPath.getNodeIdentifier(), getRootNode(), getAbsPaths());
437 }
438 return tempAugment;
439 }
440
441 /**
442 * Resolves inter file augment linking.
443 *
444 * @param tempPath temporary absolute path
Bharat saraswalb551aae2016-07-14 15:18:20 +0530445 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530446 * @return linked target node
447 */
448 private YangNode resolveInterFileAugment(YangAtomicPath tempPath, YangNode root) {
449
Bharat saraswalb551aae2016-07-14 15:18:20 +0530450 YangNode tempAugment;
451 if (!tempPath.getNodeIdentifier().getPrefix().equals(getCurPrefix())) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530452 setCurPrefix(tempPath.getNodeIdentifier().getPrefix());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530453 root = getImportedNode(getRootNode(), tempPath.getNodeIdentifier());
454 }
455 tempAugment = getAugment(tempPath.getNodeIdentifier(), root, getAbsPaths());
Bharat saraswalb551aae2016-07-14 15:18:20 +0530456 if (tempAugment == null) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530457 return resolveInterToInterFileAugment(root);
458 }
459 return tempAugment;
460 }
461
462 /**
463 * Resolves augment when prefix changed from inter file to inter file.
464 * it may be possible that the prefix used in imported module is different the
465 * given list of node identifiers.
466 *
467 * @param root root node
468 * @return target node
469 */
470 private YangNode resolveInterToInterFileAugment(YangNode root) {
471 List<YangAugment> augments = getListOfYangAugment(root);
472 int index;
473 List<YangAtomicPath> absPaths = new ArrayList<>();
474 for (YangAugment augment : augments) {
475 index = 0;
476
477 for (YangAtomicPath path : augment.getTargetNode()) {
478
479 if (!searchForAugmentInImportedNode(path.getNodeIdentifier(), index)) {
480 absPaths.clear();
481 break;
482 }
483 absPaths.add(path);
484 index++;
485 }
486 if (!absPaths.isEmpty() && absPaths.size() == getAbsPaths().size() - 1) {
487 return augment;
488 } else {
489 absPaths.clear();
490 }
491 }
492 return null;
493 }
494
495 /**
496 * Searches for the augment node in imported module when prefix has changed from
497 * inter file to inter file.
Bharat saraswalb551aae2016-07-14 15:18:20 +0530498 *
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530499 * @param nodeId node id
Bharat saraswalb551aae2016-07-14 15:18:20 +0530500 * @param index index
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530501 * @return true if found
502 */
503 private boolean searchForAugmentInImportedNode(YangNodeIdentifier nodeId, int index) {
504 YangNodeIdentifier tempNodeId = getAbsPaths().get(index).getNodeIdentifier();
505 return nodeId.getName().equals(tempNodeId.getName());
506 }
507
508 /**
509 * Returns augment node.
510 *
511 * @param tempNodeId temporary absolute path id
Bharat saraswalb551aae2016-07-14 15:18:20 +0530512 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530513 * @return linked target node
514 */
515 private YangNode getAugment(YangNodeIdentifier tempNodeId, YangNode root, List<YangAtomicPath> absPaths) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530516 String augmentName = getAugmentNodeIdentifier(tempNodeId, absPaths, root);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530517 if (augmentName != null) {
518 return searchAugmentNode(root, augmentName);
519 }
520 return null;
521 }
522
523 /**
524 * Process linking using import list.
525 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530526 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530527 * @param nodeId node identifier
528 * @return linked target node
529 */
530 private YangNode getImportedNode(YangNode root, YangNodeIdentifier nodeId) {
531
Bharat saraswalb551aae2016-07-14 15:18:20 +0530532 List<YangImport> importList;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530533
534 if (root instanceof YangModule) {
535 importList = ((YangModule) root).getImportList();
536 } else {
537 importList = ((YangSubModule) root).getImportList();
538 }
539
540 for (YangImport imported : importList) {
541 if (imported.getPrefixId().equals(nodeId.getPrefix())) {
542 return imported.getImportedNode();
543 }
544 }
545
546 return root;
547 }
548
549 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530550 * Searches in sub-module node.
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530551 *
552 * @param root root node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530553 * @return target linked node
554 */
555 private YangNode searchInSubModule(YangNode root) {
556 List<YangInclude> includeList;
557 YangNode tempNode;
558 if (root instanceof YangModule) {
559 includeList = ((YangModule) root).getIncludeList();
560 } else {
561 includeList = ((YangSubModule) root).getIncludeList();
562 }
563
564 for (YangInclude included : includeList) {
565 tempNode = parseData(included.getIncludedNode());
566 if (tempNode != null) {
567 return tempNode;
568 }
569 }
570 return null;
571 }
572
573 /**
574 * Process linking using include list.
575 *
576 * @param root root node
577 * @param tempPathName temporary path node name
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530578 * @return linked target node
579 */
Bharat saraswalb551aae2016-07-14 15:18:20 +0530580 private YangNode getIncludedNode(YangNode root, String tempPathName) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530581
Bharat saraswalb551aae2016-07-14 15:18:20 +0530582 List<YangInclude> includeList;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530583
584 if (root instanceof YangModule) {
585 includeList = ((YangModule) root).getIncludeList();
586 } else {
587 includeList = ((YangSubModule) root).getIncludeList();
588 }
589
590 for (YangInclude included : includeList) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530591 if (verifyChildNode(included.getIncludedNode(), tempPathName)) {
592 return included.getIncludedNode();
593 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530594 }
595
Bharat saraswalb551aae2016-07-14 15:18:20 +0530596 return null;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530597 }
598
599 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530600 * Verifies for child nodes in sub module.
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530601 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530602 * @param node submodule node
603 * @param name name of child node
604 * @return true if child node found
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530605 */
Bharat saraswalb551aae2016-07-14 15:18:20 +0530606 private boolean verifyChildNode(YangNode node, String name) {
607 node = node.getChild();
608 while (node != null) {
609 if (node.getName().equals(name)) {
610 return true;
611 }
612 node = node.getNextSibling();
613 }
614 return false;
615 }
616
617
618 /**
619 * Returns augment's node id.
620 *
621 * @param nodeId node identifier
622 * @param absPaths absolute paths
623 * @param root root node
624 * @return augment's node id
625 */
626 private String getAugmentNodeIdentifier(YangNodeIdentifier nodeId, List<YangAtomicPath> absPaths, YangNode root) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530627
628 Iterator<YangAtomicPath> nodeIdIterator = absPaths.iterator();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530629 YangAtomicPath tempNodeId;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530630 StringBuilder builder = new StringBuilder();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530631 String id;
632 PrefixResolverType type;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530633 while (nodeIdIterator.hasNext()) {
634 tempNodeId = nodeIdIterator.next();
635 if (!tempNodeId.getNodeIdentifier().equals(nodeId)) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530636 type = getPrefixResolverTypes().get(tempNodeId);
637 switch (type) {
638 case INTER_TO_INTRA:
639 id = "/" + tempNodeId.getNodeIdentifier().getName();
640 break;
641 case INTRA_TO_INTER:
642 if (!getRootsPrefix(root).equals(tempNodeId.getNodeIdentifier().getPrefix())) {
643 id = "/" + tempNodeId.getNodeIdentifier().getPrefix() + ":" + tempNodeId.getNodeIdentifier()
644 .getName();
645 } else {
646 id = "/" + tempNodeId.getNodeIdentifier().getName();
647 }
648 break;
649 case INTER_TO_INTER:
650 id = "/" + tempNodeId.getNodeIdentifier().getPrefix() + ":" + tempNodeId.getNodeIdentifier()
651 .getName();
652 break;
653 case NO_PREFIX_CHANGE_FOR_INTRA:
654 id = "/" + tempNodeId.getNodeIdentifier().getName();
655 break;
656 case NO_PREFIX_CHANGE_FOR_INTER:
657 if (!getRootsPrefix(root).equals(tempNodeId.getNodeIdentifier().getPrefix())) {
658 id = "/" + tempNodeId.getNodeIdentifier().getPrefix() + ":" + tempNodeId.getNodeIdentifier()
659 .getName();
660 } else {
661 id = "/" + tempNodeId.getNodeIdentifier().getName();
662 }
663 break;
664 default:
665 id = "/" + tempNodeId.getNodeIdentifier().getName();
666 break;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530667 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530668 builder.append(id);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530669 } else {
670 return builder.toString();
671 }
672 }
673 return null;
674 }
675
676 /**
677 * Searches augment node in root node.
678 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530679 * @param node root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530680 * @param tempNodeId node identifier
681 * @return target augment node
682 */
Bharat saraswalb551aae2016-07-14 15:18:20 +0530683
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530684 private YangNode searchAugmentNode(YangNode node, String tempNodeId) {
685 node = node.getChild();
686 while (node != null) {
687 if (node instanceof YangAugment) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530688 if (node.getName().equals(tempNodeId)) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530689 return node;
690 }
691 }
692 node = node.getNextSibling();
693 }
694 return null;
695 }
696
697 /**
698 * Validates for target node if target node found or not.
699 *
700 * @param tempNode temporary node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530701 * @param index current index of list
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530702 * @return false if target node found
703 */
704 private boolean validate(YangNode tempNode, int index) {
705
706 int size = getAbsPaths().size();
707 if (tempNode != null && index != size) {
708 return true;
Bharat saraswalb551aae2016-07-14 15:18:20 +0530709 } else if (tempNode != null) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530710 return false;
711 // this is your target node.
Bharat saraswalb551aae2016-07-14 15:18:20 +0530712 } else if (index != size) {
713 return true;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530714 // this could be in submodule as well.
715 }
716 return false;
717 }
718
719 /**
720 * Searches target node in root node.
721 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530722 * @param node root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530723 * @param curNodeId YANG node identifier
724 * @return linked target node
725 */
726 private YangNode searchTargetNode(YangNode node, YangNodeIdentifier curNodeId) {
727
728 if (node != null) {
729 node = node.getChild();
730 }
731
732 while (node != null) {
janani b23ccc312016-07-14 19:35:22 +0530733 if (node instanceof YangInput) {
734 if (curNodeId.getName().equalsIgnoreCase(INPUT)) {
735 return node;
736 }
737 } else if (node instanceof YangOutput) {
738 if (curNodeId.getName().equalsIgnoreCase(OUTPUT)) {
739 return node;
740 }
741 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530742 if (node.getName().equals(curNodeId.getName())) {
743 return node;
744 }
745 node = node.getNextSibling();
746 }
747 return null;
748 }
749
750 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530751 * Returns root prefix.
752 *
753 * @param root root node
754 * @return root prefix
755 */
756 private String getRootsPrefix(YangNode root) {
757 if (root instanceof YangModule) {
758 return ((YangModule) root).getPrefix();
759 } else {
760 return ((YangSubModule) root).getPrefix();
761 }
762 }
763
Bharat saraswalb551aae2016-07-14 15:18:20 +0530764 /**
765 * Resolves prefix and provides prefix resolver list.
766 *
767 * @param absolutePaths absolute paths
768 */
769 private void parsePrefixResolverList(List<YangAtomicPath> absolutePaths) {
770 Iterator<YangAtomicPath> pathIterator = absolutePaths.iterator();
771 YangAtomicPath absPath;
772 String prePrefix;
773 String curPrefix = null;
774 while (pathIterator.hasNext()) {
775 prePrefix = curPrefix;
776 absPath = pathIterator.next();
777 curPrefix = absPath.getNodeIdentifier().getPrefix();
778 if (curPrefix != null) {
779 if (!curPrefix.equals(prePrefix)) {
780 if (prePrefix != null) {
781 addToPrefixResolverList(INTER_TO_INTER, absPath);
782 } else {
783 addToPrefixResolverList(INTRA_TO_INTER, absPath);
784 }
785 } else {
786 addToPrefixResolverList(NO_PREFIX_CHANGE_FOR_INTER, absPath);
787 }
788 } else {
789 if (prePrefix != null) {
790 addToPrefixResolverList(INTER_TO_INTRA, absPath);
791 } else {
792 addToPrefixResolverList(NO_PREFIX_CHANGE_FOR_INTRA, absPath);
793 }
794 }
795 }
796
797 }
798
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530799}