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