blob: d09f8d565f49193bf643b688a7432b6eb267da7f [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;
Bharat saraswal96dfef02016-06-16 00:29:12 +053023import org.onosproject.yangutils.datamodel.utils.Parsable;
24import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vinod Kumar S38046502016-03-23 15:30:27 +053026
27import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
28
29/*
Vinod Kumar S67e7be62016-02-11 20:13:28 +053030 * The "list" statement is used to define an interior data node in the
31 * schema tree. A list node may exist in multiple instances in the data
32 * tree. Each such instance is known as a list entry. The "list"
33 * statement takes one argument, which is an identifier, followed by a
34 * block of sub-statements that holds detailed list information.
35 *
36 * A list entry is uniquely identified by the values of the list's keys,
37 * if defined.
38 *
39 * The list's sub-statements
40 *
41 * +--------------+---------+-------------+------------------+
42 * | substatement | section | cardinality |data model mapping|
43 * +--------------+---------+-------------+------------------+
44 * | anyxml | 7.10 | 0..n |-not supported |
45 * | choice | 7.9 | 0..n |-child nodes |
46 * | config | 7.19.1 | 0..1 |-boolean |
47 * | container | 7.5 | 0..n |-child nodes |
48 * | description | 7.19.3 | 0..1 |-string |
49 * | grouping | 7.11 | 0..n |-child nodes |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053050 * | if-feature | 7.18.2 | 0..n |-YangIfFeature |
Vinod Kumar S67e7be62016-02-11 20:13:28 +053051 * | key | 7.8.2 | 0..1 |-String list |
52 * | leaf | 7.6 | 0..n |-YangLeaf |
53 * | leaf-list | 7.7 | 0..n |-YangLeafList |
54 * | list | 7.8 | 0..n |-child nodes |
55 * | max-elements | 7.7.4 | 0..1 |-int |
56 * | min-elements | 7.7.3 | 0..1 |-int |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053057 * | must | 7.5.3 | 0..n |-YangMust |
Vinod Kumar S67e7be62016-02-11 20:13:28 +053058 * | ordered-by | 7.7.5 | 0..1 |-TODO |
59 * | reference | 7.19.4 | 0..1 |-string |
60 * | status | 7.19.2 | 0..1 |-YangStatus |
61 * | typedef | 7.3 | 0..n |-child nodes |
62 * | unique | 7.8.3 | 0..n |-TODO |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053063 * | uses | 7.12 | 0..n |-child nodes |
64 * | when | 7.19.5 | 0..1 |-YangWhen |
Vinod Kumar S67e7be62016-02-11 20:13:28 +053065 * +--------------+---------+-------------+------------------+
66 */
67
68/**
Bharat saraswald9822e92016-04-05 15:13:44 +053069 * Represents list data represented in YANG.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053070 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053071public class YangList
72 extends YangNode
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053073 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector, YangAugmentationHolder,
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053074 YangMustHolder, YangIfFeatureHolder, YangDataNode {
Vinod Kumar S67e7be62016-02-11 20:13:28 +053075
Bharat saraswal96dfef02016-06-16 00:29:12 +053076 private static final long serialVersionUID = 806201609L;
77
Vinod Kumar S67e7be62016-02-11 20:13:28 +053078 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053079 * Name of the YANG list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053080 */
81 private String name;
82
83 /**
84 * If list maintains config data.
85 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053086 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053087
88 /**
89 * Description of list.
90 */
91 private String description;
92
93 /**
94 * Reference RFC 6020.
95 *
96 * The "key" statement, which MUST be present if the list represents
97 * configuration, and MAY be present otherwise, takes as an argument a
98 * string that specifies a space-separated list of leaf identifiers of this
99 * list. A leaf identifier MUST NOT appear more than once in the key. Each
100 * such leaf identifier MUST refer to a child leaf of the list. The leafs
101 * can be defined directly in sub-statements to the list, or in groupings
102 * used in the list.
103 *
104 * The combined values of all the leafs specified in the key are used to
105 * uniquely identify a list entry. All key leafs MUST be given values when a
106 * list entry is created. Thus, any default values in the key leafs or their
107 * types are ignored. It also implies that any mandatory statement in the
108 * key leafs are ignored.
109 *
110 * A leaf that is part of the key can be of any built-in or derived type,
111 * except it MUST NOT be the built-in type "empty".
112 *
113 * All key leafs in a list MUST have the same value for their "config" as
114 * the list itself.
115 *
116 * List of key leaf names.
117 */
118 private List<String> keyList;
119
120 /**
121 * List of leaves.
122 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530123 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530124
125 /**
126 * List of leaf-lists.
127 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530128 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530129
130 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530131 * Reference RFC 6020.
132 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530133 * The "max-elements" statement, which is optional, takes as an argument a
134 * positive integer or the string "unbounded", which puts a constraint on
135 * valid list entries. A valid leaf-list or list always has at most
136 * max-elements entries.
137 *
138 * If no "max-elements" statement is present, it defaults to "unbounded".
139 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530140 private int maxElements = Integer.MAX_VALUE;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530141
142 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530143 * Reference RFC 6020.
144 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530145 * The "min-elements" statement, which is optional, takes as an argument a
146 * non-negative integer that puts a constraint on valid list entries. A
147 * valid leaf-list or list MUST have at least min-elements entries.
148 *
149 * If no "min-elements" statement is present, it defaults to zero.
150 *
151 * The behavior of the constraint depends on the type of the leaf-list's or
152 * list's closest ancestor node in the schema tree that is not a non-
153 * presence container:
154 *
155 * o If this ancestor is a case node, the constraint is enforced if any
156 * other node from the case exists.
157 *
158 * o Otherwise, it is enforced if the ancestor node exists.
159 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530160 private int minElements = 0;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530161
162 /**
163 * reference.
164 */
165 private String reference;
166
167 /**
168 * Status of the node.
169 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530170 private YangStatusType status = YangStatusType.CURRENT;
171
172 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530173 * List of must statement constraints.
174 */
175 private List<YangMust> mustConstraintList;
176
177 /**
178 * When data of the node.
179 */
180 private YangWhen when;
181
182 /**
183 * List of if-feature.
184 */
185 private List<YangIfFeature> ifFeatureList;
186
187 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530188 * Creates a YANG list object.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530189 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530190 public YangList() {
191 super(YangNodeType.LIST_NODE);
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530192 }
193
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530194 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530195 * Returns the when.
196 *
197 * @return the when
198 */
199 public YangWhen getWhen() {
200 return when;
201 }
202
203 /**
204 * Sets the when.
205 *
206 * @param when the when to set
207 */
208 public void setWhen(YangWhen when) {
209 this.when = when;
210 }
211
212 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530213 * Returns the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530214 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530215 * @return YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530216 */
217 @Override
218 public String getName() {
219 return name;
220 }
221
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530222 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530223 * Sets the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530224 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530225 * @param name YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530226 */
227 @Override
228 public void setName(String name) {
229 this.name = name;
230 }
231
232 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530233 * Returns the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530234 *
235 * @return the isConfig
236 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530237 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530238 return isConfig;
239 }
240
241 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530242 * Sets the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530243 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530244 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530245 */
246 public void setConfig(boolean isCfg) {
247 isConfig = isCfg;
248 }
249
250 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530251 * Returns the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530252 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530253 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530254 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530255 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530256 public String getDescription() {
257 return description;
258 }
259
260 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530261 * Sets the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530262 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530263 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530264 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530265 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530266 public void setDescription(String description) {
267 this.description = description;
268 }
269
270 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530271 * Returns the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530272 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530273 * @return the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530274 */
275 public List<String> getKeyList() {
276 return keyList;
277 }
278
279 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530280 * Sets the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530281 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530282 * @param keyList the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530283 */
284 private void setKeyList(List<String> keyList) {
285 this.keyList = keyList;
286 }
287
288 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530289 * Adds a key field name.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530290 *
291 * @param key key field name.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530292 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530293 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530294 public void addKey(String key)
295 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530296 if (getKeyList() == null) {
297 setKeyList(new LinkedList<String>());
298 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530299
300 if (getKeyList().contains(key)) {
301 throw new DataModelException("A leaf identifier must not appear more than once in the\n" +
302 " key");
303 }
304
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530305 getKeyList().add(key);
306 }
307
308 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530309 * Returns the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530310 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530311 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530312 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530313 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530314 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530315 return listOfLeaf;
316 }
317
318 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530319 * Sets the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530320 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530321 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530322 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530323 @Override
324 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530325 listOfLeaf = leafsList;
326 }
327
328 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530329 * Adds a leaf.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530330 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530331 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530332 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530333 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530334 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530335 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530336 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530337 }
338
339 getListOfLeaf().add(leaf);
340 }
341
342 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530343 * Returns the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530344 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530345 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530346 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530347 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530348 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530349 return listOfLeafList;
350 }
351
352 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530353 * Sets the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530354 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530355 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530356 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530357 @Override
358 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530359 this.listOfLeafList = listOfLeafList;
360 }
361
362 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530363 * Adds a leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530364 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530365 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530366 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530367 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530368 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530369 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530370 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530371 }
372
373 getListOfLeafList().add(leafList);
374 }
375
376 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530377 * Returns the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530378 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530379 * @return the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530380 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530381 public int getMaxElements() {
382 return maxElements;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530383 }
384
385 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530386 * Sets the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530387 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530388 * @param max the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530389 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530390 public void setMaxElements(int max) {
391 maxElements = max;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530392 }
393
394 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530395 * Returns the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530396 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530397 * @return the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530398 */
399 public int getMinElements() {
400 return minElements;
401 }
402
403 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530404 * Sets the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530405 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530406 * @param minElements the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530407 */
408 public void setMinElements(int minElements) {
409 this.minElements = minElements;
410 }
411
412 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530413 * Returns the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530414 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530415 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530416 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530417 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530418 public String getReference() {
419 return reference;
420 }
421
422 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530423 * Sets the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530424 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530425 * @param reference the reference to set
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 setReference(String reference) {
429 this.reference = reference;
430 }
431
432 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530433 * Returns the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530434 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530435 * @return the status
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 YangStatusType getStatus() {
439 return status;
440 }
441
442 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530443 * Sets the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530444 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530445 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530446 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530447 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530448 public void setStatus(YangStatusType status) {
449 this.status = status;
450 }
451
452 /**
453 * Returns the type of the parsed data.
454 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530455 * @return returns LIST_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530456 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530457 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530458 public YangConstructType getYangConstructType() {
459 return YangConstructType.LIST_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530460 }
461
462 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530463 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530464 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530465 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530466 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530467 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530468 public void validateDataOnEntry()
469 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530470 // TODO auto-generated method stub, to be implemented by parser
471 }
472
473 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530474 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530475 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530476 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530477 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530478 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530479 public void validateDataOnExit()
480 throws DataModelException {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530481 List<String> keys = getKeyList();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530482 List<YangLeaf> leaves = getListOfLeaf();
483 List<YangLeafList> leafLists = getListOfLeafList();
484
485 setDefaultConfigValueToChild(leaves, leafLists);
486 validateConfig(leaves, leafLists);
487
488 /* A list must have atleast one key leaf if config is true */
janani bdd1314f2016-05-19 17:39:50 +0530489 if (isConfig && (keys == null || leaves == null && leafLists == null) && !isUsesPresentInList()
490 && !isListPresentInGrouping()) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530491 throw new DataModelException("A list must have atleast one key leaf if config is true;");
492 } else if (keys != null) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530493 validateKey(leaves, leafLists, keys);
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530494 }
495 }
496
497 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530498 * Sets the config's value to all leaf if leaf's config statement is not
499 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530500 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530501 * @param leaves list of leaf attributes of YANG list
502 * @param leafLists list of leaf-list attributes of YANG list
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530503 */
504 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
505
Vinod Kumar S71cba682016-02-25 15:52:16 +0530506 /*
507 * If "config" is not specified, the default is the same as the parent
508 * schema node's "config" value.
509 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530510 if (leaves != null) {
511 for (YangLeaf leaf : leaves) {
512 if (leaf.isConfig() == null) {
513 leaf.setConfig(isConfig);
514 }
515 }
516 }
517
Vinod Kumar S71cba682016-02-25 15:52:16 +0530518 /*
519 * If "config" is not specified, the default is the same as the parent
520 * schema node's "config" value.
521 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530522 if (leafLists != null) {
523 for (YangLeafList leafList : leafLists) {
524 if (leafList.isConfig() == null) {
525 leafList.setConfig(isConfig);
526 }
527 }
528 }
529 }
530
531 /**
532 * Validates config statement of YANG list.
533 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530534 * @param leaves list of leaf attributes of YANG list
535 * @param leafLists list of leaf-list attributes of YANG list
536 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530537 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530538 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists)
539 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530540
Vinod Kumar S71cba682016-02-25 15:52:16 +0530541 /*
542 * If a node has "config" set to "false", no node underneath it can have
543 * "config" set to "true".
544 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530545 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530546 for (YangLeaf leaf : leaves) {
547 if (leaf.isConfig()) {
548 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
549 "it can have \"config\" set to \"true\".");
550 }
551 }
552 }
553
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530554 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530555 for (YangLeafList leafList : leafLists) {
556 if (leafList.isConfig()) {
557 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
558 "it can have \"config\" set to \"true\".");
559 }
560 }
561 }
562 }
563
564 /**
565 * Validates key statement of list.
566 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530567 * @param leaves list of leaf attributes of list
Vidyashree Rama1db15562016-05-17 16:16:15 +0530568 * @param leafLists list of leaf-list attributes of list
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530569 * @param keys list of key attributes of list
570 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530571 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530572 private void validateKey(List<YangLeaf> leaves, List<YangLeafList> leafLists, List<String> keys)
573 throws
Vidyashree Rama1db15562016-05-17 16:16:15 +0530574 DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530575 boolean leafFound = false;
576 List<YangLeaf> keyLeaves = new LinkedList<>();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530577 List<YangLeafList> keyLeafLists = new LinkedList<>();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530578
Vinod Kumar S71cba682016-02-25 15:52:16 +0530579 /*
580 * 1. Leaf identifier must refer to a child leaf of the list 2. A leaf
581 * that is part of the key must not be the built-in type "empty".
582 */
583 for (String key : keys) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530584 if (leaves != null && !leaves.isEmpty()) {
585 for (YangLeaf leaf : leaves) {
586 if (key.equals(leaf.getName())) {
587 if (leaf.getDataType().getDataType() == YangDataTypes.EMPTY) {
588 throw new DataModelException(" A leaf that is part of the key must not be the built-in " +
589 "type \"empty\".");
590 }
591 leafFound = true;
592 keyLeaves.add(leaf);
593 break;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530594 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530595 }
596 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530597
598 if (leafLists != null && !leafLists.isEmpty()) {
599 for (YangLeafList leafList : leafLists) {
600 if (key.equals(leafList.getName())) {
601 if (leafList.getDataType().getDataType() == YangDataTypes.EMPTY) {
602 throw new DataModelException(" A leaf-list that is part of the key" +
603 " must not be the built-in type \"empty\".");
604 }
605 leafFound = true;
606 keyLeafLists.add(leafList);
607 break;
608 }
609 }
610 }
611
janani bdd1314f2016-05-19 17:39:50 +0530612 if (!leafFound && !isUsesPresentInList() && !isListPresentInGrouping()) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530613 throw new DataModelException("An identifier, in key, must refer to a child leaf of the list");
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530614 }
615 leafFound = false;
616 }
617
Vinod Kumar S71cba682016-02-25 15:52:16 +0530618 /*
619 * All key leafs in a list MUST have the same value for their "config"
620 * as the list itself.
621 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530622 for (YangLeaf keyLeaf : keyLeaves) {
623 if (isConfig != keyLeaf.isConfig()) {
624 throw new DataModelException("All key leafs in a list must have the same value for their" +
625 " \"config\" as the list itself.");
626 }
627 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530628
Vidyashree Rama1db15562016-05-17 16:16:15 +0530629 /*
Vinod Kumar S71cba682016-02-25 15:52:16 +0530630 * All key leafs in a list MUST have the same value for their "config"
631 * as the list itself.
632 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530633 for (YangLeafList keyLeafList : keyLeafLists) {
634 if (isConfig() != keyLeafList.isConfig()) {
635 throw new DataModelException("All key leaf-lists in a list must have the same value for their" +
636 " \"config\" as the list itself.");
637 }
638 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530639 }
640
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530641 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530642 public void detectCollidingChild(String identifierName, YangConstructType dataType)
643 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530644 // Asks helper to detect colliding child.
645 detectCollidingChildUtil(identifierName, dataType, this);
646 }
647
648 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530649 public void detectSelfCollision(String identifierName, YangConstructType dataType)
650 throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530651 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530652 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as list \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530653 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530654 }
655 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530656
657 private boolean isUsesPresentInList() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530658 YangNode node = getChild();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530659 while (node != null) {
660 if (node instanceof YangUses) {
661 return true;
662 }
663 node = node.getNextSibling();
664 }
665 return false;
janani bdd1314f2016-05-19 17:39:50 +0530666 // TODO When grouping linking is done this method has to be modified.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530667 }
668
janani bdd1314f2016-05-19 17:39:50 +0530669 private boolean isListPresentInGrouping() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530670 YangNode node = getParent();
janani bdd1314f2016-05-19 17:39:50 +0530671 while (node != null) {
672 if (node instanceof YangGrouping) {
673 return true;
674 }
675 node = node.getParent();
676 }
677 return false;
678 // TODO When grouping linking is done this method has to be modified.
679 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530680
681 @Override
682 public List<YangIfFeature> getIfFeatureList() {
683 return ifFeatureList;
684 }
685
686 @Override
687 public void addIfFeatureList(YangIfFeature ifFeature) {
688 if (getIfFeatureList() == null) {
689 setIfFeatureList(new LinkedList<>());
690 }
691 getIfFeatureList().add(ifFeature);
692 }
693
694 @Override
695 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
696 this.ifFeatureList = ifFeatureList;
697 }
698
699 @Override
700 public List<YangMust> getListOfMust() {
701 return mustConstraintList;
702 }
703
704 @Override
705 public void setListOfMust(List<YangMust> mustConstraintList) {
706 this.mustConstraintList = mustConstraintList;
707 }
708
709 @Override
710 public void addMust(YangMust must) {
711 if (getListOfMust() == null) {
712 setListOfMust(new LinkedList<>());
713 }
714 getListOfMust().add(must);
715 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530716}