blob: a6f4592c486c4c82a325d193fbb5baf3703e56d1 [file] [log] [blame]
Vinod Kumar S67e7be62016-02-11 20:13:28 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S67e7be62016-02-11 20:13:28 +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 */
16
17package org.onosproject.yangutils.datamodel;
18
19import java.util.LinkedList;
20import java.util.List;
21
22import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
23import org.onosproject.yangutils.parser.Parsable;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053024import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar S38046502016-03-23 15:30:27 +053025
26import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
27
Vinod Kumar S67e7be62016-02-11 20:13:28 +053028/*-
29 * Reference RFC 6020.
30 *
31 * The "container" statement is used to define an interior data node in the
32 * schema tree. It takes one argument, which is an identifier, followed by a
33 * block of sub-statements that holds detailed container information.
34 *
35 * A container node does not have a value, but it has a list of child nodes in
36 * the data tree. The child nodes are defined in the container's sub-statements.
37 *
38 * Containers with Presence
39 *
40 * YANG supports two styles of containers, those that exist only for organizing
41 * the hierarchy of data nodes, and those whose presence in the configuration
42 * has an explicit meaning.
43 *
44 * In the first style, the container has no meaning of its own, existing only to
45 * contain child nodes. This is the default style.
46 *
47 * For example, the set of scrambling options for Synchronous Optical Network
48 * (SONET) interfaces may be placed inside a "scrambling" container to enhance
49 * the organization of the configuration hierarchy, and to keep these nodes
50 * together. The "scrambling" node itself has no meaning, so removing the node
51 * when it becomes empty relieves the user from performing this task.
52 *
53 * In the second style, the presence of the container itself is configuration
54 * data, representing a single bit of configuration data. The container acts as
55 * both a configuration knob and a means of organizing related configuration.
56 * These containers are explicitly created and deleted.
57 *
58 * YANG calls this style a "presence container" and it is indicated using the
59 * "presence" statement, which takes as its argument a text string indicating
60 * what the presence of the node means.
61 *
62 * The container's Substatements
63 *
64 * +--------------+---------+-------------+------------------+
65 * | substatement | section | cardinality |data model mapping|
66 * +--------------+---------+-------------+------------------+
67 * | anyxml | 7.10 | 0..n | -not supported |
68 * | choice | 7.9 | 0..n | -child nodes |
69 * | config | 7.19.1 | 0..1 | -boolean |
70 * | container | 7.5 | 0..n | -child nodes |
71 * | description | 7.19.3 | 0..1 | -string |
72 * | grouping | 7.11 | 0..n | -child nodes |
73 * | if-feature | 7.18.2 | 0..n | -TODO |
74 * | leaf | 7.6 | 0..n | -YangLeaf |
75 * | leaf-list | 7.7 | 0..n | -YangLeafList |
76 * | list | 7.8 | 0..n | -child nodes |
77 * | must | 7.5.3 | 0..n | -TODO |
78 * | presence | 7.5.5 | 0..1 | -boolean |
79 * | reference | 7.19.4 | 0..1 | -string |
80 * | status | 7.19.2 | 0..1 | -YangStatus |
81 * | typedef | 7.3 | 0..n | -child nodes |
82 * | uses | 7.12 | 0..n | -child nodes |
83 * | when | 7.19.5 | 0..1 | -TODO |
84 * +--------------+---------+-------------+------------------+
85 */
86
87/**
88 * Data model node to maintain information defined in YANG container.
89 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053090public class YangContainer extends YangNode implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector {
Vinod Kumar S67e7be62016-02-11 20:13:28 +053091
92 /**
93 * Name of the container.
94 */
95 private String name;
96
97 /**
98 * If container maintains config data.
99 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530100 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530101
102 /**
103 * Description of container.
104 */
105 private String description;
106
107 /**
108 * List of leaves contained.
109 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530110 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530111
112 /**
113 * List of leaf-lists contained.
114 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530115 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530116
117 /**
118 * If it is a presence container, then the textual documentation of presence
119 * usage.
120 */
121 private String presence;
122
123 /**
124 * Reference of the module.
125 */
126 private String reference;
127
128 /**
129 * Status of the node.
130 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530131 private YangStatusType status = YangStatusType.CURRENT;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530132
133 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530134 * Create a container node.
135 */
136 public YangContainer() {
137 super(YangNodeType.CONTAINER_NODE);
138 }
139
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530140 /**
141 * Get the YANG name of container.
142 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530143 * @return the name of container as defined in YANG file
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530144 */
145 @Override
146 public String getName() {
147 return name;
148 }
149
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530150 /**
151 * Set the YANG name of container.
152 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530153 * @param name the name of container as defined in YANG file
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530154 */
155 @Override
156 public void setName(String name) {
157 this.name = name;
158 }
159
160 /**
161 * Get the config flag.
162 *
163 * @return the isConfig
164 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530165 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530166 return isConfig;
167 }
168
169 /**
170 * Set the config flag.
171 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530172 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530173 */
174 public void setConfig(boolean isCfg) {
175 isConfig = isCfg;
176 }
177
178 /**
179 * Get the description.
180 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530181 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530182 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530183 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530184 public String getDescription() {
185 return description;
186 }
187
188 /**
189 * Set the description.
190 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530191 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530192 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530193 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530194 public void setDescription(String description) {
195 this.description = description;
196 }
197
198 /**
199 * Get the list of leaves.
200 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530201 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530202 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530203 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530204 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530205 return listOfLeaf;
206 }
207
208 /**
209 * Set the list of leaves.
210 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530211 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530212 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530213 private void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530214 listOfLeaf = leafsList;
215 }
216
217 /**
218 * Add a leaf.
219 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530220 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530221 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530222 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530223 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530224 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530225 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530226 }
227
228 getListOfLeaf().add(leaf);
229 }
230
231 /**
232 * Get the list of leaf-list.
233 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530234 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530235 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530236 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530237 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530238 return listOfLeafList;
239 }
240
241 /**
242 * Set the list of leaf-list.
243 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530244 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530245 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530246 private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530247 this.listOfLeafList = listOfLeafList;
248 }
249
250 /**
251 * Add a leaf-list.
252 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530253 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530254 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530255 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530256 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530257 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530258 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530259 }
260
261 getListOfLeafList().add(leafList);
262 }
263
264 /**
265 * Get the presence string if present.
266 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530267 * @return the presence
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530268 */
269 public String getPresence() {
270 return presence;
271 }
272
273 /**
274 * Set the presence string.
275 *
276 * @param presence the presence flag
277 */
278 public void setPresence(String presence) {
279 this.presence = presence;
280 }
281
282 /**
283 * Get the textual reference.
284 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530285 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530286 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530287 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530288 public String getReference() {
289 return reference;
290 }
291
292 /**
293 * Set the textual reference.
294 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530295 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530296 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530297 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530298 public void setReference(String reference) {
299 this.reference = reference;
300 }
301
302 /**
303 * Get the status.
304 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530305 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530306 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530307 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530308 public YangStatusType getStatus() {
309 return status;
310 }
311
312 /**
313 * Set the status.
314 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530315 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530316 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530317 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530318 public void setStatus(YangStatusType status) {
319 this.status = status;
320 }
321
322 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530323 * Returns the type of the data.
324 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530325 * @return returns CONTAINER_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530326 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530327 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530328 public YangConstructType getYangConstructType() {
329 return YangConstructType.CONTAINER_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530330 }
331
332 /**
333 * Validate the data on entering the corresponding parse tree node.
334 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530335 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530336 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530337 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530338 public void validateDataOnEntry() throws DataModelException {
339 // TODO auto-generated method stub, to be implemented by parser
340 }
341
342 /**
343 * Validate the data on exiting the corresponding parse tree node.
344 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530345 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530346 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530347 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530348 public void validateDataOnExit() throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530349 List<YangLeaf> leaves = getListOfLeaf();
350 List<YangLeafList> leafLists = getListOfLeafList();
351
352 setDefaultConfigValueToChild(leaves, leafLists);
353 validateConfig(leaves, leafLists);
354 }
355
356 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530357 * Sets the config's value to all leaf if leaf's config statement is not
358 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530359 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530360 * @param leaves list of leaf attributes of container
361 * @param leafLists list of leaf-list attributes of container
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530362 */
363 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
364
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530365 /*
366 * If "config" is not specified, the default is the same as the parent
367 * schema node's "config" value.
368 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530369 if (leaves != null) {
370 for (YangLeaf leaf : leaves) {
371 if (leaf.isConfig() == null) {
372 leaf.setConfig(isConfig);
373 }
374 }
375 }
376
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530377 /*
378 * If "config" is not specified, the default is the same as the parent
379 * schema node's "config" value.
380 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530381 if (leafLists != null) {
382 for (YangLeafList leafList : leafLists) {
383 if (leafList.isConfig() == null) {
384 leafList.setConfig(isConfig);
385 }
386 }
387 }
388 }
389
390 /**
391 * Validates config statement of container.
392 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530393 * @param leaves list of leaf attributes of container
394 * @param leafLists list of leaf-list attributes of container
395 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530396 */
397 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists) throws DataModelException {
398
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530399 /*
400 * If a node has "config" set to "false", no node underneath it can have
401 * "config" set to "true".
402 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530403 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530404 for (YangLeaf leaf : leaves) {
405 if (leaf.isConfig()) {
406 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
407 "it can have \"config\" set to \"true\".");
408 }
409 }
410 }
411
Vinod Kumar S38046502016-03-23 15:30:27 +0530412 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530413 for (YangLeafList leafList : leafLists) {
414 if (leafList.isConfig()) {
415 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
416 "it can have \"config\" set to \"true\".");
417 }
418 }
419 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530420 }
421
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530422 @Override
423 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
424 // Asks helper to detect colliding child.
425 detectCollidingChildUtil(identifierName, dataType, this);
426 }
427
428 @Override
429 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530430 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530431 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as container \""
Vinod Kumar S38046502016-03-23 15:30:27 +0530432 + getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530433 }
434 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530435}