blob: a00b77de0a2593d4f23c4233733a8a43c7f28e47 [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;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import org.onosproject.yangutils.datamodel.utils.Parsable;
23import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Vinod Kumar S38046502016-03-23 15:30:27 +053024
25import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
Bharat saraswal96dfef02016-06-16 00:29:12 +053026import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CASE_DATA;
Vinod Kumar S38046502016-03-23 15:30:27 +053027
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 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053091
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053092/**
Bharat saraswald9822e92016-04-05 15:13:44 +053093 * Represents data model node to maintain information defined in YANG case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053094 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053095public class YangCase
96 extends YangNode
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053097 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector, YangAugmentationHolder {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053098
Bharat saraswal96dfef02016-06-16 00:29:12 +053099 private static final long serialVersionUID = 806201603L;
100
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530101 /**
102 * Case name.
103 */
104 private String name;
105
106 // TODO: default field identification for the case
107
108 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530109 * Description of case.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530110 */
111 private String description;
112
113 /**
114 * List of leaves.
115 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530116 private List<YangLeaf> listOfLeaf;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530117
118 /**
119 * List of leaf lists.
120 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530121 private List<YangLeafList> listOfLeafList;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530122
123 /**
124 * Reference of the module.
125 */
126 private String reference;
127
128 /**
129 * Status of the node.
130 */
131 private YangStatusType status;
132
133 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530134 * Creates a choice node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530135 */
136 public YangCase() {
137 super(YangNodeType.CASE_NODE);
138 }
139
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530140 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530141 * Returns the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530142 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530143 * @return case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530144 */
145 @Override
146 public String getName() {
147 return name;
148 }
149
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530150 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530151 * Sets the case name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530152 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530153 * @param name case name
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530154 */
155 @Override
156 public void setName(String name) {
157 this.name = name;
158 }
159
160 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530161 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530162 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530163 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530164 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530165 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530166 public String getDescription() {
167 return description;
168 }
169
170 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530171 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530172 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530173 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530174 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530175 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530176 public void setDescription(String description) {
177 this.description = description;
178 }
179
180 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530181 * Returns the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530182 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530183 * @return the list of leaves
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530184 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530185 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530186 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530187 return listOfLeaf;
188 }
189
190 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530191 * Sets the list of leaves.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530192 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530193 * @param leafsList the list of leaf to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530194 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530195 @Override
196 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530197 listOfLeaf = leafsList;
198 }
199
200 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530201 * Adds a leaf.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530202 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530203 * @param leaf the leaf to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530204 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530205 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530206 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530207 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530208 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530209 }
210
211 getListOfLeaf().add(leaf);
212 }
213
214 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530215 * Returns the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530216 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530217 * @return the list of leaf-list
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530218 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530219 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530220 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530221 return listOfLeafList;
222 }
223
224 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530225 * Sets the list of leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530226 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530227 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530228 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530229 @Override
230 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530231 this.listOfLeafList = listOfLeafList;
232 }
233
234 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530235 * Adds a leaf-list.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530236 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530237 * @param leafList the leaf-list to be added
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530238 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530239 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530240 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530241 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530242 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530243 }
244
245 getListOfLeafList().add(leafList);
246 }
247
248 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530249 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530250 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530251 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530252 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530253 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530254 public String getReference() {
255 return reference;
256 }
257
258 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530259 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530260 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530261 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530262 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530263 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530264 public void setReference(String reference) {
265 this.reference = reference;
266 }
267
268 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530269 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530270 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530271 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530272 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530273 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530274 public YangStatusType getStatus() {
275 return status;
276 }
277
278 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530279 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530280 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530281 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530282 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530283 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530284 public void setStatus(YangStatusType status) {
285 this.status = status;
286 }
287
288 /**
289 * Returns the type of the data.
290 *
291 * @return returns CASE_DATA
292 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530293 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530294 public YangConstructType getYangConstructType() {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530295 return CASE_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530296 }
297
298 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530299 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530300 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530301 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530302 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530303 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530304 public void validateDataOnEntry()
305 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530306 // TODO auto-generated method stub, to be implemented by parser
307 }
308
309 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530310 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530311 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530312 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530313 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530314 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530315 public void validateDataOnExit()
316 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530317 // TODO auto-generated method stub, to be implemented by parser
318 }
319
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530320 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530321 public void detectCollidingChild(String identifierName, YangConstructType dataType)
322 throws DataModelException {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530323 if (!(getParent() instanceof YangChoice)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530324 throw new DataModelException("Internal Data Model Tree Error: Invalid/Missing holder in case " +
Vinod Kumar S38046502016-03-23 15:30:27 +0530325 getName());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530326 }
327 // Traverse up in tree to ask parent choice start collision detection.
Vinod Kumar S38046502016-03-23 15:30:27 +0530328 ((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530329 }
330
331 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530332 public void detectSelfCollision(String identifierName, YangConstructType dataType)
333 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530334
335 if (dataType == CASE_DATA) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530336 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530337 throw new DataModelException("YANG File Error: Identifier collision detected in case \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530338 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530339 }
340 return;
341 }
342
343 // Asks helper to detect colliding child.
344 detectCollidingChildUtil(identifierName, dataType, this);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530345 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530346}