blob: 6677a8c595f71b734971942db1e6dd91e97f3f50 [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
Bharat saraswalb1170bd2016-07-14 13:26:18 +053018import java.util.ArrayList;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053019import java.util.LinkedList;
20import java.util.List;
21
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053023import org.onosproject.yangutils.datamodel.utils.Parsable;
24import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Vinod Kumar S38046502016-03-23 15:30:27 +053025
26import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
Bharat saraswal96dfef02016-06-16 00:29:12 +053027import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CASE_DATA;
Vinod Kumar S38046502016-03-23 15:30:27 +053028
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053029/*-
30 * Reference RFC 6020.
31 *
32 * The "case" statement is used to define branches of the choice. It takes as an
33 * argument an identifier, followed by a block of sub-statements that holds
34 * detailed case information.
35 *
36 * The identifier is used to identify the case node in the schema tree. A case
37 * node does not exist in the data tree.
38 *
39 * Within a "case" statement, the "anyxml", "choice", "container", "leaf",
40 * "list", "leaf-list", and "uses" statements can be used to define child nodes
41 * to the case node. The identifiers of all these child nodes MUST be unique
42 * within all cases in a choice. For example, the following is illegal:
43 *
44 * choice interface-type { // This example is illegal YANG
45 * case a {
46 * leaf ethernet { ... }
47 * }
48 * case b {
49 * container ethernet { ...}
50 * }
51 * }
52 *
53 * As a shorthand, the "case" statement can be omitted if the branch
54 * contains a single "anyxml", "container", "leaf", "list", or
55 * "leaf-list" statement. In this case, the identifier of the case node
56 * is the same as the identifier in the branch statement. The following
57 * example:
58 *
59 * choice interface-type {
60 * container ethernet { ... }
61 * }
62 *
63 * is equivalent to:
64 *
65 * choice interface-type {
66 * case ethernet {
67 * container ethernet { ... }
68 * }
69 * }
70 *
71 * The case identifier MUST be unique within a choice.
72 *
73 * The case's sub-statements
74 *
75 * +--------------+---------+-------------+------------------+
76 * | substatement | section | cardinality |data model mapping|
77 * +--------------+---------+-------------+------------------+
78 * | anyxml | 7.10 | 0..n |-not supported |
79 * | choice | 7.9 | 0..n |-child nodes |
80 * | container | 7.5 | 0..n |-child nodes |
81 * | description | 7.19.3 | 0..1 |-string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053082 * | if-feature | 7.18.2 | 0..n |-YangIfFeature |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053083 * | leaf | 7.6 | 0..n |-YangLeaf |
84 * | leaf-list | 7.7 | 0..n |-YangLeafList |
85 * | list | 7.8 | 0..n |-child nodes |
86 * | reference | 7.19.4 | 0..1 |-string |
87 * | status | 7.19.2 | 0..1 |-YangStatus |
88 * | uses | 7.12 | 0..n |-child node |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053089 * | when | 7.19.5 | 0..1 |-YangWhen |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053090 * +--------------+---------+-------------+------------------+
91 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053092
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053093/**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Represents data model node to maintain information defined in YANG case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053095 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053096public class YangCase
97 extends YangNode
Bharat saraswalb1170bd2016-07-14 13:26:18 +053098 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector, YangAugmentableNode,
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053099 YangWhenHolder, YangIfFeatureHolder {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530100
Bharat saraswal96dfef02016-06-16 00:29:12 +0530101 private static final long serialVersionUID = 806201603L;
102
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530103 /**
104 * Case name.
105 */
106 private String name;
107
108 // TODO: default field identification for the case
109
110 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530111 * Description of case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530112 */
113 private String description;
114
115 /**
116 * List of leaves.
117 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530118 private List<YangLeaf> listOfLeaf;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530119
120 /**
121 * List of leaf lists.
122 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530123 private List<YangLeafList> listOfLeafList;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530124
125 /**
126 * Reference of the module.
127 */
128 private String reference;
129
130 /**
131 * Status of the node.
132 */
133 private YangStatusType status;
134
135 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530136 * When data of the node.
137 */
138 private YangWhen when;
139
140 /**
141 * List of if-feature.
142 */
143 private List<YangIfFeature> ifFeatureList;
144
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530145 private List<YangAugmentedInfo> yangAugmentedInfo = new ArrayList<>();
146
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530147 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530148 * Creates a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530149 */
150 public YangCase() {
151 super(YangNodeType.CASE_NODE);
152 }
153
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530154 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530155 * Returns the when.
156 *
157 * @return the when
158 */
159 @Override
160 public YangWhen getWhen() {
161 return when;
162 }
163
164 /**
165 * Sets the when.
166 *
167 * @param when the when to set
168 */
169 @Override
170 public void setWhen(YangWhen when) {
171 this.when = when;
172 }
173
174 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530175 * Returns the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530176 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530177 * @return case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530178 */
179 @Override
180 public String getName() {
181 return name;
182 }
183
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530184 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530185 * Sets the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530186 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530187 * @param name case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530188 */
189 @Override
190 public void setName(String name) {
191 this.name = name;
192 }
193
194 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530195 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530196 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530197 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530198 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530199 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530200 public String getDescription() {
201 return description;
202 }
203
204 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530205 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530206 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530207 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530208 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530209 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530210 public void setDescription(String description) {
211 this.description = description;
212 }
213
214 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530215 * Returns the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530216 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530217 * @return the list of leaves
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<YangLeaf> getListOfLeaf() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530221 return listOfLeaf;
222 }
223
224 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530225 * Sets the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530226 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530227 * @param leafsList the list of leaf to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530228 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530229 @Override
230 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530231 listOfLeaf = leafsList;
232 }
233
234 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530235 * Adds a leaf.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530236 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530237 * @param leaf the leaf to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530238 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530239 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530240 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530241 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530242 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530243 }
244
245 getListOfLeaf().add(leaf);
246 }
247
248 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530249 * Returns the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530250 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530251 * @return the list of leaf-list
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530252 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530253 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530254 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530255 return listOfLeafList;
256 }
257
258 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530259 * Sets the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530260 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530261 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530262 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530263 @Override
264 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530265 this.listOfLeafList = listOfLeafList;
266 }
267
268 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530269 * Adds a leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530270 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530271 * @param leafList the leaf-list to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530272 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530273 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530274 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530275 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530276 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530277 }
278
279 getListOfLeafList().add(leafList);
280 }
281
282 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530283 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530284 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530285 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530286 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530287 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530288 public String getReference() {
289 return reference;
290 }
291
292 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530293 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530294 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530295 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530296 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530297 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530298 public void setReference(String reference) {
299 this.reference = reference;
300 }
301
302 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530303 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530304 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530305 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530306 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530307 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530308 public YangStatusType getStatus() {
309 return status;
310 }
311
312 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530313 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530314 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530315 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530316 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530317 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530318 public void setStatus(YangStatusType status) {
319 this.status = status;
320 }
321
322 /**
323 * Returns the type of the data.
324 *
325 * @return returns CASE_DATA
326 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530327 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530328 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530329 return CASE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530330 }
331
332 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530333 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530334 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530335 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530336 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530337 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530338 public void validateDataOnEntry()
339 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530340 // TODO auto-generated method stub, to be implemented by parser
341 }
342
343 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530344 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530345 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530346 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530347 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530348 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530349 public void validateDataOnExit()
350 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530351 // TODO auto-generated method stub, to be implemented by parser
352 }
353
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530354 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530355 public void detectCollidingChild(String identifierName, YangConstructType dataType)
356 throws DataModelException {
Vidyashree Rama36f2fab2016-07-15 14:06:56 +0530357 if (!(getParent() instanceof YangChoice || getParent() instanceof YangAugment)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530358 throw new DataModelException("Internal Data Model Tree Error: Invalid/Missing holder in case " +
Vinod Kumar S38046502016-03-23 15:30:27 +0530359 getName());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530360 }
361 // Traverse up in tree to ask parent choice start collision detection.
Vinod Kumar S38046502016-03-23 15:30:27 +0530362 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530363 }
364
365 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530366 public void detectSelfCollision(String identifierName, YangConstructType dataType)
367 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530368
369 if (dataType == CASE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530370 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530371 throw new DataModelException("YANG File Error: Identifier collision detected in case \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530372 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530373 }
374 return;
375 }
376
377 // Asks helper to detect colliding child.
378 detectCollidingChildUtil(identifierName, dataType, this);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530379 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530380
381 @Override
382 public List<YangIfFeature> getIfFeatureList() {
383 return ifFeatureList;
384 }
385
386 @Override
387 public void addIfFeatureList(YangIfFeature ifFeature) {
388 if (getIfFeatureList() == null) {
389 setIfFeatureList(new LinkedList<>());
390 }
391 getIfFeatureList().add(ifFeature);
392 }
393
394 @Override
395 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
396 this.ifFeatureList = ifFeatureList;
397 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530398
399 @Override
400 public void addAugmentation(YangAugmentedInfo augmentInfo) {
401 yangAugmentedInfo.add(augmentInfo);
402 }
403
404 @Override
405 public void removeAugmentation(YangAugmentedInfo augmentInfo) {
406 yangAugmentedInfo.remove(augmentInfo);
407 }
408
409 @Override
410 public List<YangAugmentedInfo> getAugmentedInfoList() {
411 return yangAugmentedInfo;
412 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530413}