blob: 04bb2e2d3b4039d1c71baa81e1ca52dd9221a504 [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;
Vidyashree Rama405d2e62016-07-08 20:45:41 +053020import org.onosproject.yangutils.datamodel.TraversalType;
Vinod Kumar S38046502016-03-23 15:30:27 +053021import org.onosproject.yangutils.datamodel.YangNode;
Vidyashree Rama405d2e62016-07-08 20:45:41 +053022import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053023import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053024import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053025
Vidyashree Rama405d2e62016-07-08 20:45:41 +053026import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
27import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
28import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
29import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
Vinod Kumar S38046502016-03-23 15:30:27 +053030
31/**
Vidyashree Rama74453712016-04-18 12:29:39 +053032 * Representation of java code generator based on application schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053033 */
34public final class JavaCodeGeneratorUtil {
35
36 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053037 * Current YANG node.
38 */
39 private static YangNode curNode;
40
41 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042 * Creates a java code generator utility object.
Vinod Kumar S38046502016-03-23 15:30:27 +053043 */
44 private JavaCodeGeneratorUtil() {
45 }
46
47 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053048 * Returns current YANG node.
49 *
50 * @return current YANG node
51 */
52 public static YangNode getCurNode() {
53 return curNode;
54 }
55
56 /**
57 * Sets current YANG node.
58 *
59 * @param node current YANG node
60 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +053061 public static void setCurNode(YangNode node) {
62 curNode = node;
63 }
64
65 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Generates Java code files corresponding to the YANG schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053067 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053068 * @param rootNode root node of the data model tree
janani bde4ffab2016-04-15 16:18:30 +053069 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053070 * @throws TranslatorException when fails to generate java code file the current
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053071 * node
Vinod Kumar S38046502016-03-23 15:30:27 +053072 */
Bharat saraswal33dfa012016-05-17 19:59:16 +053073 public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053074 throws TranslatorException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053075
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076 YangNode codeGenNode = rootNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053077 TraversalType curTraversal = ROOT;
Vinod Kumar S38046502016-03-23 15:30:27 +053078
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079 while (codeGenNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053080 if (curTraversal != PARENT) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053081 if (!(codeGenNode instanceof JavaCodeGenerator)) {
82 throw new TranslatorException("Unsupported node to generate code");
83 }
Bharat saraswalcad0e652016-05-26 23:48:38 +053084 setCurNode(codeGenNode);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053085 try {
86 generateCodeEntry(codeGenNode, yangPlugin);
Vidyashree Rama405d2e62016-07-08 20:45:41 +053087 } catch (InvalidNodeForTranslatorException e) {
88 if (codeGenNode.getNextSibling() != null) {
89 curTraversal = SIBILING;
90 codeGenNode = codeGenNode.getNextSibling();
91 } else {
92 curTraversal = PARENT;
93 codeGenNode = codeGenNode.getParent();
94 }
95 continue;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053096 } catch (Exception e) {
97 throw new TranslatorException(e.getMessage());
98 }
99
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530101 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530102 curTraversal = CHILD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530103 codeGenNode = codeGenNode.getChild();
104 } else if (codeGenNode.getNextSibling() != null) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530105 try {
106 generateCodeExit(codeGenNode);
107 } catch (Exception e) {
108 throw new TranslatorException(e.getMessage());
109 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530110 curTraversal = SIBILING;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530111 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 } else {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530113 try {
114 generateCodeExit(codeGenNode);
115 } catch (Exception e) {
116 throw new TranslatorException(e.getMessage());
117 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530118 curTraversal = PARENT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530119 codeGenNode = codeGenNode.getParent();
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 }
121 }
122 }
123
124 /**
125 * Generates the current nodes code snippet.
126 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530127 * @param codeGenNode current data model node for which the code needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530128 * generated
129 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530130 * @throws TranslatorException when fails to generate java code file the current
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530131 * node
Vinod Kumar S38046502016-03-23 15:30:27 +0530132 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530133 private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530134 throws TranslatorException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530135
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 if (codeGenNode instanceof JavaCodeGenerator) {
137 ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +0530138 } else {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530139 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530140 "Generated data model node cannot be translated to target language code");
Vinod Kumar S38046502016-03-23 15:30:27 +0530141 }
142 }
143
144 /**
145 * Generates the current nodes code target code from the snippet.
146 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530147 * @param codeGenNode current data model node for which the code needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530148 * generated
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530149 * @throws TranslatorException when fails to generate java code file the current
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530150 * node
Vinod Kumar S38046502016-03-23 15:30:27 +0530151 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530152 private static void generateCodeExit(YangNode codeGenNode)
153 throws TranslatorException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530154
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530155 if (codeGenNode instanceof JavaCodeGenerator) {
156 ((JavaCodeGenerator) codeGenNode).generateCodeExit();
Vinod Kumar S38046502016-03-23 15:30:27 +0530157 } else {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530158 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530159 "Generated data model node cannot be translated to target language code");
160 }
161 }
162
163 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530164 * Free other YANG nodes of data-model tree when error occurs while file
165 * generation of current node.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530166 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530167 private static void freeRestResources() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530168
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530169 YangNode freedNode = getCurNode();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530170 if (getCurNode() != null) {
171 YangNode tempNode = freedNode;
172 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530173
Bharat saraswal33dfa012016-05-17 19:59:16 +0530174 while (freedNode != tempNode.getParent()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530175
Bharat saraswal33dfa012016-05-17 19:59:16 +0530176 if (curTraversal != PARENT && freedNode.getChild() != null) {
177 curTraversal = CHILD;
178 freedNode = freedNode.getChild();
179 } else if (freedNode.getNextSibling() != null) {
180 curTraversal = SIBILING;
181 if (freedNode != tempNode) {
182 free(freedNode);
183 }
184 freedNode = freedNode.getNextSibling();
185 } else {
186 curTraversal = PARENT;
187 if (freedNode != tempNode) {
188 free(freedNode);
189 }
190 freedNode = freedNode.getParent();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530191 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530192 }
193 }
194 }
195
196 /**
197 * Free the current node.
198 *
199 * @param node YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530200 */
Vidyashree Rama74453712016-04-18 12:29:39 +0530201 private static void free(YangNode node) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530202
203 YangNode parent = node.getParent();
204 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530205
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530206 if (node.getNextSibling() != null) {
207 parent.setChild(node.getNextSibling());
208 } else if (node.getPreviousSibling() != null) {
209 parent.setChild(node.getPreviousSibling());
210 }
211 node = null;
212 }
213
214 /**
215 * Delete Java code files corresponding to the YANG schema.
216 *
217 * @param rootNode root node of data-model tree
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530218 * @throws IOException when fails to delete java code file the current node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530219 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530220 public static void translatorErrorHandler(YangNode rootNode)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530221 throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530222
Bharat saraswal33dfa012016-05-17 19:59:16 +0530223 if (rootNode != null) {
224 /**
225 * Free other resources where translator has failed.
226 */
227 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530228
Bharat saraswal33dfa012016-05-17 19:59:16 +0530229 /**
230 * Start removing all open files.
231 */
232 YangNode tempNode = rootNode;
233 setCurNode(tempNode.getChild());
234 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530235
Bharat saraswal33dfa012016-05-17 19:59:16 +0530236 while (tempNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530237
Bharat saraswal33dfa012016-05-17 19:59:16 +0530238 if (curTraversal != PARENT) {
239 close(tempNode);
240 }
241 if (curTraversal != PARENT && tempNode.getChild() != null) {
242 curTraversal = CHILD;
243 tempNode = tempNode.getChild();
244 } else if (tempNode.getNextSibling() != null) {
245 curTraversal = SIBILING;
246 tempNode = tempNode.getNextSibling();
247 } else {
248 curTraversal = PARENT;
249 tempNode = tempNode.getParent();
250 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530251 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530252
253 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530254 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530255 }
256
257 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530258 * Closes all the current open file handles of node and delete all generated
259 * files.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530260 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530261 * @param node current YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530262 * @throws IOException when fails to do IO operations
263 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530264 private static void close(YangNode node)
265 throws IOException {
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530266 if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
267 .getTempJavaCodeFragmentFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530268 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 }
270 }
271}