blob: 834a0ad618d4bebe2f8cf36460650861ddf01dc6 [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
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/**
Bharat saraswald9822e92016-04-05 15:13:44 +053068 * Represents list data represented in YANG.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053069 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053070public class YangList
71 extends YangNode
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053072 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector, YangAugmentationHolder {
Vinod Kumar S67e7be62016-02-11 20:13:28 +053073
74 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053075 * Name of the YANG list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053076 */
77 private String name;
78
79 /**
80 * If list maintains config data.
81 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053082 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053083
84 /**
85 * Description of list.
86 */
87 private String description;
88
89 /**
90 * Reference RFC 6020.
91 *
92 * The "key" statement, which MUST be present if the list represents
93 * configuration, and MAY be present otherwise, takes as an argument a
94 * string that specifies a space-separated list of leaf identifiers of this
95 * list. A leaf identifier MUST NOT appear more than once in the key. Each
96 * such leaf identifier MUST refer to a child leaf of the list. The leafs
97 * can be defined directly in sub-statements to the list, or in groupings
98 * used in the list.
99 *
100 * The combined values of all the leafs specified in the key are used to
101 * uniquely identify a list entry. All key leafs MUST be given values when a
102 * list entry is created. Thus, any default values in the key leafs or their
103 * types are ignored. It also implies that any mandatory statement in the
104 * key leafs are ignored.
105 *
106 * A leaf that is part of the key can be of any built-in or derived type,
107 * except it MUST NOT be the built-in type "empty".
108 *
109 * All key leafs in a list MUST have the same value for their "config" as
110 * the list itself.
111 *
112 * List of key leaf names.
113 */
114 private List<String> keyList;
115
116 /**
117 * List of leaves.
118 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530119 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530120
121 /**
122 * List of leaf-lists.
123 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530124 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530125
126 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530127 * Reference RFC 6020.
128 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530129 * The "max-elements" statement, which is optional, takes as an argument a
130 * positive integer or the string "unbounded", which puts a constraint on
131 * valid list entries. A valid leaf-list or list always has at most
132 * max-elements entries.
133 *
134 * If no "max-elements" statement is present, it defaults to "unbounded".
135 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530136 private int maxElements = Integer.MAX_VALUE;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530137
138 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530139 * Reference RFC 6020.
140 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530141 * The "min-elements" statement, which is optional, takes as an argument a
142 * non-negative integer that puts a constraint on valid list entries. A
143 * valid leaf-list or list MUST have at least min-elements entries.
144 *
145 * If no "min-elements" statement is present, it defaults to zero.
146 *
147 * The behavior of the constraint depends on the type of the leaf-list's or
148 * list's closest ancestor node in the schema tree that is not a non-
149 * presence container:
150 *
151 * o If this ancestor is a case node, the constraint is enforced if any
152 * other node from the case exists.
153 *
154 * o Otherwise, it is enforced if the ancestor node exists.
155 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530156 private int minElements = 0;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530157
158 /**
159 * reference.
160 */
161 private String reference;
162
163 /**
164 * Status of the node.
165 */
166
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530167 private YangStatusType status = YangStatusType.CURRENT;
168
169 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530170 * Creates a YANG list object.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530171 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530172 public YangList() {
173 super(YangNodeType.LIST_NODE);
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530174 }
175
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530176 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530177 * Returns the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530178 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530179 * @return YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530180 */
181 @Override
182 public String getName() {
183 return name;
184 }
185
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530186 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530187 * Sets the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530188 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530189 * @param name YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530190 */
191 @Override
192 public void setName(String name) {
193 this.name = name;
194 }
195
196 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530197 * Returns the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530198 *
199 * @return the isConfig
200 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530201 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530202 return isConfig;
203 }
204
205 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530206 * Sets the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530207 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530208 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530209 */
210 public void setConfig(boolean isCfg) {
211 isConfig = isCfg;
212 }
213
214 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530215 * Returns the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530216 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530217 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530218 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530219 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530220 public String getDescription() {
221 return description;
222 }
223
224 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530225 * Sets the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530226 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530227 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530228 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530229 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530230 public void setDescription(String description) {
231 this.description = description;
232 }
233
234 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530235 * Returns the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530236 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530237 * @return the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530238 */
239 public List<String> getKeyList() {
240 return keyList;
241 }
242
243 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530244 * Sets the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530245 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530246 * @param keyList the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530247 */
248 private void setKeyList(List<String> keyList) {
249 this.keyList = keyList;
250 }
251
252 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530253 * Adds a key field name.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530254 *
255 * @param key key field name.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530256 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530257 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530258 public void addKey(String key)
259 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530260 if (getKeyList() == null) {
261 setKeyList(new LinkedList<String>());
262 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530263
264 if (getKeyList().contains(key)) {
265 throw new DataModelException("A leaf identifier must not appear more than once in the\n" +
266 " key");
267 }
268
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530269 getKeyList().add(key);
270 }
271
272 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530273 * Returns the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530274 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530275 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530276 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530277 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530278 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530279 return listOfLeaf;
280 }
281
282 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530283 * Sets the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530284 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530285 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530286 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530287 @Override
288 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530289 listOfLeaf = leafsList;
290 }
291
292 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530293 * Adds a leaf.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530294 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530295 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530296 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530297 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530298 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530299 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530300 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530301 }
302
303 getListOfLeaf().add(leaf);
304 }
305
306 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530307 * Returns the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530308 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530309 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530310 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530311 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530312 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530313 return listOfLeafList;
314 }
315
316 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530317 * Sets the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530318 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530319 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530320 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530321 @Override
322 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530323 this.listOfLeafList = listOfLeafList;
324 }
325
326 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530327 * Adds a leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530328 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530329 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530330 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530331 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530332 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530333 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530334 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530335 }
336
337 getListOfLeafList().add(leafList);
338 }
339
340 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530341 * Returns the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530342 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530343 * @return the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530344 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530345 public int getMaxElements() {
346 return maxElements;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530347 }
348
349 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530350 * Sets the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530351 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530352 * @param max the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530353 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530354 public void setMaxElements(int max) {
355 maxElements = max;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530356 }
357
358 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530359 * Returns the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530360 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530361 * @return the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530362 */
363 public int getMinElements() {
364 return minElements;
365 }
366
367 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530368 * Sets the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530369 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530370 * @param minElements the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530371 */
372 public void setMinElements(int minElements) {
373 this.minElements = minElements;
374 }
375
376 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530377 * Returns the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530378 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530379 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530380 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530381 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530382 public String getReference() {
383 return reference;
384 }
385
386 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530387 * Sets the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530388 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530389 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530390 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530391 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530392 public void setReference(String reference) {
393 this.reference = reference;
394 }
395
396 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530397 * Returns the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530398 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530399 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530400 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530401 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530402 public YangStatusType getStatus() {
403 return status;
404 }
405
406 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530407 * Sets the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530408 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530409 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530410 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530411 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530412 public void setStatus(YangStatusType status) {
413 this.status = status;
414 }
415
416 /**
417 * Returns the type of the parsed data.
418 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530419 * @return returns LIST_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530420 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530421 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530422 public YangConstructType getYangConstructType() {
423 return YangConstructType.LIST_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530424 }
425
426 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530427 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530428 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530429 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530430 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530431 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530432 public void validateDataOnEntry()
433 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530434 // TODO auto-generated method stub, to be implemented by parser
435 }
436
437 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530438 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530439 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530440 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530441 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530442 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530443 public void validateDataOnExit()
444 throws DataModelException {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530445 List<String> keys = getKeyList();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530446 List<YangLeaf> leaves = getListOfLeaf();
447 List<YangLeafList> leafLists = getListOfLeafList();
448
449 setDefaultConfigValueToChild(leaves, leafLists);
450 validateConfig(leaves, leafLists);
451
452 /* A list must have atleast one key leaf if config is true */
janani bdd1314f2016-05-19 17:39:50 +0530453 if (isConfig && (keys == null || leaves == null && leafLists == null) && !isUsesPresentInList()
454 && !isListPresentInGrouping()) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530455 throw new DataModelException("A list must have atleast one key leaf if config is true;");
456 } else if (keys != null) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530457 validateKey(leaves, leafLists, keys);
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530458 }
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 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530502 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists)
503 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530504
Vinod Kumar S71cba682016-02-25 15:52:16 +0530505 /*
506 * If a node has "config" set to "false", no node underneath it can have
507 * "config" set to "true".
508 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530509 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530510 for (YangLeaf leaf : leaves) {
511 if (leaf.isConfig()) {
512 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
513 "it can have \"config\" set to \"true\".");
514 }
515 }
516 }
517
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530518 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530519 for (YangLeafList leafList : leafLists) {
520 if (leafList.isConfig()) {
521 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
522 "it can have \"config\" set to \"true\".");
523 }
524 }
525 }
526 }
527
528 /**
529 * Validates key statement of list.
530 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530531 * @param leaves list of leaf attributes of list
Vidyashree Rama1db15562016-05-17 16:16:15 +0530532 * @param leafLists list of leaf-list attributes of list
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530533 * @param keys list of key attributes of list
534 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530535 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530536 private void validateKey(List<YangLeaf> leaves, List<YangLeafList> leafLists, List<String> keys)
537 throws
Vidyashree Rama1db15562016-05-17 16:16:15 +0530538 DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530539 boolean leafFound = false;
540 List<YangLeaf> keyLeaves = new LinkedList<>();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530541 List<YangLeafList> keyLeafLists = new LinkedList<>();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530542
Vinod Kumar S71cba682016-02-25 15:52:16 +0530543 /*
544 * 1. Leaf identifier must refer to a child leaf of the list 2. A leaf
545 * that is part of the key must not be the built-in type "empty".
546 */
547 for (String key : keys) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530548 if (leaves != null && !leaves.isEmpty()) {
549 for (YangLeaf leaf : leaves) {
550 if (key.equals(leaf.getName())) {
551 if (leaf.getDataType().getDataType() == YangDataTypes.EMPTY) {
552 throw new DataModelException(" A leaf that is part of the key must not be the built-in " +
553 "type \"empty\".");
554 }
555 leafFound = true;
556 keyLeaves.add(leaf);
557 break;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530558 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530559 }
560 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530561
562 if (leafLists != null && !leafLists.isEmpty()) {
563 for (YangLeafList leafList : leafLists) {
564 if (key.equals(leafList.getName())) {
565 if (leafList.getDataType().getDataType() == YangDataTypes.EMPTY) {
566 throw new DataModelException(" A leaf-list that is part of the key" +
567 " must not be the built-in type \"empty\".");
568 }
569 leafFound = true;
570 keyLeafLists.add(leafList);
571 break;
572 }
573 }
574 }
575
janani bdd1314f2016-05-19 17:39:50 +0530576 if (!leafFound && !isUsesPresentInList() && !isListPresentInGrouping()) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530577 throw new DataModelException("An identifier, in key, must refer to a child leaf of the list");
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530578 }
579 leafFound = false;
580 }
581
Vinod Kumar S71cba682016-02-25 15:52:16 +0530582 /*
583 * All key leafs in a list MUST have the same value for their "config"
584 * as the list itself.
585 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530586 for (YangLeaf keyLeaf : keyLeaves) {
587 if (isConfig != keyLeaf.isConfig()) {
588 throw new DataModelException("All key leafs in a list must have the same value for their" +
589 " \"config\" as the list itself.");
590 }
591 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530592
Vidyashree Rama1db15562016-05-17 16:16:15 +0530593 /*
Vinod Kumar S71cba682016-02-25 15:52:16 +0530594 * All key leafs in a list MUST have the same value for their "config"
595 * as the list itself.
596 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530597 for (YangLeafList keyLeafList : keyLeafLists) {
598 if (isConfig() != keyLeafList.isConfig()) {
599 throw new DataModelException("All key leaf-lists in a list must have the same value for their" +
600 " \"config\" as the list itself.");
601 }
602 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530603 }
604
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530605 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530606 public void detectCollidingChild(String identifierName, YangConstructType dataType)
607 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530608 // Asks helper to detect colliding child.
609 detectCollidingChildUtil(identifierName, dataType, this);
610 }
611
612 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530613 public void detectSelfCollision(String identifierName, YangConstructType dataType)
614 throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530615 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530616 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as list \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530617 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530618 }
619 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530620
621 private boolean isUsesPresentInList() {
622 YangNode node = this.getChild();
623 while (node != null) {
624 if (node instanceof YangUses) {
625 return true;
626 }
627 node = node.getNextSibling();
628 }
629 return false;
janani bdd1314f2016-05-19 17:39:50 +0530630 // TODO When grouping linking is done this method has to be modified.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530631 }
632
janani bdd1314f2016-05-19 17:39:50 +0530633 private boolean isListPresentInGrouping() {
634 YangNode node = this.getParent();
635 while (node != null) {
636 if (node instanceof YangGrouping) {
637 return true;
638 }
639 node = node.getParent();
640 }
641 return false;
642 // TODO When grouping linking is done this method has to be modified.
643 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530644}