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 | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.TraversalType; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangAugment; |
| 25 | import org.onosproject.yangutils.datamodel.YangCase; |
| 26 | import org.onosproject.yangutils.datamodel.YangChoice; |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangInput; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangNodeType; |
| 30 | import org.onosproject.yangutils.datamodel.YangOutput; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo; |
| 33 | import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig; |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 36 | |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.datamodel.TraversalType.CHILD; |
| 38 | import static org.onosproject.yangutils.datamodel.TraversalType.PARENT; |
| 39 | import static org.onosproject.yangutils.datamodel.TraversalType.ROOT; |
| 40 | import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
| 42 | import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangCaseNode; |
| 43 | import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.getAugmentClassName; |
| 44 | import static org.onosproject.yangutils.utils.UtilConstants.CASE; |
| 45 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 47 | |
| 48 | /** |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 49 | * Representation of java code generator based on application schema. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 50 | */ |
| 51 | public final class JavaCodeGeneratorUtil { |
| 52 | |
| 53 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 54 | * Current YANG node. |
| 55 | */ |
| 56 | private static YangNode curNode; |
| 57 | |
| 58 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 59 | * Root node. |
| 60 | */ |
| 61 | private static YangNode rootNode; |
| 62 | |
| 63 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 64 | * Creates a java code generator utility object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 65 | */ |
| 66 | private JavaCodeGeneratorUtil() { |
| 67 | } |
| 68 | |
| 69 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 70 | * Returns current YANG node. |
| 71 | * |
| 72 | * @return current YANG node |
| 73 | */ |
| 74 | public static YangNode getCurNode() { |
| 75 | return curNode; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Sets current YANG node. |
| 80 | * |
| 81 | * @param node current YANG node |
| 82 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 83 | public static void setCurNode(YangNode node) { |
| 84 | curNode = node; |
| 85 | } |
| 86 | |
| 87 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 88 | * Generates Java code files corresponding to the YANG schema. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 89 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 90 | * @param rootNode root node of the data model tree |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 91 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 92 | * @throws TranslatorException when fails to generate java code file the current node |
| 93 | * @throws IOException when fails to do IO operations |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 94 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 95 | public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 96 | throws TranslatorException, IOException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 97 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 98 | YangNode codeGenNode = rootNode; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 99 | setRootNode(rootNode); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 100 | TraversalType curTraversal = ROOT; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 101 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 102 | while (codeGenNode != null) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 103 | if (codeGenNode instanceof YangAugment) { |
| 104 | if (((YangAugment) codeGenNode).getAugmentedNode() instanceof YangChoice) { |
| 105 | addCaseNodeToChoiceTarget((YangAugment) codeGenNode); |
| 106 | } |
| 107 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 108 | if (curTraversal != PARENT) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 109 | if (!(codeGenNode instanceof JavaCodeGenerator)) { |
| 110 | throw new TranslatorException("Unsupported node to generate code"); |
| 111 | } |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 112 | setCurNode(codeGenNode); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 113 | try { |
| 114 | generateCodeEntry(codeGenNode, yangPlugin); |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 115 | } catch (InvalidNodeForTranslatorException e) { |
| 116 | if (codeGenNode.getNextSibling() != null) { |
| 117 | curTraversal = SIBILING; |
| 118 | codeGenNode = codeGenNode.getNextSibling(); |
| 119 | } else { |
| 120 | curTraversal = PARENT; |
| 121 | codeGenNode = codeGenNode.getParent(); |
| 122 | } |
| 123 | continue; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 124 | } catch (Exception e) { |
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 | } |
| 128 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 129 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 130 | if (curTraversal != PARENT && codeGenNode.getChild() != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 131 | curTraversal = CHILD; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 132 | codeGenNode = codeGenNode.getChild(); |
| 133 | } else if (codeGenNode.getNextSibling() != null) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 134 | try { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 135 | generateCodeExit(codeGenNode, yangPlugin); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 136 | } catch (Exception e) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 137 | close(codeGenNode, yangPlugin); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 138 | throw new TranslatorException(e.getMessage()); |
| 139 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 140 | curTraversal = SIBILING; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 141 | codeGenNode = codeGenNode.getNextSibling(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 142 | } else { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 143 | try { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 144 | generateCodeExit(codeGenNode, yangPlugin); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 145 | } catch (Exception e) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 146 | close(codeGenNode, yangPlugin); |
| 147 | throw new TranslatorException(e.getMessage()); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 148 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 149 | curTraversal = PARENT; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 150 | codeGenNode = codeGenNode.getParent(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Generates the current nodes code snippet. |
| 157 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 158 | * @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] | 159 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 160 | * @throws TranslatorException when fails to generate java code file the current node |
| 161 | * @throws IOException when fails to do IO operations |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 162 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 163 | private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 164 | throws TranslatorException, IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 165 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 166 | if (codeGenNode instanceof JavaCodeGenerator) { |
| 167 | ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 168 | } else { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 169 | close(codeGenNode, yangPlugin); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 170 | throw new TranslatorException( |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 171 | "Generated data model node cannot be translated to target language code"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Generates the current nodes code target code from the snippet. |
| 177 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 178 | * @param codeGenNode current data model node for which the code needs to be generated |
| 179 | * @param pluginConfig plugin configurations |
| 180 | * @throws TranslatorException when fails to generate java code file the current node |
| 181 | * @throws IOException when fails to do IO operations |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 182 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 183 | private static void generateCodeExit(YangNode codeGenNode, YangPluginConfig pluginConfig) |
| 184 | throws TranslatorException, IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 185 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 186 | if (codeGenNode instanceof JavaCodeGenerator) { |
| 187 | ((JavaCodeGenerator) codeGenNode).generateCodeExit(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 188 | } else { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 189 | close(codeGenNode, pluginConfig); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 190 | throw new TranslatorException( |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 191 | "Generated data model node cannot be translated to target language code"); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 196 | * 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] | 197 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 198 | private static void freeRestResources() { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 199 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 200 | YangNode freedNode = getCurNode(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 201 | if (getCurNode() != null) { |
| 202 | YangNode tempNode = freedNode; |
| 203 | TraversalType curTraversal = ROOT; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 204 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 205 | while (freedNode != tempNode.getParent()) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 206 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 207 | if (curTraversal != PARENT && freedNode.getChild() != null) { |
| 208 | curTraversal = CHILD; |
| 209 | freedNode = freedNode.getChild(); |
| 210 | } else if (freedNode.getNextSibling() != null) { |
| 211 | curTraversal = SIBILING; |
| 212 | if (freedNode != tempNode) { |
| 213 | free(freedNode); |
| 214 | } |
| 215 | freedNode = freedNode.getNextSibling(); |
| 216 | } else { |
| 217 | curTraversal = PARENT; |
| 218 | if (freedNode != tempNode) { |
| 219 | free(freedNode); |
| 220 | } |
| 221 | freedNode = freedNode.getParent(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 222 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Free the current node. |
| 229 | * |
| 230 | * @param node YANG node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 231 | */ |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 232 | private static void free(YangNode node) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 233 | |
| 234 | YangNode parent = node.getParent(); |
| 235 | parent.setChild(null); |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 236 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 237 | if (node.getNextSibling() != null) { |
| 238 | parent.setChild(node.getNextSibling()); |
| 239 | } else if (node.getPreviousSibling() != null) { |
| 240 | parent.setChild(node.getPreviousSibling()); |
| 241 | } |
| 242 | node = null; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Delete Java code files corresponding to the YANG schema. |
| 247 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 248 | * @param rootNode root node of data-model tree |
| 249 | * @param yangPluginConfig plugin configurations |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 250 | * @throws IOException when fails to delete java code file the current node |
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 | public static void translatorErrorHandler(YangNode rootNode, YangPluginConfig yangPluginConfig) |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 253 | throws IOException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 254 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 255 | if (rootNode != null) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 256 | //Free other resources where translator has failed. |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 257 | freeRestResources(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 258 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 259 | // Start removing all open files. |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 260 | YangNode tempNode = rootNode; |
| 261 | setCurNode(tempNode.getChild()); |
| 262 | TraversalType curTraversal = ROOT; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 263 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 264 | while (tempNode != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 265 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 266 | if (curTraversal != PARENT) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 267 | close(tempNode, yangPluginConfig); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 268 | } |
| 269 | if (curTraversal != PARENT && tempNode.getChild() != null) { |
| 270 | curTraversal = CHILD; |
| 271 | tempNode = tempNode.getChild(); |
| 272 | } else if (tempNode.getNextSibling() != null) { |
| 273 | curTraversal = SIBILING; |
| 274 | tempNode = tempNode.getNextSibling(); |
| 275 | } else { |
| 276 | curTraversal = PARENT; |
| 277 | tempNode = tempNode.getParent(); |
| 278 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 279 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 280 | |
| 281 | freeRestResources(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 282 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 286 | * 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] | 287 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 288 | * @param node current YANG node |
| 289 | * @param yangPlugin plugin configurations |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 290 | * @throws IOException when fails to do IO operations |
| 291 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 292 | private static void close(YangNode node, YangPluginConfig yangPlugin) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 293 | throws IOException { |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 294 | if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node) |
| 295 | .getTempJavaCodeFragmentFiles() != null) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 296 | ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 297 | } else { |
| 298 | |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 299 | if (getRootNode() != null) { |
| 300 | JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo(); |
| 301 | if (javaFileInfo != null) { |
| 302 | searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() + |
| 303 | javaFileInfo.getPackageFilePath()); |
| 304 | } else { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 305 | searchAndDeleteTempDir(yangPlugin.getCodeGenDir()); |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 306 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 307 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 308 | } |
| 309 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * Returns root node. |
| 313 | * |
| 314 | * @return root node |
| 315 | */ |
| 316 | private static YangNode getRootNode() { |
| 317 | return rootNode; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Sets root node. |
| 322 | * |
| 323 | * @param rootNode root node |
| 324 | */ |
| 325 | private static void setRootNode(YangNode rootNode) { |
| 326 | JavaCodeGeneratorUtil.rootNode = rootNode; |
| 327 | } |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 328 | |
| 329 | /** |
| 330 | * Searches child node in data model tree. |
| 331 | * |
| 332 | * @param parentNode parent node |
| 333 | * @param nodeType node type |
| 334 | * @param nodeName node name |
| 335 | * @return child node |
| 336 | */ |
| 337 | public static YangNode searchYangNode(YangNode parentNode, YangNodeType nodeType, String nodeName) { |
| 338 | YangNode child = parentNode.getChild(); |
| 339 | TraversalType curTraversal = ROOT; |
| 340 | if (child == null) { |
janani b | b3be133 | 2016-08-03 16:40:01 +0530 | [diff] [blame] | 341 | throw new IllegalArgumentException("Given parent node does not contain any child nodes"); |
Bharat saraswal | aab24b9 | 2016-08-02 18:43:16 +0530 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | while (child != null) { |
| 345 | if (curTraversal != PARENT) { |
| 346 | if (child instanceof YangInput || child instanceof YangOutput) { |
| 347 | if (child.getNodeType().equals(nodeType)) { |
| 348 | return child; |
| 349 | } |
| 350 | } else if (child.getName().equals(nodeName) && child.getNodeType().equals(nodeType)) { |
| 351 | return child; |
| 352 | } |
| 353 | } |
| 354 | if (curTraversal != PARENT && child.getChild() != null) { |
| 355 | curTraversal = CHILD; |
| 356 | child = child.getChild(); |
| 357 | } else if (child.getNextSibling() != null) { |
| 358 | curTraversal = SIBILING; |
| 359 | child = child.getNextSibling(); |
| 360 | } else { |
| 361 | curTraversal = PARENT; |
| 362 | child = child.getParent(); |
| 363 | } |
| 364 | } |
| 365 | return null; |
| 366 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 367 | |
| 368 | /** |
| 369 | * Adds a case node in augment when augmenting a choice node. |
| 370 | * |
| 371 | * @param augment augment node |
| 372 | */ |
| 373 | private static void addCaseNodeToChoiceTarget(YangAugment augment) { |
| 374 | YangCase javaCase = getYangCaseNode(JAVA_GENERATION); |
| 375 | |
| 376 | YangPluginConfig pluginConfig = new YangPluginConfig(); |
| 377 | javaCase.setName(getAugmentClassName(augment, pluginConfig) + getCapitalCase(CASE)); |
| 378 | |
| 379 | if (augment.getListOfLeaf() != null) { |
| 380 | augment.getListOfLeaf().forEach(javaCase::addLeaf); |
| 381 | augment.getListOfLeaf().clear(); |
| 382 | } |
| 383 | if (augment.getListOfLeafList() != null) { |
| 384 | augment.getListOfLeafList().forEach(javaCase::addLeafList); |
| 385 | augment.getListOfLeafList().clear(); |
| 386 | } |
| 387 | YangNode child = augment.getChild(); |
| 388 | List<YangNode> childNodes = new ArrayList<>(); |
| 389 | while (child != null) { |
| 390 | child.setParent(javaCase); |
| 391 | childNodes.add(child); |
| 392 | child = child.getNextSibling(); |
| 393 | } |
| 394 | augment.setChild(null); |
| 395 | try { |
| 396 | augment.addChild(javaCase); |
| 397 | for (YangNode node : childNodes) { |
| 398 | node.setNextSibling(null); |
| 399 | node.setPreviousSibling(null); |
| 400 | javaCase.addChild(node); |
| 401 | } |
| 402 | } catch (DataModelException e) { |
| 403 | System.out.print("failed to add child node due to " + javaCase.getName() + " " + e.getLocalizedMessage()); |
| 404 | } |
| 405 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 406 | } |