blob: f75fc9ce20044d51edea100e8797808026ffe005 [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
19import org.onosproject.yangutils.datamodel.YangAugment;
20import org.onosproject.yangutils.datamodel.YangSchemaNode;
21import org.onosproject.yangutils.datamodel.YangSchemaNodeContextInfo;
22import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier;
23import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
24import org.onosproject.yms.app.ydt.exceptions.YdtException;
25import org.onosproject.yms.ydt.YdtContext;
26import org.onosproject.yms.ydt.YdtContextOperationType;
27
28import java.util.HashSet;
29import java.util.List;
30import java.util.Set;
31
32import static org.onosproject.yms.app.ydt.YdtAppNodeOperationType.BOTH;
33import static org.onosproject.yms.app.ydt.YdtNodeFactory.getAppOpTypeFromYdtOpType;
34
35/**
36 * Represents YANG request work bench which contains all parameters for
37 * request handling and methods to build and obtain YANG application data tree
38 * which is data (sub)instance representation, abstract of protocol.
39 */
40public final class DefaultYdtAppContext<T extends AppData>
41 implements YdtAppContext {
42
43 /*
44 * Parent reference.
45 */
46 private YdtAppContext parent;
47
48 /*
49 * First child reference.
50 */
51 private YdtAppContext child;
52
53 /*
54 * Next sibling reference.
55 */
56 private YdtAppContext nextSibling;
57
58 /*
59 * Previous sibling reference.
60 */
61 private YdtAppContext previousSibling;
62
63 /*
64 * Last child reference.
65 */
66 private YdtAppContext lastChild;
67
68 /*
69 * YDT application tree extended information.
70 */
71 private T appData;
72
73 /*
74 * Reference for operation type for application root node.
75 */
76 private YdtAppNodeOperationType operationType;
77
78 /*
79 * Reference application node set.
80 */
81 private Set<YangSchemaNode> appSet;
82
83 /**
84 * Creates an instance of YANG application tree which is used by all node
85 * needs delete list.
86 */
87 private DefaultYdtAppContext() {
88 appSet = new HashSet<>();
89 }
90
91 /**
92 * Adds schema node of new requested augmented node in current context of
93 * application tree.
94 *
95 * @param schemaNode schema node of requested node
96 * @return addition result(true/false)
97 */
98 public boolean addSchemaToAppSet(YangSchemaNode schemaNode) {
99 return appSet.add(schemaNode);
100 }
101
102 @Override
103 public void updateAppOperationType(YdtContextOperationType ydtOpType) {
104 if (parent == null) {
105 return;
106 }
107 YdtAppNodeOperationType opType = getAppOpTypeFromYdtOpType(ydtOpType);
108 YdtAppContext curNode = this;
109 YdtAppNodeOperationType parentOpType = operationType;
110 if (parentOpType != null && opType != parentOpType) {
111 while (curNode.getOperationType() != BOTH &&
112 curNode.getParent() != null) {
113 curNode.setOperationType(BOTH);
114 curNode = curNode.getParent();
115 }
116 }
117 }
118
119 @Override
120 public void setAppData(YdtNode moduleNode, YangSchemaNode augmentNode) {
121 if (augmentNode != null) {
122 appData.setAugmentingSchemaNode(augmentNode);
123 } else {
124 appData.setModuleContext(moduleNode);
125 }
126 }
127
128 @Override
129 public AppData getAppData() {
130 return appData;
131 }
132
133 @Override
134 public YdtAppContext getParent() {
135 return parent;
136 }
137
138 @Override
139 public void setParent(YdtAppContext parent) {
140 this.parent = parent;
141 }
142
143 @Override
144 public YdtAppContext getFirstChild() {
145 return child;
146 }
147
148 @Override
149 public void setChild(YdtAppContext child) {
150 this.child = child;
151 }
152
153 @Override
154 public YdtAppContext getNextSibling() {
155 return nextSibling;
156 }
157
158 @Override
159 public void setNextSibling(YdtAppContext nextSibling) {
160 this.nextSibling = nextSibling;
161 }
162
163 @Override
164 public YdtAppContext getPreviousSibling() {
165 return previousSibling;
166 }
167
168 @Override
169 public void setPreviousSibling(YdtAppContext previousSibling) {
170 this.previousSibling = previousSibling;
171 }
172
173 @Override
174 public YdtAppNodeOperationType getOperationType() {
175 return operationType;
176 }
177
178 @Override
179 public void setOperationType(YdtAppNodeOperationType opType) {
180 operationType = opType;
181 }
182
183 @Override
184 public List<YdtContext> getDeleteNodes() {
185 return appData.getDeleteNodes();
186 }
187
188
189 @Override
190 public void addDeleteNode(YdtNode node) {
191 DefaultYdtAppContext<?> curNode = this;
192 while (curNode.getParent().getParent() != null) {
193 curNode = (DefaultYdtAppContext<?>) curNode.getParent();
194 }
195
196 curNode.appData.addDeleteNodes(node);
197 }
198
199 @Override
200 public YdtContext getModuleContext() {
201 return appData.getModuleContext();
202 }
203
204 @Override
205 public void setModuleContext(YdtContext moduleNode) {
206 appData.setModuleContext(moduleNode);
207 }
208
209 @Override
210 public YangSchemaNode getAugmentingSchemaNode() {
211 return appData.getAugmentingSchemaNode();
212 }
213
214 @Override
215 public void setAugmentingSchemaNode(YangSchemaNode schemaNode) {
216 appData.setAugmentingSchemaNode(schemaNode);
217 }
218
219
220 @Override
221 public YangSchemaNode getAugmentingSchemaNode(
222 YangSchemaNodeIdentifier id,
223 YangSchemaNodeContextInfo contextInfo) {
224 YangSchemaNode lastAugMod = null;
225 YangSchemaNode switchedNode =
226 contextInfo.getContextSwitchedNode();
227
228 while (switchedNode != null) {
229 if (switchedNode instanceof YangAugment) {
230 lastAugMod = switchedNode;
231 }
232 try {
233 switchedNode = switchedNode.getChildSchema(id)
234 .getContextSwitchedNode();
235 } catch (DataModelException e) {
236 throw new YdtException(e.getMessage());
237 }
238 }
239 return lastAugMod;
240 }
241
242 @Override
243 public YdtAppContext getLastChild() {
244 return lastChild;
245 }
246
247 @Override
248 public void setLastChild(YdtAppContext lastChild) {
249 this.lastChild = lastChild;
250 }
251
252 @Override
253 public void addChild(YdtAppContext newChild) {
254
255 if (newChild.getParent() == null) {
256 newChild.setParent(this);
257 }
258
259 // First child to be added.
260 if (getFirstChild() == null) {
261 setChild(newChild);
262 // Update last child.
263 setLastChild(newChild);
264 return;
265 }
266
267 // If the new node needs to be add as last child.
268 YdtAppContext curNode = getLastChild();
269 curNode.setNextSibling(newChild);
270 newChild.setPreviousSibling(curNode);
271 setLastChild(newChild);
272 }
273
274 @Override
275 public YangSchemaNode getYangSchemaNode() {
276 return appData.getSchemaNode();
277 }
278
279 /**
280 * Creates an instance of application tree context with module schema data.
281 *
282 * @return application tree context
283 */
284 public static DefaultYdtAppContext getModuleAppContext() {
285 DefaultYdtAppContext context =
286 new DefaultYdtAppContext<ModuleSchemaData>();
287 context.appData = new ModuleSchemaData();
288 return context;
289 }
290
291 /**
292 * Creates an instance of application tree context with augment schema data.
293 *
294 * @return application tree context
295 */
296 public static DefaultYdtAppContext getAugmentAppContext() {
297 DefaultYdtAppContext context =
298 new DefaultYdtAppContext<AugmentedSchemaData>();
299 context.appData = new AugmentedSchemaData();
300 return context;
301 }
302}