blob: b1feb31f910c15ac6f0622cd27526483e7e477fa [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;
Bharat saraswal594bc6d2016-02-22 22:15:21 +053020import org.onosproject.yangutils.translator.CachedFileHandle;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053021import org.onosproject.yangutils.utils.YangConstructType;
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 * Default value in string, needs to be converted to the target object,
59 * based on the type.
60 */
61 private String defaultValueInString;
62
63 /**
64 * Description of new type.
65 */
66 private String description;
67
68 /**
69 * reference string.
70 */
71 private String reference;
72
73 /**
74 * Status of the data type.
75 */
76 private YangStatusType status;
77
78 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +053079 * Maintain the derived type information.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053080 */
Vinod Kumar S71cba682016-02-25 15:52:16 +053081 private YangType<YangDerivedType> derivedType;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053082
83 /**
84 * Units of the data type.
85 */
86 private String units;
87
88 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053089 * package of the generated java code.
90 */
91 private String pkg;
92
93 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053094 * Create a typedef node.
95 */
96 public YangTypeDef() {
97 super(YangNodeType.TYPEDEF_NODE);
98 }
99
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530100 /**
101 * Get the default value.
102 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530103 * @return the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530104 */
105 public String getDefaultValueInString() {
106 return defaultValueInString;
107 }
108
109 /**
110 * Set the default value.
111 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 * @param defaultValueInString the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530113 */
114 public void setDefaultValueInString(String defaultValueInString) {
115 this.defaultValueInString = defaultValueInString;
116 }
117
118 /**
119 * Get the description.
120 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530121 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530122 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530123 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530124 public String getDescription() {
125 return description;
126 }
127
128 /**
129 * Set the description.
130 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530131 * @param description set 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 void setDescription(String description) {
135 this.description = description;
136 }
137
138 /**
139 * Get the textual reference.
140 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530141 * @return the reference
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 String getReference() {
145 return reference;
146 }
147
148 /**
149 * Set the textual reference.
150 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530151 * @param reference the reference to set
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 void setReference(String reference) {
155 this.reference = reference;
156 }
157
158 /**
159 * Get the status.
160 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530161 * @return the status
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 YangStatusType getStatus() {
165 return status;
166 }
167
168 /**
169 * Set the status.
170 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530171 * @param status the status to set
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 void setStatus(YangStatusType status) {
175 this.status = status;
176 }
177
178 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530179 * Get the derived type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530180 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530181 * @return the derived type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530182 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530183 public YangType<YangDerivedType> getDerivedType() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530184 return derivedType;
185 }
186
187 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530188 * Set the derived type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530189 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530190 * @param derivedType the derived type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530191 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530192 public void setDerivedType(YangType<YangDerivedType> derivedType) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530193 this.derivedType = derivedType;
194 }
195
196 /**
197 * Get the unit.
198 *
199 * @return the units
200 */
201 public String getUnits() {
202 return units;
203 }
204
205 /**
206 * Set the unit.
207 *
208 * @param units the units to set
209 */
210 public void setUnits(String units) {
211 this.units = units;
212 }
213
214 /**
215 * Returns the type of the data.
216 *
217 * @return returns TYPEDEF_DATA
218 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530219 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530220 public YangConstructType getYangConstructType() {
221 return YangConstructType.TYPEDEF_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530222 }
223
224 /**
225 * Validate the data on entering the corresponding parse tree node.
226 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530227 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530228 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530229 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530230 public void validateDataOnEntry() throws DataModelException {
231 // TODO auto-generated method stub, to be implemented by parser
232 }
233
234 /**
235 * Validate the data on exiting the corresponding parse tree node.
236 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530237 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530238 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530239 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530240 public void validateDataOnExit() throws DataModelException {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530241 YangType<YangDerivedType> type = getDerivedType();
242 if (type == null) {
243 throw new DataModelException("Typedef does not have type info.");
244 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530245 if (type.getDataType() != YangDataTypes.DERIVED
246 || type.getDataTypeName() == null) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530247 throw new DataModelException("Typedef type is not derived.");
248 }
249
250 YangDerivedType derivedTypeInfo = type.getDataTypeExtendedInfo();
251 if (derivedTypeInfo == null) {
252 throw new DataModelException("derrived type does not have derived info.");
253 }
254
255 YangType<?> baseType = derivedTypeInfo.getBaseType();
256 if (baseType == null) {
257 throw new DataModelException("Base type of a derived type is missing.");
258 }
259
260 if (derivedTypeInfo.getEffectiveYangBuiltInType() == null) {
261 /* resolve the effective type from the data tree. */
262 /*
263 * TODO: try to resolve the nested reference, if possible in the
264 * partial tree, otherwise we need to resolve finally when the
265 * complete module is created.
266 */
267 YangModule.addToResolveList(this);
268 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530269 }
270
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530271 /**
272 * Get the YANG name of the typedef.
273 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530274 * @return YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530275 */
276 @Override
277 public String getName() {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530278 if (getDerivedType() != null) {
279 return getDerivedType().getDataTypeName();
280 }
281 return null;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530282 }
283
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530284 /**
285 * Set YANG name of the typedef.
286 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530287 * @param name YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530288 */
289 @Override
290 public void setName(String name) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530291 if (getDerivedType() == null) {
292 throw new RuntimeException(
293 "Derrived Type info needs to be set in parser when the typedef listner is processed");
294 }
295 getDerivedType().setDataTypeName(name);
296 getDerivedType().setDataType(YangDataTypes.DERIVED);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530297 }
298
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530299 /**
300 * Generate java code snippet corresponding to YANG typedef.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530301 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530302 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530303 public void generateJavaCodeEntry() {
304 // TODO Auto-generated method stub
305
306 }
307
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530308 /**
309 * Free resource used for code generation of YANG typedef.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530310 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530311 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530312 public void generateJavaCodeExit() {
313 // TODO Auto-generated method stub
314
315 }
316
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530317 /**
318 * Get the mapped java package.
319 *
320 * @return the java package
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530321 */
322 @Override
323 public String getPackage() {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530324 return pkg;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530325 }
326
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530327 /**
328 * Set the mapped java package.
329 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530330 * @param pakg mapped java package
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530331 */
332 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530333 public void setPackage(String pakg) {
334 pkg = pakg;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530335
336 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530337
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530338 /**
339 * Get the file handle of the cached file used during code generation.
340 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530341 * @return cached file handle
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530342 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530343 @Override
344 public CachedFileHandle getFileHandle() {
345 // TODO Auto-generated method stub
346 return null;
347 }
348
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530349 /**
350 * Set the file handle to be used used for code generation.
351 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530352 * @param fileHandle cached file handle
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530353 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530354 @Override
355 public void setFileHandle(CachedFileHandle fileHandle) {
356 // TODO Auto-generated method stub
357
358 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530359}