blob: 7aa0d5deb225ce1133728d9b6bf47009ac6710cb [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 /**
87 * Reference RFC 6020.
88 *
89 * The "default" statement indicates if a case should be considered as the
90 * default if no child nodes from any of the choice's cases exist. The
91 * argument is the identifier of the "case" statement. If the "default"
92 * statement is missing, there is no default case.
93 *
94 * The "default" statement MUST NOT be present on choices where "mandatory"
95 * is true.
96 *
97 * The default case is only important when considering the default values of
98 * nodes under the cases. The default values for nodes under the default
99 * case are used if none of the nodes under any of the cases are present.
100 *
101 * There MUST NOT be any mandatory nodes directly under the default case.
102 *
103 * Default values for child nodes under a case are only used if one of the
104 * nodes under that case is present, or if that case is the default case. If
105 * none of the nodes under a case are present and the case is not the
106 * default case, the default values of the cases' child nodes are ignored.
107 *
108 * the default case to be used if no case members is present.
109 */
110 private String defaultCase;
111
112 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530113 * Description of choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530114 */
115 private String description;
116
117 /**
118 * Reference RFC 6020.
119 *
120 * The "mandatory" statement, which is optional, takes as an argument the
121 * string "true" or "false", and puts a constraint on valid data. If
122 * "mandatory" is "true", at least one node from exactly one of the choice's
123 * case branches MUST exist.
124 *
125 * If not specified, the default is "false".
126 *
127 * The behavior of the constraint depends on the type of the choice's
128 * closest ancestor node in the schema tree which is not a non-presence
129 * container:
130 *
131 * o If this ancestor is a case node, the constraint is enforced if any
132 * other node from the case exists.
133 *
134 * o Otherwise, it is enforced if the ancestor node exists.
135 */
136 private String mandatory;
137
138 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530139 * Reference of the choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530140 */
141 private String reference;
142
143 /**
144 * Status of the node.
145 */
146 private YangStatusType status;
147
148 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530149 * Default value in string, needs to be converted to the target object,
150 * based on the type.
151 */
152 private String defaultValueInString;
153
154 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530155 * When data of the node.
156 */
157 private YangWhen when;
158
159 /**
160 * List of if-feature.
161 */
162 private List<YangIfFeature> ifFeatureList;
163
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530164 private List<YangAugmentedInfo> yangAugmentedInfo = new ArrayList<>();
165
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530166 /**
rama-huawei6c728a92016-07-11 14:48:12 +0530167 * YANG application error information.
168 */
169 private YangAppErrorInfo yangAppErrorInfo;
170
171 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530172 * Create a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530173 */
174 public YangChoice() {
175 super(YangNodeType.CHOICE_NODE);
rama-huawei6c728a92016-07-11 14:48:12 +0530176 yangAppErrorInfo = new YangAppErrorInfo();
177 yangAppErrorInfo.setErrorTag(DATA_MISSING_ERROR_TAG);
178 yangAppErrorInfo.setErrorAppTag(MISSING_CHOICE_ERROR_APP_TAG);
179 yangAppErrorInfo.setErrorAppPath(ERROR_PATH_MISSING_CHOICE);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530180 }
181
Vinod Kumar S38046502016-03-23 15:30:27 +0530182 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530183 * Returns the when.
184 *
185 * @return the when
186 */
187 @Override
188 public YangWhen getWhen() {
189 return when;
190 }
191
192 /**
193 * Sets the when.
194 *
195 * @param when the when to set
196 */
197 @Override
198 public void setWhen(YangWhen when) {
199 this.when = when;
200 }
201
202 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530203 * Returns the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530204 *
205 * @return choice name
206 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530207 @Override
208 public String getName() {
209 return name;
210 }
211
Vinod Kumar S38046502016-03-23 15:30:27 +0530212 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530213 * Sets the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530214 *
215 * @param name choice name
216 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530217 @Override
218 public void setName(String name) {
219 this.name = name;
220 }
221
222 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530223 * Returns config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530224 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530225 * @return the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530226 */
227 public boolean isConfig() {
228 return isConfig;
229 }
230
231 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530232 * Sets config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530233 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530234 * @param isCfg the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530235 */
236 public void setConfig(boolean isCfg) {
237 isConfig = isCfg;
238 }
239
240 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530241 * Returns the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530242 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530243 * @return the default case
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530244 */
245 public String getDefaultCase() {
246 return defaultCase;
247 }
248
249 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530250 * Sets the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530251 *
252 * @param defaultCase the default case to set
253 */
254 public void setDefaultCase(String defaultCase) {
255 this.defaultCase = defaultCase;
256 }
257
258 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530259 * Returns the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530260 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530261 * @return the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530262 */
263 public String getMandatory() {
264 return mandatory;
265 }
266
267 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530268 * Sets the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530269 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530270 * @param mandatory the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530271 */
272 public void setMandatory(String mandatory) {
273 this.mandatory = mandatory;
274 }
275
276 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530277 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530278 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530279 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530280 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530281 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530282 public String getDescription() {
283 return description;
284 }
285
286 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530287 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530288 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530289 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530290 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530291 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530292 public void setDescription(String description) {
293 this.description = description;
294 }
295
296 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530297 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530298 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530299 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530300 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530301 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530302 public String getReference() {
303 return reference;
304 }
305
306 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530307 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530308 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530309 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530310 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530311 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530312 public void setReference(String reference) {
313 this.reference = reference;
314 }
315
316 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530317 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530318 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530319 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530320 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530321 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530322 public YangStatusType getStatus() {
323 return status;
324 }
325
326 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530327 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530328 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530329 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530330 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530331 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530332 public void setStatus(YangStatusType status) {
333 this.status = status;
334 }
335
336 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530337 * Returns the default value.
338 *
339 * @return the default value
340 */
341 public String getDefaultValueInString() {
342 return defaultValueInString;
343 }
344
345 /**
346 * Sets the default value.
347 *
348 * @param defaultValueInString the default value
349 */
350 public void setDefaultValueInString(String defaultValueInString) {
351 this.defaultValueInString = defaultValueInString;
352 }
353
354 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530355 * Returns the type of the data.
356 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530357 * @return choice data
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530358 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530359 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530360 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530361 return CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530362 }
363
364 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530365 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530366 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530367 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530368 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530369 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530370 public void validateDataOnEntry() throws DataModelException {
371 // TODO auto-generated method stub, to be implemented by parser
372 }
373
374 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530375 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530376 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530377 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530378 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530379 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530380 public void validateDataOnExit() throws DataModelException {
381 // TODO auto-generated method stub, to be implemented by parser
382 }
383
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530384 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530385 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
386
Bharat saraswal96dfef02016-06-16 00:29:12 +0530387 if (getParent() instanceof YangCase && dataType != YangConstructType.CASE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530388 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
389 }
390 YangNode node = getChild();
391 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530392 if (node instanceof CollisionDetector) {
393 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
394 }
395 node = node.getNextSibling();
396 }
397 }
398
399 @Override
400 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
401
402 if (dataType == CHOICE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530403 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530404 throw new DataModelException("YANG file error: Identifier collision detected in choice \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530405 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530406 }
407 return;
408 }
409
Vinod Kumar S38046502016-03-23 15:30:27 +0530410 YangNode node = getChild();
411 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530412 if (node instanceof CollisionDetector) {
413 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
414 }
415 node = node.getNextSibling();
416 }
417 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530418
419 @Override
420 public List<YangIfFeature> getIfFeatureList() {
421 return ifFeatureList;
422 }
423
424 @Override
425 public void addIfFeatureList(YangIfFeature ifFeature) {
426 if (getIfFeatureList() == null) {
427 setIfFeatureList(new LinkedList<>());
428 }
429 getIfFeatureList().add(ifFeature);
430 }
431
432 @Override
433 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
434 this.ifFeatureList = ifFeatureList;
435 }
436
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530437 @Override
438 public void addAugmentation(YangAugmentedInfo augmentInfo) {
439 yangAugmentedInfo.add(augmentInfo);
440 }
441
442 @Override
443 public void removeAugmentation(YangAugmentedInfo augmentInfo) {
444 yangAugmentedInfo.remove(augmentInfo);
445 }
446
447 @Override
448 public List<YangAugmentedInfo> getAugmentedInfoList() {
449 return yangAugmentedInfo;
450 }
rama-huawei6c728a92016-07-11 14:48:12 +0530451
452 @Override
453 public void setAppErrorInfo(YangAppErrorInfo yangAppErrorInfo) {
454 this.yangAppErrorInfo = yangAppErrorInfo;
455 }
456
457 @Override
458 public YangAppErrorInfo getAppErrorInfo() {
459 return yangAppErrorInfo;
460 }
461
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530462}