Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
| 19 | import java.io.IOException; |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame] | 20 | |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.TraversalType; |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangInput; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 25 | import org.onosproject.yangutils.datamodel.YangOutput; |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Gaurav Agrawal | 9564b55 | 2016-08-12 12:00:23 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 29 | |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.datamodel.TraversalType.CHILD; |
| 31 | import static org.onosproject.yangutils.datamodel.TraversalType.PARENT; |
| 32 | import static org.onosproject.yangutils.datamodel.TraversalType.ROOT; |
| 33 | import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 35 | |
| 36 | /** |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 37 | * Representation of java code generator based on application schema. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 38 | */ |
| 39 | public final class JavaCodeGeneratorUtil { |
| 40 | |
| 41 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 42 | * Creates a java code generator utility object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 43 | */ |
| 44 | private JavaCodeGeneratorUtil() { |
| 45 | } |
| 46 | |
| 47 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 48 | * Generates Java code files corresponding to the YANG schema. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 49 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 50 | * @param rootNode root node of the data model tree |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 51 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 52 | * @throws TranslatorException when fails to generate java code file the current node |
| 53 | * @throws IOException when fails to do IO operations |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 54 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 55 | public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 56 | throws TranslatorException, IOException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 57 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 58 | YangNode codeGenNode = rootNode; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 59 | TraversalType curTraversal = ROOT; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 60 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 61 | while (codeGenNode != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 62 | if (curTraversal != PARENT) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 63 | if (!(codeGenNode instanceof JavaCodeGenerator)) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 64 | throw new TranslatorException("Unsupported node to generate code " + |
| 65 | codeGenNode.getName() + " in " + codeGenNode.getLineNumber() + " at " |
| 66 | + codeGenNode.getCharPosition() + " in " + codeGenNode.getFileName()); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 67 | } |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 68 | try { |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 69 | generateCodeEntry(codeGenNode, yangPlugin, rootNode); |
Gaurav Agrawal | 9564b55 | 2016-08-12 12:00:23 +0530 | [diff] [blame] | 70 | codeGenNode.setNameSpaceAndAddToParentSchemaMap(); |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 71 | } 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-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 80 | } catch (Exception e) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 81 | e.printStackTrace(); |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 82 | close(codeGenNode, yangPlugin, rootNode); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 83 | throw new TranslatorException(e.getMessage()); |
| 84 | } |
| 85 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 86 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 87 | if (curTraversal != PARENT && codeGenNode.getChild() != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 88 | curTraversal = CHILD; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 89 | codeGenNode = codeGenNode.getChild(); |
| 90 | } else if (codeGenNode.getNextSibling() != null) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 91 | try { |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 92 | generateCodeExit(codeGenNode, yangPlugin, rootNode); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 93 | } catch (Exception e) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 94 | e.printStackTrace(); |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 95 | close(codeGenNode, yangPlugin, rootNode); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 96 | throw new TranslatorException(e.getMessage()); |
| 97 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 98 | curTraversal = SIBILING; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 99 | codeGenNode = codeGenNode.getNextSibling(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 100 | } else { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 101 | try { |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 102 | generateCodeExit(codeGenNode, yangPlugin, rootNode); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 103 | } catch (Exception e) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 104 | e.printStackTrace(); |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 105 | close(codeGenNode, yangPlugin, rootNode); |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 106 | throw new TranslatorException(e.getMessage()); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 107 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 108 | curTraversal = PARENT; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 109 | codeGenNode = codeGenNode.getParent(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Generates the current nodes code snippet. |
| 116 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 117 | * @param codeGenNode current data model node for which the code needs to be generated |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 118 | * @param yangPlugin YANG plugin config |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 119 | * @param rootNode YANG root node |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 120 | * @throws TranslatorException when fails to generate java code file the current node |
| 121 | * @throws IOException when fails to do IO operations |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 122 | */ |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 123 | private static void generateCodeEntry(YangNode codeGenNode, |
| 124 | YangPluginConfig yangPlugin, |
| 125 | YangNode rootNode) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 126 | throws TranslatorException, IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 127 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 128 | if (codeGenNode instanceof JavaCodeGenerator) { |
| 129 | ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 130 | } else { |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 131 | close(codeGenNode, yangPlugin, rootNode); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 132 | throw new TranslatorException( |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 133 | "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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Generates the current nodes code target code from the snippet. |
| 141 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 142 | * @param codeGenNode current data model node for which the code needs to be generated |
| 143 | * @param pluginConfig plugin configurations |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 144 | * @param rootNode YANG root node |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 145 | * @throws TranslatorException when fails to generate java code file the current node |
| 146 | * @throws IOException when fails to do IO operations |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 147 | */ |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 148 | private static void generateCodeExit(YangNode codeGenNode, |
| 149 | YangPluginConfig pluginConfig, |
| 150 | YangNode rootNode) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 151 | throws TranslatorException, IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 152 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 153 | if (codeGenNode instanceof JavaCodeGenerator) { |
| 154 | ((JavaCodeGenerator) codeGenNode).generateCodeExit(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 155 | } else { |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 156 | close(codeGenNode, pluginConfig, rootNode); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 157 | throw new TranslatorException( |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 158 | "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 saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 165 | * Free other YANG nodes of data-model tree when error occurs while file generation of current node. |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 166 | * |
| 167 | * @param freedNode current data model node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 168 | */ |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 169 | private static void freeRestResources(YangNode freedNode) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 170 | |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 171 | if (freedNode != null) { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 172 | YangNode tempNode = freedNode; |
| 173 | TraversalType curTraversal = ROOT; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 174 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 175 | while (freedNode != tempNode.getParent()) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 176 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 177 | 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 saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 192 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Free the current node. |
| 199 | * |
| 200 | * @param node YANG node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 201 | */ |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 202 | private static void free(YangNode node) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 203 | |
| 204 | YangNode parent = node.getParent(); |
| 205 | parent.setChild(null); |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 206 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 207 | 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 saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 218 | * @param rootNode root node of data-model tree |
| 219 | * @param yangPluginConfig plugin configurations |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 220 | * @throws IOException when fails to delete java code file the current node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 221 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 222 | public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig) |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 223 | throws IOException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 224 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 225 | if (rootNode != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 226 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 227 | // Start removing all open files. |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 228 | YangNode tempNode = rootNode; |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 229 | YangNode curNode = tempNode.getChild(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 230 | TraversalType curTraversal = ROOT; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 231 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 232 | while (tempNode != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 233 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 234 | if (curTraversal != PARENT) { |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 235 | close(tempNode, yangPluginConfig, rootNode); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 236 | } |
| 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 saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 247 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 248 | |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 249 | freeRestResources(curNode); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 250 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 254 | * Closes all the current open file handles of node and delete all generated files. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 255 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 256 | * @param node current YANG node |
| 257 | * @param yangPlugin plugin configurations |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 258 | * @param rootNode YANG root node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 259 | * @throws IOException when fails to do IO operations |
| 260 | */ |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 261 | private static void close(YangNode node, YangPluginConfig yangPlugin, |
| 262 | YangNode rootNode) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 263 | throws IOException { |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 264 | if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node) |
| 265 | .getTempJavaCodeFragmentFiles() != null) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 266 | ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true); |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 267 | } |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 268 | if (rootNode != null) { |
| 269 | JavaFileInfoTranslator javaFileInfo = ((JavaFileInfoContainer) rootNode).getJavaFileInfo(); |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 270 | if (javaFileInfo.getPackage() != null) { |
| 271 | searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() + |
Vidyashree Rama | 17992c1 | 2016-11-30 11:49:27 +0530 | [diff] [blame] | 272 | javaFileInfo.getPackageFilePath()); |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 273 | } else { |
| 274 | searchAndDeleteTempDir(yangPlugin.getCodeGenDir()); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 275 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 276 | } |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 277 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 278 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 279 | |
| 280 | /** |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 281 | * 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 b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 292 | throw new IllegalArgumentException("Given parent node does not contain any child nodes"); |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 293 | } |
| 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 saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 318 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 319 | } |