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