blob: a295b489c661d145558fe921284a38cff3e399f0 [file] [log] [blame]
Vinod Kumar S2ff139c2016-02-16 01:37:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S2ff139c2016-02-16 01:37:16 +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 */
16package org.onosproject.yangutils.datamodel;
17
Gaurav Agrawal338735b2016-04-18 18:53:11 +053018import java.util.LinkedList;
19import java.util.List;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053020import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
21import org.onosproject.yangutils.parser.Parsable;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053022import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053023
Vidyashree Rama210c01d2016-05-20 16:29:25 +053024import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
25
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053026/*-
27 * Reference RFC 6020.
28 *
29 * The "typedef" statement defines a new type that may be used locally in the
30 * module, in modules or submodules which include it, and by other modules that
31 * import from it. The new type is called the "derived type", and the type from
32 * which it was derived is called the "base type". All derived types can be
33 * traced back to a YANG built-in type.
34 *
35 * The "typedef" statement's argument is an identifier that is the name of the
36 * type to be defined, and MUST be followed by a block of sub-statements that
37 * holds detailed typedef information.
38 *
39 * The name of the type MUST NOT be one of the YANG built-in types. If the
40 * typedef is defined at the top level of a YANG module or submodule, the name
41 * of the type to be defined MUST be unique within the module.
42 * The typedef's sub-statements
43 *
44 * +--------------+---------+-------------+------------------+
45 * | substatement | section | cardinality |data model mapping|
46 * +--------------+---------+-------------+------------------+
47 * | default | 7.3.4 | 0..1 |-string |
48 * | description | 7.19.3 | 0..1 |-string |
49 * | reference | 7.19.4 | 0..1 |-string |
50 * | status | 7.19.2 | 0..1 |-YangStatus |
51 * | type | 7.3.2 | 1 |-yangType |
52 * | units | 7.3.3 | 0..1 |-string |
53 * +--------------+---------+-------------+------------------+
54 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +053055
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053056/**
Bharat saraswald9822e92016-04-05 15:13:44 +053057 * Represents data model node to maintain information defined in YANG typedef.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053058 */
Vidyashree Rama210c01d2016-05-20 16:29:25 +053059public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable, YangTypeHolder, CollisionDetector {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053060
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053061 /**
62 * Default value in string, needs to be converted to the target object,
63 * based on the type.
64 */
65 private String defaultValueInString;
66
67 /**
68 * Description of new type.
69 */
70 private String description;
71
72 /**
73 * reference string.
74 */
75 private String reference;
76
77 /**
78 * Status of the data type.
79 */
80 private YangStatusType status;
81
82 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053083 * Name of the typedef.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053084 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053085 private String name;
86
87 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053088 * Units of the data type.
89 */
90 private String units;
91
92 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053093 * List of YANG type, for typedef it will have single type.
94 * This is done to unify the code with union.
95 */
96 private List<YangType<?>> typeList;
97
98 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053099 * Creates a typedef node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530100 */
101 public YangTypeDef() {
102 super(YangNodeType.TYPEDEF_NODE);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530103 typeList = new LinkedList<>();
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530104 }
105
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530106 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530107 * Returns the default value.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530108 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530109 * @return the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530110 */
111 public String getDefaultValueInString() {
112 return defaultValueInString;
113 }
114
115 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530116 * Sets the default value.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530117 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530118 * @param defaultValueInString the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530119 */
120 public void setDefaultValueInString(String defaultValueInString) {
121 this.defaultValueInString = defaultValueInString;
122 }
123
124 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530125 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530126 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530127 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530128 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530129 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530130 public String getDescription() {
131 return description;
132 }
133
134 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530135 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530136 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530137 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530138 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530139 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530140 public void setDescription(String description) {
141 this.description = description;
142 }
143
144 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530145 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530146 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530147 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530148 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530149 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530150 public String getReference() {
151 return reference;
152 }
153
154 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530155 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530156 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530157 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530158 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530159 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530160 public void setReference(String reference) {
161 this.reference = reference;
162 }
163
164 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530165 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530166 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530167 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530168 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530169 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530170 public YangStatusType getStatus() {
171 return status;
172 }
173
174 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530175 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530176 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530177 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530178 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530179 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530180 public void setStatus(YangStatusType status) {
181 this.status = status;
182 }
183
184 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530185 * Returns the data type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530186 *
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530187 * @return the data type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530188 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530189 public YangType<?> getTypeDefBaseType() {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530190 if (!(getTypeList().isEmpty())) {
191 return getTypeList().get(0);
192 }
193 return null;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530194 }
195
196 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530197 * Sets the data type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530198 *
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530199 * @param dataType the data type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530200 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530201 public void setDataType(YangType<?> dataType) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530202 getTypeList().add(0, dataType);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530203 }
204
205 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530206 * Returns the unit.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530207 *
208 * @return the units
209 */
210 public String getUnits() {
211 return units;
212 }
213
214 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530215 * Sets the unit.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530216 *
217 * @param units the units to set
218 */
219 public void setUnits(String units) {
220 this.units = units;
221 }
222
223 /**
224 * Returns the type of the data.
225 *
226 * @return returns TYPEDEF_DATA
227 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530228 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530229 public YangConstructType getYangConstructType() {
230 return YangConstructType.TYPEDEF_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530231 }
232
233 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530234 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530235 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530236 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530237 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530238 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530239 public void validateDataOnEntry() throws DataModelException {
240 // TODO auto-generated method stub, to be implemented by parser
241 }
242
243 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530244 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530245 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530246 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530247 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530248 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530249 public void validateDataOnExit() throws DataModelException {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530250 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530251 }
252
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530253 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530254 * Returns the YANG name of the typedef.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530255 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530256 * @return YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530257 */
258 @Override
259 public String getName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530260 return name;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530261 }
262
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530263 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530264 * Sets YANG name of the typedef.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530265 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530266 * @param name YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530267 */
268 @Override
269 public void setName(String name) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530270 this.name = name;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530271 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530272
273 @Override
274 public List<YangType<?>> getTypeList() {
275 return typeList;
276 }
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530277
278 @Override
279 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
280 // Asks helper to detect colliding child.
281 detectCollidingChildUtil(identifierName, dataType, this);
282 }
283
284 @Override
285 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
286 if (getName().equals(identifierName)) {
287 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as typedef \""
288 + getName() + "\"");
289 }
290 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530291}