blob: a1e13ac22ce79f4689a9c7cfab96c605f9b71718 [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;
Vinod Kumar S38046502016-03-23 15:30:27 +053025
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 |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053049 * | if-feature | 7.18.2 | 0..n |-YangIfFeature |
Vinod Kumar S67e7be62016-02-11 20:13:28 +053050 * | 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 |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053056 * | must | 7.5.3 | 0..n |-YangMust |
Vinod Kumar S67e7be62016-02-11 20:13:28 +053057 * | 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 |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053062 * | uses | 7.12 | 0..n |-child nodes |
63 * | when | 7.19.5 | 0..1 |-YangWhen |
Vinod Kumar S67e7be62016-02-11 20:13:28 +053064 * +--------------+---------+-------------+------------------+
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
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053072 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector, YangAugmentationHolder,
73 YangMustHolder, YangIfFeatureHolder {
Vinod Kumar S67e7be62016-02-11 20:13:28 +053074
Bharat saraswal96dfef02016-06-16 00:29:12 +053075 private static final long serialVersionUID = 806201609L;
76
Vinod Kumar S67e7be62016-02-11 20:13:28 +053077 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053078 * Name of the YANG list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053079 */
80 private String name;
81
82 /**
83 * If list maintains config data.
84 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053085 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053086
87 /**
88 * Description of list.
89 */
90 private String description;
91
92 /**
93 * Reference RFC 6020.
94 *
95 * The "key" statement, which MUST be present if the list represents
96 * configuration, and MAY be present otherwise, takes as an argument a
97 * string that specifies a space-separated list of leaf identifiers of this
98 * list. A leaf identifier MUST NOT appear more than once in the key. Each
99 * such leaf identifier MUST refer to a child leaf of the list. The leafs
100 * can be defined directly in sub-statements to the list, or in groupings
101 * used in the list.
102 *
103 * The combined values of all the leafs specified in the key are used to
104 * uniquely identify a list entry. All key leafs MUST be given values when a
105 * list entry is created. Thus, any default values in the key leafs or their
106 * types are ignored. It also implies that any mandatory statement in the
107 * key leafs are ignored.
108 *
109 * A leaf that is part of the key can be of any built-in or derived type,
110 * except it MUST NOT be the built-in type "empty".
111 *
112 * All key leafs in a list MUST have the same value for their "config" as
113 * the list itself.
114 *
115 * List of key leaf names.
116 */
117 private List<String> keyList;
118
119 /**
120 * List of leaves.
121 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530122 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530123
124 /**
125 * List of leaf-lists.
126 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530127 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530128
129 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530130 * Reference RFC 6020.
131 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530132 * The "max-elements" statement, which is optional, takes as an argument a
133 * positive integer or the string "unbounded", which puts a constraint on
134 * valid list entries. A valid leaf-list or list always has at most
135 * max-elements entries.
136 *
137 * If no "max-elements" statement is present, it defaults to "unbounded".
138 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530139 private int maxElements = Integer.MAX_VALUE;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530140
141 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530142 * Reference RFC 6020.
143 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530144 * The "min-elements" statement, which is optional, takes as an argument a
145 * non-negative integer that puts a constraint on valid list entries. A
146 * valid leaf-list or list MUST have at least min-elements entries.
147 *
148 * If no "min-elements" statement is present, it defaults to zero.
149 *
150 * The behavior of the constraint depends on the type of the leaf-list's or
151 * list's closest ancestor node in the schema tree that is not a non-
152 * presence container:
153 *
154 * o If this ancestor is a case node, the constraint is enforced if any
155 * other node from the case exists.
156 *
157 * o Otherwise, it is enforced if the ancestor node exists.
158 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530159 private int minElements = 0;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530160
161 /**
162 * reference.
163 */
164 private String reference;
165
166 /**
167 * Status of the node.
168 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530169 private YangStatusType status = YangStatusType.CURRENT;
170
171 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530172 * List of must statement constraints.
173 */
174 private List<YangMust> mustConstraintList;
175
176 /**
177 * When data of the node.
178 */
179 private YangWhen when;
180
181 /**
182 * List of if-feature.
183 */
184 private List<YangIfFeature> ifFeatureList;
185
186 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530187 * Creates a YANG list object.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530188 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530189 public YangList() {
190 super(YangNodeType.LIST_NODE);
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530191 }
192
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530193 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530194 * Returns the when.
195 *
196 * @return the when
197 */
198 public YangWhen getWhen() {
199 return when;
200 }
201
202 /**
203 * Sets the when.
204 *
205 * @param when the when to set
206 */
207 public void setWhen(YangWhen when) {
208 this.when = when;
209 }
210
211 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530212 * Returns the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530213 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530214 * @return YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530215 */
216 @Override
217 public String getName() {
218 return name;
219 }
220
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530221 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530222 * Sets the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530223 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530224 * @param name YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530225 */
226 @Override
227 public void setName(String name) {
228 this.name = name;
229 }
230
231 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530232 * Returns the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530233 *
234 * @return the isConfig
235 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530236 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530237 return isConfig;
238 }
239
240 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530241 * Sets the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530242 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530243 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530244 */
245 public void setConfig(boolean isCfg) {
246 isConfig = isCfg;
247 }
248
249 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530250 * Returns the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530251 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530252 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530253 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530254 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530255 public String getDescription() {
256 return description;
257 }
258
259 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530260 * Sets the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530261 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530262 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530263 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530264 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530265 public void setDescription(String description) {
266 this.description = description;
267 }
268
269 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530270 * Returns the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530271 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530272 * @return the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530273 */
274 public List<String> getKeyList() {
275 return keyList;
276 }
277
278 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530279 * Sets the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530280 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530281 * @param keyList the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530282 */
283 private void setKeyList(List<String> keyList) {
284 this.keyList = keyList;
285 }
286
287 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530288 * Adds a key field name.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530289 *
290 * @param key key field name.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530291 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530292 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530293 public void addKey(String key)
294 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530295 if (getKeyList() == null) {
296 setKeyList(new LinkedList<String>());
297 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530298
299 if (getKeyList().contains(key)) {
300 throw new DataModelException("A leaf identifier must not appear more than once in the\n" +
301 " key");
302 }
303
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530304 getKeyList().add(key);
305 }
306
307 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530308 * Returns the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530309 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530310 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530311 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530312 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530313 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530314 return listOfLeaf;
315 }
316
317 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530318 * Sets the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530319 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530320 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530321 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530322 @Override
323 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530324 listOfLeaf = leafsList;
325 }
326
327 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530328 * Adds a leaf.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530329 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530330 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530331 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530332 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530333 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530334 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530335 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530336 }
337
338 getListOfLeaf().add(leaf);
339 }
340
341 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530342 * Returns the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530343 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530344 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530345 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530346 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530347 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530348 return listOfLeafList;
349 }
350
351 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530352 * Sets the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530353 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530354 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530355 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530356 @Override
357 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530358 this.listOfLeafList = listOfLeafList;
359 }
360
361 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530362 * Adds a leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530363 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530364 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530365 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530366 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530367 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530368 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530369 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530370 }
371
372 getListOfLeafList().add(leafList);
373 }
374
375 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530376 * Returns the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530377 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530378 * @return the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530379 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530380 public int getMaxElements() {
381 return maxElements;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530382 }
383
384 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530385 * Sets the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530386 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530387 * @param max the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530388 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530389 public void setMaxElements(int max) {
390 maxElements = max;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530391 }
392
393 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530394 * Returns the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530395 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530396 * @return the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530397 */
398 public int getMinElements() {
399 return minElements;
400 }
401
402 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530403 * Sets the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530404 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530405 * @param minElements the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530406 */
407 public void setMinElements(int minElements) {
408 this.minElements = minElements;
409 }
410
411 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530412 * Returns the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530413 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530414 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530415 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530416 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530417 public String getReference() {
418 return reference;
419 }
420
421 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530422 * Sets the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530423 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530424 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530425 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530426 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530427 public void setReference(String reference) {
428 this.reference = reference;
429 }
430
431 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530432 * Returns the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530433 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530434 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530435 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530436 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530437 public YangStatusType getStatus() {
438 return status;
439 }
440
441 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530442 * Sets the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530443 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530444 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530445 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530446 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530447 public void setStatus(YangStatusType status) {
448 this.status = status;
449 }
450
451 /**
452 * Returns the type of the parsed data.
453 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530454 * @return returns LIST_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530455 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530456 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530457 public YangConstructType getYangConstructType() {
458 return YangConstructType.LIST_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530459 }
460
461 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530462 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530463 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530464 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530465 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530466 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530467 public void validateDataOnEntry()
468 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530469 // TODO auto-generated method stub, to be implemented by parser
470 }
471
472 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530473 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530474 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530475 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530476 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530477 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530478 public void validateDataOnExit()
479 throws DataModelException {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530480 List<String> keys = getKeyList();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530481 List<YangLeaf> leaves = getListOfLeaf();
482 List<YangLeafList> leafLists = getListOfLeafList();
483
484 setDefaultConfigValueToChild(leaves, leafLists);
485 validateConfig(leaves, leafLists);
486
487 /* A list must have atleast one key leaf if config is true */
janani bdd1314f2016-05-19 17:39:50 +0530488 if (isConfig && (keys == null || leaves == null && leafLists == null) && !isUsesPresentInList()
489 && !isListPresentInGrouping()) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530490 throw new DataModelException("A list must have atleast one key leaf if config is true;");
491 } else if (keys != null) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530492 validateKey(leaves, leafLists, keys);
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530493 }
494 }
495
496 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530497 * Sets the config's value to all leaf if leaf's config statement is not
498 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530499 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530500 * @param leaves list of leaf attributes of YANG list
501 * @param leafLists list of leaf-list attributes of YANG list
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530502 */
503 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
504
Vinod Kumar S71cba682016-02-25 15:52:16 +0530505 /*
506 * If "config" is not specified, the default is the same as the parent
507 * schema node's "config" value.
508 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530509 if (leaves != null) {
510 for (YangLeaf leaf : leaves) {
511 if (leaf.isConfig() == null) {
512 leaf.setConfig(isConfig);
513 }
514 }
515 }
516
Vinod Kumar S71cba682016-02-25 15:52:16 +0530517 /*
518 * If "config" is not specified, the default is the same as the parent
519 * schema node's "config" value.
520 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530521 if (leafLists != null) {
522 for (YangLeafList leafList : leafLists) {
523 if (leafList.isConfig() == null) {
524 leafList.setConfig(isConfig);
525 }
526 }
527 }
528 }
529
530 /**
531 * Validates config statement of YANG list.
532 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530533 * @param leaves list of leaf attributes of YANG list
534 * @param leafLists list of leaf-list attributes of YANG list
535 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530536 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530537 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists)
538 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530539
Vinod Kumar S71cba682016-02-25 15:52:16 +0530540 /*
541 * If a node has "config" set to "false", no node underneath it can have
542 * "config" set to "true".
543 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530544 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530545 for (YangLeaf leaf : leaves) {
546 if (leaf.isConfig()) {
547 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
548 "it can have \"config\" set to \"true\".");
549 }
550 }
551 }
552
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530553 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530554 for (YangLeafList leafList : leafLists) {
555 if (leafList.isConfig()) {
556 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
557 "it can have \"config\" set to \"true\".");
558 }
559 }
560 }
561 }
562
563 /**
564 * Validates key statement of list.
565 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530566 * @param leaves list of leaf attributes of list
Vidyashree Rama1db15562016-05-17 16:16:15 +0530567 * @param leafLists list of leaf-list attributes of list
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530568 * @param keys list of key attributes of list
569 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530570 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530571 private void validateKey(List<YangLeaf> leaves, List<YangLeafList> leafLists, List<String> keys)
572 throws
Vidyashree Rama1db15562016-05-17 16:16:15 +0530573 DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530574 boolean leafFound = false;
575 List<YangLeaf> keyLeaves = new LinkedList<>();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530576 List<YangLeafList> keyLeafLists = new LinkedList<>();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530577
Vinod Kumar S71cba682016-02-25 15:52:16 +0530578 /*
579 * 1. Leaf identifier must refer to a child leaf of the list 2. A leaf
580 * that is part of the key must not be the built-in type "empty".
581 */
582 for (String key : keys) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530583 if (leaves != null && !leaves.isEmpty()) {
584 for (YangLeaf leaf : leaves) {
585 if (key.equals(leaf.getName())) {
586 if (leaf.getDataType().getDataType() == YangDataTypes.EMPTY) {
587 throw new DataModelException(" A leaf that is part of the key must not be the built-in " +
588 "type \"empty\".");
589 }
590 leafFound = true;
591 keyLeaves.add(leaf);
592 break;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530593 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530594 }
595 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530596
597 if (leafLists != null && !leafLists.isEmpty()) {
598 for (YangLeafList leafList : leafLists) {
599 if (key.equals(leafList.getName())) {
600 if (leafList.getDataType().getDataType() == YangDataTypes.EMPTY) {
601 throw new DataModelException(" A leaf-list that is part of the key" +
602 " must not be the built-in type \"empty\".");
603 }
604 leafFound = true;
605 keyLeafLists.add(leafList);
606 break;
607 }
608 }
609 }
610
janani bdd1314f2016-05-19 17:39:50 +0530611 if (!leafFound && !isUsesPresentInList() && !isListPresentInGrouping()) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530612 throw new DataModelException("An identifier, in key, must refer to a child leaf of the list");
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530613 }
614 leafFound = false;
615 }
616
Vinod Kumar S71cba682016-02-25 15:52:16 +0530617 /*
618 * All key leafs in a list MUST have the same value for their "config"
619 * as the list itself.
620 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530621 for (YangLeaf keyLeaf : keyLeaves) {
622 if (isConfig != keyLeaf.isConfig()) {
623 throw new DataModelException("All key leafs in a list must have the same value for their" +
624 " \"config\" as the list itself.");
625 }
626 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530627
Vidyashree Rama1db15562016-05-17 16:16:15 +0530628 /*
Vinod Kumar S71cba682016-02-25 15:52:16 +0530629 * All key leafs in a list MUST have the same value for their "config"
630 * as the list itself.
631 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530632 for (YangLeafList keyLeafList : keyLeafLists) {
633 if (isConfig() != keyLeafList.isConfig()) {
634 throw new DataModelException("All key leaf-lists in a list must have the same value for their" +
635 " \"config\" as the list itself.");
636 }
637 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530638 }
639
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530640 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530641 public void detectCollidingChild(String identifierName, YangConstructType dataType)
642 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530643 // Asks helper to detect colliding child.
644 detectCollidingChildUtil(identifierName, dataType, this);
645 }
646
647 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530648 public void detectSelfCollision(String identifierName, YangConstructType dataType)
649 throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530650 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530651 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as list \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530652 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530653 }
654 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530655
656 private boolean isUsesPresentInList() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530657 YangNode node = getChild();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530658 while (node != null) {
659 if (node instanceof YangUses) {
660 return true;
661 }
662 node = node.getNextSibling();
663 }
664 return false;
janani bdd1314f2016-05-19 17:39:50 +0530665 // TODO When grouping linking is done this method has to be modified.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530666 }
667
janani bdd1314f2016-05-19 17:39:50 +0530668 private boolean isListPresentInGrouping() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530669 YangNode node = getParent();
janani bdd1314f2016-05-19 17:39:50 +0530670 while (node != null) {
671 if (node instanceof YangGrouping) {
672 return true;
673 }
674 node = node.getParent();
675 }
676 return false;
677 // TODO When grouping linking is done this method has to be modified.
678 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530679
680 @Override
681 public List<YangIfFeature> getIfFeatureList() {
682 return ifFeatureList;
683 }
684
685 @Override
686 public void addIfFeatureList(YangIfFeature ifFeature) {
687 if (getIfFeatureList() == null) {
688 setIfFeatureList(new LinkedList<>());
689 }
690 getIfFeatureList().add(ifFeature);
691 }
692
693 @Override
694 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
695 this.ifFeatureList = ifFeatureList;
696 }
697
698 @Override
699 public List<YangMust> getListOfMust() {
700 return mustConstraintList;
701 }
702
703 @Override
704 public void setListOfMust(List<YangMust> mustConstraintList) {
705 this.mustConstraintList = mustConstraintList;
706 }
707
708 @Override
709 public void addMust(YangMust must) {
710 if (getListOfMust() == null) {
711 setListOfMust(new LinkedList<>());
712 }
713 getListOfMust().add(must);
714 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530715}