blob: b9da3c2104c6cfdd36d53f39829e7b95153cd387 [file] [log] [blame]
Vinod Kumar S2ff139c2016-02-16 01:37:16 +05301/*
2 * Copyright 2016 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 */
16package org.onosproject.yangutils.datamodel;
17
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053018import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
19import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
20import org.onosproject.yangutils.parser.Parsable;
21import org.onosproject.yangutils.translator.CachedFileHandle;
22import org.onosproject.yangutils.utils.YangConstructType;
23import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
24
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053025import java.util.LinkedList;
26import java.util.List;
27
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053028/*-
29 * Reference RFC 6020.
30 *
31 * The "case" statement is used to define branches of the choice. It takes as an
32 * argument an identifier, followed by a block of sub-statements that holds
33 * detailed case information.
34 *
35 * The identifier is used to identify the case node in the schema tree. A case
36 * node does not exist in the data tree.
37 *
38 * Within a "case" statement, the "anyxml", "choice", "container", "leaf",
39 * "list", "leaf-list", and "uses" statements can be used to define child nodes
40 * to the case node. The identifiers of all these child nodes MUST be unique
41 * within all cases in a choice. For example, the following is illegal:
42 *
43 * choice interface-type { // This example is illegal YANG
44 * case a {
45 * leaf ethernet { ... }
46 * }
47 * case b {
48 * container ethernet { ...}
49 * }
50 * }
51 *
52 * As a shorthand, the "case" statement can be omitted if the branch
53 * contains a single "anyxml", "container", "leaf", "list", or
54 * "leaf-list" statement. In this case, the identifier of the case node
55 * is the same as the identifier in the branch statement. The following
56 * example:
57 *
58 * choice interface-type {
59 * container ethernet { ... }
60 * }
61 *
62 * is equivalent to:
63 *
64 * choice interface-type {
65 * case ethernet {
66 * container ethernet { ... }
67 * }
68 * }
69 *
70 * The case identifier MUST be unique within a choice.
71 *
72 * The case's sub-statements
73 *
74 * +--------------+---------+-------------+------------------+
75 * | substatement | section | cardinality |data model mapping|
76 * +--------------+---------+-------------+------------------+
77 * | anyxml | 7.10 | 0..n |-not supported |
78 * | choice | 7.9 | 0..n |-child nodes |
79 * | container | 7.5 | 0..n |-child nodes |
80 * | description | 7.19.3 | 0..1 |-string |
81 * | if-feature | 7.18.2 | 0..n |-TODO |
82 * | leaf | 7.6 | 0..n |-YangLeaf |
83 * | leaf-list | 7.7 | 0..n |-YangLeafList |
84 * | list | 7.8 | 0..n |-child nodes |
85 * | reference | 7.19.4 | 0..1 |-string |
86 * | status | 7.19.2 | 0..1 |-YangStatus |
87 * | uses | 7.12 | 0..n |-child node |
88 * | when | 7.19.5 | 0..1 |-TODO |
89 * +--------------+---------+-------------+------------------+
90 */
91/**
92 * Data model node to maintain information defined in YANG case.
93 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053094public class YangCase extends YangNode
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053095 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053096
97 /**
98 * Case name.
99 */
100 private String name;
101
102 // TODO: default field identification for the case
103
104 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530105 * Description of case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530106 */
107 private String description;
108
109 /**
110 * List of leaves.
111 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530112 private List<YangLeaf> listOfLeaf;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530113
114 /**
115 * List of leaf lists.
116 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530117 private List<YangLeafList> listOfLeafList;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530118
119 /**
120 * Reference of the module.
121 */
122 private String reference;
123
124 /**
125 * Status of the node.
126 */
127 private YangStatusType status;
128
129 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530130 * Package of the generated java code.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530131 */
132 private String pkg;
133
134 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530135 * Create a choice node.
136 */
137 public YangCase() {
138 super(YangNodeType.CASE_NODE);
139 }
140
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530141 /**
142 * Get the case name.
143 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530144 * @return case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530145 */
146 @Override
147 public String getName() {
148 return name;
149 }
150
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530151 /**
152 * Set the case name.
153 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530154 * @param name case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530155 */
156 @Override
157 public void setName(String name) {
158 this.name = name;
159 }
160
161 /**
162 * Get the description.
163 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530164 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530165 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530166 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530167 public String getDescription() {
168 return description;
169 }
170
171 /**
172 * Set the description.
173 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530174 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530175 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530176 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530177 public void setDescription(String description) {
178 this.description = description;
179 }
180
181 /**
182 * Get the list of leaves.
183 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530184 * @return the list of leaves
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530185 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530186 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530187 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530188 return listOfLeaf;
189 }
190
191 /**
192 * Set the list of leaves.
193 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530194 * @param leafsList the list of leaf to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530195 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530196 private void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530197 listOfLeaf = leafsList;
198 }
199
200 /**
201 * Add a leaf.
202 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530203 * @param leaf the leaf to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530204 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530205 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530206 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530207 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530208 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530209 }
210
211 getListOfLeaf().add(leaf);
212 }
213
214 /**
215 * Get the list of leaf-list.
216 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530217 * @return the list of leaf-list
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530218 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530219 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530220 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530221 return listOfLeafList;
222 }
223
224 /**
225 * Set the list of leaf-list.
226 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530227 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530228 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530229 private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530230 this.listOfLeafList = listOfLeafList;
231 }
232
233 /**
234 * Add a leaf-list.
235 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530236 * @param leafList the leaf-list to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530237 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530238 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530239 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530240 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530241 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530242 }
243
244 getListOfLeafList().add(leafList);
245 }
246
247 /**
248 * Get the textual reference.
249 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530250 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530251 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530252 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530253 public String getReference() {
254 return reference;
255 }
256
257 /**
258 * Set the textual reference.
259 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530260 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530261 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530262 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530263 public void setReference(String reference) {
264 this.reference = reference;
265 }
266
267 /**
268 * Get the status.
269 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530270 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530271 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530272 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530273 public YangStatusType getStatus() {
274 return status;
275 }
276
277 /**
278 * Set the status.
279 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530280 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530281 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530282 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530283 public void setStatus(YangStatusType status) {
284 this.status = status;
285 }
286
287 /**
288 * Returns the type of the data.
289 *
290 * @return returns CASE_DATA
291 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530292 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530293 public YangConstructType getYangConstructType() {
294 return YangConstructType.CASE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530295 }
296
297 /**
298 * Validate the data on entering the corresponding parse tree node.
299 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530300 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530301 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530302 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530303 public void validateDataOnEntry() throws DataModelException {
304 // TODO auto-generated method stub, to be implemented by parser
305 }
306
307 /**
308 * Validate the data on exiting the corresponding parse tree node.
309 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530310 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530311 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530312 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530313 public void validateDataOnExit() throws DataModelException {
314 // TODO auto-generated method stub, to be implemented by parser
315 }
316
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530317 /**
318 * Get the mapped java package.
319 *
320 * @return the java package
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530321 */
322 @Override
323 public String getPackage() {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530324 return pkg;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530325 }
326
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530327 /**
328 * Set the mapped java package.
329 *
330 * @param pakg the package to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530331 */
332 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530333 public void setPackage(String pakg) {
334 pkg = pakg;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530335
336 }
337
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530338 /**
339 * Generate the code corresponding to YANG case info.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530340 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530341 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530342 public void generateJavaCodeEntry() {
343 // TODO Auto-generated method stub
344
345 }
346
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530347 /**
348 * Free resource used for generating code and generate valid java files
349 * corresponding to YANG case info.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530350 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530351 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530352 public void generateJavaCodeExit() {
353 // TODO Auto-generated method stub
354
355 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530356
357 @Override
358 public CachedFileHandle getFileHandle() {
359 // TODO Auto-generated method stub
360 return null;
361 }
362
363 @Override
364 public void setFileHandle(CachedFileHandle fileHandle) {
365 // TODO Auto-generated method stub
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530366 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530367
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530368 @Override
369 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
370 if ((this.getParent() == null) || (!(this.getParent() instanceof YangChoice))) {
371 throw new DataModelException("Internal Data Model Tree Error: Invalid/Missing holder in case " +
372 this.getName());
373 }
374 // Traverse up in tree to ask parent choice start collision detection.
375 ((CollisionDetector) this.getParent()).detectCollidingChild(identifierName, dataType);
376 }
377
378 @Override
379 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
380
381 if (dataType == CASE_DATA) {
382 if (this.getName().equals(identifierName)) {
383 throw new DataModelException("YANG File Error: Identifier collision detected in case \"" +
384 this.getName() + "\"");
385 }
386 return;
387 }
388
389 // Asks helper to detect colliding child.
390 detectCollidingChildUtil(identifierName, dataType, this);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530391 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530392}