blob: 09c12db8a2e9dd78fc3d7456959728023c564dd6 [file] [log] [blame]
Vinod Kumar S67e7be62016-02-11 20:13:28 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
23import org.onosproject.yangutils.parser.Parsable;
24import org.onosproject.yangutils.utils.YangConstructType;
25
26import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
27
28/*
Vinod Kumar S67e7be62016-02-11 20:13:28 +053029 * The "list" statement is used to define an interior data node in the
30 * schema tree. A list node may exist in multiple instances in the data
31 * tree. Each such instance is known as a list entry. The "list"
32 * statement takes one argument, which is an identifier, followed by a
33 * block of sub-statements that holds detailed list information.
34 *
35 * A list entry is uniquely identified by the values of the list's keys,
36 * if defined.
37 *
38 * The list's sub-statements
39 *
40 * +--------------+---------+-------------+------------------+
41 * | substatement | section | cardinality |data model mapping|
42 * +--------------+---------+-------------+------------------+
43 * | anyxml | 7.10 | 0..n |-not supported |
44 * | choice | 7.9 | 0..n |-child nodes |
45 * | config | 7.19.1 | 0..1 |-boolean |
46 * | container | 7.5 | 0..n |-child nodes |
47 * | description | 7.19.3 | 0..1 |-string |
48 * | grouping | 7.11 | 0..n |-child nodes |
49 * | if-feature | 7.18.2 | 0..n |-TODO |
50 * | key | 7.8.2 | 0..1 |-String list |
51 * | leaf | 7.6 | 0..n |-YangLeaf |
52 * | leaf-list | 7.7 | 0..n |-YangLeafList |
53 * | list | 7.8 | 0..n |-child nodes |
54 * | max-elements | 7.7.4 | 0..1 |-int |
55 * | min-elements | 7.7.3 | 0..1 |-int |
56 * | must | 7.5.3 | 0..n |-TODO |
57 * | ordered-by | 7.7.5 | 0..1 |-TODO |
58 * | reference | 7.19.4 | 0..1 |-string |
59 * | status | 7.19.2 | 0..1 |-YangStatus |
60 * | typedef | 7.3 | 0..n |-child nodes |
61 * | unique | 7.8.3 | 0..n |-TODO |
62 * | uses | 7.12 | 0..n |-child nodes(TODO)|
63 * | when | 7.19.5 | 0..1 |-TODO |
64 * +--------------+---------+-------------+------------------+
65 */
66
67/**
68 * List data represented in YANG.
69 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053070public class YangList extends YangNode
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053071 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector {
Vinod Kumar S67e7be62016-02-11 20:13:28 +053072
73 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053074 * Name of the YANG list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053075 */
76 private String name;
77
78 /**
79 * If list maintains config data.
80 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053081 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053082
83 /**
84 * Description of list.
85 */
86 private String description;
87
88 /**
89 * Reference RFC 6020.
90 *
91 * The "key" statement, which MUST be present if the list represents
92 * configuration, and MAY be present otherwise, takes as an argument a
93 * string that specifies a space-separated list of leaf identifiers of this
94 * list. A leaf identifier MUST NOT appear more than once in the key. Each
95 * such leaf identifier MUST refer to a child leaf of the list. The leafs
96 * can be defined directly in sub-statements to the list, or in groupings
97 * used in the list.
98 *
99 * The combined values of all the leafs specified in the key are used to
100 * uniquely identify a list entry. All key leafs MUST be given values when a
101 * list entry is created. Thus, any default values in the key leafs or their
102 * types are ignored. It also implies that any mandatory statement in the
103 * key leafs are ignored.
104 *
105 * A leaf that is part of the key can be of any built-in or derived type,
106 * except it MUST NOT be the built-in type "empty".
107 *
108 * All key leafs in a list MUST have the same value for their "config" as
109 * the list itself.
110 *
111 * List of key leaf names.
112 */
113 private List<String> keyList;
114
115 /**
116 * List of leaves.
117 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530118 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530119
120 /**
121 * List of leaf-lists.
122 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530123 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530124
125 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530126 * Reference RFC 6020.
127 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530128 * The "max-elements" statement, which is optional, takes as an argument a
129 * positive integer or the string "unbounded", which puts a constraint on
130 * valid list entries. A valid leaf-list or list always has at most
131 * max-elements entries.
132 *
133 * If no "max-elements" statement is present, it defaults to "unbounded".
134 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530135 private int maxElements = Integer.MAX_VALUE;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530136
137 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530138 * Reference RFC 6020.
139 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530140 * The "min-elements" statement, which is optional, takes as an argument a
141 * non-negative integer that puts a constraint on valid list entries. A
142 * valid leaf-list or list MUST have at least min-elements entries.
143 *
144 * If no "min-elements" statement is present, it defaults to zero.
145 *
146 * The behavior of the constraint depends on the type of the leaf-list's or
147 * list's closest ancestor node in the schema tree that is not a non-
148 * presence container:
149 *
150 * o If this ancestor is a case node, the constraint is enforced if any
151 * other node from the case exists.
152 *
153 * o Otherwise, it is enforced if the ancestor node exists.
154 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530155 private int minElements = 0;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530156
157 /**
158 * reference.
159 */
160 private String reference;
161
162 /**
163 * Status of the node.
164 */
165
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530166 private YangStatusType status = YangStatusType.CURRENT;
167
168 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530169 * Constructor.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530170 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530171 public YangList() {
172 super(YangNodeType.LIST_NODE);
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530173 }
174
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530175 /**
176 * Get the YANG list name.
177 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530178 * @return YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530179 */
180 @Override
181 public String getName() {
182 return name;
183 }
184
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530185 /**
186 * Set the YANG list name.
187 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530188 * @param name YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530189 */
190 @Override
191 public void setName(String name) {
192 this.name = name;
193 }
194
195 /**
196 * Get the config flag.
197 *
198 * @return the isConfig
199 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530200 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530201 return isConfig;
202 }
203
204 /**
205 * Set the config flag.
206 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530207 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530208 */
209 public void setConfig(boolean isCfg) {
210 isConfig = isCfg;
211 }
212
213 /**
214 * Get the description.
215 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530216 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530217 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530218 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530219 public String getDescription() {
220 return description;
221 }
222
223 /**
224 * Set the description.
225 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530226 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530227 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530228 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530229 public void setDescription(String description) {
230 this.description = description;
231 }
232
233 /**
234 * Get the list of key field names.
235 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530236 * @return the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530237 */
238 public List<String> getKeyList() {
239 return keyList;
240 }
241
242 /**
243 * Set the list of key field names.
244 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530245 * @param keyList the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530246 */
247 private void setKeyList(List<String> keyList) {
248 this.keyList = keyList;
249 }
250
251 /**
252 * Add a key field name.
253 *
254 * @param key key field name.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530255 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530256 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530257 public void addKey(String key) throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530258 if (getKeyList() == null) {
259 setKeyList(new LinkedList<String>());
260 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530261
262 if (getKeyList().contains(key)) {
263 throw new DataModelException("A leaf identifier must not appear more than once in the\n" +
264 " key");
265 }
266
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530267 getKeyList().add(key);
268 }
269
270 /**
271 * Get the list of leaves.
272 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530273 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530274 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530275 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530276 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530277 return listOfLeaf;
278 }
279
280 /**
281 * Set the list of leaves.
282 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530283 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530284 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530285 private void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530286 listOfLeaf = leafsList;
287 }
288
289 /**
290 * Add a leaf.
291 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530292 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530293 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530294 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530295 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530296 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530297 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530298 }
299
300 getListOfLeaf().add(leaf);
301 }
302
303 /**
304 * Get the list of leaf-list.
305 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530306 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530307 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530308 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530309 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530310 return listOfLeafList;
311 }
312
313 /**
314 * Set the list of leaf-list.
315 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530316 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530317 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530318 private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530319 this.listOfLeafList = listOfLeafList;
320 }
321
322 /**
323 * Add a leaf-list.
324 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530325 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530326 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530327 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530328 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530329 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530330 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530331 }
332
333 getListOfLeafList().add(leafList);
334 }
335
336 /**
337 * Get the max elements.
338 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530339 * @return the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530340 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530341 public int getMaxElements() {
342 return maxElements;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530343 }
344
345 /**
346 * Set the max elements.
347 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530348 * @param max the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530349 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530350 public void setMaxElements(int max) {
351 maxElements = max;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530352 }
353
354 /**
355 * Get the minimum elements.
356 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530357 * @return the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530358 */
359 public int getMinElements() {
360 return minElements;
361 }
362
363 /**
364 * Set the minimum elements.
365 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530366 * @param minElements the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530367 */
368 public void setMinElements(int minElements) {
369 this.minElements = minElements;
370 }
371
372 /**
373 * Get the textual reference.
374 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530375 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530376 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530377 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530378 public String getReference() {
379 return reference;
380 }
381
382 /**
383 * Set the textual reference.
384 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530385 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530386 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530387 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530388 public void setReference(String reference) {
389 this.reference = reference;
390 }
391
392 /**
393 * Get the status.
394 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530395 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530396 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530397 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530398 public YangStatusType getStatus() {
399 return status;
400 }
401
402 /**
403 * Set the status.
404 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530405 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530406 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530407 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530408 public void setStatus(YangStatusType status) {
409 this.status = status;
410 }
411
412 /**
413 * Returns the type of the parsed data.
414 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530415 * @return returns LIST_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530416 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530417 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530418 public YangConstructType getYangConstructType() {
419 return YangConstructType.LIST_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530420 }
421
422 /**
423 * Validate the data on entering the corresponding parse tree node.
424 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530425 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530426 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530427 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530428 public void validateDataOnEntry() throws DataModelException {
429 // TODO auto-generated method stub, to be implemented by parser
430 }
431
432 /**
433 * Validate the data on exiting the corresponding parse tree node.
434 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530435 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530436 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530437 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530438 public void validateDataOnExit() throws DataModelException {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530439 List<String> keys = getKeyList();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530440 List<YangLeaf> leaves = getListOfLeaf();
441 List<YangLeafList> leafLists = getListOfLeafList();
442
443 setDefaultConfigValueToChild(leaves, leafLists);
444 validateConfig(leaves, leafLists);
445
446 /* A list must have atleast one key leaf if config is true */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530447 if (isConfig
448 && (keys == null || leaves == null && leafLists == null)) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530449 throw new DataModelException("A list must have atleast one key leaf if config is true;");
450 } else if (keys != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530451 if (leaves != null) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530452 validateLeafKey(leaves, keys);
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530453 }
454
455 if (leafLists != null) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530456 validateLeafListKey(leafLists, keys);
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530457 }
458 }
459 }
460
461 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530462 * Sets the config's value to all leaf if leaf's config statement is not
463 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530464 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530465 * @param leaves list of leaf attributes of YANG list
466 * @param leafLists list of leaf-list attributes of YANG list
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530467 */
468 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
469
Vinod Kumar S71cba682016-02-25 15:52:16 +0530470 /*
471 * If "config" is not specified, the default is the same as the parent
472 * schema node's "config" value.
473 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530474 if (leaves != null) {
475 for (YangLeaf leaf : leaves) {
476 if (leaf.isConfig() == null) {
477 leaf.setConfig(isConfig);
478 }
479 }
480 }
481
Vinod Kumar S71cba682016-02-25 15:52:16 +0530482 /*
483 * If "config" is not specified, the default is the same as the parent
484 * schema node's "config" value.
485 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530486 if (leafLists != null) {
487 for (YangLeafList leafList : leafLists) {
488 if (leafList.isConfig() == null) {
489 leafList.setConfig(isConfig);
490 }
491 }
492 }
493 }
494
495 /**
496 * Validates config statement of YANG list.
497 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530498 * @param leaves list of leaf attributes of YANG list
499 * @param leafLists list of leaf-list attributes of YANG list
500 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530501 */
502 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists) throws DataModelException {
503
Vinod Kumar S71cba682016-02-25 15:52:16 +0530504 /*
505 * If a node has "config" set to "false", no node underneath it can have
506 * "config" set to "true".
507 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530508 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530509 for (YangLeaf leaf : leaves) {
510 if (leaf.isConfig()) {
511 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
512 "it can have \"config\" set to \"true\".");
513 }
514 }
515 }
516
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530517 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530518 for (YangLeafList leafList : leafLists) {
519 if (leafList.isConfig()) {
520 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
521 "it can have \"config\" set to \"true\".");
522 }
523 }
524 }
525 }
526
527 /**
528 * Validates key statement of list.
529 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530530 * @param leaves list of leaf attributes of list
531 * @param keys list of key attributes of list
532 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530533 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530534 private void validateLeafKey(List<YangLeaf> leaves, List<String> keys) throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530535 boolean leafFound = false;
536 List<YangLeaf> keyLeaves = new LinkedList<>();
537
Vinod Kumar S71cba682016-02-25 15:52:16 +0530538 /*
539 * 1. Leaf identifier must refer to a child leaf of the list 2. A leaf
540 * that is part of the key must not be the built-in type "empty".
541 */
542 for (String key : keys) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530543 for (YangLeaf leaf : leaves) {
544 if (key.equals(leaf.getLeafName())) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530545 if (leaf.getDataType().getDataType() == YangDataTypes.EMPTY) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530546 throw new DataModelException(" A leaf that is part of the key must not be the built-in " +
547 "type \"empty\".");
548 }
549 leafFound = true;
550 keyLeaves.add(leaf);
551 break;
552 }
553 }
554 if (!leafFound) {
555 throw new DataModelException("Leaf identifier must refer to a child leaf of the list");
556 }
557 leafFound = false;
558 }
559
Vinod Kumar S71cba682016-02-25 15:52:16 +0530560 /*
561 * All key leafs in a list MUST have the same value for their "config"
562 * as the list itself.
563 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530564 for (YangLeaf keyLeaf : keyLeaves) {
565 if (isConfig != keyLeaf.isConfig()) {
566 throw new DataModelException("All key leafs in a list must have the same value for their" +
567 " \"config\" as the list itself.");
568 }
569 }
570 }
571
572 /**
573 * Validates key statement of list.
574 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530575 * @param leafLists list of leaf-list attributes of list
576 * @param keys list of key attributes of list
577 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530578 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530579 private void validateLeafListKey(List<YangLeafList> leafLists, List<String> keys) throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530580 boolean leafFound = false;
581 List<YangLeafList> keyLeafLists = new LinkedList<>();
582
Vinod Kumar S71cba682016-02-25 15:52:16 +0530583 /*
584 * 1. Leaf identifier must refer to a child leaf of the list 2. A leaf
585 * that is part of the key must not be the built-in type "empty".
586 */
587 for (String key : keys) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530588 for (YangLeafList leafList : leafLists) {
589 if (key.equals(leafList.getLeafName())) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530590 if (leafList.getDataType().getDataType() == YangDataTypes.EMPTY) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530591 throw new DataModelException(" A leaf-list that is part of the key must not be the built-in " +
592 "type \"empty\".");
593 }
594 leafFound = true;
595 keyLeafLists.add(leafList);
596 break;
597 }
598 }
599 if (!leafFound) {
600 throw new DataModelException("Leaf-list identifier must refer to a child leaf of the list");
601 }
602 leafFound = false;
603 }
604
Vinod Kumar S71cba682016-02-25 15:52:16 +0530605 /*
606 * All key leafs in a list MUST have the same value for their "config"
607 * as the list itself.
608 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530609 for (YangLeafList keyLeafList : keyLeafLists) {
610 if (isConfig() != keyLeafList.isConfig()) {
611 throw new DataModelException("All key leaf-lists in a list must have the same value for their" +
612 " \"config\" as the list itself.");
613 }
614 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530615 }
616
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530617 @Override
618 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
619 // Asks helper to detect colliding child.
620 detectCollidingChildUtil(identifierName, dataType, this);
621 }
622
623 @Override
624 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530625 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530626 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as list \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530627 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530628 }
629 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530630}