blob: e0d6c7b8b662534b1e554aaf877bf90d2daf8b30 [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
19import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.Iterator;
22import java.util.List;
23import java.util.Map;
24import java.util.Stack;
25
26import org.onosproject.yangutils.datamodel.YangAtomicPath;
27import org.onosproject.yangutils.datamodel.YangAugment;
28import org.onosproject.yangutils.datamodel.YangImport;
29import org.onosproject.yangutils.datamodel.YangInclude;
30import org.onosproject.yangutils.datamodel.YangLeaf;
31import org.onosproject.yangutils.datamodel.YangLeafList;
32import org.onosproject.yangutils.datamodel.YangLeavesHolder;
33import org.onosproject.yangutils.datamodel.YangModule;
34import org.onosproject.yangutils.datamodel.YangNode;
35import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
36import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053037import org.onosproject.yangutils.linker.exceptions.LinkerException;
38
Bharat saraswalb551aae2016-07-14 15:18:20 +053039import static org.onosproject.yangutils.linker.impl.PrefixResolverType.INTER_TO_INTER;
40import static org.onosproject.yangutils.linker.impl.PrefixResolverType.INTER_TO_INTRA;
41import static org.onosproject.yangutils.linker.impl.PrefixResolverType.INTRA_TO_INTER;
42import static org.onosproject.yangutils.linker.impl.PrefixResolverType.NO_PREFIX_CHANGE_FOR_INTER;
43import static org.onosproject.yangutils.linker.impl.PrefixResolverType.NO_PREFIX_CHANGE_FOR_INTRA;
44
Bharat saraswalb1170bd2016-07-14 13:26:18 +053045/**
46 * Represents x-path linking.
47 *
48 * @param <T> x-path linking can be done for target node or for target leaf/leaf-list
49 */
50public class YangXpathLinker<T> {
51
Bharat saraswalb1170bd2016-07-14 13:26:18 +053052 private List<YangAtomicPath> absPaths;
53 private YangNode rootNode;
Bharat saraswalb551aae2016-07-14 15:18:20 +053054 private Map<YangAtomicPath, PrefixResolverType> prefixResolverTypes;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053055 private String curPrefix;
56 private Map<YangAtomicPath, YangNode> resolvedNodes;
57
58 /**
59 * Creates an instance of x-path linker.
60 */
61 public YangXpathLinker() {
62 absPaths = new ArrayList<>();
63 setResolvedNodes(new HashMap<>());
64 }
65
66 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +053067 * Returns prefix resolver list.
68 *
69 * @return prefix resolver list
70 */
71 public Map<YangAtomicPath, PrefixResolverType> getPrefixResolverTypes() {
72 return prefixResolverTypes;
73 }
74
75 /**
76 * Sets prefix resolver list.
77 *
78 * @param prefixResolverTypes prefix resolver list.
79 */
80 public void setPrefixResolverTypes(Map<YangAtomicPath, PrefixResolverType> prefixResolverTypes) {
81 this.prefixResolverTypes = prefixResolverTypes;
82 }
83
84 /**
85 * Adds to the prefix resolver type map.
86 *
87 * @param type resolver type
88 * @param path absolute path
89 */
90 private void addToPrefixResolverList(PrefixResolverType type, YangAtomicPath path) {
91 getPrefixResolverTypes().put(path, type);
92 }
93
94 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +053095 * Returns list of target nodes paths.
96 *
97 * @return target nodes paths
98 */
99 private List<YangAtomicPath> getAbsPaths() {
100 return absPaths;
101 }
102
103 /**
104 * Sets target nodes paths.
105 *
106 * @param absPaths target nodes paths
107 */
108 private void setAbsPaths(List<YangAtomicPath> absPaths) {
109 this.absPaths = absPaths;
110 }
111
112 /**
113 * Returns current prefix.
114 *
115 * @return current prefix
116 */
117 private String getCurPrefix() {
118 return curPrefix;
119 }
120
121 /**
122 * Sets current prefix.
123 *
124 * @param curPrefix current prefix
125 */
126 private void setCurPrefix(String curPrefix) {
127 this.curPrefix = curPrefix;
128 }
129
130 /**
131 * Return root node.
132 *
133 * @return root Node
134 */
135 private YangNode getRootNode() {
136 return rootNode;
137 }
138
139 /**
140 * Sets root node.
141 *
142 * @param rootNode root node
143 */
144 private void setRootNode(YangNode rootNode) {
145 this.rootNode = rootNode;
146 }
147
148 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530149 * Returns resolved nodes.
150 *
151 * @return resolved nodes
152 */
153 public Map<YangAtomicPath, YangNode> getResolvedNodes() {
154 return resolvedNodes;
155 }
156
157 /**
158 * Sets resolved nodes.
159 *
160 * @param resolvedNodes resolved nodes
161 */
162 private void setResolvedNodes(Map<YangAtomicPath, YangNode> resolvedNodes) {
163 this.resolvedNodes = resolvedNodes;
164 }
165
166 /**
167 * Adds node to resolved nodes.
168 *
169 * @param path absolute path
170 * @param node resolved node
171 */
172 private void addToResolvedNodes(YangAtomicPath path, YangNode node) {
173 getResolvedNodes().put(path, node);
174 }
175
176 /**
177 * Returns list of augment nodes.
178 *
179 * @param node root node
180 * @return list of augment nodes
181 */
182 public List<YangAugment> getListOfYangAugment(YangNode node) {
183 node = node.getChild();
184 List<YangAugment> augments = new ArrayList<>();
185 while (node != null) {
186 if (node instanceof YangAugment) {
187 augments.add((YangAugment) node);
188 }
189 node = node.getNextSibling();
190 }
191 return augments;
192 }
193
194 /**
195 * Process absolute node path for target leaf.
196 *
197 * @param absPaths absolute path node list
Bharat saraswalb551aae2016-07-14 15:18:20 +0530198 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530199 * @return linked target node
200 */
Bharat saraswalb551aae2016-07-14 15:18:20 +0530201 T processLeafRefXpathLinking(List<YangAtomicPath> absPaths, YangNode root) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530202
203 YangNode targetNode = null;
204 setRootNode(root);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530205 setPrefixResolverTypes(new HashMap<>());
206 parsePrefixResolverList(absPaths);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530207 YangAtomicPath leafRefPath = absPaths.get(absPaths.size() - 1);
208
209 // When leaf-ref path contains only one absolute path.
210 if (absPaths.size() == 1) {
211 targetNode = getTargetNodewhenSizeIsOne(absPaths);
212 } else {
213 absPaths.remove(absPaths.size() - 1);
214
215 setAbsPaths(absPaths);
216 targetNode = parseData(root);
217 }
218 if (targetNode == null) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530219 targetNode = searchInSubModule(root);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530220 }
221
222 if (targetNode != null) {
223 YangLeaf targetLeaf = searchReferredLeaf(targetNode, leafRefPath.getNodeIdentifier().getName());
224 if (targetLeaf == null) {
225 YangLeafList targetLeafList = searchReferredLeafList(targetNode,
226 leafRefPath.getNodeIdentifier().getName());
227 if (targetLeafList != null) {
228 return (T) targetLeafList;
229 } else {
230 throw new LinkerException(
231 "YANG file error: Unable to find base leaf/leaf-list for given leafref "
232 + leafRefPath.getNodeIdentifier().getName());
233 }
234 }
235 return (T) targetLeaf;
236 }
237 return null;
238 }
239
240 /**
241 * Returns target node when leaf-ref has only one absolute path in list.
242 *
243 * @param absPaths absolute paths
244 * @return target node
245 */
246 private YangNode getTargetNodewhenSizeIsOne(List<YangAtomicPath> absPaths) {
247 if (absPaths.get(0).getNodeIdentifier().getPrefix() != null
248 && !absPaths.get(0).getNodeIdentifier().getPrefix().equals(getRootsPrefix(getRootNode()))) {
249 return getImportedNode(getRootNode(), absPaths.get(0).getNodeIdentifier());
250 }
251 return getRootNode();
252
253 }
254
255 /**
256 * Process absolute node path linking for augment.
257 *
258 * @param absPaths absolute path node list
Bharat saraswalb551aae2016-07-14 15:18:20 +0530259 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530260 * @return linked target node
261 */
262 public YangNode processAugmentXpathLinking(List<YangAtomicPath> absPaths, YangNode root) {
263
264 setAbsPaths(absPaths);
265 setRootNode(root);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530266 setPrefixResolverTypes(new HashMap<>());
267 parsePrefixResolverList(absPaths);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530268
269 YangNode targetNode = parseData(root);
270
271 if (targetNode == null) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530272 targetNode = searchInSubModule(root);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530273 }
274 return targetNode;
275
276 }
277
278 /**
279 * Searches for the referred leaf in target node.
280 *
281 * @param targetNode target node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530282 * @param leafName leaf name
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530283 * @return target leaf
284 */
285 private YangLeaf searchReferredLeaf(YangNode targetNode, String leafName) {
286 if (!(targetNode instanceof YangLeavesHolder)) {
287 throw new LinkerException("Refered node " + targetNode.getName() +
288 "should be of type leaves holder ");
289 }
290 YangLeavesHolder holder = (YangLeavesHolder) targetNode;
291 List<YangLeaf> leaves = holder.getListOfLeaf();
292 for (YangLeaf leaf : leaves) {
293 if (leaf.getName().equals(leafName)) {
294 return leaf;
295 }
296 }
297 return null;
298 }
299
300 /**
301 * Searches for the referred leaf-list in target node.
302 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530303 * @param targetNode target node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530304 * @param leafListName leaf-list name
305 * @return target leaf-list
306 */
307 private YangLeafList searchReferredLeafList(YangNode targetNode, String leafListName) {
308 if (!(targetNode instanceof YangLeavesHolder)) {
309 throw new LinkerException("Refered node " + targetNode.getName() +
310 "should be of type leaves holder ");
311 }
312 YangLeavesHolder holder = (YangLeavesHolder) targetNode;
313 List<YangLeafList> leavesList = holder.getListOfLeafList();
314 for (YangLeafList leafList : leavesList) {
315 if (leafList.getName().equals(leafListName)) {
316 return leafList;
317 }
318 }
319 return null;
320 }
321
322 /**
323 * Process linking using for node identifier for inter/intra file.
324 *
325 * @param root root node
326 * @return linked target node
327 */
328 private YangNode parseData(YangNode root) {
329 String rootPrefix = getRootsPrefix(root);
330 Iterator<YangAtomicPath> pathIterator = getAbsPaths().iterator();
331 YangAtomicPath path = pathIterator.next();
332 if (path.getNodeIdentifier().getPrefix() != null
333 && !path.getNodeIdentifier().getPrefix().equals(rootPrefix)) {
334 return parsePath(getImportedNode(root, path.getNodeIdentifier()));
335 } else {
336 return parsePath(root);
337 }
338 }
339
340 /**
341 * Process linking of target node in root node.
342 *
343 * @param root root node
344 * @return linked target node
345 */
346 private YangNode parsePath(YangNode root) {
347
348 YangNode tempNode = root;
349 Stack<YangNode> linkerStack = new Stack<>();
350 Iterator<YangAtomicPath> pathIterator = getAbsPaths().iterator();
351 YangAtomicPath tempPath = pathIterator.next();
352 setCurPrefix(tempPath.getNodeIdentifier().getPrefix());
353 int index = 0;
Bharat saraswalb551aae2016-07-14 15:18:20 +0530354 YangNode tempAugment;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530355 do {
356
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530357 if (tempPath.getNodeIdentifier().getPrefix() == null) {
358 tempAugment = resolveIntraFileAugment(tempPath, root);
359 } else {
360 tempAugment = resolveInterFileAugment(tempPath, root);
361 }
362
363 if (tempAugment != null) {
364 linkerStack.push(tempNode);
365 tempNode = tempAugment;
366 }
367
368 tempNode = searchTargetNode(tempNode, tempPath.getNodeIdentifier());
369 if (tempNode == null && linkerStack.size() != 0) {
370 tempNode = linkerStack.peek();
371 linkerStack.pop();
372 tempNode = searchTargetNode(tempNode, tempPath.getNodeIdentifier());
373 }
374
375 if (tempNode != null) {
376 addToResolvedNodes(tempPath, tempNode);
377 }
378
379 if (index == getAbsPaths().size() - 1) {
380 break;
381 }
382 tempPath = pathIterator.next();
383 index++;
384 } while (validate(tempNode, index));
385 return tempNode;
386 }
387
388 /**
389 * Resolves intra file augment linking.
390 *
391 * @param tempPath temporary absolute path
Bharat saraswalb551aae2016-07-14 15:18:20 +0530392 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530393 * @return linked target node
394 */
395 private YangNode resolveIntraFileAugment(YangAtomicPath tempPath, YangNode root) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530396 YangNode tempAugment;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530397 if (getCurPrefix() != tempPath.getNodeIdentifier().getPrefix()) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530398 root = getIncludedNode(getRootNode(), tempPath.getNodeIdentifier().getName());
399 if (root == null) {
400 root = getIncludedNode(getRootNode(), getAugmentNodeIdentifier(tempPath.getNodeIdentifier(), absPaths,
401 getRootNode()));
402 if (root == null) {
403 root = getRootNode();
404 }
405 }
406 } else {
407 if (getCurPrefix() != null) {
408 root = getImportedNode(root, tempPath.getNodeIdentifier());
409 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530410 }
411
412 setCurPrefix(tempPath.getNodeIdentifier().getPrefix());
413 tempAugment = getAugment(tempPath.getNodeIdentifier(), root, getAbsPaths());
414 if (tempAugment == null) {
415 tempAugment = getAugment(tempPath.getNodeIdentifier(), getRootNode(), getAbsPaths());
416 }
417 return tempAugment;
418 }
419
420 /**
421 * Resolves inter file augment linking.
422 *
423 * @param tempPath temporary absolute path
Bharat saraswalb551aae2016-07-14 15:18:20 +0530424 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530425 * @return linked target node
426 */
427 private YangNode resolveInterFileAugment(YangAtomicPath tempPath, YangNode root) {
428
Bharat saraswalb551aae2016-07-14 15:18:20 +0530429 YangNode tempAugment;
430 if (!tempPath.getNodeIdentifier().getPrefix().equals(getCurPrefix())) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530431 setCurPrefix(tempPath.getNodeIdentifier().getPrefix());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530432 root = getImportedNode(getRootNode(), tempPath.getNodeIdentifier());
433 }
434 tempAugment = getAugment(tempPath.getNodeIdentifier(), root, getAbsPaths());
Bharat saraswalb551aae2016-07-14 15:18:20 +0530435 if (tempAugment == null) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530436 return resolveInterToInterFileAugment(root);
437 }
438 return tempAugment;
439 }
440
441 /**
442 * Resolves augment when prefix changed from inter file to inter file.
443 * it may be possible that the prefix used in imported module is different the
444 * given list of node identifiers.
445 *
446 * @param root root node
447 * @return target node
448 */
449 private YangNode resolveInterToInterFileAugment(YangNode root) {
450 List<YangAugment> augments = getListOfYangAugment(root);
451 int index;
452 List<YangAtomicPath> absPaths = new ArrayList<>();
453 for (YangAugment augment : augments) {
454 index = 0;
455
456 for (YangAtomicPath path : augment.getTargetNode()) {
457
458 if (!searchForAugmentInImportedNode(path.getNodeIdentifier(), index)) {
459 absPaths.clear();
460 break;
461 }
462 absPaths.add(path);
463 index++;
464 }
465 if (!absPaths.isEmpty() && absPaths.size() == getAbsPaths().size() - 1) {
466 return augment;
467 } else {
468 absPaths.clear();
469 }
470 }
471 return null;
472 }
473
474 /**
475 * Searches for the augment node in imported module when prefix has changed from
476 * inter file to inter file.
Bharat saraswalb551aae2016-07-14 15:18:20 +0530477 *
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530478 * @param nodeId node id
Bharat saraswalb551aae2016-07-14 15:18:20 +0530479 * @param index index
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530480 * @return true if found
481 */
482 private boolean searchForAugmentInImportedNode(YangNodeIdentifier nodeId, int index) {
483 YangNodeIdentifier tempNodeId = getAbsPaths().get(index).getNodeIdentifier();
484 return nodeId.getName().equals(tempNodeId.getName());
485 }
486
487 /**
488 * Returns augment node.
489 *
490 * @param tempNodeId temporary absolute path id
Bharat saraswalb551aae2016-07-14 15:18:20 +0530491 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530492 * @return linked target node
493 */
494 private YangNode getAugment(YangNodeIdentifier tempNodeId, YangNode root, List<YangAtomicPath> absPaths) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530495 String augmentName = getAugmentNodeIdentifier(tempNodeId, absPaths, root);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530496 if (augmentName != null) {
497 return searchAugmentNode(root, augmentName);
498 }
499 return null;
500 }
501
502 /**
503 * Process linking using import list.
504 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530505 * @param root root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530506 * @param nodeId node identifier
507 * @return linked target node
508 */
509 private YangNode getImportedNode(YangNode root, YangNodeIdentifier nodeId) {
510
Bharat saraswalb551aae2016-07-14 15:18:20 +0530511 List<YangImport> importList;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530512
513 if (root instanceof YangModule) {
514 importList = ((YangModule) root).getImportList();
515 } else {
516 importList = ((YangSubModule) root).getImportList();
517 }
518
519 for (YangImport imported : importList) {
520 if (imported.getPrefixId().equals(nodeId.getPrefix())) {
521 return imported.getImportedNode();
522 }
523 }
524
525 return root;
526 }
527
528 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530529 * Searches in sub-module node.
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530530 *
531 * @param root root node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530532 * @return target linked node
533 */
534 private YangNode searchInSubModule(YangNode root) {
535 List<YangInclude> includeList;
536 YangNode tempNode;
537 if (root instanceof YangModule) {
538 includeList = ((YangModule) root).getIncludeList();
539 } else {
540 includeList = ((YangSubModule) root).getIncludeList();
541 }
542
543 for (YangInclude included : includeList) {
544 tempNode = parseData(included.getIncludedNode());
545 if (tempNode != null) {
546 return tempNode;
547 }
548 }
549 return null;
550 }
551
552 /**
553 * Process linking using include list.
554 *
555 * @param root root node
556 * @param tempPathName temporary path node name
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530557 * @return linked target node
558 */
Bharat saraswalb551aae2016-07-14 15:18:20 +0530559 private YangNode getIncludedNode(YangNode root, String tempPathName) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530560
Bharat saraswalb551aae2016-07-14 15:18:20 +0530561 List<YangInclude> includeList;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530562
563 if (root instanceof YangModule) {
564 includeList = ((YangModule) root).getIncludeList();
565 } else {
566 includeList = ((YangSubModule) root).getIncludeList();
567 }
568
569 for (YangInclude included : includeList) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530570 if (verifyChildNode(included.getIncludedNode(), tempPathName)) {
571 return included.getIncludedNode();
572 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530573 }
574
Bharat saraswalb551aae2016-07-14 15:18:20 +0530575 return null;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530576 }
577
578 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530579 * Verifies for child nodes in sub module.
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530580 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530581 * @param node submodule node
582 * @param name name of child node
583 * @return true if child node found
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530584 */
Bharat saraswalb551aae2016-07-14 15:18:20 +0530585 private boolean verifyChildNode(YangNode node, String name) {
586 node = node.getChild();
587 while (node != null) {
588 if (node.getName().equals(name)) {
589 return true;
590 }
591 node = node.getNextSibling();
592 }
593 return false;
594 }
595
596
597 /**
598 * Returns augment's node id.
599 *
600 * @param nodeId node identifier
601 * @param absPaths absolute paths
602 * @param root root node
603 * @return augment's node id
604 */
605 private String getAugmentNodeIdentifier(YangNodeIdentifier nodeId, List<YangAtomicPath> absPaths, YangNode root) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530606
607 Iterator<YangAtomicPath> nodeIdIterator = absPaths.iterator();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530608 YangAtomicPath tempNodeId;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530609 StringBuilder builder = new StringBuilder();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530610 String id;
611 PrefixResolverType type;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530612 while (nodeIdIterator.hasNext()) {
613 tempNodeId = nodeIdIterator.next();
614 if (!tempNodeId.getNodeIdentifier().equals(nodeId)) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530615 type = getPrefixResolverTypes().get(tempNodeId);
616 switch (type) {
617 case INTER_TO_INTRA:
618 id = "/" + tempNodeId.getNodeIdentifier().getName();
619 break;
620 case INTRA_TO_INTER:
621 if (!getRootsPrefix(root).equals(tempNodeId.getNodeIdentifier().getPrefix())) {
622 id = "/" + tempNodeId.getNodeIdentifier().getPrefix() + ":" + tempNodeId.getNodeIdentifier()
623 .getName();
624 } else {
625 id = "/" + tempNodeId.getNodeIdentifier().getName();
626 }
627 break;
628 case INTER_TO_INTER:
629 id = "/" + tempNodeId.getNodeIdentifier().getPrefix() + ":" + tempNodeId.getNodeIdentifier()
630 .getName();
631 break;
632 case NO_PREFIX_CHANGE_FOR_INTRA:
633 id = "/" + tempNodeId.getNodeIdentifier().getName();
634 break;
635 case NO_PREFIX_CHANGE_FOR_INTER:
636 if (!getRootsPrefix(root).equals(tempNodeId.getNodeIdentifier().getPrefix())) {
637 id = "/" + tempNodeId.getNodeIdentifier().getPrefix() + ":" + tempNodeId.getNodeIdentifier()
638 .getName();
639 } else {
640 id = "/" + tempNodeId.getNodeIdentifier().getName();
641 }
642 break;
643 default:
644 id = "/" + tempNodeId.getNodeIdentifier().getName();
645 break;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530646 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530647 builder.append(id);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530648 } else {
649 return builder.toString();
650 }
651 }
652 return null;
653 }
654
655 /**
656 * Searches augment node in root node.
657 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530658 * @param node root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530659 * @param tempNodeId node identifier
660 * @return target augment node
661 */
Bharat saraswalb551aae2016-07-14 15:18:20 +0530662
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530663 private YangNode searchAugmentNode(YangNode node, String tempNodeId) {
664 node = node.getChild();
665 while (node != null) {
666 if (node instanceof YangAugment) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530667 if (node.getName().equals(tempNodeId)) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530668 return node;
669 }
670 }
671 node = node.getNextSibling();
672 }
673 return null;
674 }
675
676 /**
677 * Validates for target node if target node found or not.
678 *
679 * @param tempNode temporary node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530680 * @param index current index of list
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530681 * @return false if target node found
682 */
683 private boolean validate(YangNode tempNode, int index) {
684
685 int size = getAbsPaths().size();
686 if (tempNode != null && index != size) {
687 return true;
Bharat saraswalb551aae2016-07-14 15:18:20 +0530688 } else if (tempNode != null) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530689 return false;
690 // this is your target node.
Bharat saraswalb551aae2016-07-14 15:18:20 +0530691 } else if (index != size) {
692 return true;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530693 // this could be in submodule as well.
694 }
695 return false;
696 }
697
698 /**
699 * Searches target node in root node.
700 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530701 * @param node root node
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530702 * @param curNodeId YANG node identifier
703 * @return linked target node
704 */
705 private YangNode searchTargetNode(YangNode node, YangNodeIdentifier curNodeId) {
706
707 if (node != null) {
708 node = node.getChild();
709 }
710
711 while (node != null) {
712 if (node.getName().equals(curNodeId.getName())) {
713 return node;
714 }
715 node = node.getNextSibling();
716 }
717 return null;
718 }
719
720 /**
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530721 * Returns root prefix.
722 *
723 * @param root root node
724 * @return root prefix
725 */
726 private String getRootsPrefix(YangNode root) {
727 if (root instanceof YangModule) {
728 return ((YangModule) root).getPrefix();
729 } else {
730 return ((YangSubModule) root).getPrefix();
731 }
732 }
733
Bharat saraswalb551aae2016-07-14 15:18:20 +0530734 /**
735 * Resolves prefix and provides prefix resolver list.
736 *
737 * @param absolutePaths absolute paths
738 */
739 private void parsePrefixResolverList(List<YangAtomicPath> absolutePaths) {
740 Iterator<YangAtomicPath> pathIterator = absolutePaths.iterator();
741 YangAtomicPath absPath;
742 String prePrefix;
743 String curPrefix = null;
744 while (pathIterator.hasNext()) {
745 prePrefix = curPrefix;
746 absPath = pathIterator.next();
747 curPrefix = absPath.getNodeIdentifier().getPrefix();
748 if (curPrefix != null) {
749 if (!curPrefix.equals(prePrefix)) {
750 if (prePrefix != null) {
751 addToPrefixResolverList(INTER_TO_INTER, absPath);
752 } else {
753 addToPrefixResolverList(INTRA_TO_INTER, absPath);
754 }
755 } else {
756 addToPrefixResolverList(NO_PREFIX_CHANGE_FOR_INTER, absPath);
757 }
758 } else {
759 if (prePrefix != null) {
760 addToPrefixResolverList(INTER_TO_INTRA, absPath);
761 } else {
762 addToPrefixResolverList(NO_PREFIX_CHANGE_FOR_INTRA, absPath);
763 }
764 }
765 }
766
767 }
768
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530769}