blob: e23bf02a258a44591ef1e269e70e3949f7ae80ae [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;
Bharat saraswal96dfef02016-06-16 00:29:12 +053023import org.onosproject.yangutils.datamodel.utils.Parsable;
24import org.onosproject.yangutils.datamodel.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 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053090public class YangContainer
91 extends YangNode
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053092 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector, YangAugmentationHolder {
Vinod Kumar S67e7be62016-02-11 20:13:28 +053093
Bharat saraswal96dfef02016-06-16 00:29:12 +053094 private static final long serialVersionUID = 806201605L;
95
Vinod Kumar S67e7be62016-02-11 20:13:28 +053096 /**
97 * Name of the container.
98 */
99 private String name;
100
101 /**
102 * If container maintains config data.
103 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530104 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530105
106 /**
107 * Description of container.
108 */
109 private String description;
110
111 /**
112 * List of leaves contained.
113 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530114 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530115
116 /**
117 * List of leaf-lists contained.
118 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530119 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530120
121 /**
122 * If it is a presence container, then the textual documentation of presence
123 * usage.
124 */
125 private String presence;
126
127 /**
128 * Reference of the module.
129 */
130 private String reference;
131
132 /**
133 * Status of the node.
134 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530135 private YangStatusType status = YangStatusType.CURRENT;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530136
137 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530138 * Create a container node.
139 */
140 public YangContainer() {
141 super(YangNodeType.CONTAINER_NODE);
142 }
143
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530144 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530145 * Returns the YANG name of container.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530146 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530147 * @return the name of container as defined in YANG file
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530148 */
149 @Override
150 public String getName() {
151 return name;
152 }
153
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530154 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530155 * Sets the YANG name of container.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530156 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530157 * @param name the name of container as defined in YANG file
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530158 */
159 @Override
160 public void setName(String name) {
161 this.name = name;
162 }
163
164 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530165 * Returns the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530166 *
167 * @return the isConfig
168 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530169 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530170 return isConfig;
171 }
172
173 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530174 * Sets the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530175 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530176 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530177 */
178 public void setConfig(boolean isCfg) {
179 isConfig = isCfg;
180 }
181
182 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530183 * Returns the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530184 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530185 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530186 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530187 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530188 public String getDescription() {
189 return description;
190 }
191
192 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530193 * Sets the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530194 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530195 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530196 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530197 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530198 public void setDescription(String description) {
199 this.description = description;
200 }
201
202 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530203 * Returns the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530204 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530205 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530206 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530207 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530208 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530209 return listOfLeaf;
210 }
211
212 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530213 * Sets the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530214 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530215 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530216 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530217 @Override
218 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530219 listOfLeaf = leafsList;
220 }
221
222 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530223 * Adds a leaf.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530224 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530225 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530226 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530227 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530228 public void addLeaf(YangLeaf leaf) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530229
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530230 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530231 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530232 }
233
234 getListOfLeaf().add(leaf);
235 }
236
237 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530238 * Returns the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530239 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530240 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530241 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530242 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530243 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530244 return listOfLeafList;
245 }
246
247 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530248 * Sets the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530249 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530250 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530251 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530252 @Override
253 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530254 this.listOfLeafList = listOfLeafList;
255 }
256
257 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530258 * Adds a leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530259 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530260 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530261 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530262 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530263 public void addLeafList(YangLeafList leafList) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530264
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530265 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530266 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530267 }
268
269 getListOfLeafList().add(leafList);
270 }
271
272 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530273 * Returns the presence string if present.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530274 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530275 * @return the presence
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530276 */
277 public String getPresence() {
278 return presence;
279 }
280
281 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530282 * Sets the presence string.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530283 *
284 * @param presence the presence flag
285 */
286 public void setPresence(String presence) {
287 this.presence = presence;
288 }
289
290 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530291 * Returns the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530292 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530293 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530294 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530295 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530296 public String getReference() {
297 return reference;
298 }
299
300 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530301 * Sets the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530302 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530303 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530304 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530305 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530306 public void setReference(String reference) {
307 this.reference = reference;
308 }
309
310 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530311 * Returns the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530312 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530313 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530314 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530315 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530316 public YangStatusType getStatus() {
317 return status;
318 }
319
320 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530321 * Sets the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530322 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530323 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530324 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530325 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530326 public void setStatus(YangStatusType status) {
327 this.status = status;
328 }
329
330 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530331 * Returns the type of the data.
332 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530333 * @return returns CONTAINER_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530334 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530335 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530336 public YangConstructType getYangConstructType() {
337 return YangConstructType.CONTAINER_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530338 }
339
340 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530341 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530342 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530343 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530344 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530345 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530346 public void validateDataOnEntry()
347 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530348 // TODO auto-generated method stub, to be implemented by parser
349 }
350
351 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530352 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530353 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530354 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530355 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530356 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530357 public void validateDataOnExit()
358 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530359 List<YangLeaf> leaves = getListOfLeaf();
360 List<YangLeafList> leafLists = getListOfLeafList();
361
362 setDefaultConfigValueToChild(leaves, leafLists);
363 validateConfig(leaves, leafLists);
364 }
365
366 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530367 * Sets the config's value to all leaf if leaf's config statement is not
368 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530369 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530370 * @param leaves list of leaf attributes of container
371 * @param leafLists list of leaf-list attributes of container
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530372 */
373 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
374
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530375 /*
376 * If "config" is not specified, the default is the same as the parent
377 * schema node's "config" value.
378 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530379 if (leaves != null) {
380 for (YangLeaf leaf : leaves) {
381 if (leaf.isConfig() == null) {
382 leaf.setConfig(isConfig);
383 }
384 }
385 }
386
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530387 /*
388 * If "config" is not specified, the default is the same as the parent
389 * schema node's "config" value.
390 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530391 if (leafLists != null) {
392 for (YangLeafList leafList : leafLists) {
393 if (leafList.isConfig() == null) {
394 leafList.setConfig(isConfig);
395 }
396 }
397 }
398 }
399
400 /**
401 * Validates config statement of container.
402 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530403 * @param leaves list of leaf attributes of container
404 * @param leafLists list of leaf-list attributes of container
405 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530406 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530407 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists)
408 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530409
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530410 /*
411 * If a node has "config" set to "false", no node underneath it can have
412 * "config" set to "true".
413 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530414 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530415 for (YangLeaf leaf : leaves) {
416 if (leaf.isConfig()) {
417 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
418 "it can have \"config\" set to \"true\".");
419 }
420 }
421 }
422
Vinod Kumar S38046502016-03-23 15:30:27 +0530423 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530424 for (YangLeafList leafList : leafLists) {
425 if (leafList.isConfig()) {
426 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
427 "it can have \"config\" set to \"true\".");
428 }
429 }
430 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530431 }
432
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530433 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530434 public void detectCollidingChild(String identifierName, YangConstructType dataType)
435 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530436 // Asks helper to detect colliding child.
437 detectCollidingChildUtil(identifierName, dataType, this);
438 }
439
440 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530441 public void detectSelfCollision(String identifierName, YangConstructType dataType)
442 throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530443 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530444 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as container \""
Vinod Kumar S38046502016-03-23 15:30:27 +0530445 + getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530446 }
447 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530448}