blob: 2d285252ae30198ac2692230db260034a53dc1f4 [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
Bharat saraswalb1170bd2016-07-14 13:26:18 +053019import java.util.ArrayList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053020import java.util.LinkedList;
21import java.util.List;
22
Vinod Kumar S38046502016-03-23 15:30:27 +053023import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053024import org.onosproject.yangutils.datamodel.utils.Parsable;
25import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053026import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Vinod Kumar S38046502016-03-23 15:30:27 +053027
28import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
29
30/*
Vinod Kumar S67e7be62016-02-11 20:13:28 +053031 * The "list" statement is used to define an interior data node in the
32 * schema tree. A list node may exist in multiple instances in the data
33 * tree. Each such instance is known as a list entry. The "list"
34 * statement takes one argument, which is an identifier, followed by a
35 * block of sub-statements that holds detailed list information.
36 *
37 * A list entry is uniquely identified by the values of the list's keys,
38 * if defined.
39 *
40 * The list's sub-statements
41 *
42 * +--------------+---------+-------------+------------------+
43 * | substatement | section | cardinality |data model mapping|
44 * +--------------+---------+-------------+------------------+
45 * | anyxml | 7.10 | 0..n |-not supported |
46 * | choice | 7.9 | 0..n |-child nodes |
47 * | config | 7.19.1 | 0..1 |-boolean |
48 * | container | 7.5 | 0..n |-child nodes |
49 * | description | 7.19.3 | 0..1 |-string |
50 * | grouping | 7.11 | 0..n |-child nodes |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053051 * | if-feature | 7.18.2 | 0..n |-YangIfFeature |
Vinod Kumar S67e7be62016-02-11 20:13:28 +053052 * | key | 7.8.2 | 0..1 |-String list |
53 * | leaf | 7.6 | 0..n |-YangLeaf |
54 * | leaf-list | 7.7 | 0..n |-YangLeafList |
55 * | list | 7.8 | 0..n |-child nodes |
56 * | max-elements | 7.7.4 | 0..1 |-int |
57 * | min-elements | 7.7.3 | 0..1 |-int |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053058 * | must | 7.5.3 | 0..n |-YangMust |
Vinod Kumar S67e7be62016-02-11 20:13:28 +053059 * | ordered-by | 7.7.5 | 0..1 |-TODO |
60 * | reference | 7.19.4 | 0..1 |-string |
61 * | status | 7.19.2 | 0..1 |-YangStatus |
62 * | typedef | 7.3 | 0..n |-child nodes |
63 * | unique | 7.8.3 | 0..n |-TODO |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053064 * | uses | 7.12 | 0..n |-child nodes |
65 * | when | 7.19.5 | 0..1 |-YangWhen |
Vinod Kumar S67e7be62016-02-11 20:13:28 +053066 * +--------------+---------+-------------+------------------+
67 */
68
69/**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Represents list data represented in YANG.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053071 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053072public class YangList
73 extends YangNode
Bharat saraswalb1170bd2016-07-14 13:26:18 +053074 implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector,
75 YangAugmentableNode, YangMustHolder, YangIfFeatureHolder, YangDataNode {
Vinod Kumar S67e7be62016-02-11 20:13:28 +053076
Bharat saraswal96dfef02016-06-16 00:29:12 +053077 private static final long serialVersionUID = 806201609L;
78
Vinod Kumar S67e7be62016-02-11 20:13:28 +053079 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053080 * Name of the YANG list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053081 */
82 private String name;
83
84 /**
85 * If list maintains config data.
86 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053087 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053088
89 /**
90 * Description of list.
91 */
92 private String description;
93
94 /**
95 * Reference RFC 6020.
96 *
97 * The "key" statement, which MUST be present if the list represents
98 * configuration, and MAY be present otherwise, takes as an argument a
99 * string that specifies a space-separated list of leaf identifiers of this
100 * list. A leaf identifier MUST NOT appear more than once in the key. Each
101 * such leaf identifier MUST refer to a child leaf of the list. The leafs
102 * can be defined directly in sub-statements to the list, or in groupings
103 * used in the list.
104 *
105 * The combined values of all the leafs specified in the key are used to
106 * uniquely identify a list entry. All key leafs MUST be given values when a
107 * list entry is created. Thus, any default values in the key leafs or their
108 * types are ignored. It also implies that any mandatory statement in the
109 * key leafs are ignored.
110 *
111 * A leaf that is part of the key can be of any built-in or derived type,
112 * except it MUST NOT be the built-in type "empty".
113 *
114 * All key leafs in a list MUST have the same value for their "config" as
115 * the list itself.
116 *
117 * List of key leaf names.
118 */
119 private List<String> keyList;
120
121 /**
122 * List of leaves.
123 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530124 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530125
126 /**
127 * List of leaf-lists.
128 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530129 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530130
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530131 private List<YangAugmentedInfo> yangAugmentedInfo = new ArrayList<>();
132
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530133 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530134 * Reference RFC 6020.
135 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530136 * The "max-elements" statement, which is optional, takes as an argument a
137 * positive integer or the string "unbounded", which puts a constraint on
138 * valid list entries. A valid leaf-list or list always has at most
139 * max-elements entries.
140 *
141 * If no "max-elements" statement is present, it defaults to "unbounded".
142 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530143 private int maxElements = Integer.MAX_VALUE;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530144
145 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530146 * Reference RFC 6020.
147 *
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530148 * The "min-elements" statement, which is optional, takes as an argument a
149 * non-negative integer that puts a constraint on valid list entries. A
150 * valid leaf-list or list MUST have at least min-elements entries.
151 *
152 * If no "min-elements" statement is present, it defaults to zero.
153 *
154 * The behavior of the constraint depends on the type of the leaf-list's or
155 * list's closest ancestor node in the schema tree that is not a non-
156 * presence container:
157 *
158 * o If this ancestor is a case node, the constraint is enforced if any
159 * other node from the case exists.
160 *
161 * o Otherwise, it is enforced if the ancestor node exists.
162 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530163 private int minElements = 0;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530164
165 /**
166 * reference.
167 */
168 private String reference;
169
170 /**
171 * Status of the node.
172 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530173 private YangStatusType status = YangStatusType.CURRENT;
174
175 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530176 * List of must statement constraints.
177 */
178 private List<YangMust> mustConstraintList;
179
180 /**
181 * When data of the node.
182 */
183 private YangWhen when;
184
185 /**
186 * List of if-feature.
187 */
188 private List<YangIfFeature> ifFeatureList;
189
190 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530191 * Creates a YANG list object.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530192 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530193 public YangList() {
194 super(YangNodeType.LIST_NODE);
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530195 }
196
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530197 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530198 * Returns the when.
199 *
200 * @return the when
201 */
202 public YangWhen getWhen() {
203 return when;
204 }
205
206 /**
207 * Sets the when.
208 *
209 * @param when the when to set
210 */
211 public void setWhen(YangWhen when) {
212 this.when = when;
213 }
214
215 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530216 * Returns the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530217 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530218 * @return YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530219 */
220 @Override
221 public String getName() {
222 return name;
223 }
224
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530225 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530226 * Sets the YANG list name.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530227 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530228 * @param name YANG list name
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530229 */
230 @Override
231 public void setName(String name) {
232 this.name = name;
233 }
234
235 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530236 * Returns the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530237 *
238 * @return the isConfig
239 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530240 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530241 return isConfig;
242 }
243
244 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530245 * Sets the config flag.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530246 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530247 * @param isCfg the config flag
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530248 */
249 public void setConfig(boolean isCfg) {
250 isConfig = isCfg;
251 }
252
253 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530254 * Returns the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530255 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530256 * @return the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530257 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530258 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530259 public String getDescription() {
260 return description;
261 }
262
263 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530264 * Sets the description.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530265 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530266 * @param description set the description
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530267 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530268 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530269 public void setDescription(String description) {
270 this.description = description;
271 }
272
273 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530274 * Returns the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530275 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530276 * @return the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530277 */
278 public List<String> getKeyList() {
279 return keyList;
280 }
281
282 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530283 * Sets the list of key field names.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530284 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530285 * @param keyList the list of key field names
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530286 */
287 private void setKeyList(List<String> keyList) {
288 this.keyList = keyList;
289 }
290
291 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530292 * Adds a key field name.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530293 *
294 * @param key key field name.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530295 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530296 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530297 public void addKey(String key)
298 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530299 if (getKeyList() == null) {
300 setKeyList(new LinkedList<String>());
301 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530302
303 if (getKeyList().contains(key)) {
304 throw new DataModelException("A leaf identifier must not appear more than once in the\n" +
305 " key");
306 }
307
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530308 getKeyList().add(key);
309 }
310
311 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530312 * Returns the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530313 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530314 * @return the list of leaves
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530315 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530316 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530317 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530318 return listOfLeaf;
319 }
320
321 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530322 * Sets the list of leaves.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530323 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530324 * @param leafsList the list of leaf to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530325 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530326 @Override
327 public void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530328 listOfLeaf = leafsList;
329 }
330
331 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530332 * Adds a leaf.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530333 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530334 * @param leaf the leaf to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530335 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530336 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530337 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530338 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530339 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530340 }
341
342 getListOfLeaf().add(leaf);
343 }
344
345 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530346 * Returns the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530347 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530348 * @return the list of leaf-list
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530349 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530350 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530351 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530352 return listOfLeafList;
353 }
354
355 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530356 * Sets the list of leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530357 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530358 * @param listOfLeafList the list of leaf-list to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530359 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530360 @Override
361 public void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530362 this.listOfLeafList = listOfLeafList;
363 }
364
365 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530366 * Adds a leaf-list.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530367 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530368 * @param leafList the leaf-list to be added
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530369 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530370 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530371 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530372 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530373 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530374 }
375
376 getListOfLeafList().add(leafList);
377 }
378
379 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530380 * Returns the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530381 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530382 * @return the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530383 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530384 public int getMaxElements() {
385 return maxElements;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530386 }
387
388 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530389 * Sets the max elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530390 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530391 * @param max the max elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530392 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530393 public void setMaxElements(int max) {
394 maxElements = max;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530395 }
396
397 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530398 * Returns the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530399 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530400 * @return the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530401 */
402 public int getMinElements() {
403 return minElements;
404 }
405
406 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530407 * Sets the minimum elements.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530408 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530409 * @param minElements the minimum elements
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530410 */
411 public void setMinElements(int minElements) {
412 this.minElements = minElements;
413 }
414
415 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530416 * Returns the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530417 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530418 * @return the reference
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530419 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530420 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530421 public String getReference() {
422 return reference;
423 }
424
425 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530426 * Sets the textual reference.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530427 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530428 * @param reference the reference to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530429 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530430 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530431 public void setReference(String reference) {
432 this.reference = reference;
433 }
434
435 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530436 * Returns the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530437 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530438 * @return the status
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530439 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530440 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530441 public YangStatusType getStatus() {
442 return status;
443 }
444
445 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530446 * Sets the status.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530447 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530448 * @param status the status to set
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530449 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530450 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530451 public void setStatus(YangStatusType status) {
452 this.status = status;
453 }
454
455 /**
456 * Returns the type of the parsed data.
457 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530458 * @return returns LIST_DATA
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530459 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530460 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530461 public YangConstructType getYangConstructType() {
462 return YangConstructType.LIST_DATA;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530463 }
464
465 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530466 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530467 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530468 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530469 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530470 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530471 public void validateDataOnEntry()
472 throws DataModelException {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530473 // TODO auto-generated method stub, to be implemented by parser
474 }
475
476 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530477 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530478 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530479 * @throws DataModelException a violation of data model rules
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530480 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530481 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530482 public void validateDataOnExit()
483 throws DataModelException {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530484 List<String> keys = getKeyList();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530485 List<YangLeaf> leaves = getListOfLeaf();
486 List<YangLeafList> leafLists = getListOfLeafList();
487
488 setDefaultConfigValueToChild(leaves, leafLists);
489 validateConfig(leaves, leafLists);
490
491 /* A list must have atleast one key leaf if config is true */
janani bdd1314f2016-05-19 17:39:50 +0530492 if (isConfig && (keys == null || leaves == null && leafLists == null) && !isUsesPresentInList()
493 && !isListPresentInGrouping()) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530494 throw new DataModelException("A list must have atleast one key leaf if config is true;");
495 } else if (keys != null) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530496 validateKey(leaves, leafLists, keys);
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530497 }
498 }
499
500 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530501 * Sets the config's value to all leaf if leaf's config statement is not
502 * specified.
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530503 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530504 * @param leaves list of leaf attributes of YANG list
505 * @param leafLists list of leaf-list attributes of YANG list
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530506 */
507 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
508
Vinod Kumar S71cba682016-02-25 15:52:16 +0530509 /*
510 * If "config" is not specified, the default is the same as the parent
511 * schema node's "config" value.
512 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530513 if (leaves != null) {
514 for (YangLeaf leaf : leaves) {
515 if (leaf.isConfig() == null) {
516 leaf.setConfig(isConfig);
517 }
518 }
519 }
520
Vinod Kumar S71cba682016-02-25 15:52:16 +0530521 /*
522 * If "config" is not specified, the default is the same as the parent
523 * schema node's "config" value.
524 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530525 if (leafLists != null) {
526 for (YangLeafList leafList : leafLists) {
527 if (leafList.isConfig() == null) {
528 leafList.setConfig(isConfig);
529 }
530 }
531 }
532 }
533
534 /**
535 * Validates config statement of YANG list.
536 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530537 * @param leaves list of leaf attributes of YANG list
538 * @param leafLists list of leaf-list attributes of YANG list
539 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530540 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530541 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists)
542 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530543
Vinod Kumar S71cba682016-02-25 15:52:16 +0530544 /*
545 * If a node has "config" set to "false", no node underneath it can have
546 * "config" set to "true".
547 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530548 if (!isConfig && leaves != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530549 for (YangLeaf leaf : leaves) {
550 if (leaf.isConfig()) {
551 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
552 "it can have \"config\" set to \"true\".");
553 }
554 }
555 }
556
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530557 if (!isConfig && leafLists != null) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530558 for (YangLeafList leafList : leafLists) {
559 if (leafList.isConfig()) {
560 throw new DataModelException("If a list has \"config\" set to \"false\", no node underneath " +
561 "it can have \"config\" set to \"true\".");
562 }
563 }
564 }
565 }
566
567 /**
568 * Validates key statement of list.
569 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530570 * @param leaves list of leaf attributes of list
Vidyashree Rama1db15562016-05-17 16:16:15 +0530571 * @param leafLists list of leaf-list attributes of list
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530572 * @param keys list of key attributes of list
573 * @throws DataModelException a violation of data model rules
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530574 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530575 private void validateKey(List<YangLeaf> leaves, List<YangLeafList> leafLists, List<String> keys)
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530576 throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530577 boolean leafFound = false;
578 List<YangLeaf> keyLeaves = new LinkedList<>();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530579 List<YangLeafList> keyLeafLists = new LinkedList<>();
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530580
Vinod Kumar S71cba682016-02-25 15:52:16 +0530581 /*
582 * 1. Leaf identifier must refer to a child leaf of the list 2. A leaf
583 * that is part of the key must not be the built-in type "empty".
584 */
585 for (String key : keys) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530586 if (leaves != null && !leaves.isEmpty()) {
587 for (YangLeaf leaf : leaves) {
588 if (key.equals(leaf.getName())) {
589 if (leaf.getDataType().getDataType() == YangDataTypes.EMPTY) {
590 throw new DataModelException(" A leaf that is part of the key must not be the built-in " +
591 "type \"empty\".");
592 }
593 leafFound = true;
594 keyLeaves.add(leaf);
595 break;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530596 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530597 }
598 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530599
600 if (leafLists != null && !leafLists.isEmpty()) {
601 for (YangLeafList leafList : leafLists) {
602 if (key.equals(leafList.getName())) {
603 if (leafList.getDataType().getDataType() == YangDataTypes.EMPTY) {
604 throw new DataModelException(" A leaf-list that is part of the key" +
605 " must not be the built-in type \"empty\".");
606 }
607 leafFound = true;
608 keyLeafLists.add(leafList);
609 break;
610 }
611 }
612 }
613
janani bdd1314f2016-05-19 17:39:50 +0530614 if (!leafFound && !isUsesPresentInList() && !isListPresentInGrouping()) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530615 throw new DataModelException("An identifier, in key, must refer to a child leaf of the list");
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530616 }
617 leafFound = false;
618 }
619
Vinod Kumar S71cba682016-02-25 15:52:16 +0530620 /*
621 * All key leafs in a list MUST have the same value for their "config"
622 * as the list itself.
623 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530624 for (YangLeaf keyLeaf : keyLeaves) {
625 if (isConfig != keyLeaf.isConfig()) {
626 throw new DataModelException("All key leafs in a list must have the same value for their" +
627 " \"config\" as the list itself.");
628 }
629 }
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530630
Vidyashree Rama1db15562016-05-17 16:16:15 +0530631 /*
Vinod Kumar S71cba682016-02-25 15:52:16 +0530632 * All key leafs in a list MUST have the same value for their "config"
633 * as the list itself.
634 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530635 for (YangLeafList keyLeafList : keyLeafLists) {
636 if (isConfig() != keyLeafList.isConfig()) {
637 throw new DataModelException("All key leaf-lists in a list must have the same value for their" +
638 " \"config\" as the list itself.");
639 }
640 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530641 }
642
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530643 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530644 public void detectCollidingChild(String identifierName, YangConstructType dataType)
645 throws DataModelException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530646 // Asks helper to detect colliding child.
647 detectCollidingChildUtil(identifierName, dataType, this);
648 }
649
650 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530651 public void detectSelfCollision(String identifierName, YangConstructType dataType)
652 throws DataModelException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530653 if (getName().equals(identifierName)) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530654 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as list \"" +
Vinod Kumar S38046502016-03-23 15:30:27 +0530655 getName() + "\"");
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530656 }
657 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530658
659 private boolean isUsesPresentInList() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530660 YangNode node = getChild();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530661 while (node != null) {
662 if (node instanceof YangUses) {
663 return true;
664 }
665 node = node.getNextSibling();
666 }
667 return false;
janani bdd1314f2016-05-19 17:39:50 +0530668 // TODO When grouping linking is done this method has to be modified.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530669 }
670
janani bdd1314f2016-05-19 17:39:50 +0530671 private boolean isListPresentInGrouping() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530672 YangNode node = getParent();
janani bdd1314f2016-05-19 17:39:50 +0530673 while (node != null) {
674 if (node instanceof YangGrouping) {
675 return true;
676 }
677 node = node.getParent();
678 }
679 return false;
680 // TODO When grouping linking is done this method has to be modified.
681 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530682
683 @Override
684 public List<YangIfFeature> getIfFeatureList() {
685 return ifFeatureList;
686 }
687
688 @Override
689 public void addIfFeatureList(YangIfFeature ifFeature) {
690 if (getIfFeatureList() == null) {
691 setIfFeatureList(new LinkedList<>());
692 }
693 getIfFeatureList().add(ifFeature);
694 }
695
696 @Override
697 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
698 this.ifFeatureList = ifFeatureList;
699 }
700
701 @Override
702 public List<YangMust> getListOfMust() {
703 return mustConstraintList;
704 }
705
706 @Override
707 public void setListOfMust(List<YangMust> mustConstraintList) {
708 this.mustConstraintList = mustConstraintList;
709 }
710
711 @Override
712 public void addMust(YangMust must) {
713 if (getListOfMust() == null) {
714 setListOfMust(new LinkedList<>());
715 }
716 getListOfMust().add(must);
717 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530718
719 @Override
720 public void addAugmentation(YangAugmentedInfo augmentInfo) {
721 yangAugmentedInfo.add(augmentInfo);
722 }
723
724 @Override
725 public void removeAugmentation(YangAugmentedInfo augmentInfo) {
726 yangAugmentedInfo.remove(augmentInfo);
727 }
728
729 @Override
730 public List<YangAugmentedInfo> getAugmentedInfoList() {
731 return yangAugmentedInfo;
732 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530733}