blob: 5e78d5546713906b48a404516517602b080d3f9c [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;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053019import java.util.LinkedList;
20import java.util.List;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +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
Bharat saraswal96dfef02016-06-16 00:29:12 +053025import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053026
27/*-
28 * Reference RFC 6020.
29 *
30 * The "choice" statement defines a set of alternatives, only one of
31 * which may exist at any one time. The argument is an identifier,
32 * followed by a block of sub-statements that holds detailed choice
33 * information. The identifier is used to identify the choice node in
34 * the schema tree. A choice node does not exist in the data tree.
35 *
36 * A choice consists of a number of branches, defined with the "case"
37 * sub-statement. Each branch contains a number of child nodes. The
38 * nodes from at most one of the choice's branches exist at the same
39 * time.
40 *
41 * The choice's sub-statements
42 *
43 * +--------------+---------+-------------+------------------+
44 * | substatement | section | cardinality |data model mapping|
45 * +--------------+---------+-------------+------------------+
46 * | anyxml | 7.10 | 0..n |-not supported |
47 * | case | 7.9.2 | 0..n |-YangChoice |
48 * | config | 7.19.1 | 0..1 |-boolean |
49 * | container | 7.5 | 0..n |-child case nodes |
50 * | default | 7.9.3 | 0..1 |-string |
51 * | description | 7.19.3 | 0..1 |-string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053052 * | if-feature | 7.18.2 | 0..n |-YangIfFeature |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053053 * | leaf | 7.6 | 0..n |-child case nodes |
54 * | leaf-list | 7.7 | 0..n |-child case nodes |
55 * | list | 7.8 | 0..n |-child case nodes |
56 * | mandatory | 7.9.4 | 0..1 |-string |
57 * | reference | 7.19.4 | 0..1 |-string |
58 * | status | 7.19.2 | 0..1 |-string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053059 * | when | 7.19.5 | 0..1 |-YangWhen |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053060 * +--------------+---------+-------------+------------------+
61 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053062
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053063/**
Bharat saraswald9822e92016-04-05 15:13:44 +053064 * Represents data model node to maintain information defined in YANG choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053065 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053066public class YangChoice extends YangNode
Bharat saraswalb1170bd2016-07-14 13:26:18 +053067 implements YangCommonInfo, Parsable, CollisionDetector, YangAugmentableNode,
68 YangWhenHolder, YangIfFeatureHolder {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053069
Bharat saraswal96dfef02016-06-16 00:29:12 +053070 private static final long serialVersionUID = 806201604L;
71
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053072 /**
73 * Name of choice.
74 */
75 private String name;
76
77 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053078 * If the choice represents config data.
79 */
80 private boolean isConfig;
81
82 /**
83 * Reference RFC 6020.
84 *
85 * The "default" statement indicates if a case should be considered as the
86 * default if no child nodes from any of the choice's cases exist. The
87 * argument is the identifier of the "case" statement. If the "default"
88 * statement is missing, there is no default case.
89 *
90 * The "default" statement MUST NOT be present on choices where "mandatory"
91 * is true.
92 *
93 * The default case is only important when considering the default values of
94 * nodes under the cases. The default values for nodes under the default
95 * case are used if none of the nodes under any of the cases are present.
96 *
97 * There MUST NOT be any mandatory nodes directly under the default case.
98 *
99 * Default values for child nodes under a case are only used if one of the
100 * nodes under that case is present, or if that case is the default case. If
101 * none of the nodes under a case are present and the case is not the
102 * default case, the default values of the cases' child nodes are ignored.
103 *
104 * the default case to be used if no case members is present.
105 */
106 private String defaultCase;
107
108 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530109 * Description of choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530110 */
111 private String description;
112
113 /**
114 * Reference RFC 6020.
115 *
116 * The "mandatory" statement, which is optional, takes as an argument the
117 * string "true" or "false", and puts a constraint on valid data. If
118 * "mandatory" is "true", at least one node from exactly one of the choice's
119 * case branches MUST exist.
120 *
121 * If not specified, the default is "false".
122 *
123 * The behavior of the constraint depends on the type of the choice's
124 * closest ancestor node in the schema tree which is not a non-presence
125 * container:
126 *
127 * o If this ancestor is a case node, the constraint is enforced if any
128 * other node from the case exists.
129 *
130 * o Otherwise, it is enforced if the ancestor node exists.
131 */
132 private String mandatory;
133
134 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530135 * Reference of the choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530136 */
137 private String reference;
138
139 /**
140 * Status of the node.
141 */
142 private YangStatusType status;
143
144 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530145 * Default value in string, needs to be converted to the target object,
146 * based on the type.
147 */
148 private String defaultValueInString;
149
150 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530151 * When data of the node.
152 */
153 private YangWhen when;
154
155 /**
156 * List of if-feature.
157 */
158 private List<YangIfFeature> ifFeatureList;
159
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530160 private List<YangAugmentedInfo> yangAugmentedInfo = new ArrayList<>();
161
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530162 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530163 * Create a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530164 */
165 public YangChoice() {
166 super(YangNodeType.CHOICE_NODE);
167 }
168
Vinod Kumar S38046502016-03-23 15:30:27 +0530169 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530170 * Returns the when.
171 *
172 * @return the when
173 */
174 @Override
175 public YangWhen getWhen() {
176 return when;
177 }
178
179 /**
180 * Sets the when.
181 *
182 * @param when the when to set
183 */
184 @Override
185 public void setWhen(YangWhen when) {
186 this.when = when;
187 }
188
189 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530190 * Returns the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530191 *
192 * @return choice name
193 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530194 @Override
195 public String getName() {
196 return name;
197 }
198
Vinod Kumar S38046502016-03-23 15:30:27 +0530199 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530200 * Sets the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530201 *
202 * @param name choice name
203 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530204 @Override
205 public void setName(String name) {
206 this.name = name;
207 }
208
209 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530210 * Returns config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530211 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530212 * @return the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530213 */
214 public boolean isConfig() {
215 return isConfig;
216 }
217
218 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530219 * Sets config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530220 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530221 * @param isCfg the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530222 */
223 public void setConfig(boolean isCfg) {
224 isConfig = isCfg;
225 }
226
227 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530228 * Returns the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530229 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530230 * @return the default case
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530231 */
232 public String getDefaultCase() {
233 return defaultCase;
234 }
235
236 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530237 * Sets the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530238 *
239 * @param defaultCase the default case to set
240 */
241 public void setDefaultCase(String defaultCase) {
242 this.defaultCase = defaultCase;
243 }
244
245 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530246 * Returns the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530247 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530248 * @return the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530249 */
250 public String getMandatory() {
251 return mandatory;
252 }
253
254 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530255 * Sets the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530256 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530257 * @param mandatory the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530258 */
259 public void setMandatory(String mandatory) {
260 this.mandatory = mandatory;
261 }
262
263 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530264 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530265 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530266 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530267 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530268 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530269 public String getDescription() {
270 return description;
271 }
272
273 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530274 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530275 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530276 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530277 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530278 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530279 public void setDescription(String description) {
280 this.description = description;
281 }
282
283 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530284 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530285 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530286 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530287 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530288 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530289 public String getReference() {
290 return reference;
291 }
292
293 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530294 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530295 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530296 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530297 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530298 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530299 public void setReference(String reference) {
300 this.reference = reference;
301 }
302
303 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530304 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530305 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530306 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530307 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530308 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530309 public YangStatusType getStatus() {
310 return status;
311 }
312
313 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530314 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530315 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530316 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530317 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530318 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530319 public void setStatus(YangStatusType status) {
320 this.status = status;
321 }
322
323 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530324 * Returns the default value.
325 *
326 * @return the default value
327 */
328 public String getDefaultValueInString() {
329 return defaultValueInString;
330 }
331
332 /**
333 * Sets the default value.
334 *
335 * @param defaultValueInString the default value
336 */
337 public void setDefaultValueInString(String defaultValueInString) {
338 this.defaultValueInString = defaultValueInString;
339 }
340
341 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530342 * Returns the type of the data.
343 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530344 * @return choice data
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530345 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530346 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530347 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530348 return CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530349 }
350
351 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530352 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530353 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530354 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530355 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530356 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530357 public void validateDataOnEntry() throws DataModelException {
358 // TODO auto-generated method stub, to be implemented by parser
359 }
360
361 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530362 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530363 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530364 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530365 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530366 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530367 public void validateDataOnExit() throws DataModelException {
368 // TODO auto-generated method stub, to be implemented by parser
369 }
370
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530371 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530372 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
373
Bharat saraswal96dfef02016-06-16 00:29:12 +0530374 if (getParent() instanceof YangCase && dataType != YangConstructType.CASE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530375 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
376 }
377 YangNode node = getChild();
378 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530379 if (node instanceof CollisionDetector) {
380 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
381 }
382 node = node.getNextSibling();
383 }
384 }
385
386 @Override
387 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
388
389 if (dataType == CHOICE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530390 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530391 throw new DataModelException("YANG file error: Identifier collision detected in choice \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530392 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530393 }
394 return;
395 }
396
Vinod Kumar S38046502016-03-23 15:30:27 +0530397 YangNode node = getChild();
398 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530399 if (node instanceof CollisionDetector) {
400 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
401 }
402 node = node.getNextSibling();
403 }
404 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530405
406 @Override
407 public List<YangIfFeature> getIfFeatureList() {
408 return ifFeatureList;
409 }
410
411 @Override
412 public void addIfFeatureList(YangIfFeature ifFeature) {
413 if (getIfFeatureList() == null) {
414 setIfFeatureList(new LinkedList<>());
415 }
416 getIfFeatureList().add(ifFeature);
417 }
418
419 @Override
420 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
421 this.ifFeatureList = ifFeatureList;
422 }
423
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530424 @Override
425 public void addAugmentation(YangAugmentedInfo augmentInfo) {
426 yangAugmentedInfo.add(augmentInfo);
427 }
428
429 @Override
430 public void removeAugmentation(YangAugmentedInfo augmentInfo) {
431 yangAugmentedInfo.remove(augmentInfo);
432 }
433
434 @Override
435 public List<YangAugmentedInfo> getAugmentedInfoList() {
436 return yangAugmentedInfo;
437 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530438}