blob: 2534756d85ece91c0c6d323d726874c35552af0b [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;
Bharat saraswal6a5911f2016-08-02 18:43:16 +053022import org.onosproject.yangutils.datamodel.YangInput;
Vinod Kumar S38046502016-03-23 15:30:27 +053023import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal6a5911f2016-08-02 18:43:16 +053024import org.onosproject.yangutils.datamodel.YangNodeType;
25import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Rama405d2e62016-07-08 20:45:41 +053026import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053027import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053028import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswal6a5911f2016-08-02 18:43:16 +053029import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
Vidyashree Rama405d2e62016-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 saraswale707f032016-07-14 23:33:55 +053034import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053035
36/**
Vidyashree Rama74453712016-04-18 12:29:39 +053037 * Representation of java code generator based on application schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053038 */
39public final class JavaCodeGeneratorUtil {
40
41 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053042 * Current YANG node.
43 */
44 private static YangNode curNode;
45
46 /**
Bharat saraswale707f032016-07-14 23:33:55 +053047 * Root node.
48 */
49 private static YangNode rootNode;
50
51 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053052 * Creates a java code generator utility object.
Vinod Kumar S38046502016-03-23 15:30:27 +053053 */
54 private JavaCodeGeneratorUtil() {
55 }
56
57 /**
Bharat saraswal6ef0b762016-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 saraswal6ef0b762016-04-05 12:45:45 +053071 public static void setCurNode(YangNode node) {
72 curNode = node;
73 }
74
75 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053076 * Generates Java code files corresponding to the YANG schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053077 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053078 * @param rootNode root node of the data model tree
janani bde4ffab2016-04-15 16:18:30 +053079 * @param yangPlugin YANG plugin config
Bharat saraswale707f032016-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 S38046502016-03-23 15:30:27 +053082 */
Bharat saraswal33dfa012016-05-17 19:59:16 +053083 public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin)
Bharat saraswale707f032016-07-14 23:33:55 +053084 throws TranslatorException, IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053085
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086 YangNode codeGenNode = rootNode;
Bharat saraswale707f032016-07-14 23:33:55 +053087 setRootNode(rootNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053088 TraversalType curTraversal = ROOT;
Vinod Kumar S38046502016-03-23 15:30:27 +053089
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053090 while (codeGenNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053091 if (curTraversal != PARENT) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053092 if (!(codeGenNode instanceof JavaCodeGenerator)) {
93 throw new TranslatorException("Unsupported node to generate code");
94 }
Bharat saraswalcad0e652016-05-26 23:48:38 +053095 setCurNode(codeGenNode);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053096 try {
97 generateCodeEntry(codeGenNode, yangPlugin);
Vidyashree Rama405d2e62016-07-08 20:45:41 +053098 } catch (InvalidNodeForTranslatorException e) {
99 if (codeGenNode.getNextSibling() != null) {
100 curTraversal = SIBILING;
101 codeGenNode = codeGenNode.getNextSibling();
102 } else {
103 curTraversal = PARENT;
104 codeGenNode = codeGenNode.getParent();
105 }
106 continue;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530107 } catch (Exception e) {
Bharat saraswale707f032016-07-14 23:33:55 +0530108 close(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530109 throw new TranslatorException(e.getMessage());
110 }
111
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530113 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530114 curTraversal = CHILD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530115 codeGenNode = codeGenNode.getChild();
116 } else if (codeGenNode.getNextSibling() != null) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530117 try {
Bharat saraswale707f032016-07-14 23:33:55 +0530118 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530119 } catch (Exception e) {
Bharat saraswale707f032016-07-14 23:33:55 +0530120 close(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530121 throw new TranslatorException(e.getMessage());
122 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530123 curTraversal = SIBILING;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530124 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S38046502016-03-23 15:30:27 +0530125 } else {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530126 try {
Bharat saraswale707f032016-07-14 23:33:55 +0530127 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530128 } catch (Exception e) {
Bharat saraswale707f032016-07-14 23:33:55 +0530129 close(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530130 throw new TranslatorException(e.getMessage());
131 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530132 curTraversal = PARENT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530133 codeGenNode = codeGenNode.getParent();
Vinod Kumar S38046502016-03-23 15:30:27 +0530134 }
135 }
136 }
137
138 /**
139 * Generates the current nodes code snippet.
140 *
Bharat saraswale707f032016-07-14 23:33:55 +0530141 * @param codeGenNode current data model node for which the code needs to be generated
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530142 * @param yangPlugin YANG plugin config
Bharat saraswale707f032016-07-14 23:33:55 +0530143 * @throws TranslatorException when fails to generate java code file the current node
144 * @throws IOException when fails to do IO operations
Vinod Kumar S38046502016-03-23 15:30:27 +0530145 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530146 private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
Bharat saraswale707f032016-07-14 23:33:55 +0530147 throws TranslatorException, IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530148
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530149 if (codeGenNode instanceof JavaCodeGenerator) {
150 ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +0530151 } else {
Bharat saraswale707f032016-07-14 23:33:55 +0530152 close(codeGenNode, yangPlugin);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530153 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530154 "Generated data model node cannot be translated to target language code");
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 }
156 }
157
158 /**
159 * Generates the current nodes code target code from the snippet.
160 *
Bharat saraswale707f032016-07-14 23:33:55 +0530161 * @param codeGenNode current data model node for which the code needs to be generated
162 * @param pluginConfig plugin configurations
163 * @throws TranslatorException when fails to generate java code file the current node
164 * @throws IOException when fails to do IO operations
Vinod Kumar S38046502016-03-23 15:30:27 +0530165 */
Bharat saraswale707f032016-07-14 23:33:55 +0530166 private static void generateCodeExit(YangNode codeGenNode, YangPluginConfig pluginConfig)
167 throws TranslatorException, IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530168
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530169 if (codeGenNode instanceof JavaCodeGenerator) {
170 ((JavaCodeGenerator) codeGenNode).generateCodeExit();
Vinod Kumar S38046502016-03-23 15:30:27 +0530171 } else {
Bharat saraswale707f032016-07-14 23:33:55 +0530172 close(codeGenNode, pluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530173 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530174 "Generated data model node cannot be translated to target language code");
175 }
176 }
177
178 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530179 * Free other YANG nodes of data-model tree when error occurs while file generation of current node.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530180 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530181 private static void freeRestResources() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530182
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530183 YangNode freedNode = getCurNode();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530184 if (getCurNode() != null) {
185 YangNode tempNode = freedNode;
186 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530187
Bharat saraswal33dfa012016-05-17 19:59:16 +0530188 while (freedNode != tempNode.getParent()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530189
Bharat saraswal33dfa012016-05-17 19:59:16 +0530190 if (curTraversal != PARENT && freedNode.getChild() != null) {
191 curTraversal = CHILD;
192 freedNode = freedNode.getChild();
193 } else if (freedNode.getNextSibling() != null) {
194 curTraversal = SIBILING;
195 if (freedNode != tempNode) {
196 free(freedNode);
197 }
198 freedNode = freedNode.getNextSibling();
199 } else {
200 curTraversal = PARENT;
201 if (freedNode != tempNode) {
202 free(freedNode);
203 }
204 freedNode = freedNode.getParent();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530205 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530206 }
207 }
208 }
209
210 /**
211 * Free the current node.
212 *
213 * @param node YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530214 */
Vidyashree Rama74453712016-04-18 12:29:39 +0530215 private static void free(YangNode node) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530216
217 YangNode parent = node.getParent();
218 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530219
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530220 if (node.getNextSibling() != null) {
221 parent.setChild(node.getNextSibling());
222 } else if (node.getPreviousSibling() != null) {
223 parent.setChild(node.getPreviousSibling());
224 }
225 node = null;
226 }
227
228 /**
229 * Delete Java code files corresponding to the YANG schema.
230 *
Bharat saraswale707f032016-07-14 23:33:55 +0530231 * @param rootNode root node of data-model tree
232 * @param yangPluginConfig plugin configurations
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530233 * @throws IOException when fails to delete java code file the current node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530234 */
Bharat saraswale707f032016-07-14 23:33:55 +0530235 public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530236 throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530237
Bharat saraswal33dfa012016-05-17 19:59:16 +0530238 if (rootNode != null) {
Bharat saraswale707f032016-07-14 23:33:55 +0530239 //Free other resources where translator has failed.
Bharat saraswal33dfa012016-05-17 19:59:16 +0530240 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530241
Bharat saraswale707f032016-07-14 23:33:55 +0530242 // Start removing all open files.
Bharat saraswal33dfa012016-05-17 19:59:16 +0530243 YangNode tempNode = rootNode;
244 setCurNode(tempNode.getChild());
245 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530246
Bharat saraswal33dfa012016-05-17 19:59:16 +0530247 while (tempNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530248
Bharat saraswal33dfa012016-05-17 19:59:16 +0530249 if (curTraversal != PARENT) {
Bharat saraswale707f032016-07-14 23:33:55 +0530250 close(tempNode, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530251 }
252 if (curTraversal != PARENT && tempNode.getChild() != null) {
253 curTraversal = CHILD;
254 tempNode = tempNode.getChild();
255 } else if (tempNode.getNextSibling() != null) {
256 curTraversal = SIBILING;
257 tempNode = tempNode.getNextSibling();
258 } else {
259 curTraversal = PARENT;
260 tempNode = tempNode.getParent();
261 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530262 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530263
264 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530265 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530266 }
267
268 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530269 * Closes all the current open file handles of node and delete all generated files.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530270 *
Bharat saraswale707f032016-07-14 23:33:55 +0530271 * @param node current YANG node
272 * @param yangPlugin plugin configurations
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530273 * @throws IOException when fails to do IO operations
274 */
Bharat saraswale707f032016-07-14 23:33:55 +0530275 private static void close(YangNode node, YangPluginConfig yangPlugin)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530276 throws IOException {
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530277 if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
278 .getTempJavaCodeFragmentFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530279 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
Bharat saraswale707f032016-07-14 23:33:55 +0530280 } else {
281
282 JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo();
283 if (javaFileInfo != null) {
284 searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() +
285 javaFileInfo.getPackageFilePath());
286 } else {
287 searchAndDeleteTempDir(yangPlugin.getManagerCodeGenDir());
288 }
289
Vinod Kumar S38046502016-03-23 15:30:27 +0530290 }
291 }
Bharat saraswale707f032016-07-14 23:33:55 +0530292
293 /**
294 * Returns root node.
295 *
296 * @return root node
297 */
298 private static YangNode getRootNode() {
299 return rootNode;
300 }
301
302 /**
303 * Sets root node.
304 *
305 * @param rootNode root node
306 */
307 private static void setRootNode(YangNode rootNode) {
308 JavaCodeGeneratorUtil.rootNode = rootNode;
309 }
Bharat saraswal6a5911f2016-08-02 18:43:16 +0530310
311 /**
312 * Searches child node in data model tree.
313 *
314 * @param parentNode parent node
315 * @param nodeType node type
316 * @param nodeName node name
317 * @return child node
318 */
319 public static YangNode searchYangNode(YangNode parentNode, YangNodeType nodeType, String nodeName) {
320 YangNode child = parentNode.getChild();
321 TraversalType curTraversal = ROOT;
322 if (child == null) {
323 throw new IllegalArgumentException("given parent node does not contain any child nodes");
324 }
325
326 while (child != null) {
327 if (curTraversal != PARENT) {
328 if (child instanceof YangInput || child instanceof YangOutput) {
329 if (child.getNodeType().equals(nodeType)) {
330 return child;
331 }
332 } else if (child.getName().equals(nodeName) && child.getNodeType().equals(nodeType)) {
333 return child;
334 }
335 }
336 if (curTraversal != PARENT && child.getChild() != null) {
337 curTraversal = CHILD;
338 child = child.getChild();
339 } else if (child.getNextSibling() != null) {
340 curTraversal = SIBILING;
341 child = child.getNextSibling();
342 } else {
343 curTraversal = PARENT;
344 child = child.getParent();
345 }
346 }
347 return null;
348 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530349}