blob: 48f5d171bec82f07518ec34f577ed621f36692b6 [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
18import java.util.LinkedList;
19import java.util.List;
20
Vinod Kumar S38046502016-03-23 15:30:27 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
22import org.onosproject.yangutils.parser.Parsable;
23import org.onosproject.yangutils.utils.YangConstructType;
24
25import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
26import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
27
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053028/*-
29 * Reference RFC 6020.
30 *
31 * The "case" statement is used to define branches of the choice. It takes as an
32 * argument an identifier, followed by a block of sub-statements that holds
33 * detailed case information.
34 *
35 * The identifier is used to identify the case node in the schema tree. A case
36 * node does not exist in the data tree.
37 *
38 * Within a "case" statement, the "anyxml", "choice", "container", "leaf",
39 * "list", "leaf-list", and "uses" statements can be used to define child nodes
40 * to the case node. The identifiers of all these child nodes MUST be unique
41 * within all cases in a choice. For example, the following is illegal:
42 *
43 * choice interface-type { // This example is illegal YANG
44 * case a {
45 * leaf ethernet { ... }
46 * }
47 * case b {
48 * container ethernet { ...}
49 * }
50 * }
51 *
52 * As a shorthand, the "case" statement can be omitted if the branch
53 * contains a single "anyxml", "container", "leaf", "list", or
54 * "leaf-list" statement. In this case, the identifier of the case node
55 * is the same as the identifier in the branch statement. The following
56 * example:
57 *
58 * choice interface-type {
59 * container ethernet { ... }
60 * }
61 *
62 * is equivalent to:
63 *
64 * choice interface-type {
65 * case ethernet {
66 * container ethernet { ... }
67 * }
68 * }
69 *
70 * The case identifier MUST be unique within a choice.
71 *
72 * The case's sub-statements
73 *
74 * +--------------+---------+-------------+------------------+
75 * | substatement | section | cardinality |data model mapping|
76 * +--------------+---------+-------------+------------------+
77 * | anyxml | 7.10 | 0..n |-not supported |
78 * | choice | 7.9 | 0..n |-child nodes |
79 * | container | 7.5 | 0..n |-child nodes |
80 * | description | 7.19.3 | 0..1 |-string |
81 * | if-feature | 7.18.2 | 0..n |-TODO |
82 * | leaf | 7.6 | 0..n |-YangLeaf |
83 * | leaf-list | 7.7 | 0..n |-YangLeafList |
84 * | list | 7.8 | 0..n |-child nodes |
85 * | reference | 7.19.4 | 0..1 |-string |
86 * | status | 7.19.2 | 0..1 |-YangStatus |
87 * | uses | 7.12 | 0..n |-child node |
88 * | when | 7.19.5 | 0..1 |-TODO |
89 * +--------------+---------+-------------+------------------+
90 */
91/**
Bharat saraswald9822e92016-04-05 15:13:44 +053092 * Represents data model node to maintain information defined in YANG case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053093 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053094public class YangCase extends YangNode
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053095 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053096
97 /**
98 * Case name.
99 */
100 private String name;
101
102 // TODO: default field identification for the case
103
104 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530105 * Description of case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530106 */
107 private String description;
108
109 /**
110 * List of leaves.
111 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530112 private List<YangLeaf> listOfLeaf;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530113
114 /**
115 * List of leaf lists.
116 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530117 private List<YangLeafList> listOfLeafList;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530118
119 /**
120 * Reference of the module.
121 */
122 private String reference;
123
124 /**
125 * Status of the node.
126 */
127 private YangStatusType status;
128
129 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530130 * Creates a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530131 */
132 public YangCase() {
133 super(YangNodeType.CASE_NODE);
134 }
135
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530136 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530137 * Returns the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530138 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530139 * @return case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530140 */
141 @Override
142 public String getName() {
143 return name;
144 }
145
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530146 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530147 * Sets the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530148 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530149 * @param name case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530150 */
151 @Override
152 public void setName(String name) {
153 this.name = name;
154 }
155
156 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530157 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530158 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530159 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530160 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530161 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530162 public String getDescription() {
163 return description;
164 }
165
166 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530167 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530168 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530169 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530170 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530171 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530172 public void setDescription(String description) {
173 this.description = description;
174 }
175
176 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530177 * Returns the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530178 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530179 * @return the list of leaves
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530180 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530181 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530182 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530183 return listOfLeaf;
184 }
185
186 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530187 * Sets the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530188 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530189 * @param leafsList the list of leaf to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530190 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530191 private void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530192 listOfLeaf = leafsList;
193 }
194
195 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530196 * Adds a leaf.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530197 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530198 * @param leaf the leaf to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530199 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530200 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530201 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530202 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530203 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530204 }
205
206 getListOfLeaf().add(leaf);
207 }
208
209 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530210 * Returns the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530211 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530212 * @return the list of leaf-list
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530213 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530214 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530215 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530216 return listOfLeafList;
217 }
218
219 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530220 * Sets the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530221 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530222 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530223 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530224 private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530225 this.listOfLeafList = listOfLeafList;
226 }
227
228 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530229 * Adds a leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530230 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530231 * @param leafList the leaf-list to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530232 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530233 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530234 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530235 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530236 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530237 }
238
239 getListOfLeafList().add(leafList);
240 }
241
242 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530243 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530244 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530245 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530246 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530247 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530248 public String getReference() {
249 return reference;
250 }
251
252 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530253 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530254 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530255 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530256 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530257 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530258 public void setReference(String reference) {
259 this.reference = reference;
260 }
261
262 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530263 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530264 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530265 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530266 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530267 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530268 public YangStatusType getStatus() {
269 return status;
270 }
271
272 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530273 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530274 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530275 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530276 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530277 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530278 public void setStatus(YangStatusType status) {
279 this.status = status;
280 }
281
282 /**
283 * Returns the type of the data.
284 *
285 * @return returns CASE_DATA
286 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530287 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530288 public YangConstructType getYangConstructType() {
289 return YangConstructType.CASE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530290 }
291
292 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530293 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530294 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530295 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530296 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530297 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530298 public void validateDataOnEntry() throws DataModelException {
299 // TODO auto-generated method stub, to be implemented by parser
300 }
301
302 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530303 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530304 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530305 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530306 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530307 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530308 public void validateDataOnExit() throws DataModelException {
309 // TODO auto-generated method stub, to be implemented by parser
310 }
311
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530312 @Override
313 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530314 if (getParent() == null || !(getParent() instanceof YangChoice)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530315 throw new DataModelException("Internal Data Model Tree Error: Invalid/Missing holder in case " +
Vinod Kumar S38046502016-03-23 15:30:27 +0530316 getName());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530317 }
318 // Traverse up in tree to ask parent choice start collision detection.
Vinod Kumar S38046502016-03-23 15:30:27 +0530319 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530320 }
321
322 @Override
323 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
324
325 if (dataType == CASE_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 case \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530328 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530329 }
330 return;
331 }
332
333 // Asks helper to detect colliding child.
334 detectCollidingChildUtil(identifierName, dataType, this);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530335 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530336}