blob: eb7d6fd3ef439aaa16aa58a09054fd9f459ca1b3 [file] [log] [blame]
Vinod Kumar S2ff139c2016-02-16 01:37:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S2ff139c2016-02-16 01:37:16 +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 */
16package org.onosproject.yangutils.datamodel;
17
18import java.util.LinkedList;
19import java.util.List;
20
Vinod Kumar S38046502016-03-23 15:30:27 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import org.onosproject.yangutils.datamodel.utils.Parsable;
23import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Vinod Kumar S38046502016-03-23 15:30:27 +053024
25import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
Bharat saraswal96dfef02016-06-16 00:29:12 +053026import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CASE_DATA;
Vinod Kumar S38046502016-03-23 15:30:27 +053027
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 |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053081 * | if-feature | 7.18.2 | 0..n |-YangIfFeature |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053082 * | 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 |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053088 * | when | 7.19.5 | 0..1 |-YangWhen |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053089 * +--------------+---------+-------------+------------------+
90 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053091
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053092/**
Bharat saraswald9822e92016-04-05 15:13:44 +053093 * Represents data model node to maintain information defined in YANG case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053094 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053095public class YangCase
96 extends YangNode
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053097 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector, YangAugmentationHolder,
98 YangWhenHolder, YangIfFeatureHolder {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053099
Bharat saraswal96dfef02016-06-16 00:29:12 +0530100 private static final long serialVersionUID = 806201603L;
101
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530102 /**
103 * Case name.
104 */
105 private String name;
106
107 // TODO: default field identification for the case
108
109 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530110 * Description of case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530111 */
112 private String description;
113
114 /**
115 * List of leaves.
116 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530117 private List<YangLeaf> listOfLeaf;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530118
119 /**
120 * List of leaf lists.
121 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530122 private List<YangLeafList> listOfLeafList;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530123
124 /**
125 * Reference of the module.
126 */
127 private String reference;
128
129 /**
130 * Status of the node.
131 */
132 private YangStatusType status;
133
134 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530135 * When data of the node.
136 */
137 private YangWhen when;
138
139 /**
140 * List of if-feature.
141 */
142 private List<YangIfFeature> ifFeatureList;
143
144 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530145 * Creates a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530146 */
147 public YangCase() {
148 super(YangNodeType.CASE_NODE);
149 }
150
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530151 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530152 * Returns the when.
153 *
154 * @return the when
155 */
156 @Override
157 public YangWhen getWhen() {
158 return when;
159 }
160
161 /**
162 * Sets the when.
163 *
164 * @param when the when to set
165 */
166 @Override
167 public void setWhen(YangWhen when) {
168 this.when = when;
169 }
170
171 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530172 * Returns the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530173 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530174 * @return case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530175 */
176 @Override
177 public String getName() {
178 return name;
179 }
180
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530181 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530182 * Sets the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530183 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530184 * @param name case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530185 */
186 @Override
187 public void setName(String name) {
188 this.name = name;
189 }
190
191 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530192 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530193 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530194 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530195 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530196 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530197 public String getDescription() {
198 return description;
199 }
200
201 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530202 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530203 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530204 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530205 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530206 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530207 public void setDescription(String description) {
208 this.description = description;
209 }
210
211 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530212 * Returns the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530213 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530214 * @return the list of leaves
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530215 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530216 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530217 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530218 return listOfLeaf;
219 }
220
221 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530222 * Sets the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530223 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530224 * @param leafsList the list of leaf to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530225 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530226 @Override
227 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530228 listOfLeaf = leafsList;
229 }
230
231 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530232 * Adds a leaf.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530233 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530234 * @param leaf the leaf to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530235 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530236 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530237 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530238 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530239 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530240 }
241
242 getListOfLeaf().add(leaf);
243 }
244
245 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530246 * Returns the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530247 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530248 * @return the list of leaf-list
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530249 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530250 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530251 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530252 return listOfLeafList;
253 }
254
255 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530256 * Sets the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530257 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530258 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530259 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530260 @Override
261 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530262 this.listOfLeafList = listOfLeafList;
263 }
264
265 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530266 * Adds a leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530267 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530268 * @param leafList the leaf-list to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530269 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530270 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530271 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530272 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530273 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530274 }
275
276 getListOfLeafList().add(leafList);
277 }
278
279 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530280 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530281 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530282 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530283 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530284 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530285 public String getReference() {
286 return reference;
287 }
288
289 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530290 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530291 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530292 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530293 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530294 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530295 public void setReference(String reference) {
296 this.reference = reference;
297 }
298
299 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530300 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530301 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530302 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530303 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530304 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530305 public YangStatusType getStatus() {
306 return status;
307 }
308
309 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530310 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530311 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530312 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530313 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530314 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530315 public void setStatus(YangStatusType status) {
316 this.status = status;
317 }
318
319 /**
320 * Returns the type of the data.
321 *
322 * @return returns CASE_DATA
323 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530324 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530325 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530326 return CASE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530327 }
328
329 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530330 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530331 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530332 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530333 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530334 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530335 public void validateDataOnEntry()
336 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530337 // TODO auto-generated method stub, to be implemented by parser
338 }
339
340 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530341 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530342 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530343 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530344 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530345 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530346 public void validateDataOnExit()
347 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530348 // TODO auto-generated method stub, to be implemented by parser
349 }
350
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530351 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530352 public void detectCollidingChild(String identifierName, YangConstructType dataType)
353 throws DataModelException {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530354 if (!(getParent() instanceof YangChoice)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530355 throw new DataModelException("Internal Data Model Tree Error: Invalid/Missing holder in case " +
Vinod Kumar S38046502016-03-23 15:30:27 +0530356 getName());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530357 }
358 // Traverse up in tree to ask parent choice start collision detection.
Vinod Kumar S38046502016-03-23 15:30:27 +0530359 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530360 }
361
362 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530363 public void detectSelfCollision(String identifierName, YangConstructType dataType)
364 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530365
366 if (dataType == CASE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530367 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530368 throw new DataModelException("YANG File Error: Identifier collision detected in case \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530369 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530370 }
371 return;
372 }
373
374 // Asks helper to detect colliding child.
375 detectCollidingChildUtil(identifierName, dataType, this);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530376 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530377
378 @Override
379 public List<YangIfFeature> getIfFeatureList() {
380 return ifFeatureList;
381 }
382
383 @Override
384 public void addIfFeatureList(YangIfFeature ifFeature) {
385 if (getIfFeatureList() == null) {
386 setIfFeatureList(new LinkedList<>());
387 }
388 getIfFeatureList().add(ifFeature);
389 }
390
391 @Override
392 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
393 this.ifFeatureList = ifFeatureList;
394 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530395}