blob: c7a9bbfba37e2a5614ef2cee0674976d55b85f4a [file] [log] [blame]
chengfan0c32d712016-10-11 21:03:23 +08001/*
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.protocol.restconf.server.utils.parser.api;
18
19import org.onosproject.yms.ydt.YdtContext;
20import org.onosproject.yms.ydt.YdtContextOperationType;
21import org.onosproject.yms.ydt.YdtExtendedInfoType;
22import org.onosproject.yms.ydt.YdtType;
23
24import java.util.HashSet;
25import java.util.Set;
26
27/**
28 * A test class which represents a general instance node in YANG data tree.
29 */
30public class TestYdtNode implements YdtContext {
31
32 private TestYdtNode parent;
33 private TestYdtNode child;
34 private TestYdtNode nextSibling;
35 private TestYdtNode previousSibling;
36 private TestYdtNode lastChild;
37 private YdtType ydtType;
38 private String value;
39 private final Set<String> valueSet = new HashSet<>();
40 private TestYangSchemaId id;
41 private YdtContextOperationType ydtContextOperationType;
42
43 /**
44 * Creates a general YANG instance node object.
45 *
46 * @param id node identifier of YDT multi instance node .
47 * @param ydtType type of YDT node to be added
48 */
49 public TestYdtNode(TestYangSchemaId id, YdtType ydtType) {
50 this.id = id;
51 this.ydtType = ydtType;
52 }
53
54 @Override
55 public String getName() {
56 return id.getName();
57 }
58
59 @Override
60 public String getNamespace() {
61 return id.getNameSpace();
62 }
63
64 @Override
sonu guptaeff184b2016-11-24 12:43:49 +053065 public String getModuleNameAsNameSpace() {
66 return null;
67 }
68
69 @Override
chengfan0c32d712016-10-11 21:03:23 +080070 public <T> T getYdtContextExtendedInfo() {
71 return null;
72 }
73
74 @Override
75 public YdtExtendedInfoType getYdtExtendedInfoType() {
76 return null;
77 }
78
79 @Override
80 public YdtType getYdtType() {
81 return ydtType;
82 }
83
84 @Override
85 public TestYdtNode getParent() {
86 return parent;
87 }
88
89 @Override
90 public TestYdtNode getFirstChild() {
91 return child;
92 }
93
94 @Override
95 public TestYdtNode getLastChild() {
96 return lastChild;
97 }
98
99 @Override
100 public TestYdtNode getNextSibling() {
101 return nextSibling;
102 }
103
104 @Override
105 public YdtContext getPreviousSibling() {
106 return previousSibling;
107 }
108
109 @Override
110 public String getValue() {
111 return value;
112 }
113
114 @Override
115 public Set<String> getValueSet() {
116 return valueSet;
117 }
118
119 /**
120 * Sets the parent of node.
121 *
122 * @param parent node
123 */
124 public void setParent(TestYdtNode parent) {
125 this.parent = parent;
126 }
127
128 /**
129 * Sets the first instance of a child node.
130 *
131 * @param child is only child to be set
132 */
133 public void setChild(TestYdtNode child) {
134 this.child = child;
135 }
136
137 /**
138 * Sets the last instance of a child node.
139 *
140 * @param child is last child to be set
141 */
142 public void setLastChild(TestYdtNode child) {
143 lastChild = child;
144 }
145
146 /**
147 * Sets the next sibling of node.
148 *
149 * @param sibling YANG node
150 */
151 public void setNextSibling(TestYdtNode sibling) {
152 nextSibling = sibling;
153 }
154
155 /**
156 * Sets the previous sibling.
157 *
158 * @param previousSibling points to predecessor sibling
159 */
160 public void setPreviousSibling(TestYdtNode previousSibling) {
161 this.previousSibling = previousSibling;
162 }
163
164 /**
165 * Returns object node identifier.
166 *
167 * @return node identifier
168 */
169 public TestYangSchemaId getYdtNodeIdentifier() {
170 return id;
171 }
172
173 /**
174 * Adds a child node.
175 * The children sibling list will be sorted based on node
176 * type. This will add single child or sub-tree based on isAtomic flag.
177 *
178 * @param newChild refers to a new child to be added
179 */
180 public void addChild(YdtContext newChild) {
181 TestYdtNode node = (TestYdtNode) newChild;
182
183 if (node.getParent() == null) {
184 node.setParent(this);
185 }
186
187 // If new node needs to be added as first child.
188 if (getFirstChild() == null) {
189 setChild(node);
190 setLastChild(node);
191 return;
192 }
193
194 // If new node needs to be added as last child.
195 TestYdtNode curNode = getLastChild();
196 curNode.setNextSibling(node);
197 node.setPreviousSibling(curNode);
198 setLastChild(node);
199 }
200
201 /**
202 * Adds the given value to the non single instance leaf node.
203 *
204 * @param value value in a single instance node
205 */
206 public void addValue(String value) {
207 this.value = value;
208 }
209
210 /**
211 * Adds the given valueSet to the non multi instance leaf node.
212 *
213 * @param values value set in a multi instance leaf node
214 */
215 public void addValueSet(Set<String> values) {
216 valueSet.addAll(values);
217 }
218
219
220 /**
221 * Sets the context operation type for the YDT node.
222 *
223 * @param opType context operation type
224 */
225 public void setYdtContextOperationType(YdtContextOperationType opType) {
226 this.ydtContextOperationType = opType;
227 }
228
229 /**
230 * Returns the context operation type for the YDT node.
231 *
232 * @return context operation type
233 */
234 public YdtContextOperationType getYdtContextOperationType() {
235 return ydtContextOperationType;
236 }
237}