blob: 085a5c8a44b7d6eb74e6a66b6aa4a9715a6f04b0 [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 */
Bharat saraswald50c6382016-07-14 21:57:13 +053060public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable, YangTypeHolder, CollisionDetector,
61 YangTranslatorOperatorNode {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053062
Bharat saraswal96dfef02016-06-16 00:29:12 +053063 private static final long serialVersionUID = 806201615L;
64
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053065 /**
66 * Default value in string, needs to be converted to the target object,
67 * based on the type.
68 */
69 private String defaultValueInString;
70
71 /**
72 * Description of new type.
73 */
74 private String description;
75
76 /**
77 * reference string.
78 */
79 private String reference;
80
81 /**
82 * Status of the data type.
83 */
84 private YangStatusType status;
85
86 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053087 * Name of the typedef.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053088 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053089 private String name;
90
91 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053092 * Units of the data type.
93 */
94 private String units;
95
96 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053097 * List of YANG type, for typedef it will have single type.
98 * This is done to unify the code with union.
99 */
100 private List<YangType<?>> typeList;
101
102 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530103 * Creates a typedef node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530104 */
105 public YangTypeDef() {
106 super(YangNodeType.TYPEDEF_NODE);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530107 typeList = new LinkedList<>();
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530108 }
109
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530110 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530111 * Returns the default value.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530112 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530113 * @return the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530114 */
115 public String getDefaultValueInString() {
116 return defaultValueInString;
117 }
118
119 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530120 * Sets the default value.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530121 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530122 * @param defaultValueInString the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530123 */
124 public void setDefaultValueInString(String defaultValueInString) {
125 this.defaultValueInString = defaultValueInString;
126 }
127
128 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530129 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530130 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530131 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530132 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530133 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530134 public String getDescription() {
135 return description;
136 }
137
138 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530139 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530140 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530141 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530142 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530143 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530144 public void setDescription(String description) {
145 this.description = description;
146 }
147
148 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530149 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530150 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530151 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530152 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530153 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530154 public String getReference() {
155 return reference;
156 }
157
158 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530159 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530160 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530161 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530162 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530163 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530164 public void setReference(String reference) {
165 this.reference = reference;
166 }
167
168 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530169 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530170 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530171 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530172 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530173 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530174 public YangStatusType getStatus() {
175 return status;
176 }
177
178 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530179 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530180 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530181 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530182 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530183 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530184 public void setStatus(YangStatusType status) {
185 this.status = status;
186 }
187
188 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530189 * Returns the data type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530190 *
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530191 * @return the data type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530192 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530193 public YangType<?> getTypeDefBaseType() {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530194 if (!getTypeList().isEmpty()) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530195 return getTypeList().get(0);
196 }
197 return null;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530198 }
199
200 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530201 * Sets the data type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530202 *
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530203 * @param dataType the data type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530204 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530205 public void setDataType(YangType<?> dataType) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530206 getTypeList().add(0, dataType);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530207 }
208
209 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530210 * Returns the unit.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530211 *
212 * @return the units
213 */
214 public String getUnits() {
215 return units;
216 }
217
218 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530219 * Sets the unit.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530220 *
221 * @param units the units to set
222 */
223 public void setUnits(String units) {
224 this.units = units;
225 }
226
227 /**
228 * Returns the type of the data.
229 *
230 * @return returns TYPEDEF_DATA
231 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530232 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530233 public YangConstructType getYangConstructType() {
234 return YangConstructType.TYPEDEF_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530235 }
236
237 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530238 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530239 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530240 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530241 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530242 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530243 public void validateDataOnEntry() throws DataModelException {
244 // TODO auto-generated method stub, to be implemented by parser
245 }
246
247 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530248 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530249 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530250 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530251 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530252 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530253 public void validateDataOnExit() throws DataModelException {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530254 if (defaultValueInString != null && !defaultValueInString.isEmpty() && getTypeDefBaseType() != null) {
255 getTypeDefBaseType().isValidValue(defaultValueInString);
256 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530257 }
258
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530259 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530260 * Returns the YANG name of the typedef.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530261 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530262 * @return YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530263 */
264 @Override
265 public String getName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530266 return name;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530267 }
268
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530269 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530270 * Sets YANG name of the typedef.
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530271 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530272 * @param name YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530273 */
274 @Override
275 public void setName(String name) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530276 this.name = name;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530277 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530278
279 @Override
280 public List<YangType<?>> getTypeList() {
281 return typeList;
282 }
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530283
284 @Override
285 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
286 // Asks helper to detect colliding child.
287 detectCollidingChildUtil(identifierName, dataType, this);
288 }
289
290 @Override
291 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
292 if (getName().equals(identifierName)) {
293 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as typedef \""
294 + getName() + "\"");
295 }
296 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530297}