blob: b7bdf0827da18f41189bf0188cd30f5a27211fd8 [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;
rama-huawei6c728a92016-07-11 14:48:12 +053026import static org.onosproject.yangutils.datamodel.utils.YangErrMsgConstants.DATA_MISSING_ERROR_TAG;
27import static org.onosproject.yangutils.datamodel.utils.YangErrMsgConstants.ERROR_PATH_MISSING_CHOICE;
28import static org.onosproject.yangutils.datamodel.utils.YangErrMsgConstants.MISSING_CHOICE_ERROR_APP_TAG;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053029
30/*-
31 * Reference RFC 6020.
32 *
33 * The "choice" statement defines a set of alternatives, only one of
34 * which may exist at any one time. The argument is an identifier,
35 * followed by a block of sub-statements that holds detailed choice
36 * information. The identifier is used to identify the choice node in
37 * the schema tree. A choice node does not exist in the data tree.
38 *
39 * A choice consists of a number of branches, defined with the "case"
40 * sub-statement. Each branch contains a number of child nodes. The
41 * nodes from at most one of the choice's branches exist at the same
42 * time.
43 *
44 * The choice's sub-statements
45 *
46 * +--------------+---------+-------------+------------------+
47 * | substatement | section | cardinality |data model mapping|
48 * +--------------+---------+-------------+------------------+
49 * | anyxml | 7.10 | 0..n |-not supported |
50 * | case | 7.9.2 | 0..n |-YangChoice |
51 * | config | 7.19.1 | 0..1 |-boolean |
52 * | container | 7.5 | 0..n |-child case nodes |
53 * | default | 7.9.3 | 0..1 |-string |
54 * | description | 7.19.3 | 0..1 |-string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053055 * | if-feature | 7.18.2 | 0..n |-YangIfFeature |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053056 * | leaf | 7.6 | 0..n |-child case nodes |
57 * | leaf-list | 7.7 | 0..n |-child case nodes |
58 * | list | 7.8 | 0..n |-child case nodes |
59 * | mandatory | 7.9.4 | 0..1 |-string |
60 * | reference | 7.19.4 | 0..1 |-string |
61 * | status | 7.19.2 | 0..1 |-string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053062 * | when | 7.19.5 | 0..1 |-YangWhen |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053063 * +--------------+---------+-------------+------------------+
64 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053065
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053066/**
Bharat saraswald9822e92016-04-05 15:13:44 +053067 * Represents data model node to maintain information defined in YANG choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053068 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053069public class YangChoice extends YangNode
Bharat saraswalb1170bd2016-07-14 13:26:18 +053070 implements YangCommonInfo, Parsable, CollisionDetector, YangAugmentableNode,
rama-huawei6c728a92016-07-11 14:48:12 +053071 YangWhenHolder, YangIfFeatureHolder, YangAppErrorHolder {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053072
Bharat saraswal96dfef02016-06-16 00:29:12 +053073 private static final long serialVersionUID = 806201604L;
74
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053075 /**
76 * Name of choice.
77 */
78 private String name;
79
80 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053081 * If the choice represents config data.
82 */
83 private boolean isConfig;
84
85 /**
86 * Reference RFC 6020.
87 *
88 * The "default" statement indicates if a case should be considered as the
89 * default if no child nodes from any of the choice's cases exist. The
90 * argument is the identifier of the "case" statement. If the "default"
91 * statement is missing, there is no default case.
92 *
93 * The "default" statement MUST NOT be present on choices where "mandatory"
94 * is true.
95 *
96 * The default case is only important when considering the default values of
97 * nodes under the cases. The default values for nodes under the default
98 * case are used if none of the nodes under any of the cases are present.
99 *
100 * There MUST NOT be any mandatory nodes directly under the default case.
101 *
102 * Default values for child nodes under a case are only used if one of the
103 * nodes under that case is present, or if that case is the default case. If
104 * none of the nodes under a case are present and the case is not the
105 * default case, the default values of the cases' child nodes are ignored.
106 *
107 * the default case to be used if no case members is present.
108 */
109 private String defaultCase;
110
111 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 * Description of choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530113 */
114 private String description;
115
116 /**
117 * Reference RFC 6020.
118 *
119 * The "mandatory" statement, which is optional, takes as an argument the
120 * string "true" or "false", and puts a constraint on valid data. If
121 * "mandatory" is "true", at least one node from exactly one of the choice's
122 * case branches MUST exist.
123 *
124 * If not specified, the default is "false".
125 *
126 * The behavior of the constraint depends on the type of the choice's
127 * closest ancestor node in the schema tree which is not a non-presence
128 * container:
129 *
130 * o If this ancestor is a case node, the constraint is enforced if any
131 * other node from the case exists.
132 *
133 * o Otherwise, it is enforced if the ancestor node exists.
134 */
135 private String mandatory;
136
137 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530138 * Reference of the choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530139 */
140 private String reference;
141
142 /**
143 * Status of the node.
144 */
145 private YangStatusType status;
146
147 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530148 * Default value in string, needs to be converted to the target object,
149 * based on the type.
150 */
151 private String defaultValueInString;
152
153 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530154 * When data of the node.
155 */
156 private YangWhen when;
157
158 /**
159 * List of if-feature.
160 */
161 private List<YangIfFeature> ifFeatureList;
162
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530163 private List<YangAugmentedInfo> yangAugmentedInfo = new ArrayList<>();
164
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530165 /**
rama-huawei6c728a92016-07-11 14:48:12 +0530166 * YANG application error information.
167 */
168 private YangAppErrorInfo yangAppErrorInfo;
169
170 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530171 * Create a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530172 */
173 public YangChoice() {
174 super(YangNodeType.CHOICE_NODE);
rama-huawei6c728a92016-07-11 14:48:12 +0530175 yangAppErrorInfo = new YangAppErrorInfo();
176 yangAppErrorInfo.setErrorTag(DATA_MISSING_ERROR_TAG);
177 yangAppErrorInfo.setErrorAppTag(MISSING_CHOICE_ERROR_APP_TAG);
178 yangAppErrorInfo.setErrorAppPath(ERROR_PATH_MISSING_CHOICE);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530179 }
180
Vinod Kumar S38046502016-03-23 15:30:27 +0530181 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530182 * Returns the when.
183 *
184 * @return the when
185 */
186 @Override
187 public YangWhen getWhen() {
188 return when;
189 }
190
191 /**
192 * Sets the when.
193 *
194 * @param when the when to set
195 */
196 @Override
197 public void setWhen(YangWhen when) {
198 this.when = when;
199 }
200
201 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530202 * Returns the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530203 *
204 * @return choice name
205 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530206 @Override
207 public String getName() {
208 return name;
209 }
210
Vinod Kumar S38046502016-03-23 15:30:27 +0530211 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530212 * Sets the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530213 *
214 * @param name choice name
215 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530216 @Override
217 public void setName(String name) {
218 this.name = name;
219 }
220
221 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530222 * Returns config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530223 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530224 * @return the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530225 */
226 public boolean isConfig() {
227 return isConfig;
228 }
229
230 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530231 * Sets config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530232 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530233 * @param isCfg the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530234 */
235 public void setConfig(boolean isCfg) {
236 isConfig = isCfg;
237 }
238
239 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530240 * Returns the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530241 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530242 * @return the default case
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530243 */
244 public String getDefaultCase() {
245 return defaultCase;
246 }
247
248 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530249 * Sets the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530250 *
251 * @param defaultCase the default case to set
252 */
253 public void setDefaultCase(String defaultCase) {
254 this.defaultCase = defaultCase;
255 }
256
257 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530258 * Returns the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530259 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530260 * @return the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530261 */
262 public String getMandatory() {
263 return mandatory;
264 }
265
266 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530267 * Sets the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530268 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530269 * @param mandatory the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530270 */
271 public void setMandatory(String mandatory) {
272 this.mandatory = mandatory;
273 }
274
275 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530276 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530277 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530278 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530279 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530280 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530281 public String getDescription() {
282 return description;
283 }
284
285 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530286 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530287 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530288 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530289 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530290 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530291 public void setDescription(String description) {
292 this.description = description;
293 }
294
295 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530296 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530297 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530298 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530299 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530300 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530301 public String getReference() {
302 return reference;
303 }
304
305 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530306 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530307 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530308 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530309 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530310 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530311 public void setReference(String reference) {
312 this.reference = reference;
313 }
314
315 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530316 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530317 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530318 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530319 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530320 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530321 public YangStatusType getStatus() {
322 return status;
323 }
324
325 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530326 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530327 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530328 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530329 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530330 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530331 public void setStatus(YangStatusType status) {
332 this.status = status;
333 }
334
335 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530336 * Returns the default value.
337 *
338 * @return the default value
339 */
340 public String getDefaultValueInString() {
341 return defaultValueInString;
342 }
343
344 /**
345 * Sets the default value.
346 *
347 * @param defaultValueInString the default value
348 */
349 public void setDefaultValueInString(String defaultValueInString) {
350 this.defaultValueInString = defaultValueInString;
351 }
352
353 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530354 * Returns the type of the data.
355 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530356 * @return choice data
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530357 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530358 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530359 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530360 return CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530361 }
362
363 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530364 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530365 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530366 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530367 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530368 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530369 public void validateDataOnEntry() throws DataModelException {
370 // TODO auto-generated method stub, to be implemented by parser
371 }
372
373 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530374 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530375 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530376 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530377 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530378 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530379 public void validateDataOnExit() throws DataModelException {
380 // TODO auto-generated method stub, to be implemented by parser
381 }
382
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530383 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530384 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
385
Bharat saraswal96dfef02016-06-16 00:29:12 +0530386 if (getParent() instanceof YangCase && dataType != YangConstructType.CASE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530387 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
388 }
389 YangNode node = getChild();
390 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530391 if (node instanceof CollisionDetector) {
392 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
393 }
394 node = node.getNextSibling();
395 }
396 }
397
398 @Override
399 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
400
401 if (dataType == CHOICE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530402 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530403 throw new DataModelException("YANG file error: Identifier collision detected in choice \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530404 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530405 }
406 return;
407 }
408
Vinod Kumar S38046502016-03-23 15:30:27 +0530409 YangNode node = getChild();
410 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530411 if (node instanceof CollisionDetector) {
412 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
413 }
414 node = node.getNextSibling();
415 }
416 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530417
418 @Override
419 public List<YangIfFeature> getIfFeatureList() {
420 return ifFeatureList;
421 }
422
423 @Override
424 public void addIfFeatureList(YangIfFeature ifFeature) {
425 if (getIfFeatureList() == null) {
426 setIfFeatureList(new LinkedList<>());
427 }
428 getIfFeatureList().add(ifFeature);
429 }
430
431 @Override
432 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
433 this.ifFeatureList = ifFeatureList;
434 }
435
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530436 @Override
437 public void addAugmentation(YangAugmentedInfo augmentInfo) {
438 yangAugmentedInfo.add(augmentInfo);
439 }
440
441 @Override
442 public void removeAugmentation(YangAugmentedInfo augmentInfo) {
443 yangAugmentedInfo.remove(augmentInfo);
444 }
445
446 @Override
447 public List<YangAugmentedInfo> getAugmentedInfoList() {
448 return yangAugmentedInfo;
449 }
rama-huawei6c728a92016-07-11 14:48:12 +0530450
451 @Override
452 public void setAppErrorInfo(YangAppErrorInfo yangAppErrorInfo) {
453 this.yangAppErrorInfo = yangAppErrorInfo;
454 }
455
456 @Override
457 public YangAppErrorInfo getAppErrorInfo() {
458 return yangAppErrorInfo;
459 }
460
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530461}