blob: ccb824be9105ae6fb37ad166883b964c45032683 [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 */
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
94 /**
95 * Name of the container.
96 */
97 private String name;
98
99 /**
100 * If container maintains config data.
101 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530102 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530103
104 /**
105 * Description of container.
106 */
107 private String description;
108
109 /**
110 * List of leaves contained.
111 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530112 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530113
114 /**
115 * List of leaf-lists contained.
116 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530117 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530118
119 /**
120 * If it is a presence container, then the textual documentation of presence
121 * usage.
122 */
123 private String presence;
124
125 /**
126 * Reference of the module.
127 */
128 private String reference;
129
130 /**
131 * Status of the node.
132 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530133 private YangStatusType status = YangStatusType.CURRENT;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530134
135 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530136 * Create a container node.
137 */
138 public YangContainer() {
139 super(YangNodeType.CONTAINER_NODE);
140 }
141
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530142 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530143 * Returns the YANG name of container.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530144 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530145 * @return the name of container as defined in YANG file
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530146 */
147 @Override
148 public String getName() {
149 return name;
150 }
151
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530152 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530153 * Sets the YANG name of container.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530154 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530155 * @param name the name of container as defined in YANG file
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530156 */
157 @Override
158 public void setName(String name) {
159 this.name = name;
160 }
161
162 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530163 * Returns the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530164 *
165 * @return the isConfig
166 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530167 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530168 return isConfig;
169 }
170
171 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530172 * Sets the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530173 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530174 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530175 */
176 public void setConfig(boolean isCfg) {
177 isConfig = isCfg;
178 }
179
180 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530181 * Returns the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530182 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530183 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530184 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530185 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530186 public String getDescription() {
187 return description;
188 }
189
190 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530191 * Sets the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530192 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530193 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530194 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530195 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530196 public void setDescription(String description) {
197 this.description = description;
198 }
199
200 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530201 * Returns the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530202 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530203 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530204 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530205 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530206 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530207 return listOfLeaf;
208 }
209
210 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530211 * Sets the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530212 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530213 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530214 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530215 @Override
216 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530217 listOfLeaf = leafsList;
218 }
219
220 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530221 * Adds a leaf.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530222 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530223 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530224 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530225 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530226 public void addLeaf(YangLeaf leaf) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530227
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530228 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530229 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530230 }
231
232 getListOfLeaf().add(leaf);
233 }
234
235 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530236 * Returns the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530237 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530238 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530239 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530240 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530241 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530242 return listOfLeafList;
243 }
244
245 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530246 * Sets the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530247 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530248 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530249 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530250 @Override
251 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530252 this.listOfLeafList = listOfLeafList;
253 }
254
255 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530256 * Adds a leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530257 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530258 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530259 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530260 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530261 public void addLeafList(YangLeafList leafList) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530262
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530263 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530264 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530265 }
266
267 getListOfLeafList().add(leafList);
268 }
269
270 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530271 * Returns the presence string if present.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530272 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530273 * @return the presence
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530274 */
275 public String getPresence() {
276 return presence;
277 }
278
279 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530280 * Sets the presence string.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530281 *
282 * @param presence the presence flag
283 */
284 public void setPresence(String presence) {
285 this.presence = presence;
286 }
287
288 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530289 * Returns the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530290 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530291 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530292 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530293 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530294 public String getReference() {
295 return reference;
296 }
297
298 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530299 * Sets the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530300 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530301 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530302 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530303 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530304 public void setReference(String reference) {
305 this.reference = reference;
306 }
307
308 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530309 * Returns the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530310 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530311 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530312 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530313 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530314 public YangStatusType getStatus() {
315 return status;
316 }
317
318 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530319 * Sets the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530320 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530321 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530322 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530323 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530324 public void setStatus(YangStatusType status) {
325 this.status = status;
326 }
327
328 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530329 * Returns the type of the data.
330 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530331 * @return returns CONTAINER_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530332 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530333 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530334 public YangConstructType getYangConstructType() {
335 return YangConstructType.CONTAINER_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530336 }
337
338 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530339 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530340 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530341 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530342 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530343 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530344 public void validateDataOnEntry()
345 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530346 // TODO auto-generated method stub, to be implemented by parser
347 }
348
349 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530350 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530351 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530352 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530353 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530354 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530355 public void validateDataOnExit()
356 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530357 List<YangLeaf> leaves = getListOfLeaf();
358 List<YangLeafList> leafLists = getListOfLeafList();
359
360 setDefaultConfigValueToChild(leaves, leafLists);
361 validateConfig(leaves, leafLists);
362 }
363
364 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530365 * Sets the config's value to all leaf if leaf's config statement is not
366 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530367 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530368 * @param leaves list of leaf attributes of container
369 * @param leafLists list of leaf-list attributes of container
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530370 */
371 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
372
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530373 /*
374 * If "config" is not specified, the default is the same as the parent
375 * schema node's "config" value.
376 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530377 if (leaves != null) {
378 for (YangLeaf leaf : leaves) {
379 if (leaf.isConfig() == null) {
380 leaf.setConfig(isConfig);
381 }
382 }
383 }
384
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530385 /*
386 * If "config" is not specified, the default is the same as the parent
387 * schema node's "config" value.
388 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530389 if (leafLists != null) {
390 for (YangLeafList leafList : leafLists) {
391 if (leafList.isConfig() == null) {
392 leafList.setConfig(isConfig);
393 }
394 }
395 }
396 }
397
398 /**
399 * Validates config statement of container.
400 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530401 * @param leaves list of leaf attributes of container
402 * @param leafLists list of leaf-list attributes of container
403 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530404 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530405 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists)
406 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530407
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530408 /*
409 * If a node has "config" set to "false", no node underneath it can have
410 * "config" set to "true".
411 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530412 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530413 for (YangLeaf leaf : leaves) {
414 if (leaf.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 }
420
Vinod Kumar S38046502016-03-23 15:30:27 +0530421 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530422 for (YangLeafList leafList : leafLists) {
423 if (leafList.isConfig()) {
424 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
425 "it can have \"config\" set to \"true\".");
426 }
427 }
428 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530429 }
430
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530431 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530432 public void detectCollidingChild(String identifierName, YangConstructType dataType)
433 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530434 // Asks helper to detect colliding child.
435 detectCollidingChildUtil(identifierName, dataType, this);
436 }
437
438 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530439 public void detectSelfCollision(String identifierName, YangConstructType dataType)
440 throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530441 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530442 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as container \""
Vinod Kumar S38046502016-03-23 15:30:27 +0530443 + getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530444 }
445 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530446}