blob: 632c5b7e2f86f2aa844922d359b86a0feda80106 [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 |
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
Bharat saraswal96dfef02016-06-16 00:29:12 +053074 private static final long serialVersionUID = 806201609L;
75
Vinod Kumar S67e7be62016-02-11 20:13:28 +053076 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053077 * Name of the YANG list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053078 */
79 private String name;
80
81 /**
82 * If list maintains config data.
83 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053084 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053085
86 /**
87 * Description of list.
88 */
89 private String description;
90
91 /**
92 * Reference RFC 6020.
93 *
94 * The "key" statement, which MUST be present if the list represents
95 * configuration, and MAY be present otherwise, takes as an argument a
96 * string that specifies a space-separated list of leaf identifiers of this
97 * list. A leaf identifier MUST NOT appear more than once in the key. Each
98 * such leaf identifier MUST refer to a child leaf of the list. The leafs
99 * can be defined directly in sub-statements to the list, or in groupings
100 * used in the list.
101 *
102 * The combined values of all the leafs specified in the key are used to
103 * uniquely identify a list entry. All key leafs MUST be given values when a
104 * list entry is created. Thus, any default values in the key leafs or their
105 * types are ignored. It also implies that any mandatory statement in the
106 * key leafs are ignored.
107 *
108 * A leaf that is part of the key can be of any built-in or derived type,
109 * except it MUST NOT be the built-in type "empty".
110 *
111 * All key leafs in a list MUST have the same value for their "config" as
112 * the list itself.
113 *
114 * List of key leaf names.
115 */
116 private List<String> keyList;
117
118 /**
119 * List of leaves.
120 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530121 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530122
123 /**
124 * List of leaf-lists.
125 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530126 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530127
128 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530129 * Reference RFC 6020.
130 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530131 * The "max-elements" statement, which is optional, takes as an argument a
132 * positive integer or the string "unbounded", which puts a constraint on
133 * valid list entries. A valid leaf-list or list always has at most
134 * max-elements entries.
135 *
136 * If no "max-elements" statement is present, it defaults to "unbounded".
137 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530138 private int maxElements = Integer.MAX_VALUE;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530139
140 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530141 * Reference RFC 6020.
142 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530143 * The "min-elements" statement, which is optional, takes as an argument a
144 * non-negative integer that puts a constraint on valid list entries. A
145 * valid leaf-list or list MUST have at least min-elements entries.
146 *
147 * If no "min-elements" statement is present, it defaults to zero.
148 *
149 * The behavior of the constraint depends on the type of the leaf-list's or
150 * list's closest ancestor node in the schema tree that is not a non-
151 * presence container:
152 *
153 * o If this ancestor is a case node, the constraint is enforced if any
154 * other node from the case exists.
155 *
156 * o Otherwise, it is enforced if the ancestor node exists.
157 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530158 private int minElements = 0;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530159
160 /**
161 * reference.
162 */
163 private String reference;
164
165 /**
166 * Status of the node.
167 */
168
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530169 private YangStatusType status = YangStatusType.CURRENT;
170
171 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530172 * Creates a YANG list object.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530173 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530174 public YangList() {
175 super(YangNodeType.LIST_NODE);
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530176 }
177
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530178 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530179 * Returns the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530180 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530181 * @return YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530182 */
183 @Override
184 public String getName() {
185 return name;
186 }
187
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530188 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530189 * Sets the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530190 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530191 * @param name YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530192 */
193 @Override
194 public void setName(String name) {
195 this.name = name;
196 }
197
198 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530199 * Returns the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530200 *
201 * @return the isConfig
202 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530203 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530204 return isConfig;
205 }
206
207 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530208 * Sets the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530209 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530210 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530211 */
212 public void setConfig(boolean isCfg) {
213 isConfig = isCfg;
214 }
215
216 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530217 * Returns the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530218 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530219 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530220 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530221 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530222 public String getDescription() {
223 return description;
224 }
225
226 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530227 * Sets the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530228 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530229 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530230 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530231 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530232 public void setDescription(String description) {
233 this.description = description;
234 }
235
236 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530237 * Returns the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530238 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530239 * @return the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530240 */
241 public List<String> getKeyList() {
242 return keyList;
243 }
244
245 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530246 * Sets the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530247 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530248 * @param keyList the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530249 */
250 private void setKeyList(List<String> keyList) {
251 this.keyList = keyList;
252 }
253
254 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530255 * Adds a key field name.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530256 *
257 * @param key key field name.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530258 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530259 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530260 public void addKey(String key)
261 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530262 if (getKeyList() == null) {
263 setKeyList(new LinkedList<String>());
264 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530265
266 if (getKeyList().contains(key)) {
267 throw new DataModelException("A leaf identifier must not appear more than once in the\n" +
268 " key");
269 }
270
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530271 getKeyList().add(key);
272 }
273
274 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530275 * Returns the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530276 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530277 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530278 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530279 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530280 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530281 return listOfLeaf;
282 }
283
284 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530285 * Sets the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530286 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530287 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530288 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530289 @Override
290 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530291 listOfLeaf = leafsList;
292 }
293
294 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530295 * Adds a leaf.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530296 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530297 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530298 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530299 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530300 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530301 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530302 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530303 }
304
305 getListOfLeaf().add(leaf);
306 }
307
308 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530309 * Returns the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530310 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530311 * @return the list of leaf-list
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<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530315 return listOfLeafList;
316 }
317
318 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530319 * Sets the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530320 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530321 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530322 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530323 @Override
324 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530325 this.listOfLeafList = listOfLeafList;
326 }
327
328 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530329 * Adds a leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530330 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530331 * @param leafList the leaf-list 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 addLeafList(YangLeafList leafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530335 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530336 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530337 }
338
339 getListOfLeafList().add(leafList);
340 }
341
342 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530343 * Returns the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530344 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530345 * @return the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530346 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530347 public int getMaxElements() {
348 return maxElements;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530349 }
350
351 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530352 * Sets the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530353 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530354 * @param max the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530355 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530356 public void setMaxElements(int max) {
357 maxElements = max;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530358 }
359
360 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530361 * Returns the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530362 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530363 * @return the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530364 */
365 public int getMinElements() {
366 return minElements;
367 }
368
369 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530370 * Sets the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530371 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530372 * @param minElements the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530373 */
374 public void setMinElements(int minElements) {
375 this.minElements = minElements;
376 }
377
378 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530379 * Returns the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530380 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530381 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530382 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530383 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530384 public String getReference() {
385 return reference;
386 }
387
388 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530389 * Sets the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530390 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530391 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530392 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530393 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530394 public void setReference(String reference) {
395 this.reference = reference;
396 }
397
398 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530399 * Returns the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530400 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530401 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530402 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530403 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530404 public YangStatusType getStatus() {
405 return status;
406 }
407
408 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530409 * Sets the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530410 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530411 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530412 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530413 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530414 public void setStatus(YangStatusType status) {
415 this.status = status;
416 }
417
418 /**
419 * Returns the type of the parsed data.
420 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530421 * @return returns LIST_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530422 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530423 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530424 public YangConstructType getYangConstructType() {
425 return YangConstructType.LIST_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530426 }
427
428 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530429 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530430 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530431 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530432 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530433 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530434 public void validateDataOnEntry()
435 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530436 // TODO auto-generated method stub, to be implemented by parser
437 }
438
439 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530440 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530441 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530442 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530443 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530444 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530445 public void validateDataOnExit()
446 throws DataModelException {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530447 List<String> keys = getKeyList();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530448 List<YangLeaf> leaves = getListOfLeaf();
449 List<YangLeafList> leafLists = getListOfLeafList();
450
451 setDefaultConfigValueToChild(leaves, leafLists);
452 validateConfig(leaves, leafLists);
453
454 /* A list must have atleast one key leaf if config is true */
janani bdd1314f2016-05-19 17:39:50 +0530455 if (isConfig && (keys == null || leaves == null && leafLists == null) && !isUsesPresentInList()
456 && !isListPresentInGrouping()) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530457 throw new DataModelException("A list must have atleast one key leaf if config is true;");
458 } else if (keys != null) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530459 validateKey(leaves, leafLists, keys);
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530460 }
461 }
462
463 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530464 * Sets the config's value to all leaf if leaf's config statement is not
465 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530466 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530467 * @param leaves list of leaf attributes of YANG list
468 * @param leafLists list of leaf-list attributes of YANG list
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530469 */
470 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
471
Vinod Kumar S71cba682016-02-25 15:52:16 +0530472 /*
473 * If "config" is not specified, the default is the same as the parent
474 * schema node's "config" value.
475 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530476 if (leaves != null) {
477 for (YangLeaf leaf : leaves) {
478 if (leaf.isConfig() == null) {
479 leaf.setConfig(isConfig);
480 }
481 }
482 }
483
Vinod Kumar S71cba682016-02-25 15:52:16 +0530484 /*
485 * If "config" is not specified, the default is the same as the parent
486 * schema node's "config" value.
487 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530488 if (leafLists != null) {
489 for (YangLeafList leafList : leafLists) {
490 if (leafList.isConfig() == null) {
491 leafList.setConfig(isConfig);
492 }
493 }
494 }
495 }
496
497 /**
498 * Validates config statement of YANG list.
499 *
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
502 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530503 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530504 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists)
505 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530506
Vinod Kumar S71cba682016-02-25 15:52:16 +0530507 /*
508 * If a node has "config" set to "false", no node underneath it can have
509 * "config" set to "true".
510 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530511 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530512 for (YangLeaf leaf : leaves) {
513 if (leaf.isConfig()) {
514 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
515 "it can have \"config\" set to \"true\".");
516 }
517 }
518 }
519
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530520 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530521 for (YangLeafList leafList : leafLists) {
522 if (leafList.isConfig()) {
523 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
524 "it can have \"config\" set to \"true\".");
525 }
526 }
527 }
528 }
529
530 /**
531 * Validates key statement of list.
532 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530533 * @param leaves list of leaf attributes of list
Vidyashree Rama1db15562016-05-17 16:16:15 +0530534 * @param leafLists list of leaf-list attributes of list
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530535 * @param keys list of key attributes of 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 validateKey(List<YangLeaf> leaves, List<YangLeafList> leafLists, List<String> keys)
539 throws
Vidyashree Rama1db15562016-05-17 16:16:15 +0530540 DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530541 boolean leafFound = false;
542 List<YangLeaf> keyLeaves = new LinkedList<>();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530543 List<YangLeafList> keyLeafLists = new LinkedList<>();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530544
Vinod Kumar S71cba682016-02-25 15:52:16 +0530545 /*
546 * 1. Leaf identifier must refer to a child leaf of the list 2. A leaf
547 * that is part of the key must not be the built-in type "empty".
548 */
549 for (String key : keys) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530550 if (leaves != null && !leaves.isEmpty()) {
551 for (YangLeaf leaf : leaves) {
552 if (key.equals(leaf.getName())) {
553 if (leaf.getDataType().getDataType() == YangDataTypes.EMPTY) {
554 throw new DataModelException(" A leaf that is part of the key must not be the built-in " +
555 "type \"empty\".");
556 }
557 leafFound = true;
558 keyLeaves.add(leaf);
559 break;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530560 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530561 }
562 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530563
564 if (leafLists != null && !leafLists.isEmpty()) {
565 for (YangLeafList leafList : leafLists) {
566 if (key.equals(leafList.getName())) {
567 if (leafList.getDataType().getDataType() == YangDataTypes.EMPTY) {
568 throw new DataModelException(" A leaf-list that is part of the key" +
569 " must not be the built-in type \"empty\".");
570 }
571 leafFound = true;
572 keyLeafLists.add(leafList);
573 break;
574 }
575 }
576 }
577
janani bdd1314f2016-05-19 17:39:50 +0530578 if (!leafFound && !isUsesPresentInList() && !isListPresentInGrouping()) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530579 throw new DataModelException("An identifier, in key, must refer to a child leaf of the list");
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530580 }
581 leafFound = false;
582 }
583
Vinod Kumar S71cba682016-02-25 15:52:16 +0530584 /*
585 * All key leafs in a list MUST have the same value for their "config"
586 * as the list itself.
587 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530588 for (YangLeaf keyLeaf : keyLeaves) {
589 if (isConfig != keyLeaf.isConfig()) {
590 throw new DataModelException("All key leafs in a list must have the same value for their" +
591 " \"config\" as the list itself.");
592 }
593 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530594
Vidyashree Rama1db15562016-05-17 16:16:15 +0530595 /*
Vinod Kumar S71cba682016-02-25 15:52:16 +0530596 * All key leafs in a list MUST have the same value for their "config"
597 * as the list itself.
598 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530599 for (YangLeafList keyLeafList : keyLeafLists) {
600 if (isConfig() != keyLeafList.isConfig()) {
601 throw new DataModelException("All key leaf-lists in a list must have the same value for their" +
602 " \"config\" as the list itself.");
603 }
604 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530605 }
606
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530607 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530608 public void detectCollidingChild(String identifierName, YangConstructType dataType)
609 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530610 // Asks helper to detect colliding child.
611 detectCollidingChildUtil(identifierName, dataType, this);
612 }
613
614 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530615 public void detectSelfCollision(String identifierName, YangConstructType dataType)
616 throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530617 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530618 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as list \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530619 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530620 }
621 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530622
623 private boolean isUsesPresentInList() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530624 YangNode node = getChild();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530625 while (node != null) {
626 if (node instanceof YangUses) {
627 return true;
628 }
629 node = node.getNextSibling();
630 }
631 return false;
janani bdd1314f2016-05-19 17:39:50 +0530632 // TODO When grouping linking is done this method has to be modified.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530633 }
634
janani bdd1314f2016-05-19 17:39:50 +0530635 private boolean isListPresentInGrouping() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530636 YangNode node = getParent();
janani bdd1314f2016-05-19 17:39:50 +0530637 while (node != null) {
638 if (node instanceof YangGrouping) {
639 return true;
640 }
641 node = node.getParent();
642 }
643 return false;
644 // TODO When grouping linking is done this method has to be modified.
645 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530646}