blob: e8d60bf6b23aafffef602d40d76375c104b221ef [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
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053018import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
19import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053020import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar S38046502016-03-23 15:30:27 +053021
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053022import static org.onosproject.yangutils.utils.YangConstructType.CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053023
24/*-
25 * Reference RFC 6020.
26 *
27 * The "choice" statement defines a set of alternatives, only one of
28 * which may exist at any one time. The argument is an identifier,
29 * followed by a block of sub-statements that holds detailed choice
30 * information. The identifier is used to identify the choice node in
31 * the schema tree. A choice node does not exist in the data tree.
32 *
33 * A choice consists of a number of branches, defined with the "case"
34 * sub-statement. Each branch contains a number of child nodes. The
35 * nodes from at most one of the choice's branches exist at the same
36 * time.
37 *
38 * The choice's sub-statements
39 *
40 * +--------------+---------+-------------+------------------+
41 * | substatement | section | cardinality |data model mapping|
42 * +--------------+---------+-------------+------------------+
43 * | anyxml | 7.10 | 0..n |-not supported |
44 * | case | 7.9.2 | 0..n |-YangChoice |
45 * | config | 7.19.1 | 0..1 |-boolean |
46 * | container | 7.5 | 0..n |-child case nodes |
47 * | default | 7.9.3 | 0..1 |-string |
48 * | description | 7.19.3 | 0..1 |-string |
49 * | if-feature | 7.18.2 | 0..n |-TODO |
50 * | leaf | 7.6 | 0..n |-child case nodes |
51 * | leaf-list | 7.7 | 0..n |-child case nodes |
52 * | list | 7.8 | 0..n |-child case nodes |
53 * | mandatory | 7.9.4 | 0..1 |-string |
54 * | reference | 7.19.4 | 0..1 |-string |
55 * | status | 7.19.2 | 0..1 |-string |
56 * | when | 7.19.5 | 0..1 |-TODO |
57 * +--------------+---------+-------------+------------------+
58 */
59/**
Bharat saraswald9822e92016-04-05 15:13:44 +053060 * Represents data model node to maintain information defined in YANG choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053061 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053062public class YangChoice extends YangNode
63 implements YangCommonInfo, Parsable, CollisionDetector, YangAugmentationHolder {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053064
65 /**
66 * Name of choice.
67 */
68 private String name;
69
70 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053071 * If the choice represents config data.
72 */
73 private boolean isConfig;
74
75 /**
76 * Reference RFC 6020.
77 *
78 * The "default" statement indicates if a case should be considered as the
79 * default if no child nodes from any of the choice's cases exist. The
80 * argument is the identifier of the "case" statement. If the "default"
81 * statement is missing, there is no default case.
82 *
83 * The "default" statement MUST NOT be present on choices where "mandatory"
84 * is true.
85 *
86 * The default case is only important when considering the default values of
87 * nodes under the cases. The default values for nodes under the default
88 * case are used if none of the nodes under any of the cases are present.
89 *
90 * There MUST NOT be any mandatory nodes directly under the default case.
91 *
92 * Default values for child nodes under a case are only used if one of the
93 * nodes under that case is present, or if that case is the default case. If
94 * none of the nodes under a case are present and the case is not the
95 * default case, the default values of the cases' child nodes are ignored.
96 *
97 * the default case to be used if no case members is present.
98 */
99 private String defaultCase;
100
101 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530102 * Description of choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530103 */
104 private String description;
105
106 /**
107 * Reference RFC 6020.
108 *
109 * The "mandatory" statement, which is optional, takes as an argument the
110 * string "true" or "false", and puts a constraint on valid data. If
111 * "mandatory" is "true", at least one node from exactly one of the choice's
112 * case branches MUST exist.
113 *
114 * If not specified, the default is "false".
115 *
116 * The behavior of the constraint depends on the type of the choice's
117 * closest ancestor node in the schema tree which is not a non-presence
118 * container:
119 *
120 * o If this ancestor is a case node, the constraint is enforced if any
121 * other node from the case exists.
122 *
123 * o Otherwise, it is enforced if the ancestor node exists.
124 */
125 private String mandatory;
126
127 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530128 * Reference of the choice.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530129 */
130 private String reference;
131
132 /**
133 * Status of the node.
134 */
135 private YangStatusType status;
136
137 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530138 * Default value in string, needs to be converted to the target object,
139 * based on the type.
140 */
141 private String defaultValueInString;
142
143 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530144 * Create a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530145 */
146 public YangChoice() {
147 super(YangNodeType.CHOICE_NODE);
148 }
149
Vinod Kumar S38046502016-03-23 15:30:27 +0530150 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530151 * Returns the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530152 *
153 * @return choice name
154 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530155 @Override
156 public String getName() {
157 return name;
158 }
159
Vinod Kumar S38046502016-03-23 15:30:27 +0530160 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530161 * Sets the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530162 *
163 * @param name choice name
164 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530165 @Override
166 public void setName(String name) {
167 this.name = name;
168 }
169
170 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530171 * Returns config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530172 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530173 * @return the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530174 */
175 public boolean isConfig() {
176 return isConfig;
177 }
178
179 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530180 * Sets config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530181 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530182 * @param isCfg the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530183 */
184 public void setConfig(boolean isCfg) {
185 isConfig = isCfg;
186 }
187
188 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530189 * Returns the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530190 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530191 * @return the default case
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530192 */
193 public String getDefaultCase() {
194 return defaultCase;
195 }
196
197 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530198 * Sets the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530199 *
200 * @param defaultCase the default case to set
201 */
202 public void setDefaultCase(String defaultCase) {
203 this.defaultCase = defaultCase;
204 }
205
206 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530207 * Returns the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530208 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530209 * @return the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530210 */
211 public String getMandatory() {
212 return mandatory;
213 }
214
215 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530216 * Sets the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530217 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530218 * @param mandatory the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530219 */
220 public void setMandatory(String mandatory) {
221 this.mandatory = mandatory;
222 }
223
224 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530225 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530226 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530227 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530228 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530229 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530230 public String getDescription() {
231 return description;
232 }
233
234 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530235 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530236 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530237 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530238 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530239 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530240 public void setDescription(String description) {
241 this.description = description;
242 }
243
244 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530245 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530246 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530247 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530248 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530249 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530250 public String getReference() {
251 return reference;
252 }
253
254 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530255 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530256 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530257 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530258 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530259 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530260 public void setReference(String reference) {
261 this.reference = reference;
262 }
263
264 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530265 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530266 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530267 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530268 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530269 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530270 public YangStatusType getStatus() {
271 return status;
272 }
273
274 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530275 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530276 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530277 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530278 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530279 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530280 public void setStatus(YangStatusType status) {
281 this.status = status;
282 }
283
284 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530285 * Returns the default value.
286 *
287 * @return the default value
288 */
289 public String getDefaultValueInString() {
290 return defaultValueInString;
291 }
292
293 /**
294 * Sets the default value.
295 *
296 * @param defaultValueInString the default value
297 */
298 public void setDefaultValueInString(String defaultValueInString) {
299 this.defaultValueInString = defaultValueInString;
300 }
301
302 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530303 * Returns the type of the data.
304 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530305 * @return choice data
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530306 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530307 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530308 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530309 return CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530310 }
311
312 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530313 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530314 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530315 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530316 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530317 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530318 public void validateDataOnEntry() throws DataModelException {
319 // TODO auto-generated method stub, to be implemented by parser
320 }
321
322 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530323 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530324 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530325 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530326 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530327 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530328 public void validateDataOnExit() throws DataModelException {
329 // TODO auto-generated method stub, to be implemented by parser
330 }
331
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530332 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530333 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
334
Vinod Kumar S38046502016-03-23 15:30:27 +0530335 if (this.getParent() instanceof YangCase && dataType != YangConstructType.CASE_DATA) {
336 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
337 }
338 YangNode node = getChild();
339 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530340 if (node instanceof CollisionDetector) {
341 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
342 }
343 node = node.getNextSibling();
344 }
345 }
346
347 @Override
348 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
349
350 if (dataType == CHOICE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530351 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530352 throw new DataModelException("YANG file error: Identifier collision detected in choice \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530353 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530354 }
355 return;
356 }
357
Vinod Kumar S38046502016-03-23 15:30:27 +0530358 YangNode node = getChild();
359 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530360 if (node instanceof CollisionDetector) {
361 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
362 }
363 node = node.getNextSibling();
364 }
365 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530366}