blob: d0f69afa4dda89e8fb91fdb2c90bac44ccf21813 [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;
Bharat saraswal96dfef02016-06-16 00:29:12 +053020
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053021import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import org.onosproject.yangutils.datamodel.utils.Parsable;
23import org.onosproject.yangutils.datamodel.utils.YangConstructType;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053024
Vidyashree Rama210c01d2016-05-20 16:29:25 +053025import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
26
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053027/*-
28 * Reference RFC 6020.
29 *
30 * The "typedef" statement defines a new type that may be used locally in the
31 * module, in modules or submodules which include it, and by other modules that
32 * import from it. The new type is called the "derived type", and the type from
33 * which it was derived is called the "base type". All derived types can be
34 * traced back to a YANG built-in type.
35 *
36 * The "typedef" statement's argument is an identifier that is the name of the
37 * type to be defined, and MUST be followed by a block of sub-statements that
38 * holds detailed typedef information.
39 *
40 * The name of the type MUST NOT be one of the YANG built-in types. If the
41 * typedef is defined at the top level of a YANG module or submodule, the name
42 * of the type to be defined MUST be unique within the module.
43 * The typedef's sub-statements
44 *
45 * +--------------+---------+-------------+------------------+
46 * | substatement | section | cardinality |data model mapping|
47 * +--------------+---------+-------------+------------------+
48 * | default | 7.3.4 | 0..1 |-string |
49 * | description | 7.19.3 | 0..1 |-string |
50 * | reference | 7.19.4 | 0..1 |-string |
51 * | status | 7.19.2 | 0..1 |-YangStatus |
52 * | type | 7.3.2 | 1 |-yangType |
53 * | units | 7.3.3 | 0..1 |-string |
54 * +--------------+---------+-------------+------------------+
55 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +053056
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053057/**
Bharat saraswald9822e92016-04-05 15:13:44 +053058 * Represents data model node to maintain information defined in YANG typedef.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053059 */
Vidyashree Rama210c01d2016-05-20 16:29:25 +053060public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable, YangTypeHolder, CollisionDetector {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053061
Bharat saraswal96dfef02016-06-16 00:29:12 +053062 private static final long serialVersionUID = 806201615L;
63
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053064 /**
65 * Default value in string, needs to be converted to the target object,
66 * based on the type.
67 */
68 private String defaultValueInString;
69
70 /**
71 * Description of new type.
72 */
73 private String description;
74
75 /**
76 * reference string.
77 */
78 private String reference;
79
80 /**
81 * Status of the data type.
82 */
83 private YangStatusType status;
84
85 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053086 * Name of the typedef.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053087 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053088 private String name;
89
90 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053091 * Units of the data type.
92 */
93 private String units;
94
95 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053096 * List of YANG type, for typedef it will have single type.
97 * This is done to unify the code with union.
98 */
99 private List<YangType<?>> typeList;
100
101 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530102 * Creates a typedef node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530103 */
104 public YangTypeDef() {
105 super(YangNodeType.TYPEDEF_NODE);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530106 typeList = new LinkedList<>();
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530107 }
108
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530109 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530110 * Returns the default value.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530111 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 * @return the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530113 */
114 public String getDefaultValueInString() {
115 return defaultValueInString;
116 }
117
118 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530119 * Sets the default value.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530120 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530121 * @param defaultValueInString the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530122 */
123 public void setDefaultValueInString(String defaultValueInString) {
124 this.defaultValueInString = defaultValueInString;
125 }
126
127 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530128 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530129 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530130 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530131 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530132 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530133 public String getDescription() {
134 return description;
135 }
136
137 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530138 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530139 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530140 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530141 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530142 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530143 public void setDescription(String description) {
144 this.description = description;
145 }
146
147 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530148 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530149 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530150 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530151 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530152 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530153 public String getReference() {
154 return reference;
155 }
156
157 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530158 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530159 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530161 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530162 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530163 public void setReference(String reference) {
164 this.reference = reference;
165 }
166
167 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530168 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530169 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530170 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530171 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530172 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530173 public YangStatusType getStatus() {
174 return status;
175 }
176
177 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530178 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530179 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530180 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530181 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530182 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530183 public void setStatus(YangStatusType status) {
184 this.status = status;
185 }
186
187 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530188 * Returns the data type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530189 *
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530190 * @return the data type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530191 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530192 public YangType<?> getTypeDefBaseType() {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530193 if (!getTypeList().isEmpty()) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530194 return getTypeList().get(0);
195 }
196 return null;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530197 }
198
199 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530200 * Sets the data type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530201 *
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530202 * @param dataType the data type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530203 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530204 public void setDataType(YangType<?> dataType) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530205 getTypeList().add(0, dataType);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530206 }
207
208 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530209 * Returns the unit.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530210 *
211 * @return the units
212 */
213 public String getUnits() {
214 return units;
215 }
216
217 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530218 * Sets the unit.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530219 *
220 * @param units the units to set
221 */
222 public void setUnits(String units) {
223 this.units = units;
224 }
225
226 /**
227 * Returns the type of the data.
228 *
229 * @return returns TYPEDEF_DATA
230 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530231 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530232 public YangConstructType getYangConstructType() {
233 return YangConstructType.TYPEDEF_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530234 }
235
236 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530237 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530238 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530239 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530240 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530241 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530242 public void validateDataOnEntry() throws DataModelException {
243 // TODO auto-generated method stub, to be implemented by parser
244 }
245
246 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530247 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530248 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530249 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530250 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530251 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530252 public void validateDataOnExit() throws DataModelException {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530253 // TODO auto-generated method stub, to be implemented by parser
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530254 }
255
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530256 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530257 * Returns the YANG name of the typedef.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530258 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530259 * @return YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530260 */
261 @Override
262 public String getName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530263 return name;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530264 }
265
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530266 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530267 * Sets YANG name of the typedef.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530268 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530269 * @param name YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530270 */
271 @Override
272 public void setName(String name) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530273 this.name = name;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530274 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530275
276 @Override
277 public List<YangType<?>> getTypeList() {
278 return typeList;
279 }
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530280
281 @Override
282 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
283 // Asks helper to detect colliding child.
284 detectCollidingChildUtil(identifierName, dataType, this);
285 }
286
287 @Override
288 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
289 if (getName().equals(identifierName)) {
290 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as typedef \""
291 + getName() + "\"");
292 }
293 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530294}