blob: 03502d49d831d714814e9fa4c2aad34f5425b30c [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
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.
74 */
sonu guptaeff184b2016-11-24 12:43:49 +053075 DefaultYdtAppContext(T data) {
76 appData = data;
sonu gupta1bb37b82016-11-11 16:51:18 +053077 }
78
79 @Override
80 public void updateAppOperationType(YdtContextOperationType ydtOpType) {
81 if (parent == null) {
82 return;
83 }
84 YdtAppNodeOperationType opType = getAppOpTypeFromYdtOpType(ydtOpType);
85 YdtAppContext curNode = this;
86 YdtAppNodeOperationType parentOpType = operationType;
87 if (parentOpType != null && opType != parentOpType) {
88 while (curNode.getOperationType() != BOTH &&
89 curNode.getParent() != null) {
90 curNode.setOperationType(BOTH);
91 curNode = curNode.getParent();
92 }
93 }
94 }
95
96 @Override
97 public void setAppData(YdtNode moduleNode, YangSchemaNode augmentNode) {
98 if (augmentNode != null) {
sonu guptaeff184b2016-11-24 12:43:49 +053099 ((AugmentAppData) appData).setAugmentingSchemaNode(augmentNode);
sonu gupta1bb37b82016-11-11 16:51:18 +0530100 } else {
sonu guptaeff184b2016-11-24 12:43:49 +0530101 ((ModuleAppData) appData).setModuleContext(moduleNode);
sonu gupta1bb37b82016-11-11 16:51:18 +0530102 }
103 }
104
105 @Override
106 public AppData getAppData() {
107 return appData;
108 }
109
110 @Override
111 public YdtAppContext getParent() {
112 return parent;
113 }
114
115 @Override
116 public void setParent(YdtAppContext parent) {
117 this.parent = parent;
118 }
119
120 @Override
121 public YdtAppContext getFirstChild() {
122 return child;
123 }
124
sonu guptaeff184b2016-11-24 12:43:49 +0530125 /**
126 * Sets the context of first child.
127 *
128 * @param child node
129 */
130 private void setChild(YdtAppContext child) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530131 this.child = child;
132 }
133
134 @Override
135 public YdtAppContext getNextSibling() {
136 return nextSibling;
137 }
138
139 @Override
sonu guptaeff184b2016-11-24 12:43:49 +0530140 public void setNextSibling(YdtAppContext context) {
141 this.nextSibling = context;
sonu gupta1bb37b82016-11-11 16:51:18 +0530142 }
143
144 @Override
145 public YdtAppContext getPreviousSibling() {
146 return previousSibling;
147 }
148
149 @Override
sonu guptaeff184b2016-11-24 12:43:49 +0530150 public void setPreviousSibling(YdtAppContext context) {
151 this.previousSibling = context;
sonu gupta1bb37b82016-11-11 16:51:18 +0530152 }
153
154 @Override
155 public YdtAppNodeOperationType getOperationType() {
156 return operationType;
157 }
158
159 @Override
160 public void setOperationType(YdtAppNodeOperationType opType) {
161 operationType = opType;
162 }
163
164 @Override
165 public List<YdtContext> getDeleteNodes() {
sonu guptaeff184b2016-11-24 12:43:49 +0530166 return ((ModuleAppData) appData).getDeleteNodes();
sonu gupta1bb37b82016-11-11 16:51:18 +0530167 }
168
sonu gupta1bb37b82016-11-11 16:51:18 +0530169 @Override
170 public void addDeleteNode(YdtNode node) {
171 DefaultYdtAppContext<?> curNode = this;
172 while (curNode.getParent().getParent() != null) {
173 curNode = (DefaultYdtAppContext<?>) curNode.getParent();
174 }
sonu guptaeff184b2016-11-24 12:43:49 +0530175 ((ModuleAppData) curNode.appData).addDeleteNodes(node);
sonu gupta1bb37b82016-11-11 16:51:18 +0530176 }
177
178 @Override
179 public YdtContext getModuleContext() {
sonu guptaeff184b2016-11-24 12:43:49 +0530180 return ((ModuleAppData) appData).getModuleContext();
sonu gupta1bb37b82016-11-11 16:51:18 +0530181 }
182
183 @Override
184 public YangSchemaNode getAugmentingSchemaNode() {
sonu guptaeff184b2016-11-24 12:43:49 +0530185 return ((AugmentAppData) appData).getAugmentingSchemaNode();
sonu gupta1bb37b82016-11-11 16:51:18 +0530186 }
187
188 @Override
189 public void setAugmentingSchemaNode(YangSchemaNode schemaNode) {
sonu guptaeff184b2016-11-24 12:43:49 +0530190 ((AugmentAppData) appData).setAugmentingSchemaNode(schemaNode);
sonu gupta1bb37b82016-11-11 16:51:18 +0530191 }
192
193 @Override
194 public YdtAppContext getLastChild() {
195 return lastChild;
196 }
197
sonu guptaeff184b2016-11-24 12:43:49 +0530198 /**
199 * Sets the context of last child.
200 *
201 * @param child node
202 */
203 private void setLastChild(YdtAppContext child) {
204 lastChild = child;
sonu gupta1bb37b82016-11-11 16:51:18 +0530205 }
206
207 @Override
208 public void addChild(YdtAppContext newChild) {
209
210 if (newChild.getParent() == null) {
211 newChild.setParent(this);
212 }
213
214 // First child to be added.
215 if (getFirstChild() == null) {
216 setChild(newChild);
217 // Update last child.
218 setLastChild(newChild);
219 return;
220 }
221
222 // If the new node needs to be add as last child.
223 YdtAppContext curNode = getLastChild();
224 curNode.setNextSibling(newChild);
225 newChild.setPreviousSibling(curNode);
226 setLastChild(newChild);
227 }
228
229 @Override
230 public YangSchemaNode getYangSchemaNode() {
231 return appData.getSchemaNode();
232 }
sonu gupta1bb37b82016-11-11 16:51:18 +0530233}