blob: 39304338d08718704b5abde51fe9492b98e29b74 [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/**
Bharat saraswald9822e92016-04-05 15:13:44 +053088 * Represents data model node to maintain information defined in YANG container.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053089 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053090public class YangContainer extends YangNode
91 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector, YangAugmentationHolder {
Vinod Kumar S67e7be62016-02-11 20:13:28 +053092
93 /**
94 * Name of the container.
95 */
96 private String name;
97
98 /**
99 * If container maintains config data.
100 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530101 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530102
103 /**
104 * Description of container.
105 */
106 private String description;
107
108 /**
109 * List of leaves contained.
110 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530111 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530112
113 /**
114 * List of leaf-lists contained.
115 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530116 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530117
118 /**
119 * If it is a presence container, then the textual documentation of presence
120 * usage.
121 */
122 private String presence;
123
124 /**
125 * Reference of the module.
126 */
127 private String reference;
128
129 /**
130 * Status of the node.
131 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530132 private YangStatusType status = YangStatusType.CURRENT;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530133
134 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530135 * Create a container node.
136 */
137 public YangContainer() {
138 super(YangNodeType.CONTAINER_NODE);
139 }
140
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530141 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530142 * Returns the YANG name of container.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530143 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530144 * @return the name of container as defined in YANG file
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530145 */
146 @Override
147 public String getName() {
148 return name;
149 }
150
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530151 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530152 * Sets the YANG name of container.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530153 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530154 * @param name the name of container as defined in YANG file
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530155 */
156 @Override
157 public void setName(String name) {
158 this.name = name;
159 }
160
161 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530162 * Returns the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530163 *
164 * @return the isConfig
165 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530166 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530167 return isConfig;
168 }
169
170 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530171 * Sets the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530172 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530173 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530174 */
175 public void setConfig(boolean isCfg) {
176 isConfig = isCfg;
177 }
178
179 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530180 * Returns the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530181 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530182 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530183 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530184 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530185 public String getDescription() {
186 return description;
187 }
188
189 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530190 * Sets the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530191 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530192 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530193 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530194 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530195 public void setDescription(String description) {
196 this.description = description;
197 }
198
199 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530200 * Returns the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530201 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530202 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530203 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530204 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530205 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530206 return listOfLeaf;
207 }
208
209 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530210 * Sets the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530211 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530212 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530213 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530214 private void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530215 listOfLeaf = leafsList;
216 }
217
218 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530219 * Adds a leaf.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530220 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530221 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530222 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530223 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530224 public void addLeaf(YangLeaf leaf) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530225
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530226 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530227 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530228 }
229
230 getListOfLeaf().add(leaf);
231 }
232
233 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530234 * Returns the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530235 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530236 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530237 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530238 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530239 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530240 return listOfLeafList;
241 }
242
243 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530244 * Sets the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530245 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530246 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530247 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530248 private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530249 this.listOfLeafList = listOfLeafList;
250 }
251
252 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530253 * Adds a leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530254 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530255 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530256 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530257 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530258 public void addLeafList(YangLeafList leafList) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530259
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530260 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530261 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530262 }
263
264 getListOfLeafList().add(leafList);
265 }
266
267 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530268 * Returns the presence string if present.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530269 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530270 * @return the presence
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530271 */
272 public String getPresence() {
273 return presence;
274 }
275
276 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530277 * Sets the presence string.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530278 *
279 * @param presence the presence flag
280 */
281 public void setPresence(String presence) {
282 this.presence = presence;
283 }
284
285 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530286 * Returns the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530287 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530288 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530289 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530290 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530291 public String getReference() {
292 return reference;
293 }
294
295 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530296 * Sets the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530297 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530298 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530299 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530300 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530301 public void setReference(String reference) {
302 this.reference = reference;
303 }
304
305 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530306 * Returns the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530307 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530308 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530309 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530310 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530311 public YangStatusType getStatus() {
312 return status;
313 }
314
315 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530316 * Sets the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530317 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530318 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530319 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530320 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530321 public void setStatus(YangStatusType status) {
322 this.status = status;
323 }
324
325 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530326 * Returns the type of the data.
327 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530328 * @return returns CONTAINER_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530329 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530330 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530331 public YangConstructType getYangConstructType() {
332 return YangConstructType.CONTAINER_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530333 }
334
335 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530336 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530337 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530338 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530339 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530340 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530341 public void validateDataOnEntry() throws DataModelException {
342 // TODO auto-generated method stub, to be implemented by parser
343 }
344
345 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530346 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530347 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530348 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530349 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530350 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530351 public void validateDataOnExit() throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530352 List<YangLeaf> leaves = getListOfLeaf();
353 List<YangLeafList> leafLists = getListOfLeafList();
354
355 setDefaultConfigValueToChild(leaves, leafLists);
356 validateConfig(leaves, leafLists);
357 }
358
359 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530360 * Sets the config's value to all leaf if leaf's config statement is not
361 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530362 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530363 * @param leaves list of leaf attributes of container
364 * @param leafLists list of leaf-list attributes of container
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530365 */
366 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
367
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530368 /*
369 * If "config" is not specified, the default is the same as the parent
370 * schema node's "config" value.
371 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530372 if (leaves != null) {
373 for (YangLeaf leaf : leaves) {
374 if (leaf.isConfig() == null) {
375 leaf.setConfig(isConfig);
376 }
377 }
378 }
379
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530380 /*
381 * If "config" is not specified, the default is the same as the parent
382 * schema node's "config" value.
383 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530384 if (leafLists != null) {
385 for (YangLeafList leafList : leafLists) {
386 if (leafList.isConfig() == null) {
387 leafList.setConfig(isConfig);
388 }
389 }
390 }
391 }
392
393 /**
394 * Validates config statement of container.
395 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530396 * @param leaves list of leaf attributes of container
397 * @param leafLists list of leaf-list attributes of container
398 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530399 */
400 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists) throws DataModelException {
401
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530402 /*
403 * If a node has "config" set to "false", no node underneath it can have
404 * "config" set to "true".
405 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530406 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530407 for (YangLeaf leaf : leaves) {
408 if (leaf.isConfig()) {
409 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
410 "it can have \"config\" set to \"true\".");
411 }
412 }
413 }
414
Vinod Kumar S38046502016-03-23 15:30:27 +0530415 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530416 for (YangLeafList leafList : leafLists) {
417 if (leafList.isConfig()) {
418 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
419 "it can have \"config\" set to \"true\".");
420 }
421 }
422 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530423 }
424
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530425 @Override
426 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
427 // Asks helper to detect colliding child.
428 detectCollidingChildUtil(identifierName, dataType, this);
429 }
430
431 @Override
432 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530433 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530434 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as container \""
Vinod Kumar S38046502016-03-23 15:30:27 +0530435 + getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530436 }
437 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530438}