blob: 77ff157f4f6ab462c791cb136206dd88d621d448 [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 saraswal2d90b0c2016-08-04 02:00:03 +053020import java.util.ArrayList;
21import java.util.List;
22
Vidyashree Rama405d2e62016-07-08 20:45:41 +053023import org.onosproject.yangutils.datamodel.TraversalType;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053024import org.onosproject.yangutils.datamodel.YangAugment;
25import org.onosproject.yangutils.datamodel.YangCase;
26import org.onosproject.yangutils.datamodel.YangChoice;
Bharat saraswal6a5911f2016-08-02 18:43:16 +053027import org.onosproject.yangutils.datamodel.YangInput;
Vinod Kumar S38046502016-03-23 15:30:27 +053028import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal6a5911f2016-08-02 18:43:16 +053029import org.onosproject.yangutils.datamodel.YangNodeType;
30import org.onosproject.yangutils.datamodel.YangOutput;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053031import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
janani bf9819ff2016-08-03 16:40:01 +053032import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
33import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Vidyashree Rama405d2e62016-07-08 20:45:41 +053034import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053035import org.onosproject.yangutils.translator.exception.TranslatorException;
janani bf9819ff2016-08-03 16:40:01 +053036
Vidyashree Rama405d2e62016-07-08 20:45:41 +053037import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
38import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
39import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
40import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053041import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
42import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangCaseNode;
43import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.getAugmentClassName;
44import static org.onosproject.yangutils.utils.UtilConstants.CASE;
45import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Bharat saraswale707f032016-07-14 23:33:55 +053046import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053047
48/**
Vidyashree Rama74453712016-04-18 12:29:39 +053049 * Representation of java code generator based on application schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053050 */
51public final class JavaCodeGeneratorUtil {
52
53 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053054 * Current YANG node.
55 */
56 private static YangNode curNode;
57
58 /**
Bharat saraswale707f032016-07-14 23:33:55 +053059 * Root node.
60 */
61 private static YangNode rootNode;
62
63 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053064 * Creates a java code generator utility object.
Vinod Kumar S38046502016-03-23 15:30:27 +053065 */
66 private JavaCodeGeneratorUtil() {
67 }
68
69 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053070 * Returns current YANG node.
71 *
72 * @return current YANG node
73 */
74 public static YangNode getCurNode() {
75 return curNode;
76 }
77
78 /**
79 * Sets current YANG node.
80 *
81 * @param node current YANG node
82 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +053083 public static void setCurNode(YangNode node) {
84 curNode = node;
85 }
86
87 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053088 * Generates Java code files corresponding to the YANG schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053089 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053090 * @param rootNode root node of the data model tree
janani bde4ffab2016-04-15 16:18:30 +053091 * @param yangPlugin YANG plugin config
Bharat saraswale707f032016-07-14 23:33:55 +053092 * @throws TranslatorException when fails to generate java code file the current node
93 * @throws IOException when fails to do IO operations
Vinod Kumar S38046502016-03-23 15:30:27 +053094 */
Bharat saraswal33dfa012016-05-17 19:59:16 +053095 public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin)
Bharat saraswale707f032016-07-14 23:33:55 +053096 throws TranslatorException, IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053097
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053098 YangNode codeGenNode = rootNode;
Bharat saraswale707f032016-07-14 23:33:55 +053099 setRootNode(rootNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530100 TraversalType curTraversal = ROOT;
Vinod Kumar S38046502016-03-23 15:30:27 +0530101
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530102 while (codeGenNode != null) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530103 if (codeGenNode instanceof YangAugment) {
104 if (((YangAugment) codeGenNode).getAugmentedNode() instanceof YangChoice) {
105 addCaseNodeToChoiceTarget((YangAugment) codeGenNode);
106 }
107 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530108 if (curTraversal != PARENT) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530109 if (!(codeGenNode instanceof JavaCodeGenerator)) {
110 throw new TranslatorException("Unsupported node to generate code");
111 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530112 setCurNode(codeGenNode);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530113 try {
114 generateCodeEntry(codeGenNode, yangPlugin);
Vidyashree Rama405d2e62016-07-08 20:45:41 +0530115 } catch (InvalidNodeForTranslatorException e) {
116 if (codeGenNode.getNextSibling() != null) {
117 curTraversal = SIBILING;
118 codeGenNode = codeGenNode.getNextSibling();
119 } else {
120 curTraversal = PARENT;
121 codeGenNode = codeGenNode.getParent();
122 }
123 continue;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530124 } catch (Exception e) {
Bharat saraswale707f032016-07-14 23:33:55 +0530125 close(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530126 throw new TranslatorException(e.getMessage());
127 }
128
Vinod Kumar S38046502016-03-23 15:30:27 +0530129 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530130 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530131 curTraversal = CHILD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530132 codeGenNode = codeGenNode.getChild();
133 } else if (codeGenNode.getNextSibling() != null) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530134 try {
Bharat saraswale707f032016-07-14 23:33:55 +0530135 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530136 } catch (Exception e) {
Bharat saraswale707f032016-07-14 23:33:55 +0530137 close(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530138 throw new TranslatorException(e.getMessage());
139 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530140 curTraversal = SIBILING;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530141 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S38046502016-03-23 15:30:27 +0530142 } else {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530143 try {
Bharat saraswale707f032016-07-14 23:33:55 +0530144 generateCodeExit(codeGenNode, yangPlugin);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530145 } catch (Exception e) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530146 close(codeGenNode, yangPlugin);
147 throw new TranslatorException(e.getMessage());
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530148 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530149 curTraversal = PARENT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530150 codeGenNode = codeGenNode.getParent();
Vinod Kumar S38046502016-03-23 15:30:27 +0530151 }
152 }
153 }
154
155 /**
156 * Generates the current nodes code snippet.
157 *
Bharat saraswale707f032016-07-14 23:33:55 +0530158 * @param codeGenNode current data model node for which the code needs to be generated
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530159 * @param yangPlugin YANG plugin config
Bharat saraswale707f032016-07-14 23:33:55 +0530160 * @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 saraswal33dfa012016-05-17 19:59:16 +0530163 private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
Bharat saraswale707f032016-07-14 23:33:55 +0530164 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).generateCodeEntry(yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +0530168 } else {
Bharat saraswale707f032016-07-14 23:33:55 +0530169 close(codeGenNode, yangPlugin);
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");
Vinod Kumar S38046502016-03-23 15:30:27 +0530172 }
173 }
174
175 /**
176 * Generates the current nodes code target code from the snippet.
177 *
Bharat saraswale707f032016-07-14 23:33:55 +0530178 * @param codeGenNode current data model node for which the code needs to be generated
179 * @param pluginConfig plugin configurations
180 * @throws TranslatorException when fails to generate java code file the current node
181 * @throws IOException when fails to do IO operations
Vinod Kumar S38046502016-03-23 15:30:27 +0530182 */
Bharat saraswale707f032016-07-14 23:33:55 +0530183 private static void generateCodeExit(YangNode codeGenNode, YangPluginConfig pluginConfig)
184 throws TranslatorException, IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530185
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530186 if (codeGenNode instanceof JavaCodeGenerator) {
187 ((JavaCodeGenerator) codeGenNode).generateCodeExit();
Vinod Kumar S38046502016-03-23 15:30:27 +0530188 } else {
Bharat saraswale707f032016-07-14 23:33:55 +0530189 close(codeGenNode, pluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530190 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530191 "Generated data model node cannot be translated to target language code");
192 }
193 }
194
195 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530196 * Free other YANG nodes of data-model tree when error occurs while file generation of current node.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530197 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530198 private static void freeRestResources() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530199
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530200 YangNode freedNode = getCurNode();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530201 if (getCurNode() != null) {
202 YangNode tempNode = freedNode;
203 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530204
Bharat saraswal33dfa012016-05-17 19:59:16 +0530205 while (freedNode != tempNode.getParent()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530206
Bharat saraswal33dfa012016-05-17 19:59:16 +0530207 if (curTraversal != PARENT && freedNode.getChild() != null) {
208 curTraversal = CHILD;
209 freedNode = freedNode.getChild();
210 } else if (freedNode.getNextSibling() != null) {
211 curTraversal = SIBILING;
212 if (freedNode != tempNode) {
213 free(freedNode);
214 }
215 freedNode = freedNode.getNextSibling();
216 } else {
217 curTraversal = PARENT;
218 if (freedNode != tempNode) {
219 free(freedNode);
220 }
221 freedNode = freedNode.getParent();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530222 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530223 }
224 }
225 }
226
227 /**
228 * Free the current node.
229 *
230 * @param node YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530231 */
Vidyashree Rama74453712016-04-18 12:29:39 +0530232 private static void free(YangNode node) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530233
234 YangNode parent = node.getParent();
235 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530236
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530237 if (node.getNextSibling() != null) {
238 parent.setChild(node.getNextSibling());
239 } else if (node.getPreviousSibling() != null) {
240 parent.setChild(node.getPreviousSibling());
241 }
242 node = null;
243 }
244
245 /**
246 * Delete Java code files corresponding to the YANG schema.
247 *
Bharat saraswale707f032016-07-14 23:33:55 +0530248 * @param rootNode root node of data-model tree
249 * @param yangPluginConfig plugin configurations
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530250 * @throws IOException when fails to delete java code file the current node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530251 */
Bharat saraswale707f032016-07-14 23:33:55 +0530252 public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530253 throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530254
Bharat saraswal33dfa012016-05-17 19:59:16 +0530255 if (rootNode != null) {
Bharat saraswale707f032016-07-14 23:33:55 +0530256 //Free other resources where translator has failed.
Bharat saraswal33dfa012016-05-17 19:59:16 +0530257 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530258
Bharat saraswale707f032016-07-14 23:33:55 +0530259 // Start removing all open files.
Bharat saraswal33dfa012016-05-17 19:59:16 +0530260 YangNode tempNode = rootNode;
261 setCurNode(tempNode.getChild());
262 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530263
Bharat saraswal33dfa012016-05-17 19:59:16 +0530264 while (tempNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530265
Bharat saraswal33dfa012016-05-17 19:59:16 +0530266 if (curTraversal != PARENT) {
Bharat saraswale707f032016-07-14 23:33:55 +0530267 close(tempNode, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530268 }
269 if (curTraversal != PARENT && tempNode.getChild() != null) {
270 curTraversal = CHILD;
271 tempNode = tempNode.getChild();
272 } else if (tempNode.getNextSibling() != null) {
273 curTraversal = SIBILING;
274 tempNode = tempNode.getNextSibling();
275 } else {
276 curTraversal = PARENT;
277 tempNode = tempNode.getParent();
278 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530279 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530280
281 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530282 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530283 }
284
285 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530286 * Closes all the current open file handles of node and delete all generated files.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530287 *
Bharat saraswale707f032016-07-14 23:33:55 +0530288 * @param node current YANG node
289 * @param yangPlugin plugin configurations
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530290 * @throws IOException when fails to do IO operations
291 */
Bharat saraswale707f032016-07-14 23:33:55 +0530292 private static void close(YangNode node, YangPluginConfig yangPlugin)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530293 throws IOException {
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530294 if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
295 .getTempJavaCodeFragmentFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530296 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
Bharat saraswale707f032016-07-14 23:33:55 +0530297 } else {
298
janani bf9819ff2016-08-03 16:40:01 +0530299 if (getRootNode() != null) {
300 JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo();
301 if (javaFileInfo != null) {
302 searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() +
303 javaFileInfo.getPackageFilePath());
304 } else {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530305 searchAndDeleteTempDir(yangPlugin.getCodeGenDir());
janani bf9819ff2016-08-03 16:40:01 +0530306 }
Bharat saraswale707f032016-07-14 23:33:55 +0530307 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530308 }
309 }
Bharat saraswale707f032016-07-14 23:33:55 +0530310
311 /**
312 * Returns root node.
313 *
314 * @return root node
315 */
316 private static YangNode getRootNode() {
317 return rootNode;
318 }
319
320 /**
321 * Sets root node.
322 *
323 * @param rootNode root node
324 */
325 private static void setRootNode(YangNode rootNode) {
326 JavaCodeGeneratorUtil.rootNode = rootNode;
327 }
Bharat saraswal6a5911f2016-08-02 18:43:16 +0530328
329 /**
330 * Searches child node in data model tree.
331 *
332 * @param parentNode parent node
333 * @param nodeType node type
334 * @param nodeName node name
335 * @return child node
336 */
337 public static YangNode searchYangNode(YangNode parentNode, YangNodeType nodeType, String nodeName) {
338 YangNode child = parentNode.getChild();
339 TraversalType curTraversal = ROOT;
340 if (child == null) {
janani bf9819ff2016-08-03 16:40:01 +0530341 throw new IllegalArgumentException("Given parent node does not contain any child nodes");
Bharat saraswal6a5911f2016-08-02 18:43:16 +0530342 }
343
344 while (child != null) {
345 if (curTraversal != PARENT) {
346 if (child instanceof YangInput || child instanceof YangOutput) {
347 if (child.getNodeType().equals(nodeType)) {
348 return child;
349 }
350 } else if (child.getName().equals(nodeName) && child.getNodeType().equals(nodeType)) {
351 return child;
352 }
353 }
354 if (curTraversal != PARENT && child.getChild() != null) {
355 curTraversal = CHILD;
356 child = child.getChild();
357 } else if (child.getNextSibling() != null) {
358 curTraversal = SIBILING;
359 child = child.getNextSibling();
360 } else {
361 curTraversal = PARENT;
362 child = child.getParent();
363 }
364 }
365 return null;
366 }
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530367
368 /**
369 * Adds a case node in augment when augmenting a choice node.
370 *
371 * @param augment augment node
372 */
373 private static void addCaseNodeToChoiceTarget(YangAugment augment) {
374 YangCase javaCase = getYangCaseNode(JAVA_GENERATION);
375
376 YangPluginConfig pluginConfig = new YangPluginConfig();
377 javaCase.setName(getAugmentClassName(augment, pluginConfig) + getCapitalCase(CASE));
378
379 if (augment.getListOfLeaf() != null) {
380 augment.getListOfLeaf().forEach(javaCase::addLeaf);
381 augment.getListOfLeaf().clear();
382 }
383 if (augment.getListOfLeafList() != null) {
384 augment.getListOfLeafList().forEach(javaCase::addLeafList);
385 augment.getListOfLeafList().clear();
386 }
387 YangNode child = augment.getChild();
388 List<YangNode> childNodes = new ArrayList<>();
389 while (child != null) {
390 child.setParent(javaCase);
391 childNodes.add(child);
392 child = child.getNextSibling();
393 }
394 augment.setChild(null);
395 try {
396 augment.addChild(javaCase);
397 for (YangNode node : childNodes) {
398 node.setNextSibling(null);
399 node.setPreviousSibling(null);
400 javaCase.addChild(node);
401 }
402 } catch (DataModelException e) {
403 System.out.print("failed to add child node due to " + javaCase.getName() + " " + e.getLocalizedMessage());
404 }
405 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530406}