blob: 3060fd4965b7897bc0a373fcca52e29fafc5ba4b [file] [log] [blame]
Vinod Kumar S19f39c72016-02-09 20:12:31 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S19f39c72016-02-09 20:12:31 +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 saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053020import java.util.LinkedList;
21import java.util.List;
Vinod Kumar S19f39c72016-02-09 20:12:31 +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 S19f39c72016-02-09 20:12:31 +053025
26/*
27 * Reference:RFC 6020.
28 * The "leaf" statement is used to define a leaf node in the schema
29 * tree. It takes one argument, which is an identifier, followed by a
30 * block of sub-statements that holds detailed leaf information.
31 *
32 * A leaf node has a value, but no child nodes in the data tree.
33 * Conceptually, the value in the data tree is always in the canonical
34 * form.
35 *
36 * A leaf node exists in zero or one instances in the data tree.
37 *
38 * The "leaf" statement is used to define a scalar variable of a
39 * particular built-in or derived type.
40 *
41 * The leaf's sub-statements
42 *
43 * +--------------+---------+-------------+------------------+
44 * | substatement | section | cardinality |data model mapping|
45 * +--------------+---------+-------------+------------------+
46 * | config | 7.19.1 | 0..1 | - boolean |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053047 * | default | 7.6.4 | 0..1 | - string |
Vinod Kumar S19f39c72016-02-09 20:12:31 +053048 * | description | 7.19.3 | 0..1 | - string |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053049 * | if-feature | 7.18.2 | 0..n | - YangIfFeature |
Vinod Kumar S19f39c72016-02-09 20:12:31 +053050 * | mandatory | 7.6.5 | 0..1 | - boolean |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053051 * | must | 7.5.3 | 0..n | - YangMust |
Vinod Kumar S19f39c72016-02-09 20:12:31 +053052 * | reference | 7.19.4 | 0..1 | - string |
53 * | status | 7.19.2 | 0..1 | - YangStatus |
54 * | type | 7.6.3 | 1 | - YangType |
55 * | units | 7.3.3 | 0..1 | - String |
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053056 * | when | 7.19.5 | 0..1 | - YangWhen |
Vinod Kumar S19f39c72016-02-09 20:12:31 +053057 * +--------------+---------+-------------+------------------+
58 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053059
Vinod Kumar S19f39c72016-02-09 20:12:31 +053060/**
Bharat saraswald9822e92016-04-05 15:13:44 +053061 * Represents leaf data represented in YANG.
Vinod Kumar S19f39c72016-02-09 20:12:31 +053062 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053063public class YangLeaf
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053064 implements YangCommonInfo, Parsable, Cloneable, Serializable,
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053065 YangMustHolder, YangIfFeatureHolder, YangWhenHolder, YangDataNode {
Bharat saraswal96dfef02016-06-16 00:29:12 +053066
67 private static final long serialVersionUID = 806201635L;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053068
69 /**
70 * Name of leaf.
71 */
72 private String name;
73
74 /**
75 * If the leaf is a config parameter.
76 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053077 private Boolean isConfig;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053078
79 /**
80 * description of leaf.
81 */
82 private String description;
83
84 /**
85 * If mandatory leaf.
86 */
87 private boolean isMandatory;
88
89 /**
90 * The textual reference to this leaf.
91 */
92 private String reference;
93
94 /**
95 * Status of leaf in YANG definition.
96 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053097 private YangStatusType status = YangStatusType.CURRENT;
Vinod Kumar S19f39c72016-02-09 20:12:31 +053098
99 /**
100 * Textual units info.
101 */
102 private String units;
103
104 /**
105 * Data type of the leaf.
106 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530107 private YangType<?> dataType;
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530108
109 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530110 * Default value in string, needs to be converted to the target object,
111 * based on the type.
112 */
113 private String defaultValueInString;
114
115 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530116 * When data of the leaf.
117 */
118 private YangWhen when;
119
120 /**
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530121 * YANG Node in which the leaf is contained.
122 */
Bharat saraswal96dfef02016-06-16 00:29:12 +0530123 private transient YangLeavesHolder containedIn;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530124
125 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530126 * List of must statement constraints.
127 */
128 private List<YangMust> mustConstraintList;
129
130 /**
131 * List of if-feature.
132 */
133 private List<YangIfFeature> ifFeatureList;
134
135 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530136 * Creates a YANG leaf.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530137 */
138 public YangLeaf() {
139 }
140
141 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530142 * Returns the name of leaf.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530143 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530144 * @return the leaf name
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530145 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530146 public String getName() {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530147 return name;
148 }
149
150 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530151 * Sets the name of leaf.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530152 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530153 * @param leafName the leaf name to set
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530154 */
155 public void setLeafName(String leafName) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530156 name = leafName;
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530157 }
158
159 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530160 * Returns the config flag.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530161 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530162 * @return if config flag
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530163 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530164 public Boolean isConfig() {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530165 return isConfig;
166 }
167
168 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530169 * Sets the config flag.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530170 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530171 * @param isCfg the flag value to set
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530172 */
173 public void setConfig(boolean isCfg) {
174 isConfig = isCfg;
175 }
176
177 /**
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530178 * Returns the when.
179 *
180 * @return the when
181 */
182 @Override
183 public YangWhen getWhen() {
184 return when;
185 }
186
187 /**
188 * Sets the when.
189 *
190 * @param when the when to set
191 */
192 @Override
193 public void setWhen(YangWhen when) {
194 this.when = when;
195 }
196
197 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530198 * Returns the description.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530199 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530200 * @return the description
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530201 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530202 @Override
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530203 public String getDescription() {
204 return description;
205 }
206
207 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530208 * Sets the description.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530209 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530210 * @param description set the description
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530211 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530212 @Override
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530213 public void setDescription(String description) {
214 this.description = description;
215 }
216
217 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530218 * Returns if the leaf is mandatory.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530219 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530220 * @return if leaf is mandatory
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530221 */
222 public boolean isMandatory() {
223 return isMandatory;
224 }
225
226 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530227 * Sets if the leaf is mandatory.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530228 *
229 * @param isReq if the leaf is mandatory
230 */
231 public void setMandatory(boolean isReq) {
232 isMandatory = isReq;
233 }
234
235 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530236 * Returns the textual reference.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530237 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530238 * @return the reference
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530239 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530240 @Override
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530241 public String getReference() {
242 return reference;
243 }
244
245 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530246 * Sets the textual reference.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530247 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530248 * @param reference the reference to set
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530249 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530250 @Override
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530251 public void setReference(String reference) {
252 this.reference = reference;
253 }
254
255 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530256 * Returns the status.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530257 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530258 * @return the status
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530259 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530260 @Override
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530261 public YangStatusType getStatus() {
262 return status;
263 }
264
265 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530266 * Sets the status.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530267 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530268 * @param status the status to set
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530269 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530270 @Override
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530271 public void setStatus(YangStatusType status) {
272 this.status = status;
273 }
274
275 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530276 * Returns the units.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530277 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530278 * @return the units
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530279 */
280 public String getUnits() {
281 return units;
282 }
283
284 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530285 * Sets the units.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530286 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530287 * @param units the units to set
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530288 */
289 public void setUnits(String units) {
290 this.units = units;
291 }
292
293 /**
Vidyashree Rama1db15562016-05-17 16:16:15 +0530294 * Returns the default value.
295 *
296 * @return the default value
297 */
298 public String getDefaultValueInString() {
299 return defaultValueInString;
300 }
301
302 /**
303 * Sets the default value.
304 *
305 * @param defaultValueInString the default value
306 */
307 public void setDefaultValueInString(String defaultValueInString) {
308 this.defaultValueInString = defaultValueInString;
309 }
310
311 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530312 * Returns the data type.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530313 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530314 * @return the data type
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530315 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530316 public YangType<?> getDataType() {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530317 return dataType;
318 }
319
320 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530321 * Sets the data type.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530322 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530323 * @param dataType the data type to set
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530324 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530325 public void setDataType(YangType<?> dataType) {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530326 this.dataType = dataType;
327 }
328
329 /**
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530330 * Retrieves the YANG node in which the leaf is defined.
331 *
332 * @return the YANG node in which the leaf is defined
333 */
334 public YangLeavesHolder getContainedIn() {
335 return containedIn;
336 }
337
338 /**
339 * Assigns the YANG node in which the leaf is defined.
340 *
341 * @param containedIn the YANG node in which the leaf is defined
342 */
343 public void setContainedIn(YangLeavesHolder containedIn) {
344 this.containedIn = containedIn;
345 }
346
347 @Override
348 public YangLeaf clone()
349 throws CloneNotSupportedException {
350 return (YangLeaf) super.clone();
351 }
352
353 /**
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530354 * Returns the type of the parsed data.
355 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530356 * @return returns LEAF_DATA
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530357 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530358 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530359 public YangConstructType getYangConstructType() {
360 return YangConstructType.LEAF_DATA;
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530361 }
362
363 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530364 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530365 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530366 * @throws DataModelException a violation of data model rules
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530367 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530368 @Override
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530369 public void validateDataOnEntry()
370 throws DataModelException {
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530371 // TODO auto-generated method stub, to be implemented by parser
372
373 }
374
375 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530376 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530377 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530378 * @throws DataModelException a violation of data model rules
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530379 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530380 @Override
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530381 public void validateDataOnExit()
382 throws DataModelException {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530383 if (defaultValueInString != null && !defaultValueInString.isEmpty() && dataType != null) {
384 dataType.isValidValue(defaultValueInString);
385 }
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530386 }
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530387
388 @Override
389 public List<YangMust> getListOfMust() {
390 return mustConstraintList;
391 }
392
393 @Override
394 public void setListOfMust(List<YangMust> mustConstraintList) {
395 this.mustConstraintList = mustConstraintList;
396 }
397
398 @Override
399 public void addMust(YangMust must) {
400 if (getListOfMust() == null) {
401 setListOfMust(new LinkedList<>());
402 }
403 getListOfMust().add(must);
404 }
405
406 @Override
407 public List<YangIfFeature> getIfFeatureList() {
408 return ifFeatureList;
409 }
410
411 @Override
412 public void addIfFeatureList(YangIfFeature ifFeature) {
413 if (getIfFeatureList() == null) {
414 setIfFeatureList(new LinkedList<>());
415 }
416 getIfFeatureList().add(ifFeature);
417 }
418
419 @Override
420 public void setIfFeatureList(List<YangIfFeature> ifFeatureList) {
421 this.ifFeatureList = ifFeatureList;
422 }
Vinod Kumar S19f39c72016-02-09 20:12:31 +0530423}