blob: 43a486f0ad76a3c301860647b42974b413f6145a [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 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053062public class YangChoice extends YangNode implements YangCommonInfo, Parsable, CollisionDetector {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053063
64 /**
65 * Name of choice.
66 */
67 private String name;
68
69 /**
Vinod Kumar S2ff139c2016-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 Agrawal8e8770a2016-02-27 03:57:50 +0530101 * Description of choice.
Vinod Kumar S2ff139c2016-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 Agrawal8e8770a2016-02-27 03:57:50 +0530127 * Reference of the choice.
Vinod Kumar S2ff139c2016-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 Agrawal8e8770a2016-02-27 03:57:50 +0530137 * Create a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530138 */
139 public YangChoice() {
140 super(YangNodeType.CHOICE_NODE);
141 }
142
Vinod Kumar S38046502016-03-23 15:30:27 +0530143 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530144 * Returns the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530145 *
146 * @return choice name
147 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530148 @Override
149 public String getName() {
150 return name;
151 }
152
Vinod Kumar S38046502016-03-23 15:30:27 +0530153 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530154 * Sets the choice name.
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 *
156 * @param name choice name
157 */
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530158 @Override
159 public void setName(String name) {
160 this.name = name;
161 }
162
163 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530164 * Returns config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530165 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530166 * @return the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530167 */
168 public boolean isConfig() {
169 return isConfig;
170 }
171
172 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530173 * Sets config flag.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530174 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530175 * @param isCfg the config flag
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530176 */
177 public void setConfig(boolean isCfg) {
178 isConfig = isCfg;
179 }
180
181 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530182 * Returns the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530183 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530184 * @return the default case
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530185 */
186 public String getDefaultCase() {
187 return defaultCase;
188 }
189
190 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530191 * Sets the default case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530192 *
193 * @param defaultCase the default case to set
194 */
195 public void setDefaultCase(String defaultCase) {
196 this.defaultCase = defaultCase;
197 }
198
199 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530200 * Returns the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530201 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530202 * @return the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530203 */
204 public String getMandatory() {
205 return mandatory;
206 }
207
208 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530209 * Sets the mandatory status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530210 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530211 * @param mandatory the mandatory status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530212 */
213 public void setMandatory(String mandatory) {
214 this.mandatory = mandatory;
215 }
216
217 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530218 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530219 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530220 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530221 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530222 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530223 public String getDescription() {
224 return description;
225 }
226
227 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530228 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530229 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530230 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530231 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530232 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530233 public void setDescription(String description) {
234 this.description = description;
235 }
236
237 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530238 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530239 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530240 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530241 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530242 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530243 public String getReference() {
244 return reference;
245 }
246
247 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530248 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530249 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530250 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530251 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530252 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530253 public void setReference(String reference) {
254 this.reference = reference;
255 }
256
257 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530258 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530259 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530260 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530261 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530262 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530263 public YangStatusType getStatus() {
264 return status;
265 }
266
267 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530268 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530269 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530270 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530271 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530272 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530273 public void setStatus(YangStatusType status) {
274 this.status = status;
275 }
276
277 /**
278 * Returns the type of the data.
279 *
280 * @return returns CHOICE_DATA
281 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530282 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530283 public YangConstructType getYangConstructType() {
284 return YangConstructType.CHOICE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530285 }
286
287 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530288 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530289 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530290 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530291 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530292 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530293 public void validateDataOnEntry() throws DataModelException {
294 // TODO auto-generated method stub, to be implemented by parser
295 }
296
297 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530298 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530299 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530300 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530301 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530302 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530303 public void validateDataOnExit() throws DataModelException {
304 // TODO auto-generated method stub, to be implemented by parser
305 }
306
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530307 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530308 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
309
Vinod Kumar S38046502016-03-23 15:30:27 +0530310 if (this.getParent() instanceof YangCase && dataType != YangConstructType.CASE_DATA) {
311 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
312 }
313 YangNode node = getChild();
314 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530315 if (node instanceof CollisionDetector) {
316 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
317 }
318 node = node.getNextSibling();
319 }
320 }
321
322 @Override
323 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
324
325 if (dataType == CHOICE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530326 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530327 throw new DataModelException("YANG file error: Identifier collision detected in choice \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530328 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530329 }
330 return;
331 }
332
Vinod Kumar S38046502016-03-23 15:30:27 +0530333 YangNode node = getChild();
334 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530335 if (node instanceof CollisionDetector) {
336 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
337 }
338 node = node.getNextSibling();
339 }
340 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530341}