blob: c6fcea49bb2d8de79f67e3c6a12bf63251ccd193 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57: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 */
16
17package org.onosproject.yangutils.translator.tojava;
18
19import java.io.File;
20import java.io.IOException;
Bharat saraswal594bc6d2016-02-22 22:15:21 +053021import java.util.ArrayList;
Bharat saraswal870c56f2016-02-20 21:57:16 +053022import java.util.LinkedList;
23import java.util.List;
24import java.util.SortedSet;
25import java.util.TreeSet;
26
27import org.onosproject.yangutils.datamodel.YangType;
28import org.onosproject.yangutils.translator.CachedFileHandle;
29import org.onosproject.yangutils.translator.GeneratedFileType;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053030import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
Bharat saraswal870c56f2016-02-20 21:57:16 +053031import org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053032import org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator;
Bharat saraswal870c56f2016-02-20 21:57:16 +053033import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
34import org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator;
35import org.onosproject.yangutils.utils.UtilConstants;
Bharat saraswal870c56f2016-02-20 21:57:16 +053036
37/**
38 * Maintain the information about the java file to be generated.
39 */
40public class CachedJavaFileHandle implements CachedFileHandle {
41
Bharat saraswal870c56f2016-02-20 21:57:16 +053042 private static final int MAX_CACHABLE_ATTR = 64;
43 private static final String JAVA_FILE_EXTENSION = ".java";
44 private static final String TEMP_FILE_EXTENSION = ".tmp";
45
46 /**
47 * The type(s) of java source file(s) to be generated when the cached file
48 * handle is closed.
49 */
50 private GeneratedFileType genFileTypes;
51
52 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053053 * Java package in which the class/interface needs to be generated.
54 */
55 private String pkg;
56
57 /**
Bharat saraswal594bc6d2016-02-22 22:15:21 +053058 * Java package in which the child class/interface needs to be generated.
59 */
60 private String childsPkg;
61
62 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053063 * Name of the object in YANG file.
64 */
65 private String yangName;
66
67 /**
68 * Sorted set of import info, to be used to maintain the set of classes to
69 * be imported in the generated class.
70 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +053071 private SortedSet<String> importSet;
Bharat saraswal870c56f2016-02-20 21:57:16 +053072
73 /**
74 * Cached list of attribute info.
75 */
76 private List<AttributeInfo> attributeList;
77
78 /**
Bharat saraswal4bf8b152016-02-25 02:26:43 +053079 * File generation directory path.
80 */
81 private String filePath;
82
83 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053084 * Prevent invoking default constructor.
85 */
Bharat saraswal4bf8b152016-02-25 02:26:43 +053086 public CachedJavaFileHandle() {
Bharat saraswal870c56f2016-02-20 21:57:16 +053087 setCachedAttributeList(new LinkedList<AttributeInfo>());
88 }
89
90 /**
91 * Create a cached file handle which takes care of adding attributes to the
92 * generated java file.
93 *
94 * @param pcg package in which class/interface need to be generated.
95 * @param yangName name of the attribute in YANG file.
96 * @param types the types of files that needs to be generated.
97 * @throws IOException file IO exception.
98 */
99 public CachedJavaFileHandle(String pcg, String yangName, GeneratedFileType types) throws IOException {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530100 setGeneratedFileTypes(types);
101 setPackage(pcg);
102 setYangName(yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530103 }
104
105 /**
106 * Get the types of files being generated corresponding to the YANG
107 * definition.
108 *
109 * @return the types of files being generated corresponding to the YANG
110 * definition.
111 */
112 public GeneratedFileType getGeneratedFileTypes() {
113 return genFileTypes;
114 }
115
116 /**
117 * Set the types of files being generated corresponding to the YANG
118 * definition.
119 *
120 * @param fileTypes the types of files being generated corresponding to the
121 * YANG definition.
122 */
123 public void setGeneratedFileTypes(GeneratedFileType fileTypes) {
124 genFileTypes = fileTypes;
125 }
126
127 /**
128 * Get the corresponding name defined in YANG.
129 *
130 * @return the corresponding name defined in YANG.
131 */
132 public String getYangName() {
133 return yangName;
134 }
135
136 /**
137 * Set the corresponding name defined in YANG.
138 *
139 * @param yangName the corresponding name defined in YANG.
140 */
141 public void setYangName(String yangName) {
142 this.yangName = yangName;
143 }
144
145 /**
146 * Get the java package.
147 *
148 * @return the java package.
149 */
150 public String getPackage() {
151 return pkg;
152 }
153
154 /**
155 * Set the java package.
156 *
157 * @param pcg the package to set
158 */
159 public void setPackage(String pcg) {
160 pkg = pcg;
161 }
162
163 /**
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530164 * Get the java package.
165 *
166 * @return the java package.
167 */
168 public String getChildsPackage() {
169 return childsPkg;
170 }
171
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530172 @Override
173 public void setChildsPackage(String pcg) {
174 childsPkg = pcg;
175 }
176
177 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530178 * Get the set containing the imported class/interface info.
179 *
180 * @return the set containing the imported class/interface info.
181 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530182 public SortedSet<String> getImportSet() {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530183 return importSet;
184 }
185
186 /**
187 * Assign the set containing the imported class/interface info.
188 *
189 * @param importSet the set containing the imported class/interface info.
190 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530191 private void setImportSet(SortedSet<String> importSet) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530192 this.importSet = importSet;
193 }
194
195 /**
196 * Add an imported class/interface info is it is not already part of the
197 * set. If already part of the set, return false, else add to set and return
198 * true.
199 *
200 * @param importInfo class/interface info being imported.
201 * @return status of new addition of class/interface to the import set
202 */
203 public boolean addImportInfo(ImportInfo importInfo) {
204 /*
205 * implement the import info adding. The return value will be used to
206 * check if the qualified name will be used or class/interface name will
207 * be used in the generated class.
208 */
209 if (getImportSet() == null) {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530210 setImportSet(new TreeSet<String>());
Bharat saraswal870c56f2016-02-20 21:57:16 +0530211 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530212 return getImportSet().add(JavaCodeSnippetGen.getImportText(importInfo));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530213 }
214
215 /**
216 * Get the list of cached attribute list.
217 *
218 * @return the set containing the imported class/interface info.
219 */
220 public List<AttributeInfo> getCachedAttributeList() {
221 return attributeList;
222 }
223
224 /**
225 * Set the cached attribute list.
226 *
227 * @param attrList attribute list.
228 */
229 private void setCachedAttributeList(List<AttributeInfo> attrList) {
230 attributeList = attrList;
231 }
232
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530233 @Override
234 public void setFilePath(String path) {
235 filePath = path;
236 }
237
238 /**
239 * Set the cached attribute list.
240 *
241 * @param attrList attribute list.
242 */
243 private String getFilePath() {
244 return filePath;
245 }
246
Bharat saraswal870c56f2016-02-20 21:57:16 +0530247 /**
248 * Flush the cached attribute list to the serialized file.
249 */
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530250 private void flushCacheAttrToSerFile(String className) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530251
252 for (AttributeInfo attr : getCachedAttributeList()) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530253 JavaFileGenerator.parseAttributeInfo(attr, getGeneratedFileTypes(), className);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530254 }
255
256 /*
257 * clear the contents from the cached attribute list.
258 */
259 getCachedAttributeList().clear();
260 }
261
262 /**
263 * Add a new attribute to the file(s).
264 *
265 * @param attrType data type of the added attribute.
266 * @param name name of the attribute.
267 * @param isListAttr if the current added attribute needs to be maintained
268 * in a list.
269 */
270 @Override
271 public void addAttributeInfo(YangType<?> attrType, String name, boolean isListAttr) {
272
273 AttributeInfo newAttr = new AttributeInfo();
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530274 if (attrType != null) {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530275 newAttr.setAttributeType(attrType);
276 } else {
277 ImportInfo importInfo = new ImportInfo();
278 importInfo.setPkgInfo(getChildsPackage());
279 importInfo.setClassInfo(JavaIdentifierSyntax.getCaptialCase(name));
280 if (getImportSet() != null) {
281 getImportSet().add(JavaCodeSnippetGen.getImportText(importInfo));
282 } else {
283 SortedSet<String> newImportInfo = new TreeSet<>();
284 newImportInfo.add(JavaCodeSnippetGen.getImportText(importInfo));
285 setImportSet(newImportInfo);
286 }
287
288 newAttr.setQualifiedName(getQualifiedFlag(JavaCodeSnippetGen.getImportText(importInfo)));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530289 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530290 newAttr.setAttributeName(name);
291 newAttr.setListAttr(isListAttr);
292
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530293 if (newAttr.isListAttr()) {
294 newAttr.setAttributeType(AttributesJavaDataType.getListString(newAttr));
295 }
296
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530297 if (isListAttr) {
298 String listImport = UtilConstants.COLLECTION_IMPORTS + UtilConstants.LIST + UtilConstants.SEMI_COLAN
299 + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE;
300 if (getImportSet() != null) {
301 getImportSet().add(listImport);
302 } else {
303 SortedSet<String> newImportInfo = new TreeSet<>();
304 newImportInfo.add(listImport);
305 setImportSet(newImportInfo);
306 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530307
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530308 newAttr.setQualifiedName(getQualifiedFlag(listImport));
309 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530310
311 if (getCachedAttributeList() != null) {
312 if (getCachedAttributeList().size() == MAX_CACHABLE_ATTR) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530313 flushCacheAttrToSerFile(getYangName());
Bharat saraswal870c56f2016-02-20 21:57:16 +0530314 }
315 getCachedAttributeList().add(newAttr);
316 } else {
317 List<AttributeInfo> newAttributeInfo = new LinkedList<>();
318 newAttributeInfo.add(newAttr);
319 setCachedAttributeList(newAttributeInfo);
320 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530321 }
322
323 /**
324 * Check if the import set does not have a class info same as the new class
325 * info, if so the new class info be added to the import set. Otherwise
326 * check if the corresponding package info is same as the new package info,
327 * if so no need to qualified access, otherwise, it needs qualified access.
328 *
329 * @param newImportInfo new import info to be check for qualified access or
330 * not and updated in the import set accordingly.
331 * @return if the new attribute needs to be accessed in a qualified manner.
332 */
333 private boolean getQualifiedFlag(String newImportInfo) {
334 for (String curImportInfo : getImportSet()) {
335 if (curImportInfo.equals(newImportInfo)) {
336 /*
337 * If import is already existing import with same package, we
338 * don't need qualified access, otherwise it needs to be
339 * qualified access.
340 */
341 return !curImportInfo.equals(newImportInfo);
342 }
343 }
344
345 getImportSet().add(newImportInfo);
346 return false;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530347 }
348
349 /**
350 * Flushes the cached contents to the target file, frees used resources.
351 */
352 @Override
353 public void close() throws IOException {
354
355 String className = getYangName();
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530356 className = JavaIdentifierSyntax.getCaptialCase(className);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530357 String filePath = getFilePath();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530358 GeneratedFileType fileType = getGeneratedFileTypes();
359
Bharat saraswal870c56f2016-02-20 21:57:16 +0530360 /*
361 * TODO: add the file header using
362 * JavaCodeSnippetGen.getFileHeaderComment
363 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530364
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530365 List<String> imports = new LinkedList<>();
366
Bharat saraswal870c56f2016-02-20 21:57:16 +0530367 if (getCachedAttributeList() != null) {
368 MethodsGenerator.setAttrInfo(getCachedAttributeList());
369 for (AttributeInfo attr : getCachedAttributeList()) {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530370
371 if (getImportSet() != null) {
372 imports = new ArrayList<>(getImportSet());
373 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530374 }
375 }
376
Bharat saraswal870c56f2016-02-20 21:57:16 +0530377 /**
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530378 * Start generation of files.
379 */
380 if (fileType.equals(GeneratedFileType.INTERFACE) || fileType.equals(GeneratedFileType.ALL)) {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530381
382 /**
383 * Create interface file.
384 */
385 String interfaceFileName = className;
386 File interfaceFile = new File(filePath + File.separator + interfaceFileName + JAVA_FILE_EXTENSION);
387 interfaceFile = JavaFileGenerator.generateInterfaceFile(interfaceFile, className, imports,
388 getCachedAttributeList(), getPackage());
389
390 /**
391 * Create temp builder interface file.
392 */
393 String builderInterfaceFileName = className + UtilConstants.BUILDER + UtilConstants.INTERFACE;
394 File builderInterfaceFile = new File(
395 filePath + File.separator + builderInterfaceFileName + TEMP_FILE_EXTENSION);
396 builderInterfaceFile = JavaFileGenerator.generateBuilderInterfaceFile(builderInterfaceFile, className,
397 getPackage(), getCachedAttributeList());
398
399 /**
400 * Append builder interface file to interface file and close it.
401 */
402 JavaFileGenerator.appendFileContents(builderInterfaceFile, interfaceFile);
403 JavaFileGenerator.insert(interfaceFile,
404 JavaFileGenerator.closeFile(GeneratedFileType.INTERFACE, interfaceFileName));
405
406 /**
407 * Remove temp files.
408 */
409 JavaFileGenerator.clean(builderInterfaceFile);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530410 }
411
412 if (fileType.equals(GeneratedFileType.BUILDER_CLASS) || fileType.equals(GeneratedFileType.ALL)) {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530413
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530414 /**
415 * Create builder class file.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530416 */
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530417 String builderFileName = className + UtilConstants.BUILDER;
418 File builderFile = new File(filePath + File.separator + builderFileName + JAVA_FILE_EXTENSION);
419 MethodsGenerator.setBuilderClassName(className + UtilConstants.BUILDER);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530420
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530421 builderFile = JavaFileGenerator.generateBuilderClassFile(builderFile, className, imports, getPackage(),
422 getCachedAttributeList());
Bharat saraswal870c56f2016-02-20 21:57:16 +0530423
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530424 /**
425 * Create temp impl class file.
426 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530427
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530428 String implFileName = className + UtilConstants.IMPL;
429 File implTempFile = new File(filePath + File.separator + implFileName + TEMP_FILE_EXTENSION);
430 implTempFile = JavaFileGenerator.generateImplClassFile(implTempFile, className, getPackage(),
431 getCachedAttributeList());
Bharat saraswal870c56f2016-02-20 21:57:16 +0530432
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530433 /**
434 * Append impl class to builder class and close it.
435 */
436 JavaFileGenerator.appendFileContents(implTempFile, builderFile);
437 JavaFileGenerator.insert(builderFile,
438 JavaFileGenerator.closeFile(GeneratedFileType.BUILDER_CLASS, builderFileName));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530439
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530440 /**
441 * Remove temp files.
442 */
443 JavaFileGenerator.clean(implTempFile);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530444 }
445 }
446}