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 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 42 | * Current YANG node. |
| 43 | */ |
| 44 | private static YangNode curNode; |
| 45 | |
| 46 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 47 | * Root node. |
| 48 | */ |
| 49 | private static YangNode rootNode; |
| 50 | |
| 51 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 52 | * Creates a java code generator utility object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 53 | */ |
| 54 | private JavaCodeGeneratorUtil() { |
| 55 | } |
| 56 | |
| 57 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 58 | * 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 saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 71 | public static void setCurNode(YangNode node) { |
| 72 | curNode = node; |
| 73 | } |
| 74 | |
| 75 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 76 | * Generates Java code files corresponding to the YANG schema. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 77 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 78 | * @param rootNode root node of the data model tree |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 79 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 80 | * @throws TranslatorException when fails to generate java code file the current node |
| 81 | * @throws IOException when fails to do IO operations |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 82 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 83 | public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 84 | throws TranslatorException, IOException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 85 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 86 | YangNode codeGenNode = rootNode; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 87 | setRootNode(rootNode); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 88 | TraversalType curTraversal = ROOT; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 89 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 90 | while (codeGenNode != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 91 | if (curTraversal != PARENT) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 92 | if (!(codeGenNode instanceof JavaCodeGenerator)) { |
| 93 | throw new TranslatorException("Unsupported node to generate code"); |
| 94 | } |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 95 | setCurNode(codeGenNode); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 96 | try { |
| 97 | generateCodeEntry(codeGenNode, yangPlugin); |
Gaurav Agrawal | 9564b55 | 2016-08-12 12:00:23 +0530 | [diff] [blame] | 98 | codeGenNode.setNameSpaceAndAddToParentSchemaMap(); |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 99 | } catch (InvalidNodeForTranslatorException e) { |
| 100 | if (codeGenNode.getNextSibling() != null) { |
| 101 | curTraversal = SIBILING; |
| 102 | codeGenNode = codeGenNode.getNextSibling(); |
| 103 | } else { |
| 104 | curTraversal = PARENT; |
| 105 | codeGenNode = codeGenNode.getParent(); |
| 106 | } |
| 107 | continue; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 108 | } catch (Exception e) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 109 | e.printStackTrace(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 110 | close(codeGenNode, yangPlugin); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 111 | throw new TranslatorException(e.getMessage()); |
| 112 | } |
| 113 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 114 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 115 | if (curTraversal != PARENT && codeGenNode.getChild() != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 116 | curTraversal = CHILD; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 117 | codeGenNode = codeGenNode.getChild(); |
| 118 | } else if (codeGenNode.getNextSibling() != null) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 119 | try { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 120 | generateCodeExit(codeGenNode, yangPlugin); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 121 | } catch (Exception e) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 122 | e.printStackTrace(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 123 | close(codeGenNode, yangPlugin); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 124 | throw new TranslatorException(e.getMessage()); |
| 125 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 126 | curTraversal = SIBILING; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 127 | codeGenNode = codeGenNode.getNextSibling(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 128 | } else { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 129 | try { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 130 | generateCodeExit(codeGenNode, yangPlugin); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 131 | } catch (Exception e) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 132 | e.printStackTrace(); |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 133 | close(codeGenNode, yangPlugin); |
| 134 | throw new TranslatorException(e.getMessage()); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 135 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 136 | curTraversal = PARENT; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 137 | codeGenNode = codeGenNode.getParent(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Generates the current nodes code snippet. |
| 144 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 145 | * @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] | 146 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 147 | * @throws TranslatorException when fails to generate java code file the current node |
| 148 | * @throws IOException when fails to do IO operations |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 149 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 150 | private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin) |
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).generateCodeEntry(yangPlugin); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 155 | } else { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 156 | close(codeGenNode, yangPlugin); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 157 | throw new TranslatorException( |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 158 | "Generated data model node cannot be translated to target language code"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Generates the current nodes code target code from the snippet. |
| 164 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 165 | * @param codeGenNode current data model node for which the code needs to be generated |
| 166 | * @param pluginConfig plugin configurations |
| 167 | * @throws TranslatorException when fails to generate java code file the current node |
| 168 | * @throws IOException when fails to do IO operations |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 169 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 170 | private static void generateCodeExit(YangNode codeGenNode, YangPluginConfig pluginConfig) |
| 171 | throws TranslatorException, IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 172 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 173 | if (codeGenNode instanceof JavaCodeGenerator) { |
| 174 | ((JavaCodeGenerator) codeGenNode).generateCodeExit(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 175 | } else { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 176 | close(codeGenNode, pluginConfig); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 177 | throw new TranslatorException( |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 178 | "Generated data model node cannot be translated to target language code"); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 183 | * Free other YANG nodes of data-model tree when error occurs while file generation of current node. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 184 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 185 | private static void freeRestResources() { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 186 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 187 | YangNode freedNode = getCurNode(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 188 | if (getCurNode() != null) { |
| 189 | YangNode tempNode = freedNode; |
| 190 | TraversalType curTraversal = ROOT; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 191 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 192 | while (freedNode != tempNode.getParent()) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 193 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 194 | if (curTraversal != PARENT && freedNode.getChild() != null) { |
| 195 | curTraversal = CHILD; |
| 196 | freedNode = freedNode.getChild(); |
| 197 | } else if (freedNode.getNextSibling() != null) { |
| 198 | curTraversal = SIBILING; |
| 199 | if (freedNode != tempNode) { |
| 200 | free(freedNode); |
| 201 | } |
| 202 | freedNode = freedNode.getNextSibling(); |
| 203 | } else { |
| 204 | curTraversal = PARENT; |
| 205 | if (freedNode != tempNode) { |
| 206 | free(freedNode); |
| 207 | } |
| 208 | freedNode = freedNode.getParent(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 209 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Free the current node. |
| 216 | * |
| 217 | * @param node YANG node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 218 | */ |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 219 | private static void free(YangNode node) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 220 | |
| 221 | YangNode parent = node.getParent(); |
| 222 | parent.setChild(null); |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 223 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 224 | if (node.getNextSibling() != null) { |
| 225 | parent.setChild(node.getNextSibling()); |
| 226 | } else if (node.getPreviousSibling() != null) { |
| 227 | parent.setChild(node.getPreviousSibling()); |
| 228 | } |
| 229 | node = null; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Delete Java code files corresponding to the YANG schema. |
| 234 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 235 | * @param rootNode root node of data-model tree |
| 236 | * @param yangPluginConfig plugin configurations |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 237 | * @throws IOException when fails to delete java code file the current node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 238 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 239 | public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig) |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 240 | throws IOException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 241 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 242 | if (rootNode != null) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 243 | //Free other resources where translator has failed. |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 244 | freeRestResources(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 245 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 246 | // Start removing all open files. |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 247 | YangNode tempNode = rootNode; |
| 248 | setCurNode(tempNode.getChild()); |
| 249 | TraversalType curTraversal = ROOT; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 250 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 251 | while (tempNode != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 252 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 253 | if (curTraversal != PARENT) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 254 | close(tempNode, yangPluginConfig); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 255 | } |
| 256 | if (curTraversal != PARENT && tempNode.getChild() != null) { |
| 257 | curTraversal = CHILD; |
| 258 | tempNode = tempNode.getChild(); |
| 259 | } else if (tempNode.getNextSibling() != null) { |
| 260 | curTraversal = SIBILING; |
| 261 | tempNode = tempNode.getNextSibling(); |
| 262 | } else { |
| 263 | curTraversal = PARENT; |
| 264 | tempNode = tempNode.getParent(); |
| 265 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 266 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 267 | |
| 268 | freeRestResources(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 269 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 273 | * 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] | 274 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 275 | * @param node current YANG node |
| 276 | * @param yangPlugin plugin configurations |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 277 | * @throws IOException when fails to do IO operations |
| 278 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 279 | private static void close(YangNode node, YangPluginConfig yangPlugin) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 280 | throws IOException { |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 281 | if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node) |
| 282 | .getTempJavaCodeFragmentFiles() != null) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 283 | ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 284 | } else { |
| 285 | |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 286 | if (getRootNode() != null) { |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 287 | JavaFileInfoTranslator javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo(); |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 288 | if (javaFileInfo != null) { |
| 289 | searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() + |
| 290 | javaFileInfo.getPackageFilePath()); |
| 291 | } else { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 292 | searchAndDeleteTempDir(yangPlugin.getCodeGenDir()); |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 293 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 294 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 295 | } |
| 296 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * Returns root node. |
| 300 | * |
| 301 | * @return root node |
| 302 | */ |
| 303 | private static YangNode getRootNode() { |
| 304 | return rootNode; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Sets root node. |
| 309 | * |
| 310 | * @param rootNode root node |
| 311 | */ |
| 312 | private static void setRootNode(YangNode rootNode) { |
| 313 | JavaCodeGeneratorUtil.rootNode = rootNode; |
| 314 | } |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * Searches child node in data model tree. |
| 318 | * |
| 319 | * @param parentNode parent node |
| 320 | * @param nodeType node type |
| 321 | * @param nodeName node name |
| 322 | * @return child node |
| 323 | */ |
| 324 | public static YangNode searchYangNode(YangNode parentNode, YangNodeType nodeType, String nodeName) { |
| 325 | YangNode child = parentNode.getChild(); |
| 326 | TraversalType curTraversal = ROOT; |
| 327 | if (child == null) { |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 328 | throw new IllegalArgumentException("Given parent node does not contain any child nodes"); |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | while (child != null) { |
| 332 | if (curTraversal != PARENT) { |
| 333 | if (child instanceof YangInput || child instanceof YangOutput) { |
| 334 | if (child.getNodeType().equals(nodeType)) { |
| 335 | return child; |
| 336 | } |
| 337 | } else if (child.getName().equals(nodeName) && child.getNodeType().equals(nodeType)) { |
| 338 | return child; |
| 339 | } |
| 340 | } |
| 341 | if (curTraversal != PARENT && child.getChild() != null) { |
| 342 | curTraversal = CHILD; |
| 343 | child = child.getChild(); |
| 344 | } else if (child.getNextSibling() != null) { |
| 345 | curTraversal = SIBILING; |
| 346 | child = child.getNextSibling(); |
| 347 | } else { |
| 348 | curTraversal = PARENT; |
| 349 | child = child.getParent(); |
| 350 | } |
| 351 | } |
| 352 | return null; |
| 353 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 354 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 355 | } |