blob: bde310dcf5bd11c3bc42d1f92769c7fdef4e0aee [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 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053042 * Creates a java code generator utility object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053043 */
44 private JavaCodeGeneratorUtil() {
45 }
46
47 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053048 * Generates Java code files corresponding to the YANG schema.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053049 *
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053050 * @param rootNode root node of the data model tree
janani b1c6acc42016-04-15 16:18:30 +053051 * @param yangPlugin YANG plugin config
Bharat saraswal64e7e232016-07-14 23:33:55 +053052 * @throws TranslatorException when fails to generate java code file the current node
53 * @throws IOException when fails to do IO operations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053054 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +053055 public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin)
Bharat saraswal64e7e232016-07-14 23:33:55 +053056 throws TranslatorException, IOException {
Bharat saraswal780eca32016-04-05 12:45:45 +053057
Vinod Kumar S79a374b2016-04-30 21:09:15 +053058 YangNode codeGenNode = rootNode;
Bharat saraswal780eca32016-04-05 12:45:45 +053059 TraversalType curTraversal = ROOT;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053060
Vinod Kumar S79a374b2016-04-30 21:09:15 +053061 while (codeGenNode != null) {
Bharat saraswal780eca32016-04-05 12:45:45 +053062 if (curTraversal != PARENT) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053063 if (!(codeGenNode instanceof JavaCodeGenerator)) {
Bharat saraswale3175d32016-08-31 17:50:11 +053064 throw new TranslatorException("Unsupported node to generate code " +
65 codeGenNode.getName() + " in " + codeGenNode.getLineNumber() + " at "
66 + codeGenNode.getCharPosition() + " in " + codeGenNode.getFileName());
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053067 }
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053068 try {
Vidyashree Rama17992c12016-11-30 11:49:27 +053069 generateCodeEntry(codeGenNode, yangPlugin, rootNode);
Gaurav Agrawal9564b552016-08-12 12:00:23 +053070 codeGenNode.setNameSpaceAndAddToParentSchemaMap();
Vidyashree Ramaa89bdd32016-07-08 20:45:41 +053071 } catch (InvalidNodeForTranslatorException e) {
72 if (codeGenNode.getNextSibling() != null) {
73 curTraversal = SIBILING;
74 codeGenNode = codeGenNode.getNextSibling();
75 } else {
76 curTraversal = PARENT;
77 codeGenNode = codeGenNode.getParent();
78 }
79 continue;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053080 } catch (Exception e) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +053081 e.printStackTrace();
Vidyashree Rama17992c12016-11-30 11:49:27 +053082 close(codeGenNode, yangPlugin, rootNode);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053083 throw new TranslatorException(e.getMessage());
84 }
85
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053086 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +053087 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal780eca32016-04-05 12:45:45 +053088 curTraversal = CHILD;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053089 codeGenNode = codeGenNode.getChild();
90 } else if (codeGenNode.getNextSibling() != null) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053091 try {
Vidyashree Rama17992c12016-11-30 11:49:27 +053092 generateCodeExit(codeGenNode, yangPlugin, rootNode);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053093 } catch (Exception e) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +053094 e.printStackTrace();
Vidyashree Rama17992c12016-11-30 11:49:27 +053095 close(codeGenNode, yangPlugin, rootNode);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053096 throw new TranslatorException(e.getMessage());
97 }
Bharat saraswal780eca32016-04-05 12:45:45 +053098 curTraversal = SIBILING;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053099 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530100 } else {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530101 try {
Vidyashree Rama17992c12016-11-30 11:49:27 +0530102 generateCodeExit(codeGenNode, yangPlugin, rootNode);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530103 } catch (Exception e) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530104 e.printStackTrace();
Vidyashree Rama17992c12016-11-30 11:49:27 +0530105 close(codeGenNode, yangPlugin, rootNode);
Bharat saraswale50edca2016-08-05 01:58:25 +0530106 throw new TranslatorException(e.getMessage());
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530107 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530108 curTraversal = PARENT;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530109 codeGenNode = codeGenNode.getParent();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530110 }
111 }
112 }
113
114 /**
115 * Generates the current nodes code snippet.
116 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530117 * @param codeGenNode current data model node for which the code needs to be generated
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530118 * @param yangPlugin YANG plugin config
Vidyashree Rama17992c12016-11-30 11:49:27 +0530119 * @param rootNode YANG root node
Bharat saraswal64e7e232016-07-14 23:33:55 +0530120 * @throws TranslatorException when fails to generate java code file the current node
121 * @throws IOException when fails to do IO operations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530122 */
Vidyashree Rama17992c12016-11-30 11:49:27 +0530123 private static void generateCodeEntry(YangNode codeGenNode,
124 YangPluginConfig yangPlugin,
125 YangNode rootNode)
Bharat saraswal64e7e232016-07-14 23:33:55 +0530126 throws TranslatorException, IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530127
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530128 if (codeGenNode instanceof JavaCodeGenerator) {
129 ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530130 } else {
Vidyashree Rama17992c12016-11-30 11:49:27 +0530131 close(codeGenNode, yangPlugin, rootNode);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530132 throw new TranslatorException(
Bharat saraswale3175d32016-08-31 17:50:11 +0530133 "Generated data model node cannot be translated to target language code for " +
134 codeGenNode.getName() + " in " + codeGenNode.getLineNumber()
135 + " at " + codeGenNode.getCharPosition() + " in " + codeGenNode.getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530136 }
137 }
138
139 /**
140 * Generates the current nodes code target code from the snippet.
141 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530142 * @param codeGenNode current data model node for which the code needs to be generated
143 * @param pluginConfig plugin configurations
Vidyashree Rama17992c12016-11-30 11:49:27 +0530144 * @param rootNode YANG root node
Bharat saraswal64e7e232016-07-14 23:33:55 +0530145 * @throws TranslatorException when fails to generate java code file the current node
146 * @throws IOException when fails to do IO operations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530147 */
Vidyashree Rama17992c12016-11-30 11:49:27 +0530148 private static void generateCodeExit(YangNode codeGenNode,
149 YangPluginConfig pluginConfig,
150 YangNode rootNode)
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).generateCodeExit();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530155 } else {
Vidyashree Rama17992c12016-11-30 11:49:27 +0530156 close(codeGenNode, pluginConfig, rootNode);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530157 throw new TranslatorException(
Bharat saraswale3175d32016-08-31 17:50:11 +0530158 "Generated data model node cannot be translated to target language code for " +
159 codeGenNode.getName() + " in " + codeGenNode.getLineNumber()
160 + " at " + codeGenNode.getCharPosition() + " in " + codeGenNode.getFileName());
Bharat saraswal780eca32016-04-05 12:45:45 +0530161 }
162 }
163
164 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530165 * Free other YANG nodes of data-model tree when error occurs while file generation of current node.
Vidyashree Rama17992c12016-11-30 11:49:27 +0530166 *
167 * @param freedNode current data model node
Bharat saraswal780eca32016-04-05 12:45:45 +0530168 */
Vidyashree Rama17992c12016-11-30 11:49:27 +0530169 private static void freeRestResources(YangNode freedNode) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530170
Vidyashree Rama17992c12016-11-30 11:49:27 +0530171 if (freedNode != null) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530172 YangNode tempNode = freedNode;
173 TraversalType curTraversal = ROOT;
Bharat saraswal780eca32016-04-05 12:45:45 +0530174
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530175 while (freedNode != tempNode.getParent()) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530176
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530177 if (curTraversal != PARENT && freedNode.getChild() != null) {
178 curTraversal = CHILD;
179 freedNode = freedNode.getChild();
180 } else if (freedNode.getNextSibling() != null) {
181 curTraversal = SIBILING;
182 if (freedNode != tempNode) {
183 free(freedNode);
184 }
185 freedNode = freedNode.getNextSibling();
186 } else {
187 curTraversal = PARENT;
188 if (freedNode != tempNode) {
189 free(freedNode);
190 }
191 freedNode = freedNode.getParent();
Bharat saraswal780eca32016-04-05 12:45:45 +0530192 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530193 }
194 }
195 }
196
197 /**
198 * Free the current node.
199 *
200 * @param node YANG node
Bharat saraswal780eca32016-04-05 12:45:45 +0530201 */
Vidyashree Rama02f115f2016-04-18 12:29:39 +0530202 private static void free(YangNode node) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530203
204 YangNode parent = node.getParent();
205 parent.setChild(null);
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530206
Bharat saraswal780eca32016-04-05 12:45:45 +0530207 if (node.getNextSibling() != null) {
208 parent.setChild(node.getNextSibling());
209 } else if (node.getPreviousSibling() != null) {
210 parent.setChild(node.getPreviousSibling());
211 }
212 node = null;
213 }
214
215 /**
216 * Delete Java code files corresponding to the YANG schema.
217 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530218 * @param rootNode root node of data-model tree
219 * @param yangPluginConfig plugin configurations
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530220 * @throws IOException when fails to delete java code file the current node
Bharat saraswal780eca32016-04-05 12:45:45 +0530221 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530222 public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig)
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530223 throws IOException {
Bharat saraswal780eca32016-04-05 12:45:45 +0530224
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530225 if (rootNode != null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530226
Bharat saraswal64e7e232016-07-14 23:33:55 +0530227 // Start removing all open files.
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530228 YangNode tempNode = rootNode;
Vidyashree Rama17992c12016-11-30 11:49:27 +0530229 YangNode curNode = tempNode.getChild();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530230 TraversalType curTraversal = ROOT;
Bharat saraswal780eca32016-04-05 12:45:45 +0530231
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530232 while (tempNode != null) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530233
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530234 if (curTraversal != PARENT) {
Vidyashree Rama17992c12016-11-30 11:49:27 +0530235 close(tempNode, yangPluginConfig, rootNode);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530236 }
237 if (curTraversal != PARENT && tempNode.getChild() != null) {
238 curTraversal = CHILD;
239 tempNode = tempNode.getChild();
240 } else if (tempNode.getNextSibling() != null) {
241 curTraversal = SIBILING;
242 tempNode = tempNode.getNextSibling();
243 } else {
244 curTraversal = PARENT;
245 tempNode = tempNode.getParent();
246 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530247 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530248
Vidyashree Rama17992c12016-11-30 11:49:27 +0530249 freeRestResources(curNode);
Bharat saraswal780eca32016-04-05 12:45:45 +0530250 }
Bharat saraswal780eca32016-04-05 12:45:45 +0530251 }
252
253 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530254 * Closes all the current open file handles of node and delete all generated files.
Bharat saraswal780eca32016-04-05 12:45:45 +0530255 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530256 * @param node current YANG node
257 * @param yangPlugin plugin configurations
Vidyashree Rama17992c12016-11-30 11:49:27 +0530258 * @param rootNode YANG root node
Bharat saraswal780eca32016-04-05 12:45:45 +0530259 * @throws IOException when fails to do IO operations
260 */
Vidyashree Rama17992c12016-11-30 11:49:27 +0530261 private static void close(YangNode node, YangPluginConfig yangPlugin,
262 YangNode rootNode)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530263 throws IOException {
Vidyashree Rama5daea742016-05-20 16:29:25 +0530264 if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
265 .getTempJavaCodeFragmentFiles() != null) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530266 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
Bharat saraswale3175d32016-08-31 17:50:11 +0530267 }
Vidyashree Rama17992c12016-11-30 11:49:27 +0530268 if (rootNode != null) {
269 JavaFileInfoTranslator javaFileInfo = ((JavaFileInfoContainer) rootNode).getJavaFileInfo();
Bharat saraswale3175d32016-08-31 17:50:11 +0530270 if (javaFileInfo.getPackage() != null) {
271 searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() +
Vidyashree Rama17992c12016-11-30 11:49:27 +0530272 javaFileInfo.getPackageFilePath());
Bharat saraswale3175d32016-08-31 17:50:11 +0530273 } else {
274 searchAndDeleteTempDir(yangPlugin.getCodeGenDir());
Bharat saraswal64e7e232016-07-14 23:33:55 +0530275 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530276 }
Bharat saraswale3175d32016-08-31 17:50:11 +0530277
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530278 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530279
280 /**
Bharat saraswalaab24b92016-08-02 18:43:16 +0530281 * Searches child node in data model tree.
282 *
283 * @param parentNode parent node
284 * @param nodeType node type
285 * @param nodeName node name
286 * @return child node
287 */
288 public static YangNode searchYangNode(YangNode parentNode, YangNodeType nodeType, String nodeName) {
289 YangNode child = parentNode.getChild();
290 TraversalType curTraversal = ROOT;
291 if (child == null) {
janani bb3be1332016-08-03 16:40:01 +0530292 throw new IllegalArgumentException("Given parent node does not contain any child nodes");
Bharat saraswalaab24b92016-08-02 18:43:16 +0530293 }
294
295 while (child != null) {
296 if (curTraversal != PARENT) {
297 if (child instanceof YangInput || child instanceof YangOutput) {
298 if (child.getNodeType().equals(nodeType)) {
299 return child;
300 }
301 } else if (child.getName().equals(nodeName) && child.getNodeType().equals(nodeType)) {
302 return child;
303 }
304 }
305 if (curTraversal != PARENT && child.getChild() != null) {
306 curTraversal = CHILD;
307 child = child.getChild();
308 } else if (child.getNextSibling() != null) {
309 curTraversal = SIBILING;
310 child = child.getNextSibling();
311 } else {
312 curTraversal = PARENT;
313 child = child.getParent();
314 }
315 }
316 return null;
317 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530318
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530319}