blob: 59418372d469636b037561fab1464f9b080be147 [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 */
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530141 * Returns the YANG name of container.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530142 *
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530151 * Sets the YANG name of container.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530152 *
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530161 * Returns the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530162 *
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530170 * Sets the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530171 *
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530179 * Returns the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530180 *
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530189 * Sets the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530190 *
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530199 * Returns the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530200 *
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530209 * Sets the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530210 *
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530218 * Adds a leaf.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530219 *
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) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530224
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530225 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530226 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530227 }
228
229 getListOfLeaf().add(leaf);
230 }
231
232 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530233 * Returns the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530234 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530235 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530236 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530237 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530238 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530239 return listOfLeafList;
240 }
241
242 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530243 * Sets the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530244 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530245 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530246 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530247 private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530248 this.listOfLeafList = listOfLeafList;
249 }
250
251 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530252 * Adds a leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530253 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530254 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530255 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530256 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530257 public void addLeafList(YangLeafList leafList) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530258
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530259 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530260 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530261 }
262
263 getListOfLeafList().add(leafList);
264 }
265
266 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530267 * Returns the presence string if present.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530268 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530269 * @return the presence
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530270 */
271 public String getPresence() {
272 return presence;
273 }
274
275 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530276 * Sets the presence string.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530277 *
278 * @param presence the presence flag
279 */
280 public void setPresence(String presence) {
281 this.presence = presence;
282 }
283
284 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530285 * Returns the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530286 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530287 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530288 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530289 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530290 public String getReference() {
291 return reference;
292 }
293
294 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530295 * Sets the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530296 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530297 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530298 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530299 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530300 public void setReference(String reference) {
301 this.reference = reference;
302 }
303
304 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530305 * Returns the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530306 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530307 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530308 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530309 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530310 public YangStatusType getStatus() {
311 return status;
312 }
313
314 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530315 * Sets the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530316 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530317 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530318 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530319 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530320 public void setStatus(YangStatusType status) {
321 this.status = status;
322 }
323
324 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530325 * Returns the type of the data.
326 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530327 * @return returns CONTAINER_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530328 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530329 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530330 public YangConstructType getYangConstructType() {
331 return YangConstructType.CONTAINER_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530332 }
333
334 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530335 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530336 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530337 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530338 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530339 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530340 public void validateDataOnEntry() throws DataModelException {
341 // TODO auto-generated method stub, to be implemented by parser
342 }
343
344 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530345 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530346 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530347 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530348 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530349 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530350 public void validateDataOnExit() throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530351 List<YangLeaf> leaves = getListOfLeaf();
352 List<YangLeafList> leafLists = getListOfLeafList();
353
354 setDefaultConfigValueToChild(leaves, leafLists);
355 validateConfig(leaves, leafLists);
356 }
357
358 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530359 * Sets the config's value to all leaf if leaf's config statement is not
360 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530361 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530362 * @param leaves list of leaf attributes of container
363 * @param leafLists list of leaf-list attributes of container
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530364 */
365 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
366
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530367 /*
368 * If "config" is not specified, the default is the same as the parent
369 * schema node's "config" value.
370 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530371 if (leaves != null) {
372 for (YangLeaf leaf : leaves) {
373 if (leaf.isConfig() == null) {
374 leaf.setConfig(isConfig);
375 }
376 }
377 }
378
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530379 /*
380 * If "config" is not specified, the default is the same as the parent
381 * schema node's "config" value.
382 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530383 if (leafLists != null) {
384 for (YangLeafList leafList : leafLists) {
385 if (leafList.isConfig() == null) {
386 leafList.setConfig(isConfig);
387 }
388 }
389 }
390 }
391
392 /**
393 * Validates config statement of container.
394 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530395 * @param leaves list of leaf attributes of container
396 * @param leafLists list of leaf-list attributes of container
397 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530398 */
399 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists) throws DataModelException {
400
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530401 /*
402 * If a node has "config" set to "false", no node underneath it can have
403 * "config" set to "true".
404 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530405 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530406 for (YangLeaf leaf : leaves) {
407 if (leaf.isConfig()) {
408 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
409 "it can have \"config\" set to \"true\".");
410 }
411 }
412 }
413
Vinod Kumar S38046502016-03-23 15:30:27 +0530414 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530415 for (YangLeafList leafList : leafLists) {
416 if (leafList.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 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530422 }
423
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530424 @Override
425 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
426 // Asks helper to detect colliding child.
427 detectCollidingChildUtil(identifierName, dataType, this);
428 }
429
430 @Override
431 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530432 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530433 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as container \""
Vinod Kumar S38046502016-03-23 15:30:27 +0530434 + getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530435 }
436 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530437}