blob: 79857eb5085219d4f7dbd799bb7377a480978376 [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-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 saraswale707f032016-07-14 23:33:55 +053020
Vidyashree Rama405d2e62016-07-08 20:45:41 +053021import org.onosproject.yangutils.datamodel.TraversalType;
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.YangNode;
Vidyashree Rama405d2e62016-07-08 20:45:41 +053023import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053024import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053025import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053026
Vidyashree Rama405d2e62016-07-08 20:45:41 +053027import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
28import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
29import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
30import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
Bharat saraswale707f032016-07-14 23:33:55 +053031import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053032
33/**
Vidyashree Rama74453712016-04-18 12:29:39 +053034 * Representation of java code generator based on application schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053035 */
36public final class JavaCodeGeneratorUtil {
37
38 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053039 * Current YANG node.
40 */
41 private static YangNode curNode;
42
43 /**
Bharat saraswale707f032016-07-14 23:33:55 +053044 * Root node.
45 */
46 private static YangNode rootNode;
47
48 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049 * Creates a java code generator utility object.
Vinod Kumar S38046502016-03-23 15:30:27 +053050 */
51 private JavaCodeGeneratorUtil() {
52 }
53
54 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053055 * Returns current YANG node.
56 *
57 * @return current YANG node
58 */
59 public static YangNode getCurNode() {
60 return curNode;
61 }
62
63 /**
64 * Sets current YANG node.
65 *
66 * @param node current YANG node
67 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +053068 public static void setCurNode(YangNode node) {
69 curNode = node;
70 }
71
72 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053073 * Generates Java code files corresponding to the YANG schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053074 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053075 * @param rootNode root node of the data model tree
janani bde4ffab2016-04-15 16:18:30 +053076 * @param yangPlugin YANG plugin config
Bharat saraswale707f032016-07-14 23:33:55 +053077 * @throws TranslatorException when fails to generate java code file the current node
78 * @throws IOException when fails to do IO operations
Vinod Kumar S38046502016-03-23 15:30:27 +053079 */
Bharat saraswal33dfa012016-05-17 19:59:16 +053080 public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin)
Bharat saraswale707f032016-07-14 23:33:55 +053081 throws TranslatorException, IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053082
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083 YangNode codeGenNode = rootNode;
Bharat saraswale707f032016-07-14 23:33:55 +053084 setRootNode(rootNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053085 TraversalType curTraversal = ROOT;
Vinod Kumar S38046502016-03-23 15:30:27 +053086
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087 while (codeGenNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053088 if (curTraversal != PARENT) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053089 if (!(codeGenNode instanceof JavaCodeGenerator)) {
90 throw new TranslatorException("Unsupported node to generate code");
91 }
Bharat saraswalcad0e652016-05-26 23:48:38 +053092 setCurNode(codeGenNode);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053093 try {
94 generateCodeEntry(codeGenNode, yangPlugin);
Vidyashree Rama405d2e62016-07-08 20:45:41 +053095 } catch (InvalidNodeForTranslatorException e) {
96 if (codeGenNode.getNextSibling() != null) {
97 curTraversal = SIBILING;
98 codeGenNode = codeGenNode.getNextSibling();
99 } else {
100 curTraversal = PARENT;
101 codeGenNode = codeGenNode.getParent();
102 }
103 continue;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530104 } catch (Exception e) {
Bharat saraswale707f032016-07-14 23:33:55 +0530105 close(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530106 throw new TranslatorException(e.getMessage());
107 }
108
Vinod Kumar S38046502016-03-23 15:30:27 +0530109 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530110 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530111 curTraversal = CHILD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530112 codeGenNode = codeGenNode.getChild();
113 } else if (codeGenNode.getNextSibling() != null) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530114 try {
Bharat saraswale707f032016-07-14 23:33:55 +0530115 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530116 } catch (Exception e) {
Bharat saraswale707f032016-07-14 23:33:55 +0530117 close(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530118 throw new TranslatorException(e.getMessage());
119 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530120 curTraversal = SIBILING;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530121 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S38046502016-03-23 15:30:27 +0530122 } else {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530123 try {
Bharat saraswale707f032016-07-14 23:33:55 +0530124 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530125 } catch (Exception e) {
Bharat saraswale707f032016-07-14 23:33:55 +0530126 close(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530127 throw new TranslatorException(e.getMessage());
128 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530129 curTraversal = PARENT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530130 codeGenNode = codeGenNode.getParent();
Vinod Kumar S38046502016-03-23 15:30:27 +0530131 }
132 }
133 }
134
135 /**
136 * Generates the current nodes code snippet.
137 *
Bharat saraswale707f032016-07-14 23:33:55 +0530138 * @param codeGenNode current data model node for which the code needs to be generated
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530139 * @param yangPlugin YANG plugin config
Bharat saraswale707f032016-07-14 23:33:55 +0530140 * @throws TranslatorException when fails to generate java code file the current node
141 * @throws IOException when fails to do IO operations
Vinod Kumar S38046502016-03-23 15:30:27 +0530142 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530143 private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
Bharat saraswale707f032016-07-14 23:33:55 +0530144 throws TranslatorException, IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530145
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530146 if (codeGenNode instanceof JavaCodeGenerator) {
147 ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +0530148 } else {
Bharat saraswale707f032016-07-14 23:33:55 +0530149 close(codeGenNode, yangPlugin);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530150 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530151 "Generated data model node cannot be translated to target language code");
Vinod Kumar S38046502016-03-23 15:30:27 +0530152 }
153 }
154
155 /**
156 * Generates the current nodes code target code from the snippet.
157 *
Bharat saraswale707f032016-07-14 23:33:55 +0530158 * @param codeGenNode current data model node for which the code needs to be generated
159 * @param pluginConfig plugin configurations
160 * @throws TranslatorException when fails to generate java code file the current node
161 * @throws IOException when fails to do IO operations
Vinod Kumar S38046502016-03-23 15:30:27 +0530162 */
Bharat saraswale707f032016-07-14 23:33:55 +0530163 private static void generateCodeExit(YangNode codeGenNode, YangPluginConfig pluginConfig)
164 throws TranslatorException, IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530165
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530166 if (codeGenNode instanceof JavaCodeGenerator) {
167 ((JavaCodeGenerator) codeGenNode).generateCodeExit();
Vinod Kumar S38046502016-03-23 15:30:27 +0530168 } else {
Bharat saraswale707f032016-07-14 23:33:55 +0530169 close(codeGenNode, pluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530170 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530171 "Generated data model node cannot be translated to target language code");
172 }
173 }
174
175 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530176 * Free other YANG nodes of data-model tree when error occurs while file generation of current node.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530177 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530178 private static void freeRestResources() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530179
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 YangNode freedNode = getCurNode();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530181 if (getCurNode() != null) {
182 YangNode tempNode = freedNode;
183 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530184
Bharat saraswal33dfa012016-05-17 19:59:16 +0530185 while (freedNode != tempNode.getParent()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530186
Bharat saraswal33dfa012016-05-17 19:59:16 +0530187 if (curTraversal != PARENT && freedNode.getChild() != null) {
188 curTraversal = CHILD;
189 freedNode = freedNode.getChild();
190 } else if (freedNode.getNextSibling() != null) {
191 curTraversal = SIBILING;
192 if (freedNode != tempNode) {
193 free(freedNode);
194 }
195 freedNode = freedNode.getNextSibling();
196 } else {
197 curTraversal = PARENT;
198 if (freedNode != tempNode) {
199 free(freedNode);
200 }
201 freedNode = freedNode.getParent();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530202 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530203 }
204 }
205 }
206
207 /**
208 * Free the current node.
209 *
210 * @param node YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530211 */
Vidyashree Rama74453712016-04-18 12:29:39 +0530212 private static void free(YangNode node) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530213
214 YangNode parent = node.getParent();
215 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530216
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530217 if (node.getNextSibling() != null) {
218 parent.setChild(node.getNextSibling());
219 } else if (node.getPreviousSibling() != null) {
220 parent.setChild(node.getPreviousSibling());
221 }
222 node = null;
223 }
224
225 /**
226 * Delete Java code files corresponding to the YANG schema.
227 *
Bharat saraswale707f032016-07-14 23:33:55 +0530228 * @param rootNode root node of data-model tree
229 * @param yangPluginConfig plugin configurations
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530230 * @throws IOException when fails to delete java code file the current node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530231 */
Bharat saraswale707f032016-07-14 23:33:55 +0530232 public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530233 throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530234
Bharat saraswal33dfa012016-05-17 19:59:16 +0530235 if (rootNode != null) {
Bharat saraswale707f032016-07-14 23:33:55 +0530236 //Free other resources where translator has failed.
Bharat saraswal33dfa012016-05-17 19:59:16 +0530237 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530238
Bharat saraswale707f032016-07-14 23:33:55 +0530239 // Start removing all open files.
Bharat saraswal33dfa012016-05-17 19:59:16 +0530240 YangNode tempNode = rootNode;
241 setCurNode(tempNode.getChild());
242 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530243
Bharat saraswal33dfa012016-05-17 19:59:16 +0530244 while (tempNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530245
Bharat saraswal33dfa012016-05-17 19:59:16 +0530246 if (curTraversal != PARENT) {
Bharat saraswale707f032016-07-14 23:33:55 +0530247 close(tempNode, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530248 }
249 if (curTraversal != PARENT && tempNode.getChild() != null) {
250 curTraversal = CHILD;
251 tempNode = tempNode.getChild();
252 } else if (tempNode.getNextSibling() != null) {
253 curTraversal = SIBILING;
254 tempNode = tempNode.getNextSibling();
255 } else {
256 curTraversal = PARENT;
257 tempNode = tempNode.getParent();
258 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530259 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530260
261 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530262 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530263 }
264
265 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530266 * Closes all the current open file handles of node and delete all generated files.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530267 *
Bharat saraswale707f032016-07-14 23:33:55 +0530268 * @param node current YANG node
269 * @param yangPlugin plugin configurations
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530270 * @throws IOException when fails to do IO operations
271 */
Bharat saraswale707f032016-07-14 23:33:55 +0530272 private static void close(YangNode node, YangPluginConfig yangPlugin)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530273 throws IOException {
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530274 if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
275 .getTempJavaCodeFragmentFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530276 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
Bharat saraswale707f032016-07-14 23:33:55 +0530277 } else {
278
279 JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo();
280 if (javaFileInfo != null) {
281 searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() +
282 javaFileInfo.getPackageFilePath());
283 } else {
284 searchAndDeleteTempDir(yangPlugin.getManagerCodeGenDir());
285 }
286
Vinod Kumar S38046502016-03-23 15:30:27 +0530287 }
288 }
Bharat saraswale707f032016-07-14 23:33:55 +0530289
290 /**
291 * Returns root node.
292 *
293 * @return root node
294 */
295 private static YangNode getRootNode() {
296 return rootNode;
297 }
298
299 /**
300 * Sets root node.
301 *
302 * @param rootNode root node
303 */
304 private static void setRootNode(YangNode rootNode) {
305 JavaCodeGeneratorUtil.rootNode = rootNode;
306 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530307}