blob: 724c9cff909adb68337d174e1b39a1e7070e56e0 [file] [log] [blame]
Vinod Kumar S9f26ae52016-03-23 15:30:27 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S9f26ae52016-03-23 15:30:27 +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 */
16
17package org.onosproject.yangutils.translator.tojava;
18
19import java.io.IOException;
Bharat saraswale304c252016-08-16 20:56:20 +053020
Vidyashree Ramaa89bdd32016-07-08 20:45:41 +053021import org.onosproject.yangutils.datamodel.TraversalType;
Bharat saraswalaab24b92016-08-02 18:43:16 +053022import org.onosproject.yangutils.datamodel.YangInput;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053023import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalaab24b92016-08-02 18:43:16 +053024import org.onosproject.yangutils.datamodel.YangNodeType;
25import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Ramaa89bdd32016-07-08 20:45:41 +053026import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
Bharat saraswal780eca32016-04-05 12:45:45 +053027import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal9564b552016-08-12 12:00:23 +053028import org.onosproject.yangutils.utils.io.YangPluginConfig;
janani bb3be1332016-08-03 16:40:01 +053029
Vidyashree Ramaa89bdd32016-07-08 20:45:41 +053030import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
31import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
32import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
33import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
Bharat saraswal64e7e232016-07-14 23:33:55 +053034import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053035
36/**
Vidyashree Rama02f115f2016-04-18 12:29:39 +053037 * Representation of java code generator based on application schema.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053038 */
39public final class JavaCodeGeneratorUtil {
40
41 /**
Bharat saraswal780eca32016-04-05 12:45:45 +053042 * Current YANG node.
43 */
44 private static YangNode curNode;
45
46 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053047 * Root node.
48 */
49 private static YangNode rootNode;
50
51 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053052 * Creates a java code generator utility object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053053 */
54 private JavaCodeGeneratorUtil() {
55 }
56
57 /**
Bharat saraswal780eca32016-04-05 12:45:45 +053058 * Returns current YANG node.
59 *
60 * @return current YANG node
61 */
62 public static YangNode getCurNode() {
63 return curNode;
64 }
65
66 /**
67 * Sets current YANG node.
68 *
69 * @param node current YANG node
70 */
Bharat saraswal780eca32016-04-05 12:45:45 +053071 public static void setCurNode(YangNode node) {
72 curNode = node;
73 }
74
75 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053076 * Generates Java code files corresponding to the YANG schema.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053078 * @param rootNode root node of the data model tree
janani b1c6acc42016-04-15 16:18:30 +053079 * @param yangPlugin YANG plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +053080 * @throws TranslatorException when fails to generate java code file the current node
81 * @throws IOException when fails to do IO operations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053082 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +053083 public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin)
Bharat saraswal64e7e232016-07-14 23:33:55 +053084 throws TranslatorException, IOException {
Bharat saraswal780eca32016-04-05 12:45:45 +053085
Vinod Kumar S79a374b2016-04-30 21:09:15 +053086 YangNode codeGenNode = rootNode;
Bharat saraswal64e7e232016-07-14 23:33:55 +053087 setRootNode(rootNode);
Bharat saraswal780eca32016-04-05 12:45:45 +053088 TraversalType curTraversal = ROOT;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053089
Vinod Kumar S79a374b2016-04-30 21:09:15 +053090 while (codeGenNode != null) {
Bharat saraswal780eca32016-04-05 12:45:45 +053091 if (curTraversal != PARENT) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053092 if (!(codeGenNode instanceof JavaCodeGenerator)) {
Bharat saraswale3175d32016-08-31 17:50:11 +053093 throw new TranslatorException("Unsupported node to generate code " +
94 codeGenNode.getName() + " in " + codeGenNode.getLineNumber() + " at "
95 + codeGenNode.getCharPosition() + " in " + codeGenNode.getFileName());
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053096 }
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +053097 setCurNode(codeGenNode);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053098 try {
99 generateCodeEntry(codeGenNode, yangPlugin);
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530100 codeGenNode.setNameSpaceAndAddToParentSchemaMap();
Vidyashree Ramaa89bdd32016-07-08 20:45:41 +0530101 } catch (InvalidNodeForTranslatorException e) {
102 if (codeGenNode.getNextSibling() != null) {
103 curTraversal = SIBILING;
104 codeGenNode = codeGenNode.getNextSibling();
105 } else {
106 curTraversal = PARENT;
107 codeGenNode = codeGenNode.getParent();
108 }
109 continue;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530110 } catch (Exception e) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530111 e.printStackTrace();
Bharat saraswal64e7e232016-07-14 23:33:55 +0530112 close(codeGenNode, yangPlugin);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530113 throw new TranslatorException(e.getMessage());
114 }
115
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530116 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530117 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530118 curTraversal = CHILD;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530119 codeGenNode = codeGenNode.getChild();
120 } else if (codeGenNode.getNextSibling() != null) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530121 try {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530122 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530123 } catch (Exception e) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530124 e.printStackTrace();
Bharat saraswal64e7e232016-07-14 23:33:55 +0530125 close(codeGenNode, yangPlugin);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530126 throw new TranslatorException(e.getMessage());
127 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530128 curTraversal = SIBILING;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530129 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530130 } else {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530131 try {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530132 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530133 } catch (Exception e) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530134 e.printStackTrace();
Bharat saraswale50edca2016-08-05 01:58:25 +0530135 close(codeGenNode, yangPlugin);
136 throw new TranslatorException(e.getMessage());
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530137 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530138 curTraversal = PARENT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530139 codeGenNode = codeGenNode.getParent();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530140 }
141 }
142 }
143
144 /**
145 * Generates the current nodes code snippet.
146 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530147 * @param codeGenNode current data model node for which the code needs to be generated
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530148 * @param yangPlugin YANG plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +0530149 * @throws TranslatorException when fails to generate java code file the current node
150 * @throws IOException when fails to do IO operations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530151 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530152 private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
Bharat saraswal64e7e232016-07-14 23:33:55 +0530153 throws TranslatorException, IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530154
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530155 if (codeGenNode instanceof JavaCodeGenerator) {
156 ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530157 } else {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530158 close(codeGenNode, yangPlugin);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530159 throw new TranslatorException(
Bharat saraswale3175d32016-08-31 17:50:11 +0530160 "Generated data model node cannot be translated to target language code for " +
161 codeGenNode.getName() + " in " + codeGenNode.getLineNumber()
162 + " at " + codeGenNode.getCharPosition() + " in " + codeGenNode.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530163 }
164 }
165
166 /**
167 * Generates the current nodes code target code from the snippet.
168 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530169 * @param codeGenNode current data model node for which the code needs to be generated
170 * @param pluginConfig plugin configurations
171 * @throws TranslatorException when fails to generate java code file the current node
172 * @throws IOException when fails to do IO operations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530173 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530174 private static void generateCodeExit(YangNode codeGenNode, YangPluginConfig pluginConfig)
175 throws TranslatorException, IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530176
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530177 if (codeGenNode instanceof JavaCodeGenerator) {
178 ((JavaCodeGenerator) codeGenNode).generateCodeExit();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530179 } else {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530180 close(codeGenNode, pluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530181 throw new TranslatorException(
Bharat saraswale3175d32016-08-31 17:50:11 +0530182 "Generated data model node cannot be translated to target language code for " +
183 codeGenNode.getName() + " in " + codeGenNode.getLineNumber()
184 + " at " + codeGenNode.getCharPosition() + " in " + codeGenNode.getFileName());
Bharat saraswal780eca32016-04-05 12:45:45 +0530185 }
186 }
187
188 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530189 * Free other YANG nodes of data-model tree when error occurs while file generation of current node.
Bharat saraswal780eca32016-04-05 12:45:45 +0530190 */
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530191 private static void freeRestResources() {
Bharat saraswal780eca32016-04-05 12:45:45 +0530192
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530193 YangNode freedNode = getCurNode();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530194 if (getCurNode() != null) {
195 YangNode tempNode = freedNode;
196 TraversalType curTraversal = ROOT;
Bharat saraswal780eca32016-04-05 12:45:45 +0530197
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530198 while (freedNode != tempNode.getParent()) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530199
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530200 if (curTraversal != PARENT && freedNode.getChild() != null) {
201 curTraversal = CHILD;
202 freedNode = freedNode.getChild();
203 } else if (freedNode.getNextSibling() != null) {
204 curTraversal = SIBILING;
205 if (freedNode != tempNode) {
206 free(freedNode);
207 }
208 freedNode = freedNode.getNextSibling();
209 } else {
210 curTraversal = PARENT;
211 if (freedNode != tempNode) {
212 free(freedNode);
213 }
214 freedNode = freedNode.getParent();
Bharat saraswal780eca32016-04-05 12:45:45 +0530215 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530216 }
217 }
218 }
219
220 /**
221 * Free the current node.
222 *
223 * @param node YANG node
Bharat saraswal780eca32016-04-05 12:45:45 +0530224 */
Vidyashree Rama02f115f2016-04-18 12:29:39 +0530225 private static void free(YangNode node) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530226
227 YangNode parent = node.getParent();
228 parent.setChild(null);
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530229
Bharat saraswal780eca32016-04-05 12:45:45 +0530230 if (node.getNextSibling() != null) {
231 parent.setChild(node.getNextSibling());
232 } else if (node.getPreviousSibling() != null) {
233 parent.setChild(node.getPreviousSibling());
234 }
235 node = null;
236 }
237
238 /**
239 * Delete Java code files corresponding to the YANG schema.
240 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530241 * @param rootNode root node of data-model tree
242 * @param yangPluginConfig plugin configurations
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530243 * @throws IOException when fails to delete java code file the current node
Bharat saraswal780eca32016-04-05 12:45:45 +0530244 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530245 public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig)
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530246 throws IOException {
Bharat saraswal780eca32016-04-05 12:45:45 +0530247
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530248 if (rootNode != null) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530249 //Free other resources where translator has failed.
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530250 freeRestResources();
Bharat saraswal780eca32016-04-05 12:45:45 +0530251
Bharat saraswal64e7e232016-07-14 23:33:55 +0530252 // Start removing all open files.
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530253 YangNode tempNode = rootNode;
254 setCurNode(tempNode.getChild());
255 TraversalType curTraversal = ROOT;
Bharat saraswal780eca32016-04-05 12:45:45 +0530256
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530257 while (tempNode != null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530258
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530259 if (curTraversal != PARENT) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530260 close(tempNode, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530261 }
262 if (curTraversal != PARENT && tempNode.getChild() != null) {
263 curTraversal = CHILD;
264 tempNode = tempNode.getChild();
265 } else if (tempNode.getNextSibling() != null) {
266 curTraversal = SIBILING;
267 tempNode = tempNode.getNextSibling();
268 } else {
269 curTraversal = PARENT;
270 tempNode = tempNode.getParent();
271 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530272 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530273
274 freeRestResources();
Bharat saraswal780eca32016-04-05 12:45:45 +0530275 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530276 }
277
278 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530279 * Closes all the current open file handles of node and delete all generated files.
Bharat saraswal780eca32016-04-05 12:45:45 +0530280 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530281 * @param node current YANG node
282 * @param yangPlugin plugin configurations
Bharat saraswal780eca32016-04-05 12:45:45 +0530283 * @throws IOException when fails to do IO operations
284 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530285 private static void close(YangNode node, YangPluginConfig yangPlugin)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530286 throws IOException {
Vidyashree Rama5daea742016-05-20 16:29:25 +0530287 if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
288 .getTempJavaCodeFragmentFiles() != null) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530289 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
Bharat saraswale3175d32016-08-31 17:50:11 +0530290 }
291 if (getRootNode() != null) {
292 JavaFileInfoTranslator javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo();
293 if (javaFileInfo.getPackage() != null) {
294 searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() +
295 javaFileInfo.getPackageFilePath());
296 } else {
297 searchAndDeleteTempDir(yangPlugin.getCodeGenDir());
Bharat saraswal64e7e232016-07-14 23:33:55 +0530298 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530299 }
Bharat saraswale3175d32016-08-31 17:50:11 +0530300
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530301 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530302
303 /**
304 * Returns root node.
305 *
306 * @return root node
307 */
308 private static YangNode getRootNode() {
309 return rootNode;
310 }
311
312 /**
313 * Sets root node.
314 *
315 * @param rootNode root node
316 */
317 private static void setRootNode(YangNode rootNode) {
318 JavaCodeGeneratorUtil.rootNode = rootNode;
319 }
Bharat saraswalaab24b92016-08-02 18:43:16 +0530320
321 /**
322 * Searches child node in data model tree.
323 *
324 * @param parentNode parent node
325 * @param nodeType node type
326 * @param nodeName node name
327 * @return child node
328 */
329 public static YangNode searchYangNode(YangNode parentNode, YangNodeType nodeType, String nodeName) {
330 YangNode child = parentNode.getChild();
331 TraversalType curTraversal = ROOT;
332 if (child == null) {
janani bb3be1332016-08-03 16:40:01 +0530333 throw new IllegalArgumentException("Given parent node does not contain any child nodes");
Bharat saraswalaab24b92016-08-02 18:43:16 +0530334 }
335
336 while (child != null) {
337 if (curTraversal != PARENT) {
338 if (child instanceof YangInput || child instanceof YangOutput) {
339 if (child.getNodeType().equals(nodeType)) {
340 return child;
341 }
342 } else if (child.getName().equals(nodeName) && child.getNodeType().equals(nodeType)) {
343 return child;
344 }
345 }
346 if (curTraversal != PARENT && child.getChild() != null) {
347 curTraversal = CHILD;
348 child = child.getChild();
349 } else if (child.getNextSibling() != null) {
350 curTraversal = SIBILING;
351 child = child.getNextSibling();
352 } else {
353 curTraversal = PARENT;
354 child = child.getParent();
355 }
356 }
357 return null;
358 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530359
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530360}