blob: 5262119aa4bdcda3e084172276f3f2c760a69abe [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
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053018import java.util.LinkedList;
19import java.util.List;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053020import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053021import org.onosproject.yangutils.datamodel.utils.Parsable;
22import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Vinod Kumar S38046502016-03-23 15:30:27 +053023
Bharat saraswal96dfef02016-06-16 00:29:12 +053024import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053025
26/*-
27 * Reference RFC 6020.
28 *
29 * The "choice" statement defines a set of alternatives, only one of
30 * which may exist at any one time. The argument is an identifier,
31 * followed by a block of sub-statements that holds detailed choice
32 * information. The identifier is used to identify the choice node in
33 * the schema tree. A choice node does not exist in the data tree.
34 *
35 * A choice consists of a number of branches, defined with the "case"
36 * sub-statement. Each branch contains a number of child nodes. The
37 * nodes from at most one of the choice's branches exist at the same
38 * time.
39 *
40 * The choice's sub-statements
41 *
42 * +--------------+---------+-------------+------------------+
43 * | substatement | section | cardinality |data model mapping|
44 * +--------------+---------+-------------+------------------+
45 * | anyxml | 7.10 | 0..n |-not supported |
46 * | case | 7.9.2 | 0..n |-YangChoice |
47 * | config | 7.19.1 | 0..1 |-boolean |
48 * | container | 7.5 | 0..n |-child case nodes |
49 * | default | 7.9.3 | 0..1 |-string |
50 * | description | 7.19.3 | 0..1 |-string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053051 * | if-feature | 7.18.2 | 0..n |-YangIfFeature |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053052 * | leaf | 7.6 | 0..n |-child case nodes |
53 * | leaf-list | 7.7 | 0..n |-child case nodes |
54 * | list | 7.8 | 0..n |-child case nodes |
55 * | mandatory | 7.9.4 | 0..1 |-string |
56 * | reference | 7.19.4 | 0..1 |-string |
57 * | status | 7.19.2 | 0..1 |-string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053058 * | when | 7.19.5 | 0..1 |-YangWhen |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053059 * +--------------+---------+-------------+------------------+
60 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053061
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053062/**
Bharat saraswald9822e92016-04-05 15:13:44 +053063 * Represents data model node to maintain information defined in YANG choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053064 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053065public class YangChoice extends YangNode
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053066 implements YangCommonInfo, Parsable, CollisionDetector, YangAugmentationHolder, YangWhenHolder,
67 YangIfFeatureHolder {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053068
Bharat saraswal96dfef02016-06-16 00:29:12 +053069 private static final long serialVersionUID = 806201604L;
70
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053071 /**
72 * Name of choice.
73 */
74 private String name;
75
76 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053077 * If the choice represents config data.
78 */
79 private boolean isConfig;
80
81 /**
82 * Reference RFC 6020.
83 *
84 * The "default" statement indicates if a case should be considered as the
85 * default if no child nodes from any of the choice's cases exist. The
86 * argument is the identifier of the "case" statement. If the "default"
87 * statement is missing, there is no default case.
88 *
89 * The "default" statement MUST NOT be present on choices where "mandatory"
90 * is true.
91 *
92 * The default case is only important when considering the default values of
93 * nodes under the cases. The default values for nodes under the default
94 * case are used if none of the nodes under any of the cases are present.
95 *
96 * There MUST NOT be any mandatory nodes directly under the default case.
97 *
98 * Default values for child nodes under a case are only used if one of the
99 * nodes under that case is present, or if that case is the default case. If
100 * none of the nodes under a case are present and the case is not the
101 * default case, the default values of the cases' child nodes are ignored.
102 *
103 * the default case to be used if no case members is present.
104 */
105 private String defaultCase;
106
107 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530108 * Description of choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530109 */
110 private String description;
111
112 /**
113 * Reference RFC 6020.
114 *
115 * The "mandatory" statement, which is optional, takes as an argument the
116 * string "true" or "false", and puts a constraint on valid data. If
117 * "mandatory" is "true", at least one node from exactly one of the choice's
118 * case branches MUST exist.
119 *
120 * If not specified, the default is "false".
121 *
122 * The behavior of the constraint depends on the type of the choice's
123 * closest ancestor node in the schema tree which is not a non-presence
124 * container:
125 *
126 * o If this ancestor is a case node, the constraint is enforced if any
127 * other node from the case exists.
128 *
129 * o Otherwise, it is enforced if the ancestor node exists.
130 */
131 private String mandatory;
132
133 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530134 * Reference of the choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530135 */
136 private String reference;
137
138 /**
139 * Status of the node.
140 */
141 private YangStatusType status;
142
143 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530144 * Default value in string, needs to be converted to the target object,
145 * based on the type.
146 */
147 private String defaultValueInString;
148
149 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530150 * When data of the node.
151 */
152 private YangWhen when;
153
154 /**
155 * List of if-feature.
156 */
157 private List<YangIfFeature> ifFeatureList;
158
159 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 * Create a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530161 */
162 public YangChoice() {
163 super(YangNodeType.CHOICE_NODE);
164 }
165
Vinod Kumar S38046502016-03-23 15:30:27 +0530166 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530167 * Returns the when.
168 *
169 * @return the when
170 */
171 @Override
172 public YangWhen getWhen() {
173 return when;
174 }
175
176 /**
177 * Sets the when.
178 *
179 * @param when the when to set
180 */
181 @Override
182 public void setWhen(YangWhen when) {
183 this.when = when;
184 }
185
186 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530187 * Returns the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530188 *
189 * @return choice name
190 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530191 @Override
192 public String getName() {
193 return name;
194 }
195
Vinod Kumar S38046502016-03-23 15:30:27 +0530196 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530197 * Sets the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530198 *
199 * @param name choice name
200 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530201 @Override
202 public void setName(String name) {
203 this.name = name;
204 }
205
206 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530207 * Returns config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530208 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530209 * @return the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530210 */
211 public boolean isConfig() {
212 return isConfig;
213 }
214
215 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530216 * Sets config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530217 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530218 * @param isCfg the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530219 */
220 public void setConfig(boolean isCfg) {
221 isConfig = isCfg;
222 }
223
224 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530225 * Returns the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530226 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530227 * @return the default case
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530228 */
229 public String getDefaultCase() {
230 return defaultCase;
231 }
232
233 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530234 * Sets the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530235 *
236 * @param defaultCase the default case to set
237 */
238 public void setDefaultCase(String defaultCase) {
239 this.defaultCase = defaultCase;
240 }
241
242 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530243 * Returns the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530244 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530245 * @return the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530246 */
247 public String getMandatory() {
248 return mandatory;
249 }
250
251 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530252 * Sets the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530253 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530254 * @param mandatory the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530255 */
256 public void setMandatory(String mandatory) {
257 this.mandatory = mandatory;
258 }
259
260 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530261 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530262 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530263 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530264 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530265 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530266 public String getDescription() {
267 return description;
268 }
269
270 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530271 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530272 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530273 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530274 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530275 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530276 public void setDescription(String description) {
277 this.description = description;
278 }
279
280 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530281 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530282 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530283 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530284 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530285 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530286 public String getReference() {
287 return reference;
288 }
289
290 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530291 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530292 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530293 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530294 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530295 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530296 public void setReference(String reference) {
297 this.reference = reference;
298 }
299
300 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530301 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530302 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530303 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530304 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530305 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530306 public YangStatusType getStatus() {
307 return status;
308 }
309
310 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530311 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530312 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530313 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530314 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530315 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530316 public void setStatus(YangStatusType status) {
317 this.status = status;
318 }
319
320 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530321 * Returns the default value.
322 *
323 * @return the default value
324 */
325 public String getDefaultValueInString() {
326 return defaultValueInString;
327 }
328
329 /**
330 * Sets the default value.
331 *
332 * @param defaultValueInString the default value
333 */
334 public void setDefaultValueInString(String defaultValueInString) {
335 this.defaultValueInString = defaultValueInString;
336 }
337
338 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530339 * Returns the type of the data.
340 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530341 * @return choice data
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530342 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530343 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530344 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530345 return CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530346 }
347
348 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530349 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530350 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530351 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530352 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530353 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530354 public void validateDataOnEntry() throws DataModelException {
355 // TODO auto-generated method stub, to be implemented by parser
356 }
357
358 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530359 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530360 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530361 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530362 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530363 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530364 public void validateDataOnExit() throws DataModelException {
365 // TODO auto-generated method stub, to be implemented by parser
366 }
367
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530368 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530369 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
370
Bharat saraswal96dfef02016-06-16 00:29:12 +0530371 if (getParent() instanceof YangCase && dataType != YangConstructType.CASE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530372 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
373 }
374 YangNode node = getChild();
375 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530376 if (node instanceof CollisionDetector) {
377 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
378 }
379 node = node.getNextSibling();
380 }
381 }
382
383 @Override
384 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
385
386 if (dataType == CHOICE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530387 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530388 throw new DataModelException("YANG file error: Identifier collision detected in choice \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530389 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530390 }
391 return;
392 }
393
Vinod Kumar S38046502016-03-23 15:30:27 +0530394 YangNode node = getChild();
395 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530396 if (node instanceof CollisionDetector) {
397 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
398 }
399 node = node.getNextSibling();
400 }
401 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530402
403 @Override
404 public List<YangIfFeature> getIfFeatureList() {
405 return ifFeatureList;
406 }
407
408 @Override
409 public void addIfFeatureList(YangIfFeature ifFeature) {
410 if (getIfFeatureList() == null) {
411 setIfFeatureList(new LinkedList<>());
412 }
413 getIfFeatureList().add(ifFeature);
414 }
415
416 @Override
417 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
418 this.ifFeatureList = ifFeatureList;
419 }
420
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530421}