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