blob: 48b763bf06219c2d78827b82657816d1997f9be5 [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;
janani b23ccc312016-07-14 19:35:22 +053021
Vinod Kumar S2ff139c2016-02-16 01:37:16 +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
Bharat saraswal96dfef02016-06-16 00:29:12 +053026import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CHOICE_DATA;
rama-huawei6c728a92016-07-11 14:48:12 +053027import static org.onosproject.yangutils.datamodel.utils.YangErrMsgConstants.DATA_MISSING_ERROR_TAG;
28import static org.onosproject.yangutils.datamodel.utils.YangErrMsgConstants.ERROR_PATH_MISSING_CHOICE;
29import static org.onosproject.yangutils.datamodel.utils.YangErrMsgConstants.MISSING_CHOICE_ERROR_APP_TAG;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053030
31/*-
32 * Reference RFC 6020.
33 *
34 * The "choice" statement defines a set of alternatives, only one of
35 * which may exist at any one time. The argument is an identifier,
36 * followed by a block of sub-statements that holds detailed choice
37 * information. The identifier is used to identify the choice node in
38 * the schema tree. A choice node does not exist in the data tree.
39 *
40 * A choice consists of a number of branches, defined with the "case"
41 * sub-statement. Each branch contains a number of child nodes. The
42 * nodes from at most one of the choice's branches exist at the same
43 * time.
44 *
45 * The choice's sub-statements
46 *
47 * +--------------+---------+-------------+------------------+
48 * | substatement | section | cardinality |data model mapping|
49 * +--------------+---------+-------------+------------------+
50 * | anyxml | 7.10 | 0..n |-not supported |
51 * | case | 7.9.2 | 0..n |-YangChoice |
52 * | config | 7.19.1 | 0..1 |-boolean |
53 * | container | 7.5 | 0..n |-child case nodes |
54 * | default | 7.9.3 | 0..1 |-string |
55 * | description | 7.19.3 | 0..1 |-string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053056 * | if-feature | 7.18.2 | 0..n |-YangIfFeature |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053057 * | leaf | 7.6 | 0..n |-child case nodes |
58 * | leaf-list | 7.7 | 0..n |-child case nodes |
59 * | list | 7.8 | 0..n |-child case nodes |
60 * | mandatory | 7.9.4 | 0..1 |-string |
61 * | reference | 7.19.4 | 0..1 |-string |
62 * | status | 7.19.2 | 0..1 |-string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053063 * | when | 7.19.5 | 0..1 |-YangWhen |
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053064 * +--------------+---------+-------------+------------------+
65 */
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053066
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053067/**
Bharat saraswald9822e92016-04-05 15:13:44 +053068 * Represents data model node to maintain information defined in YANG choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053069 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053070public class YangChoice extends YangNode
Bharat saraswalb1170bd2016-07-14 13:26:18 +053071 implements YangCommonInfo, Parsable, CollisionDetector, YangAugmentableNode,
rama-huawei6c728a92016-07-11 14:48:12 +053072 YangWhenHolder, YangIfFeatureHolder, YangAppErrorHolder {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053073
Bharat saraswal96dfef02016-06-16 00:29:12 +053074 private static final long serialVersionUID = 806201604L;
75
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053076 /**
77 * Name of choice.
78 */
79 private String name;
80
81 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053082 * If the choice represents config data.
83 */
84 private boolean isConfig;
85
86 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053087 * Description of choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053088 */
89 private String description;
90
91 /**
92 * Reference RFC 6020.
93 *
94 * The "mandatory" statement, which is optional, takes as an argument the
95 * string "true" or "false", and puts a constraint on valid data. If
96 * "mandatory" is "true", at least one node from exactly one of the choice's
97 * case branches MUST exist.
98 *
99 * If not specified, the default is "false".
100 *
101 * The behavior of the constraint depends on the type of the choice's
102 * closest ancestor node in the schema tree which is not a non-presence
103 * container:
104 *
105 * o If this ancestor is a case node, the constraint is enforced if any
106 * other node from the case exists.
107 *
108 * o Otherwise, it is enforced if the ancestor node exists.
109 */
110 private String mandatory;
111
112 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530113 * Reference of the choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530114 */
115 private String reference;
116
117 /**
118 * Status of the node.
119 */
120 private YangStatusType status;
121
122 /**
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530123 * Reference RFC 6020.
124 * <p>
125 * The "default" statement indicates if a case should be considered as the
126 * default if no child nodes from any of the choice's cases exist. The
127 * argument is the identifier of the "case" statement. If the "default"
128 * statement is missing, there is no default case.
129 * <p>
130 * The "default" statement MUST NOT be present on choices where "mandatory"
131 * is true.
132 * <p>
133 * The default case is only important when considering the default values of
134 * nodes under the cases. The default values for nodes under the default
135 * case are used if none of the nodes under any of the cases are present.
136 * <p>
137 * There MUST NOT be any mandatory nodes directly under the default case.
138 * <p>
139 * Default values for child nodes under a case are only used if one of the
140 * nodes under that case is present, or if that case is the default case. If
141 * none of the nodes under a case are present and the case is not the
142 * default case, the default values of the cases' child nodes are ignored.
143 * <p>
144 * the default case to be used if no case members is present.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530145 */
146 private String defaultValueInString;
147
148 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530149 * When data of the node.
150 */
151 private YangWhen when;
152
153 /**
154 * List of if-feature.
155 */
156 private List<YangIfFeature> ifFeatureList;
157
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530158 private List<YangAugmentedInfo> yangAugmentedInfo = new ArrayList<>();
159
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530160 /**
rama-huawei6c728a92016-07-11 14:48:12 +0530161 * YANG application error information.
162 */
163 private YangAppErrorInfo yangAppErrorInfo;
164
165 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530166 * Create a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530167 */
168 public YangChoice() {
169 super(YangNodeType.CHOICE_NODE);
rama-huawei6c728a92016-07-11 14:48:12 +0530170 yangAppErrorInfo = new YangAppErrorInfo();
171 yangAppErrorInfo.setErrorTag(DATA_MISSING_ERROR_TAG);
172 yangAppErrorInfo.setErrorAppTag(MISSING_CHOICE_ERROR_APP_TAG);
173 yangAppErrorInfo.setErrorAppPath(ERROR_PATH_MISSING_CHOICE);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530174 }
175
Vinod Kumar S38046502016-03-23 15:30:27 +0530176 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530177 * Returns the when.
178 *
179 * @return the when
180 */
181 @Override
182 public YangWhen getWhen() {
183 return when;
184 }
185
186 /**
187 * Sets the when.
188 *
189 * @param when the when to set
190 */
191 @Override
192 public void setWhen(YangWhen when) {
193 this.when = when;
194 }
195
196 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530197 * Returns the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530198 *
199 * @return choice name
200 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530201 @Override
202 public String getName() {
203 return name;
204 }
205
Vinod Kumar S38046502016-03-23 15:30:27 +0530206 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530207 * Sets the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530208 *
209 * @param name choice name
210 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530211 @Override
212 public void setName(String name) {
213 this.name = name;
214 }
215
216 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530217 * Returns config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530218 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530219 * @return the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530220 */
221 public boolean isConfig() {
222 return isConfig;
223 }
224
225 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530226 * Sets config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530227 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530228 * @param isCfg the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530229 */
230 public void setConfig(boolean isCfg) {
231 isConfig = isCfg;
232 }
233
234 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530235 * Returns the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530236 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530237 * @return the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530238 */
239 public String getMandatory() {
240 return mandatory;
241 }
242
243 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530244 * Sets the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530245 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530246 * @param mandatory the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530247 */
248 public void setMandatory(String mandatory) {
249 this.mandatory = mandatory;
250 }
251
252 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530253 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530254 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530255 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530256 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530257 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530258 public String getDescription() {
259 return description;
260 }
261
262 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530263 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530264 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530265 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530266 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530267 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530268 public void setDescription(String description) {
269 this.description = description;
270 }
271
272 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530273 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530274 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530275 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530276 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530277 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530278 public String getReference() {
279 return reference;
280 }
281
282 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530283 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530284 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530285 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530286 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530287 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530288 public void setReference(String reference) {
289 this.reference = reference;
290 }
291
292 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530293 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530294 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530295 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530296 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530297 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530298 public YangStatusType getStatus() {
299 return status;
300 }
301
302 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530303 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530304 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530305 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530306 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530307 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530308 public void setStatus(YangStatusType status) {
309 this.status = status;
310 }
311
312 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530313 * Returns the default value.
314 *
315 * @return the default value
316 */
317 public String getDefaultValueInString() {
318 return defaultValueInString;
319 }
320
321 /**
322 * Sets the default value.
323 *
324 * @param defaultValueInString the default value
325 */
326 public void setDefaultValueInString(String defaultValueInString) {
327 this.defaultValueInString = defaultValueInString;
328 }
329
330 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530331 * Returns the type of the data.
332 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530333 * @return choice data
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530334 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530335 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530336 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530337 return CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530338 }
339
340 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530341 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530342 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530343 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530344 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530345 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530346 public void validateDataOnEntry() throws DataModelException {
347 // TODO auto-generated method stub, to be implemented by parser
348 }
349
350 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530351 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530352 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530353 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530354 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530355 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530356 public void validateDataOnExit() throws DataModelException {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530357 if (defaultValueInString != null && !defaultValueInString.isEmpty()) {
358 YangNode node = getChild();
359 boolean matched = false;
360 // Check whether default string matches the case
361 while (node != null) {
362 if (node instanceof YangCase) {
363 if (defaultValueInString.equals(((YangCase) node).getName())) {
364 matched = true;
365 break;
366 }
367 }
368 node = node.getNextSibling();
369 }
370
371 if (!matched) {
372 throw new DataModelException("YANG file error: default string \"" + defaultValueInString
373 + "\" not matching choice \"" + getName() + "\" case.");
374 }
375 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530376 }
377
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530378 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530379 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
380
Bharat saraswal96dfef02016-06-16 00:29:12 +0530381 if (getParent() instanceof YangCase && dataType != YangConstructType.CASE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530382 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
383 }
384 YangNode node = getChild();
385 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530386 if (node instanceof CollisionDetector) {
387 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
388 }
389 node = node.getNextSibling();
390 }
391 }
392
393 @Override
394 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
395
396 if (dataType == CHOICE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530397 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530398 throw new DataModelException("YANG file error: Identifier collision detected in choice \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530399 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530400 }
401 return;
402 }
403
Vinod Kumar S38046502016-03-23 15:30:27 +0530404 YangNode node = getChild();
405 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530406 if (node instanceof CollisionDetector) {
407 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
408 }
409 node = node.getNextSibling();
410 }
411 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530412
413 @Override
414 public List<YangIfFeature> getIfFeatureList() {
415 return ifFeatureList;
416 }
417
418 @Override
419 public void addIfFeatureList(YangIfFeature ifFeature) {
420 if (getIfFeatureList() == null) {
421 setIfFeatureList(new LinkedList<>());
422 }
423 getIfFeatureList().add(ifFeature);
424 }
425
426 @Override
427 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
428 this.ifFeatureList = ifFeatureList;
429 }
430
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530431 @Override
432 public void addAugmentation(YangAugmentedInfo augmentInfo) {
433 yangAugmentedInfo.add(augmentInfo);
434 }
435
436 @Override
437 public void removeAugmentation(YangAugmentedInfo augmentInfo) {
438 yangAugmentedInfo.remove(augmentInfo);
439 }
440
441 @Override
442 public List<YangAugmentedInfo> getAugmentedInfoList() {
443 return yangAugmentedInfo;
444 }
rama-huawei6c728a92016-07-11 14:48:12 +0530445
446 @Override
447 public void setAppErrorInfo(YangAppErrorInfo yangAppErrorInfo) {
448 this.yangAppErrorInfo = yangAppErrorInfo;
449 }
450
451 @Override
452 public YangAppErrorInfo getAppErrorInfo() {
453 return yangAppErrorInfo;
454 }
455
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530456}