blob: 6f9dbebe732c73bfc732199b01bf861e1592f30a [file] [log] [blame]
sonu gupta1bb37b82016-11-11 16:51:18 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sonu gupta1bb37b82016-11-11 16:51:18 +05303 *
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
sonu gupta1bb37b82016-11-11 16:51:18 +053019import org.onosproject.yangutils.datamodel.YangSchemaNode;
sonu gupta1bb37b82016-11-11 16:51:18 +053020import org.onosproject.yms.ydt.YdtContext;
21import org.onosproject.yms.ydt.YdtContextOperationType;
22
sonu gupta1bb37b82016-11-11 16:51:18 +053023import java.util.List;
sonu gupta1bb37b82016-11-11 16:51:18 +053024
25import static org.onosproject.yms.app.ydt.YdtAppNodeOperationType.BOTH;
sonu guptaeff184b2016-11-24 12:43:49 +053026import static org.onosproject.yms.app.ydt.YdtUtils.getAppOpTypeFromYdtOpType;
sonu gupta1bb37b82016-11-11 16:51:18 +053027
28/**
sonu guptaeff184b2016-11-24 12:43:49 +053029 * Abstraction of an entity which represents YDT application context
30 * information. This context information will be used by protocol to obtain
31 * the information associated with YDT application tree node.
sonu gupta1bb37b82016-11-11 16:51:18 +053032 */
33public final class DefaultYdtAppContext<T extends AppData>
34 implements YdtAppContext {
35
36 /*
37 * Parent reference.
38 */
39 private YdtAppContext parent;
40
41 /*
42 * First child reference.
43 */
44 private YdtAppContext child;
45
46 /*
47 * Next sibling reference.
48 */
49 private YdtAppContext nextSibling;
50
51 /*
52 * Previous sibling reference.
53 */
54 private YdtAppContext previousSibling;
55
56 /*
57 * Last child reference.
58 */
59 private YdtAppContext lastChild;
60
61 /*
62 * YDT application tree extended information.
63 */
sonu guptaeff184b2016-11-24 12:43:49 +053064 private final T appData;
sonu gupta1bb37b82016-11-11 16:51:18 +053065
66 /*
67 * Reference for operation type for application root node.
68 */
69 private YdtAppNodeOperationType operationType;
70
sonu gupta1bb37b82016-11-11 16:51:18 +053071 /**
72 * Creates an instance of YANG application tree which is used by all node
73 * needs delete list.
Bharat saraswal1f371982016-11-23 15:32:57 +053074 *
75 * @param data application data
sonu gupta1bb37b82016-11-11 16:51:18 +053076 */
sonu guptaeff184b2016-11-24 12:43:49 +053077 DefaultYdtAppContext(T data) {
78 appData = data;
sonu gupta1bb37b82016-11-11 16:51:18 +053079 }
80
81 @Override
82 public void updateAppOperationType(YdtContextOperationType ydtOpType) {
83 if (parent == null) {
84 return;
85 }
86 YdtAppNodeOperationType opType = getAppOpTypeFromYdtOpType(ydtOpType);
87 YdtAppContext curNode = this;
88 YdtAppNodeOperationType parentOpType = operationType;
Sonu Gupta1f418aa2017-02-23 20:48:46 +053089 if (opType != parentOpType) {
90 if (parentOpType != null) {
91 while (curNode.getOperationType() != BOTH &&
92 curNode.getParent() != null) {
93 curNode.setOperationType(BOTH);
94 curNode = curNode.getParent();
95 }
96 } else {
97 // If operation type for ydt node is "NONE" then in that
98 // case operation type for module node in app tree set as null.
99 // Once the target node operation type received by ydt then
100 // operation type for module node will be updated with the
101 // same target node operation type in app tree.
102 while (curNode.getParent() != null && curNode
103 .getOperationType() == null) {
104 curNode.setOperationType(opType);
105 curNode = curNode.getParent();
106 }
sonu gupta1bb37b82016-11-11 16:51:18 +0530107 }
108 }
109 }
110
111 @Override
112 public void setAppData(YdtNode moduleNode, YangSchemaNode augmentNode) {
113 if (augmentNode != null) {
sonu guptaeff184b2016-11-24 12:43:49 +0530114 ((AugmentAppData) appData).setAugmentingSchemaNode(augmentNode);
sonu gupta1bb37b82016-11-11 16:51:18 +0530115 } else {
sonu guptaeff184b2016-11-24 12:43:49 +0530116 ((ModuleAppData) appData).setModuleContext(moduleNode);
sonu gupta1bb37b82016-11-11 16:51:18 +0530117 }
118 }
119
120 @Override
121 public AppData getAppData() {
122 return appData;
123 }
124
125 @Override
126 public YdtAppContext getParent() {
127 return parent;
128 }
129
130 @Override
131 public void setParent(YdtAppContext parent) {
132 this.parent = parent;
133 }
134
135 @Override
136 public YdtAppContext getFirstChild() {
137 return child;
138 }
139
sonu guptaeff184b2016-11-24 12:43:49 +0530140 /**
141 * Sets the context of first child.
142 *
143 * @param child node
144 */
145 private void setChild(YdtAppContext child) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530146 this.child = child;
147 }
148
149 @Override
150 public YdtAppContext getNextSibling() {
151 return nextSibling;
152 }
153
154 @Override
sonu guptaeff184b2016-11-24 12:43:49 +0530155 public void setNextSibling(YdtAppContext context) {
156 this.nextSibling = context;
sonu gupta1bb37b82016-11-11 16:51:18 +0530157 }
158
159 @Override
160 public YdtAppContext getPreviousSibling() {
161 return previousSibling;
162 }
163
164 @Override
sonu guptaeff184b2016-11-24 12:43:49 +0530165 public void setPreviousSibling(YdtAppContext context) {
166 this.previousSibling = context;
sonu gupta1bb37b82016-11-11 16:51:18 +0530167 }
168
169 @Override
170 public YdtAppNodeOperationType getOperationType() {
171 return operationType;
172 }
173
174 @Override
175 public void setOperationType(YdtAppNodeOperationType opType) {
176 operationType = opType;
177 }
178
179 @Override
180 public List<YdtContext> getDeleteNodes() {
sonu guptaeff184b2016-11-24 12:43:49 +0530181 return ((ModuleAppData) appData).getDeleteNodes();
sonu gupta1bb37b82016-11-11 16:51:18 +0530182 }
183
sonu gupta1bb37b82016-11-11 16:51:18 +0530184 @Override
185 public void addDeleteNode(YdtNode node) {
186 DefaultYdtAppContext<?> curNode = this;
187 while (curNode.getParent().getParent() != null) {
188 curNode = (DefaultYdtAppContext<?>) curNode.getParent();
189 }
sonu guptaeff184b2016-11-24 12:43:49 +0530190 ((ModuleAppData) curNode.appData).addDeleteNodes(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530191 }
192
193 @Override
194 public YdtContext getModuleContext() {
sonu guptaeff184b2016-11-24 12:43:49 +0530195 return ((ModuleAppData) appData).getModuleContext();
sonu gupta1bb37b82016-11-11 16:51:18 +0530196 }
197
198 @Override
199 public YangSchemaNode getAugmentingSchemaNode() {
sonu guptaeff184b2016-11-24 12:43:49 +0530200 return ((AugmentAppData) appData).getAugmentingSchemaNode();
sonu gupta1bb37b82016-11-11 16:51:18 +0530201 }
202
203 @Override
204 public void setAugmentingSchemaNode(YangSchemaNode schemaNode) {
sonu guptaeff184b2016-11-24 12:43:49 +0530205 ((AugmentAppData) appData).setAugmentingSchemaNode(schemaNode);
sonu gupta1bb37b82016-11-11 16:51:18 +0530206 }
207
208 @Override
209 public YdtAppContext getLastChild() {
210 return lastChild;
211 }
212
sonu guptaeff184b2016-11-24 12:43:49 +0530213 /**
214 * Sets the context of last child.
215 *
216 * @param child node
217 */
218 private void setLastChild(YdtAppContext child) {
219 lastChild = child;
sonu gupta1bb37b82016-11-11 16:51:18 +0530220 }
221
222 @Override
223 public void addChild(YdtAppContext newChild) {
224
225 if (newChild.getParent() == null) {
226 newChild.setParent(this);
227 }
228
229 // First child to be added.
230 if (getFirstChild() == null) {
231 setChild(newChild);
232 // Update last child.
233 setLastChild(newChild);
234 return;
235 }
236
237 // If the new node needs to be add as last child.
238 YdtAppContext curNode = getLastChild();
239 curNode.setNextSibling(newChild);
240 newChild.setPreviousSibling(curNode);
241 setLastChild(newChild);
242 }
243
244 @Override
245 public YangSchemaNode getYangSchemaNode() {
246 return appData.getSchemaNode();
247 }
sonu gupta1bb37b82016-11-11 16:51:18 +0530248}