blob: a679cd9a9f16c3ca6d26e32e79bfc064924ccc11 [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
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053018import java.io.IOException;
19
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053020import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
21import org.onosproject.yangutils.parser.Parsable;
Bharat saraswal594bc6d2016-02-22 22:15:21 +053022import org.onosproject.yangutils.translator.CachedFileHandle;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053023import org.onosproject.yangutils.translator.GeneratedFileType;
24import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
25import org.onosproject.yangutils.utils.UtilConstants;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053026import org.onosproject.yangutils.utils.YangConstructType;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053027import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053028
29/*-
30 * Reference RFC 6020.
31 *
32 * The "typedef" statement defines a new type that may be used locally in the
33 * module, in modules or submodules which include it, and by other modules that
34 * import from it. The new type is called the "derived type", and the type from
35 * which it was derived is called the "base type". All derived types can be
36 * traced back to a YANG built-in type.
37 *
38 * The "typedef" statement's argument is an identifier that is the name of the
39 * type to be defined, and MUST be followed by a block of sub-statements that
40 * holds detailed typedef information.
41 *
42 * The name of the type MUST NOT be one of the YANG built-in types. If the
43 * typedef is defined at the top level of a YANG module or submodule, the name
44 * of the type to be defined MUST be unique within the module.
45 * The typedef's sub-statements
46 *
47 * +--------------+---------+-------------+------------------+
48 * | substatement | section | cardinality |data model mapping|
49 * +--------------+---------+-------------+------------------+
50 * | default | 7.3.4 | 0..1 |-string |
51 * | description | 7.19.3 | 0..1 |-string |
52 * | reference | 7.19.4 | 0..1 |-string |
53 * | status | 7.19.2 | 0..1 |-YangStatus |
54 * | type | 7.3.2 | 1 |-yangType |
55 * | units | 7.3.3 | 0..1 |-string |
56 * +--------------+---------+-------------+------------------+
57 */
58/**
59 * Data model node to maintain information defined in YANG typedef.
60 */
61public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
62
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053063 /**
64 * Default value in string, needs to be converted to the target object,
65 * based on the type.
66 */
67 private String defaultValueInString;
68
69 /**
70 * Description of new type.
71 */
72 private String description;
73
74 /**
75 * reference string.
76 */
77 private String reference;
78
79 /**
80 * Status of the data type.
81 */
82 private YangStatusType status;
83
84 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +053085 * Maintain the derived type information.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053086 */
Vinod Kumar S71cba682016-02-25 15:52:16 +053087 private YangType<YangDerivedType> derivedType;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053088
89 /**
90 * Units of the data type.
91 */
92 private String units;
93
94 /**
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053095 * package of the generated java code.
96 */
97 private String pkg;
98
99 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530100 * Cached Java File Handle.
101 */
102 private CachedFileHandle fileHandle;
103
104 /**
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530105 * Create a typedef node.
106 */
107 public YangTypeDef() {
108 super(YangNodeType.TYPEDEF_NODE);
109 }
110
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530111 /**
112 * Get the default value.
113 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530114 * @return the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530115 */
116 public String getDefaultValueInString() {
117 return defaultValueInString;
118 }
119
120 /**
121 * Set the default value.
122 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530123 * @param defaultValueInString the default value
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530124 */
125 public void setDefaultValueInString(String defaultValueInString) {
126 this.defaultValueInString = defaultValueInString;
127 }
128
129 /**
130 * Get the description.
131 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530132 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530133 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530134 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530135 public String getDescription() {
136 return description;
137 }
138
139 /**
140 * Set the description.
141 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530142 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530143 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530144 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530145 public void setDescription(String description) {
146 this.description = description;
147 }
148
149 /**
150 * Get the textual reference.
151 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530152 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530153 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530154 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530155 public String getReference() {
156 return reference;
157 }
158
159 /**
160 * Set the textual reference.
161 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530162 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530163 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530164 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530165 public void setReference(String reference) {
166 this.reference = reference;
167 }
168
169 /**
170 * Get the status.
171 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530172 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530173 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530174 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530175 public YangStatusType getStatus() {
176 return status;
177 }
178
179 /**
180 * Set the status.
181 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530182 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530183 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530184 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530185 public void setStatus(YangStatusType status) {
186 this.status = status;
187 }
188
189 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530190 * Get the derived type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530191 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530192 * @return the derived type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530193 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530194 public YangType<YangDerivedType> getDerivedType() {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530195 return derivedType;
196 }
197
198 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +0530199 * Set the derived type.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530200 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530201 * @param derivedType the derived type
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530202 */
Vinod Kumar S71cba682016-02-25 15:52:16 +0530203 public void setDerivedType(YangType<YangDerivedType> derivedType) {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530204 this.derivedType = derivedType;
205 }
206
207 /**
208 * Get the unit.
209 *
210 * @return the units
211 */
212 public String getUnits() {
213 return units;
214 }
215
216 /**
217 * Set the unit.
218 *
219 * @param units the units to set
220 */
221 public void setUnits(String units) {
222 this.units = units;
223 }
224
225 /**
226 * Returns the type of the data.
227 *
228 * @return returns TYPEDEF_DATA
229 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530230 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530231 public YangConstructType getYangConstructType() {
232 return YangConstructType.TYPEDEF_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530233 }
234
235 /**
236 * Validate the data on entering the corresponding parse tree node.
237 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530238 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530239 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530240 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530241 public void validateDataOnEntry() throws DataModelException {
242 // TODO auto-generated method stub, to be implemented by parser
243 }
244
245 /**
246 * Validate the data on exiting the corresponding parse tree node.
247 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530248 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530249 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530250 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530251 public void validateDataOnExit() throws DataModelException {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530252 YangType<YangDerivedType> type = getDerivedType();
253 if (type == null) {
254 throw new DataModelException("Typedef does not have type info.");
255 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530256 if (type.getDataType() != YangDataTypes.DERIVED
257 || type.getDataTypeName() == null) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530258 throw new DataModelException("Typedef type is not derived.");
259 }
260
261 YangDerivedType derivedTypeInfo = type.getDataTypeExtendedInfo();
262 if (derivedTypeInfo == null) {
263 throw new DataModelException("derrived type does not have derived info.");
264 }
265
266 YangType<?> baseType = derivedTypeInfo.getBaseType();
267 if (baseType == null) {
268 throw new DataModelException("Base type of a derived type is missing.");
269 }
270
271 if (derivedTypeInfo.getEffectiveYangBuiltInType() == null) {
272 /* resolve the effective type from the data tree. */
273 /*
274 * TODO: try to resolve the nested reference, if possible in the
275 * partial tree, otherwise we need to resolve finally when the
276 * complete module is created.
277 */
278 YangModule.addToResolveList(this);
279 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530280 }
281
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530282 /**
283 * Get the YANG name of the typedef.
284 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530285 * @return YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530286 */
287 @Override
288 public String getName() {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530289 if (getDerivedType() != null) {
290 return getDerivedType().getDataTypeName();
291 }
292 return null;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530293 }
294
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530295 /**
296 * Set YANG name of the typedef.
297 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530298 * @param name YANG name of the typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530299 */
300 @Override
301 public void setName(String name) {
Vinod Kumar S71cba682016-02-25 15:52:16 +0530302 if (getDerivedType() == null) {
303 throw new RuntimeException(
304 "Derrived Type info needs to be set in parser when the typedef listner is processed");
305 }
306 getDerivedType().setDataTypeName(name);
307 getDerivedType().setDataType(YangDataTypes.DERIVED);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530308 }
309
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530310 /**
311 * Generate java code snippet corresponding to YANG typedef.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530312 *
313 * @param codeGenDir code generation directory
314 * @throws IOException when fails to generate files for typedef
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530315 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530316 @Override
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530317 public void generateJavaCodeEntry(String codeGenDir) throws IOException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530318
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530319 YangNode parent = getParent();
320 String typeDefPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), parent.getName());
321
322 typeDefPkg = JavaIdentifierSyntax.getCamelCase(typeDefPkg).toLowerCase();
323 setPackage(typeDefPkg);
324
325 CachedFileHandle handle = null;
326 try {
327 FileSystemUtil.createPackage(codeGenDir + getPackage(), parent.getName() + UtilConstants.CHILDREN);
328 handle = FileSystemUtil.createSourceFiles(getPackage(), getName(),
329 GeneratedFileType.GENERATE_TYPEDEF_CLASS);
330 handle.setRelativeFilePath(getPackage().replace(".", "/"));
331 handle.setCodeGenFilePath(codeGenDir);
332 } catch (IOException e) {
333 throw new IOException("Failed to create the source files.");
334 }
335 setFileHandle(handle);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530336 getDerivedType().getDataTypeExtendedInfo().getBaseType().setJavaPackage(getPackage());
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530337 addAttributeInfo();
338 addAttributeInParent();
339 }
340
341 /**
342 * Adds current node attribute to parent file.
343 */
344 private void addAttributeInParent() {
345 if (getParent() != null) {
346 getParent().getFileHandle().addAttributeInfo(null, getName(), false);
347 }
348 }
349
350 /**
351 * Adds attribute to file handle.
352 */
353 private void addAttributeInfo() {
354 getFileHandle().addAttributeInfo(getDerivedType().getDataTypeExtendedInfo().getBaseType(),
355 JavaIdentifierSyntax.getCamelCase(getName()), false);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530356 }
357
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530358 /**
359 * Free resource used for code generation of YANG typedef.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530360 *
361 * @throws IOException when fails to generate files
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530362 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530363 @Override
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530364 public void generateJavaCodeExit() throws IOException {
365 getFileHandle().close();
366 return;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530367 }
368
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530369 /**
370 * Get the mapped java package.
371 *
372 * @return the java package
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530373 */
374 @Override
375 public String getPackage() {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530376 return pkg;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530377 }
378
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530379 /**
380 * Set the mapped java package.
381 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530382 * @param pakg mapped java package
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530383 */
384 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530385 public void setPackage(String pakg) {
386 pkg = pakg;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530387
388 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530389
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530390 /**
391 * Get the file handle of the cached file used during code generation.
392 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530393 * @return cached file handle
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530394 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530395 @Override
396 public CachedFileHandle getFileHandle() {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530397 return fileHandle;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530398 }
399
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530400 /**
401 * Set the file handle to be used used for code generation.
402 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530403 * @param handle cached file handle
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530404 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530405 @Override
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530406 public void setFileHandle(CachedFileHandle handle) {
407 fileHandle = handle;
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530408 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530409}