blob: a1abd8b16de7fa78e1fe55e03aa131aa3bf95d29 [file] [log] [blame]
Vinod Kumar S2ff139c2016-02-16 01:37:16 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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
18import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
19import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053020import org.onosproject.yangutils.utils.YangConstructType;
Bharat saraswal594bc6d2016-02-22 22:15:21 +053021import org.onosproject.yangutils.translator.CachedFileHandle;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053022
23/*-
24 * Reference RFC 6020.
25 *
26 * The "typedef" statement defines a new type that may be used locally in the
27 * module, in modules or submodules which include it, and by other modules that
28 * import from it. The new type is called the "derived type", and the type from
29 * which it was derived is called the "base type". All derived types can be
30 * traced back to a YANG built-in type.
31 *
32 * The "typedef" statement's argument is an identifier that is the name of the
33 * type to be defined, and MUST be followed by a block of sub-statements that
34 * holds detailed typedef information.
35 *
36 * The name of the type MUST NOT be one of the YANG built-in types. If the
37 * typedef is defined at the top level of a YANG module or submodule, the name
38 * of the type to be defined MUST be unique within the module.
39 * The typedef's sub-statements
40 *
41 * +--------------+---------+-------------+------------------+
42 * | substatement | section | cardinality |data model mapping|
43 * +--------------+---------+-------------+------------------+
44 * | default | 7.3.4 | 0..1 |-string |
45 * | description | 7.19.3 | 0..1 |-string |
46 * | reference | 7.19.4 | 0..1 |-string |
47 * | status | 7.19.2 | 0..1 |-YangStatus |
48 * | type | 7.3.2 | 1 |-yangType |
49 * | units | 7.3.3 | 0..1 |-string |
50 * +--------------+---------+-------------+------------------+
51 */
52/**
53 * Data model node to maintain information defined in YANG typedef.
54 */
55public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
56
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053057
58 /**
59 * Default value in string, needs to be converted to the target object,
60 * based on the type.
61 */
62 private String defaultValueInString;
63
64 /**
65 * Description of new type.
66 */
67 private String description;
68
69 /**
70 * reference string.
71 */
72 private String reference;
73
74 /**
75 * Status of the data type.
76 */
77 private YangStatusType status;
78
79 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +053080 * Maintain the derived type information.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053081 */
Vinod Kumar S71cba682016-02-25 15:52:16 +053082 private YangType<YangDerivedType> derivedType;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053083
84 /**
85 * Units of the data type.
86 */
87 private String units;
88
89 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053090 * package of the generated java code.
91 */
92 private String pkg;
93
94 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053095 * Create a typedef node.
96 */
97 public YangTypeDef() {
98 super(YangNodeType.TYPEDEF_NODE);
99 }
100
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530101
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530102
103 /**
104 * Get the default value.
105 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530106 * @return the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530107 */
108 public String getDefaultValueInString() {
109 return defaultValueInString;
110 }
111
112 /**
113 * Set the default value.
114 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530115 * @param defaultValueInString the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530116 */
117 public void setDefaultValueInString(String defaultValueInString) {
118 this.defaultValueInString = defaultValueInString;
119 }
120
121 /**
122 * Get the description.
123 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530124 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530125 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530126 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530127 public String getDescription() {
128 return description;
129 }
130
131 /**
132 * Set the description.
133 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530134 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530135 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530136 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530137 public void setDescription(String description) {
138 this.description = description;
139 }
140
141 /**
142 * Get the textual reference.
143 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530144 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530145 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530146 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530147 public String getReference() {
148 return reference;
149 }
150
151 /**
152 * Set the textual reference.
153 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530154 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530155 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530156 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530157 public void setReference(String reference) {
158 this.reference = reference;
159 }
160
161 /**
162 * Get the status.
163 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530164 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530165 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530166 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530167 public YangStatusType getStatus() {
168 return status;
169 }
170
171 /**
172 * Set the status.
173 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530174 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530175 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530176 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530177 public void setStatus(YangStatusType status) {
178 this.status = status;
179 }
180
181 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530182 * Get the derived type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530183 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530184 * @return the derived type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530185 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530186 public YangType<YangDerivedType> getDerivedType() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530187 return derivedType;
188 }
189
190 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530191 * Set the derived type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530192 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530193 * @param derivedType the derived type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530194 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530195 public void setDerivedType(YangType<YangDerivedType> derivedType) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530196 this.derivedType = derivedType;
197 }
198
199 /**
200 * Get the unit.
201 *
202 * @return the units
203 */
204 public String getUnits() {
205 return units;
206 }
207
208 /**
209 * Set the unit.
210 *
211 * @param units the units to set
212 */
213 public void setUnits(String units) {
214 this.units = units;
215 }
216
217 /**
218 * Returns the type of the data.
219 *
220 * @return returns TYPEDEF_DATA
221 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530222 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530223 public YangConstructType getYangConstructType() {
224 return YangConstructType.TYPEDEF_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530225 }
226
227 /**
228 * Validate the data on entering the corresponding parse tree node.
229 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530230 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530231 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530232 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530233 public void validateDataOnEntry() throws DataModelException {
234 // TODO auto-generated method stub, to be implemented by parser
235 }
236
237 /**
238 * Validate the data on exiting the corresponding parse tree node.
239 *
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 validateDataOnExit() throws DataModelException {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530244 YangType<YangDerivedType> type = getDerivedType();
245 if (type == null) {
246 throw new DataModelException("Typedef does not have type info.");
247 }
248 if ((type.getDataType() != YangDataTypes.DERIVED)
249 || (type.getDataTypeName() == null)) {
250 throw new DataModelException("Typedef type is not derived.");
251 }
252
253 YangDerivedType derivedTypeInfo = type.getDataTypeExtendedInfo();
254 if (derivedTypeInfo == null) {
255 throw new DataModelException("derrived type does not have derived info.");
256 }
257
258 YangType<?> baseType = derivedTypeInfo.getBaseType();
259 if (baseType == null) {
260 throw new DataModelException("Base type of a derived type is missing.");
261 }
262
263 if (derivedTypeInfo.getEffectiveYangBuiltInType() == null) {
264 /* resolve the effective type from the data tree. */
265 /*
266 * TODO: try to resolve the nested reference, if possible in the
267 * partial tree, otherwise we need to resolve finally when the
268 * complete module is created.
269 */
270 YangModule.addToResolveList(this);
271 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530272 }
273
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530274 /**
275 * Get the YANG name of the typedef.
276 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530277 * @return YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530278 */
279 @Override
280 public String getName() {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530281 if (getDerivedType() != null) {
282 return getDerivedType().getDataTypeName();
283 }
284 return null;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530285 }
286
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530287 /**
288 * Set YANG name of the typedef.
289 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530290 * @param name YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530291 */
292 @Override
293 public void setName(String name) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530294 if (getDerivedType() == null) {
295 throw new RuntimeException(
296 "Derrived Type info needs to be set in parser when the typedef listner is processed");
297 }
298 getDerivedType().setDataTypeName(name);
299 getDerivedType().setDataType(YangDataTypes.DERIVED);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530300 }
301
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530302 /**
303 * Generate java code snippet corresponding to YANG typedef.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530304 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530305 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530306 public void generateJavaCodeEntry() {
307 // TODO Auto-generated method stub
308
309 }
310
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530311 /**
312 * Free resource used for code generation of YANG typedef.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530313 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530314 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530315 public void generateJavaCodeExit() {
316 // TODO Auto-generated method stub
317
318 }
319
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530320 /**
321 * Get the mapped java package.
322 *
323 * @return the java package
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530324 */
325 @Override
326 public String getPackage() {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530327 return pkg;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530328 }
329
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530330 /**
331 * Set the mapped java package.
332 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530333 * @param pakg mapped java package
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530334 */
335 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530336 public void setPackage(String pakg) {
337 pkg = pakg;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530338
339 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530340
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530341 /**
342 * Get the file handle of the cached file used during code generation.
343 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530344 * @return cached file handle
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530345 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530346 @Override
347 public CachedFileHandle getFileHandle() {
348 // TODO Auto-generated method stub
349 return null;
350 }
351
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530352 /**
353 * Set the file handle to be used used for code generation.
354 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530355 * @param fileHandle cached file handle
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530356 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530357 @Override
358 public void setFileHandle(CachedFileHandle fileHandle) {
359 // TODO Auto-generated method stub
360
361 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530362}