blob: 440ca5760373d4db3a2ace75037218735c4789d4 [file] [log] [blame]
Vinod Kumar S5a39e012016-02-16 01:37:16 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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 S5a39e012016-02-16 01:37:16 +053018import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
19import org.onosproject.yangutils.parser.Parsable;
Bharat saraswal5e3c45c2016-02-22 22:15:21 +053020import org.onosproject.yangutils.translator.CachedFileHandle;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053021import org.onosproject.yangutils.utils.YangConstructType;
22import static org.onosproject.yangutils.utils.YangConstructType.CHOICE_DATA;
Vinod Kumar S5a39e012016-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/**
60 * Data model node to maintain information defined in YANG choice.
61 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053062public class YangChoice extends YangNode implements YangCommonInfo, Parsable, CollisionDetector {
Vinod Kumar S5a39e012016-02-16 01:37:16 +053063
64 /**
65 * Name of choice.
66 */
67 private String name;
68
69 /**
Vinod Kumar S5a39e012016-02-16 01:37:16 +053070 * If the choice represents config data.
71 */
72 private boolean isConfig;
73
74 /**
75 * Reference RFC 6020.
76 *
77 * The "default" statement indicates if a case should be considered as the
78 * default if no child nodes from any of the choice's cases exist. The
79 * argument is the identifier of the "case" statement. If the "default"
80 * statement is missing, there is no default case.
81 *
82 * The "default" statement MUST NOT be present on choices where "mandatory"
83 * is true.
84 *
85 * The default case is only important when considering the default values of
86 * nodes under the cases. The default values for nodes under the default
87 * case are used if none of the nodes under any of the cases are present.
88 *
89 * There MUST NOT be any mandatory nodes directly under the default case.
90 *
91 * Default values for child nodes under a case are only used if one of the
92 * nodes under that case is present, or if that case is the default case. If
93 * none of the nodes under a case are present and the case is not the
94 * default case, the default values of the cases' child nodes are ignored.
95 *
96 * the default case to be used if no case members is present.
97 */
98 private String defaultCase;
99
100 /**
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530101 * Description of choice.
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530102 */
103 private String description;
104
105 /**
106 * Reference RFC 6020.
107 *
108 * The "mandatory" statement, which is optional, takes as an argument the
109 * string "true" or "false", and puts a constraint on valid data. If
110 * "mandatory" is "true", at least one node from exactly one of the choice's
111 * case branches MUST exist.
112 *
113 * If not specified, the default is "false".
114 *
115 * The behavior of the constraint depends on the type of the choice's
116 * closest ancestor node in the schema tree which is not a non-presence
117 * container:
118 *
119 * o If this ancestor is a case node, the constraint is enforced if any
120 * other node from the case exists.
121 *
122 * o Otherwise, it is enforced if the ancestor node exists.
123 */
124 private String mandatory;
125
126 /**
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530127 * Reference of the choice.
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530128 */
129 private String reference;
130
131 /**
132 * Status of the node.
133 */
134 private YangStatusType status;
135
136 /**
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530137 * Create a choice node.
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530138 */
139 public YangChoice() {
140 super(YangNodeType.CHOICE_NODE);
141 }
142
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530143 /*
144 * (non-Javadoc)
145 *
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530146 * @see org.onosproject.yangutils.datamodel.YangNode#getName()
147 */
148 @Override
149 public String getName() {
150 return name;
151 }
152
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530153 /*
154 * (non-Javadoc)
155 *
156 * @see
157 * org.onosproject.yangutils.datamodel.YangNode#setName(java.lang.String)
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530158 */
159 @Override
160 public void setName(String name) {
161 this.name = name;
162 }
163
164 /**
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530165 * Get config flag.
166 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530167 * @return the config flag
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530168 */
169 public boolean isConfig() {
170 return isConfig;
171 }
172
173 /**
174 * Set config flag.
175 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530176 * @param isCfg the config flag
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530177 */
178 public void setConfig(boolean isCfg) {
179 isConfig = isCfg;
180 }
181
182 /**
183 * Get the default case.
184 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530185 * @return the default case
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530186 */
187 public String getDefaultCase() {
188 return defaultCase;
189 }
190
191 /**
192 * Set the default case.
193 *
194 * @param defaultCase the default case to set
195 */
196 public void setDefaultCase(String defaultCase) {
197 this.defaultCase = defaultCase;
198 }
199
200 /**
201 * Get the mandatory status.
202 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530203 * @return the mandatory status
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530204 */
205 public String getMandatory() {
206 return mandatory;
207 }
208
209 /**
210 * Set the mandatory status.
211 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530212 * @param mandatory the mandatory status
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530213 */
214 public void setMandatory(String mandatory) {
215 this.mandatory = mandatory;
216 }
217
218 /**
219 * Get the description.
220 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530221 * @return the description
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530222 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530223 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530224 public String getDescription() {
225 return description;
226 }
227
228 /**
229 * Set the description.
230 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530231 * @param description set the description
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530232 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530233 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530234 public void setDescription(String description) {
235 this.description = description;
236 }
237
238 /**
239 * Get the textual reference.
240 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530241 * @return the reference
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530242 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530243 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530244 public String getReference() {
245 return reference;
246 }
247
248 /**
249 * Set the textual reference.
250 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530251 * @param reference the reference to set
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530252 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530253 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530254 public void setReference(String reference) {
255 this.reference = reference;
256 }
257
258 /**
259 * Get the status.
260 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530261 * @return the status
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530262 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530263 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530264 public YangStatusType getStatus() {
265 return status;
266 }
267
268 /**
269 * Set the status.
270 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530271 * @param status the status to set
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530272 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530273 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530274 public void setStatus(YangStatusType status) {
275 this.status = status;
276 }
277
278 /**
279 * Returns the type of the data.
280 *
281 * @return returns CHOICE_DATA
282 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530283 @Override
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530284 public YangConstructType getYangConstructType() {
285 return YangConstructType.CHOICE_DATA;
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530286 }
287
288 /**
289 * Validate the data on entering the corresponding parse tree node.
290 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530291 * @throws DataModelException a violation of data model rules
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530292 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530293 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530294 public void validateDataOnEntry() throws DataModelException {
295 // TODO auto-generated method stub, to be implemented by parser
296 }
297
298 /**
299 * Validate the data on exiting the corresponding parse tree node.
300 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530301 * @throws DataModelException a violation of data model rules
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530302 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530303 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530304 public void validateDataOnExit() throws DataModelException {
305 // TODO auto-generated method stub, to be implemented by parser
306 }
307
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530308 /*
309 * (non-Javadoc)
310 *
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530311 * @see org.onosproject.yangutils.datamodel.YangNode#getPackage()
312 */
313 @Override
314 public String getPackage() {
315 // TODO Auto-generated method stub
316 return null;
317 }
318
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530319 /*
320 * (non-Javadoc)
321 *
322 * @see
323 * org.onosproject.yangutils.datamodel.YangNode#setPackage(java.lang.String)
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530324 */
325 @Override
326 public void setPackage(String pkg) {
327 // TODO Auto-generated method stub
328
329 }
330
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530331 /*
332 * (non-Javadoc)
333 *
334 * @see
335 * org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeEntry(
336 * )
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530337 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530338 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530339 public void generateJavaCodeEntry() {
340 // TODO Auto-generated method stub
341
342 }
343
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530344 /*
345 * (non-Javadoc)
346 *
347 * @see
348 * org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeExit()
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530349 */
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530350 @Override
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530351 public void generateJavaCodeExit() {
352 // TODO Auto-generated method stub
353
354 }
Bharat saraswal5e3c45c2016-02-22 22:15:21 +0530355
356 @Override
357 public CachedFileHandle getFileHandle() {
358 // TODO Auto-generated method stub
359 return null;
360 }
361
362 @Override
363 public void setFileHandle(CachedFileHandle fileHandle) {
364 // TODO Auto-generated method stub
365
366 }
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530367
368 @Override
369 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
370
371 YangNode node = this.getChild();
372 while ((node != null)) {
373 if (node instanceof CollisionDetector) {
374 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
375 }
376 node = node.getNextSibling();
377 }
378 }
379
380 @Override
381 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
382
383 if (dataType == CHOICE_DATA) {
384 if (this.getName().equals(identifierName)) {
385 throw new DataModelException("YANG file error: Identifier collision detected in choice \"" +
386 this.getName() + "\"");
387 }
388 return;
389 }
390
391 YangNode node = this.getChild();
392 while ((node != null)) {
393 if (node instanceof CollisionDetector) {
394 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
395 }
396 node = node.getNextSibling();
397 }
398 }
Vinod Kumar S5a39e012016-02-16 01:37:16 +0530399}