blob: bb692cf050e457f23ba2f2c040338e03cd3cdab6 [file] [log] [blame]
sonu gupta1bb37b82016-11-11 16:51: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.yms.app.ydt;
18
19
20import org.onosproject.yms.app.utils.TraversalType;
21import org.onosproject.yms.ydt.YdtContext;
22import org.onosproject.yms.ydt.YdtListener;
23
24import static org.onosproject.yms.app.utils.TraversalType.CHILD;
25import static org.onosproject.yms.app.utils.TraversalType.PARENT;
26import static org.onosproject.yms.app.utils.TraversalType.ROOT;
27import static org.onosproject.yms.app.utils.TraversalType.SIBLING;
28
29/**
30 * Represents implementation of YDT walker, which walks the YDT.
31 */
32public class DefaultYdtWalker implements YdtExtendedWalker {
33
34 @Override
35 public void walk(YdtListener ydtListener, YdtContext rootNode) {
36 walkTree(ydtListener, rootNode, false);
37 }
38
39 /**
40 * Walks the YANG data tree till the node provided by the user.
41 * Protocols implements YDT listener and YDT Extended Listener and
42 * walks YDT tree with input as implemented object.
43 * YDT walker provides call backs to implemented methods.
44 *
45 * @param ydtListener YDT listener implemented by the protocol
46 * @param rootNode root node of YDT
47 * @param isExtended flag denotes the call type
48 */
49 private void walkTree(YdtListener ydtListener, YdtContext rootNode,
50 boolean isExtended) {
51 YdtContext curNode = rootNode;
52 TraversalType curTraversal = ROOT;
53
54 while (curNode != null) {
55 if (curTraversal != PARENT) {
56
57 // Visit (curNode) for entry callback
58 if (isExtended) {
59 ((YdtExtendedListener) ydtListener)
60 .enterYdtNode((YdtExtendedContext) curNode);
61 } else {
62 ydtListener.enterYdtNode(curNode);
63 }
64 }
65 if (curTraversal != PARENT &&
66 curNode.getFirstChild() != null) {
67 curTraversal = CHILD;
68 curNode = curNode.getFirstChild();
69 } else if (curNode.getNextSibling() != null) {
70 // Revisit (curNode) for exit callback
71 exitCallBack(ydtListener, curNode, isExtended);
72
73 /*
74 *Stop traversing the tree , tree need to be traversed
75 * till user requested node
76 */
77 if (curNode.equals(rootNode)) {
78 return;
79 }
80 curTraversal = SIBLING;
81 curNode = curNode.getNextSibling();
82 } else {
83 // Revisit (curNode) for exit callback
84 exitCallBack(ydtListener, curNode, isExtended);
85
86 /*
87 *Stop traversing the tree , tree need to be traversed
88 * till user requested node
89 */
90 if (curNode.equals(rootNode)) {
91 return;
92 }
93
94 curTraversal = PARENT;
95 curNode = curNode.getParent();
96 }
97 }
98 }
99
100 /**
101 * Provides exit call back per node on the basis of extended flag,
102 * If isExtended set then YdtExtendedListener exit node call back will
103 * be provided else YdtListener with respective type of context
104 * (YdtContext/YdtExtendedContext).
105 *
106 * @param ydtListener YDT listener implemented by the protocol
107 * @param curNode current node of YDT
108 * @param isExtended flag denotes the call type
109 */
110 private void exitCallBack(YdtListener ydtListener, YdtContext curNode,
111 boolean isExtended) {
112 if (isExtended) {
113 ((YdtExtendedListener) ydtListener)
114 .exitYdtNode((YdtExtendedContext) curNode);
115 } else {
116 ydtListener.exitYdtNode(curNode);
117 }
118 }
119
120 @Override
121 public void walk(YdtExtendedListener ydtExtendedListener,
122 YdtExtendedContext rootNode) {
123 walkTree(ydtExtendedListener, rootNode, true);
124 }
125}