blob: 089a1b3b24f37b82451dcf549b347067e92d3c10 [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)) {
93 throw new TranslatorException("Unsupported node to generate code");
94 }
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +053095 setCurNode(codeGenNode);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053096 try {
97 generateCodeEntry(codeGenNode, yangPlugin);
Gaurav Agrawal9564b552016-08-12 12:00:23 +053098 codeGenNode.setNameSpaceAndAddToParentSchemaMap();
Vidyashree Ramaa89bdd32016-07-08 20:45:41 +053099 } catch (InvalidNodeForTranslatorException e) {
100 if (codeGenNode.getNextSibling() != null) {
101 curTraversal = SIBILING;
102 codeGenNode = codeGenNode.getNextSibling();
103 } else {
104 curTraversal = PARENT;
105 codeGenNode = codeGenNode.getParent();
106 }
107 continue;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530108 } catch (Exception e) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530109 e.printStackTrace();
Bharat saraswal64e7e232016-07-14 23:33:55 +0530110 close(codeGenNode, yangPlugin);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530111 throw new TranslatorException(e.getMessage());
112 }
113
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530114 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530115 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530116 curTraversal = CHILD;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530117 codeGenNode = codeGenNode.getChild();
118 } else if (codeGenNode.getNextSibling() != null) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530119 try {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530120 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530121 } catch (Exception e) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530122 e.printStackTrace();
Bharat saraswal64e7e232016-07-14 23:33:55 +0530123 close(codeGenNode, yangPlugin);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530124 throw new TranslatorException(e.getMessage());
125 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530126 curTraversal = SIBILING;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530127 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530128 } else {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530129 try {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530130 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530131 } catch (Exception e) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530132 e.printStackTrace();
Bharat saraswale50edca2016-08-05 01:58:25 +0530133 close(codeGenNode, yangPlugin);
134 throw new TranslatorException(e.getMessage());
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530135 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530136 curTraversal = PARENT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530137 codeGenNode = codeGenNode.getParent();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530138 }
139 }
140 }
141
142 /**
143 * Generates the current nodes code snippet.
144 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530145 * @param codeGenNode current data model node for which the code needs to be generated
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530146 * @param yangPlugin YANG plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +0530147 * @throws TranslatorException when fails to generate java code file the current node
148 * @throws IOException when fails to do IO operations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530149 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530150 private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
Bharat saraswal64e7e232016-07-14 23:33:55 +0530151 throws TranslatorException, IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530152
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530153 if (codeGenNode instanceof JavaCodeGenerator) {
154 ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530155 } else {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530156 close(codeGenNode, yangPlugin);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530157 throw new TranslatorException(
Bharat saraswal780eca32016-04-05 12:45:45 +0530158 "Generated data model node cannot be translated to target language code");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530159 }
160 }
161
162 /**
163 * Generates the current nodes code target code from the snippet.
164 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530165 * @param codeGenNode current data model node for which the code needs to be generated
166 * @param pluginConfig plugin configurations
167 * @throws TranslatorException when fails to generate java code file the current node
168 * @throws IOException when fails to do IO operations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530169 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530170 private static void generateCodeExit(YangNode codeGenNode, YangPluginConfig pluginConfig)
171 throws TranslatorException, IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530172
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530173 if (codeGenNode instanceof JavaCodeGenerator) {
174 ((JavaCodeGenerator) codeGenNode).generateCodeExit();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530175 } else {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530176 close(codeGenNode, pluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530177 throw new TranslatorException(
Bharat saraswal780eca32016-04-05 12:45:45 +0530178 "Generated data model node cannot be translated to target language code");
179 }
180 }
181
182 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530183 * Free other YANG nodes of data-model tree when error occurs while file generation of current node.
Bharat saraswal780eca32016-04-05 12:45:45 +0530184 */
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530185 private static void freeRestResources() {
Bharat saraswal780eca32016-04-05 12:45:45 +0530186
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530187 YangNode freedNode = getCurNode();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530188 if (getCurNode() != null) {
189 YangNode tempNode = freedNode;
190 TraversalType curTraversal = ROOT;
Bharat saraswal780eca32016-04-05 12:45:45 +0530191
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530192 while (freedNode != tempNode.getParent()) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530193
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530194 if (curTraversal != PARENT && freedNode.getChild() != null) {
195 curTraversal = CHILD;
196 freedNode = freedNode.getChild();
197 } else if (freedNode.getNextSibling() != null) {
198 curTraversal = SIBILING;
199 if (freedNode != tempNode) {
200 free(freedNode);
201 }
202 freedNode = freedNode.getNextSibling();
203 } else {
204 curTraversal = PARENT;
205 if (freedNode != tempNode) {
206 free(freedNode);
207 }
208 freedNode = freedNode.getParent();
Bharat saraswal780eca32016-04-05 12:45:45 +0530209 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530210 }
211 }
212 }
213
214 /**
215 * Free the current node.
216 *
217 * @param node YANG node
Bharat saraswal780eca32016-04-05 12:45:45 +0530218 */
Vidyashree Rama02f115f2016-04-18 12:29:39 +0530219 private static void free(YangNode node) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530220
221 YangNode parent = node.getParent();
222 parent.setChild(null);
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530223
Bharat saraswal780eca32016-04-05 12:45:45 +0530224 if (node.getNextSibling() != null) {
225 parent.setChild(node.getNextSibling());
226 } else if (node.getPreviousSibling() != null) {
227 parent.setChild(node.getPreviousSibling());
228 }
229 node = null;
230 }
231
232 /**
233 * Delete Java code files corresponding to the YANG schema.
234 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530235 * @param rootNode root node of data-model tree
236 * @param yangPluginConfig plugin configurations
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530237 * @throws IOException when fails to delete java code file the current node
Bharat saraswal780eca32016-04-05 12:45:45 +0530238 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530239 public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig)
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530240 throws IOException {
Bharat saraswal780eca32016-04-05 12:45:45 +0530241
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530242 if (rootNode != null) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530243 //Free other resources where translator has failed.
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530244 freeRestResources();
Bharat saraswal780eca32016-04-05 12:45:45 +0530245
Bharat saraswal64e7e232016-07-14 23:33:55 +0530246 // Start removing all open files.
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530247 YangNode tempNode = rootNode;
248 setCurNode(tempNode.getChild());
249 TraversalType curTraversal = ROOT;
Bharat saraswal780eca32016-04-05 12:45:45 +0530250
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530251 while (tempNode != null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530252
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530253 if (curTraversal != PARENT) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530254 close(tempNode, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530255 }
256 if (curTraversal != PARENT && tempNode.getChild() != null) {
257 curTraversal = CHILD;
258 tempNode = tempNode.getChild();
259 } else if (tempNode.getNextSibling() != null) {
260 curTraversal = SIBILING;
261 tempNode = tempNode.getNextSibling();
262 } else {
263 curTraversal = PARENT;
264 tempNode = tempNode.getParent();
265 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530266 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530267
268 freeRestResources();
Bharat saraswal780eca32016-04-05 12:45:45 +0530269 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530270 }
271
272 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530273 * Closes all the current open file handles of node and delete all generated files.
Bharat saraswal780eca32016-04-05 12:45:45 +0530274 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530275 * @param node current YANG node
276 * @param yangPlugin plugin configurations
Bharat saraswal780eca32016-04-05 12:45:45 +0530277 * @throws IOException when fails to do IO operations
278 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530279 private static void close(YangNode node, YangPluginConfig yangPlugin)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530280 throws IOException {
Vidyashree Rama5daea742016-05-20 16:29:25 +0530281 if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
282 .getTempJavaCodeFragmentFiles() != null) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530283 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530284 } else {
285
janani bb3be1332016-08-03 16:40:01 +0530286 if (getRootNode() != null) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530287 JavaFileInfoTranslator javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo();
janani bb3be1332016-08-03 16:40:01 +0530288 if (javaFileInfo != null) {
289 searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() +
290 javaFileInfo.getPackageFilePath());
291 } else {
Bharat saraswal8beac342016-08-04 02:00:03 +0530292 searchAndDeleteTempDir(yangPlugin.getCodeGenDir());
janani bb3be1332016-08-03 16:40:01 +0530293 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530294 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530295 }
296 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530297
298 /**
299 * Returns root node.
300 *
301 * @return root node
302 */
303 private static YangNode getRootNode() {
304 return rootNode;
305 }
306
307 /**
308 * Sets root node.
309 *
310 * @param rootNode root node
311 */
312 private static void setRootNode(YangNode rootNode) {
313 JavaCodeGeneratorUtil.rootNode = rootNode;
314 }
Bharat saraswalaab24b92016-08-02 18:43:16 +0530315
316 /**
317 * Searches child node in data model tree.
318 *
319 * @param parentNode parent node
320 * @param nodeType node type
321 * @param nodeName node name
322 * @return child node
323 */
324 public static YangNode searchYangNode(YangNode parentNode, YangNodeType nodeType, String nodeName) {
325 YangNode child = parentNode.getChild();
326 TraversalType curTraversal = ROOT;
327 if (child == null) {
janani bb3be1332016-08-03 16:40:01 +0530328 throw new IllegalArgumentException("Given parent node does not contain any child nodes");
Bharat saraswalaab24b92016-08-02 18:43:16 +0530329 }
330
331 while (child != null) {
332 if (curTraversal != PARENT) {
333 if (child instanceof YangInput || child instanceof YangOutput) {
334 if (child.getNodeType().equals(nodeType)) {
335 return child;
336 }
337 } else if (child.getName().equals(nodeName) && child.getNodeType().equals(nodeType)) {
338 return child;
339 }
340 }
341 if (curTraversal != PARENT && child.getChild() != null) {
342 curTraversal = CHILD;
343 child = child.getChild();
344 } else if (child.getNextSibling() != null) {
345 curTraversal = SIBILING;
346 child = child.getNextSibling();
347 } else {
348 curTraversal = PARENT;
349 child = child.getParent();
350 }
351 }
352 return null;
353 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530354
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530355}