blob: e1e037af45aaea0ef9fdfa1b26178b4e4d607f19 [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,
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053099 YangWhenHolder, YangIfFeatureHolder, YangIsFilterContentNodes {
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);
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530152 listOfLeaf = new LinkedList<>();
153 listOfLeafList = new LinkedList<>();
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530154 }
155
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530156 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530157 * Returns the when.
158 *
159 * @return the when
160 */
161 @Override
162 public YangWhen getWhen() {
163 return when;
164 }
165
166 /**
167 * Sets the when.
168 *
169 * @param when the when to set
170 */
171 @Override
172 public void setWhen(YangWhen when) {
173 this.when = when;
174 }
175
176 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530177 * Returns the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530178 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530179 * @return case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530180 */
181 @Override
182 public String getName() {
183 return name;
184 }
185
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530186 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530187 * Sets the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530188 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530189 * @param name case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530190 */
191 @Override
192 public void setName(String name) {
193 this.name = name;
194 }
195
196 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530197 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530198 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530199 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530200 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530201 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530202 public String getDescription() {
203 return description;
204 }
205
206 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530207 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530208 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530209 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530210 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530211 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530212 public void setDescription(String description) {
213 this.description = description;
214 }
215
216 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530217 * Returns the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530218 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530219 * @return the list of leaves
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530220 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530221 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530222 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530223 return listOfLeaf;
224 }
225
226 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530227 * Sets the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530228 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530229 * @param leafsList the list of leaf to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530230 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530231 @Override
232 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530233 listOfLeaf = leafsList;
234 }
235
236 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530237 * Adds a leaf.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530238 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530239 * @param leaf the leaf to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530240 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530241 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530242 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530243 if (getListOfLeaf() == null) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530244 setListOfLeaf(new LinkedList<>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530245 }
246
247 getListOfLeaf().add(leaf);
248 }
249
250 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530251 * Returns the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530252 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530253 * @return the list of leaf-list
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530254 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530255 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530256 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530257 return listOfLeafList;
258 }
259
260 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530261 * Sets the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530262 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530263 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530264 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530265 @Override
266 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530267 this.listOfLeafList = listOfLeafList;
268 }
269
270 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530271 * Adds a leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530272 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530273 * @param leafList the leaf-list to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530274 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530275 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530276 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530277 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530278 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530279 }
280
281 getListOfLeafList().add(leafList);
282 }
283
284 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530285 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530286 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530287 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530288 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530289 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530290 public String getReference() {
291 return reference;
292 }
293
294 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530295 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530296 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530297 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530298 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530299 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530300 public void setReference(String reference) {
301 this.reference = reference;
302 }
303
304 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530305 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530306 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530307 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530308 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530309 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530310 public YangStatusType getStatus() {
311 return status;
312 }
313
314 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530315 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530316 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530317 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530318 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530319 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530320 public void setStatus(YangStatusType status) {
321 this.status = status;
322 }
323
324 /**
325 * Returns the type of the data.
326 *
327 * @return returns CASE_DATA
328 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530329 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530330 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530331 return CASE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530332 }
333
334 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530335 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530336 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530337 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530338 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530339 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530340 public void validateDataOnEntry()
341 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530342 // TODO auto-generated method stub, to be implemented by parser
343 }
344
345 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530346 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530347 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530348 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530349 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530350 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530351 public void validateDataOnExit()
352 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530353 // TODO auto-generated method stub, to be implemented by parser
354 }
355
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530356 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530357 public void detectCollidingChild(String identifierName, YangConstructType dataType)
358 throws DataModelException {
Vidyashree Rama36f2fab2016-07-15 14:06:56 +0530359 if (!(getParent() instanceof YangChoice || getParent() instanceof YangAugment)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530360 throw new DataModelException("Internal Data Model Tree Error: Invalid/Missing holder in case " +
Vinod Kumar S38046502016-03-23 15:30:27 +0530361 getName());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530362 }
363 // Traverse up in tree to ask parent choice start collision detection.
Vinod Kumar S38046502016-03-23 15:30:27 +0530364 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530365 }
366
367 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530368 public void detectSelfCollision(String identifierName, YangConstructType dataType)
369 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530370
371 if (dataType == CASE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530372 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530373 throw new DataModelException("YANG File Error: Identifier collision detected in case \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530374 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530375 }
376 return;
377 }
378
379 // Asks helper to detect colliding child.
380 detectCollidingChildUtil(identifierName, dataType, this);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530381 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530382
383 @Override
384 public List<YangIfFeature> getIfFeatureList() {
385 return ifFeatureList;
386 }
387
388 @Override
389 public void addIfFeatureList(YangIfFeature ifFeature) {
390 if (getIfFeatureList() == null) {
391 setIfFeatureList(new LinkedList<>());
392 }
393 getIfFeatureList().add(ifFeature);
394 }
395
396 @Override
397 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
398 this.ifFeatureList = ifFeatureList;
399 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530400
401 @Override
402 public void addAugmentation(YangAugmentedInfo augmentInfo) {
403 yangAugmentedInfo.add(augmentInfo);
404 }
405
406 @Override
407 public void removeAugmentation(YangAugmentedInfo augmentInfo) {
408 yangAugmentedInfo.remove(augmentInfo);
409 }
410
411 @Override
412 public List<YangAugmentedInfo> getAugmentedInfoList() {
413 return yangAugmentedInfo;
414 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530415}