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; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangNode; |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame^] | 22 | import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 25 | |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame^] | 26 | import static org.onosproject.yangutils.datamodel.TraversalType.CHILD; |
| 27 | import static org.onosproject.yangutils.datamodel.TraversalType.PARENT; |
| 28 | import static org.onosproject.yangutils.datamodel.TraversalType.ROOT; |
| 29 | import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 30 | |
| 31 | /** |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 32 | * Representation of java code generator based on application schema. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 33 | */ |
| 34 | public final class JavaCodeGeneratorUtil { |
| 35 | |
| 36 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 37 | * Current YANG node. |
| 38 | */ |
| 39 | private static YangNode curNode; |
| 40 | |
| 41 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 42 | * Creates a java code generator utility object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 43 | */ |
| 44 | private JavaCodeGeneratorUtil() { |
| 45 | } |
| 46 | |
| 47 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 48 | * Returns current YANG node. |
| 49 | * |
| 50 | * @return current YANG node |
| 51 | */ |
| 52 | public static YangNode getCurNode() { |
| 53 | return curNode; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets current YANG node. |
| 58 | * |
| 59 | * @param node current YANG node |
| 60 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 61 | public static void setCurNode(YangNode node) { |
| 62 | curNode = node; |
| 63 | } |
| 64 | |
| 65 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 66 | * Generates Java code files corresponding to the YANG schema. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 67 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 68 | * @param rootNode root node of the data model tree |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 69 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 70 | * @throws TranslatorException when fails to generate java code file the current |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 71 | * node |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 72 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 73 | public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin) |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 74 | throws TranslatorException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 75 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 76 | YangNode codeGenNode = rootNode; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 77 | TraversalType curTraversal = ROOT; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 78 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 79 | while (codeGenNode != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 80 | if (curTraversal != PARENT) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 81 | if (!(codeGenNode instanceof JavaCodeGenerator)) { |
| 82 | throw new TranslatorException("Unsupported node to generate code"); |
| 83 | } |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 84 | setCurNode(codeGenNode); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 85 | try { |
| 86 | generateCodeEntry(codeGenNode, yangPlugin); |
Vidyashree Rama | a89bdd3 | 2016-07-08 20:45:41 +0530 | [diff] [blame^] | 87 | } catch (InvalidNodeForTranslatorException e) { |
| 88 | if (codeGenNode.getNextSibling() != null) { |
| 89 | curTraversal = SIBILING; |
| 90 | codeGenNode = codeGenNode.getNextSibling(); |
| 91 | } else { |
| 92 | curTraversal = PARENT; |
| 93 | codeGenNode = codeGenNode.getParent(); |
| 94 | } |
| 95 | continue; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 96 | } catch (Exception e) { |
| 97 | throw new TranslatorException(e.getMessage()); |
| 98 | } |
| 99 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 100 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 101 | if (curTraversal != PARENT && codeGenNode.getChild() != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 102 | curTraversal = CHILD; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 103 | codeGenNode = codeGenNode.getChild(); |
| 104 | } else if (codeGenNode.getNextSibling() != null) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 105 | try { |
| 106 | generateCodeExit(codeGenNode); |
| 107 | } catch (Exception e) { |
| 108 | throw new TranslatorException(e.getMessage()); |
| 109 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 110 | curTraversal = SIBILING; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 111 | codeGenNode = codeGenNode.getNextSibling(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 112 | } else { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 113 | try { |
| 114 | generateCodeExit(codeGenNode); |
| 115 | } catch (Exception e) { |
| 116 | throw new TranslatorException(e.getMessage()); |
| 117 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 118 | curTraversal = PARENT; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 119 | codeGenNode = codeGenNode.getParent(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Generates the current nodes code snippet. |
| 126 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 127 | * @param codeGenNode current data model node for which the code needs to be |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 128 | * generated |
| 129 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 130 | * @throws TranslatorException when fails to generate java code file the current |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 131 | * node |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 132 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 133 | private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin) |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 134 | throws TranslatorException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 135 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 136 | if (codeGenNode instanceof JavaCodeGenerator) { |
| 137 | ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 138 | } else { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 139 | throw new TranslatorException( |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 140 | "Generated data model node cannot be translated to target language code"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Generates the current nodes code target code from the snippet. |
| 146 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 147 | * @param codeGenNode current data model node for which the code needs to be |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 148 | * generated |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 149 | * @throws TranslatorException when fails to generate java code file the current |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 150 | * node |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 151 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 152 | private static void generateCodeExit(YangNode codeGenNode) |
| 153 | throws TranslatorException { |
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).generateCodeExit(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 157 | } else { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 158 | throw new TranslatorException( |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 159 | "Generated data model node cannot be translated to target language code"); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 164 | * Free other YANG nodes of data-model tree when error occurs while file |
| 165 | * generation of current node. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 166 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 167 | private static void freeRestResources() { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 168 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 169 | YangNode freedNode = getCurNode(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 170 | if (getCurNode() != null) { |
| 171 | YangNode tempNode = freedNode; |
| 172 | TraversalType curTraversal = ROOT; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 173 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 174 | while (freedNode != tempNode.getParent()) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 175 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 176 | if (curTraversal != PARENT && freedNode.getChild() != null) { |
| 177 | curTraversal = CHILD; |
| 178 | freedNode = freedNode.getChild(); |
| 179 | } else if (freedNode.getNextSibling() != null) { |
| 180 | curTraversal = SIBILING; |
| 181 | if (freedNode != tempNode) { |
| 182 | free(freedNode); |
| 183 | } |
| 184 | freedNode = freedNode.getNextSibling(); |
| 185 | } else { |
| 186 | curTraversal = PARENT; |
| 187 | if (freedNode != tempNode) { |
| 188 | free(freedNode); |
| 189 | } |
| 190 | freedNode = freedNode.getParent(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 191 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Free the current node. |
| 198 | * |
| 199 | * @param node YANG node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 200 | */ |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 201 | private static void free(YangNode node) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 202 | |
| 203 | YangNode parent = node.getParent(); |
| 204 | parent.setChild(null); |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 205 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 206 | if (node.getNextSibling() != null) { |
| 207 | parent.setChild(node.getNextSibling()); |
| 208 | } else if (node.getPreviousSibling() != null) { |
| 209 | parent.setChild(node.getPreviousSibling()); |
| 210 | } |
| 211 | node = null; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Delete Java code files corresponding to the YANG schema. |
| 216 | * |
| 217 | * @param rootNode root node of data-model tree |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 218 | * @throws IOException when fails to delete java code file the current node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 219 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 220 | public static void translatorErrorHandler(YangNode rootNode) |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 221 | throws IOException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 222 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 223 | if (rootNode != null) { |
| 224 | /** |
| 225 | * Free other resources where translator has failed. |
| 226 | */ |
| 227 | freeRestResources(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 228 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 229 | /** |
| 230 | * Start removing all open files. |
| 231 | */ |
| 232 | YangNode tempNode = rootNode; |
| 233 | setCurNode(tempNode.getChild()); |
| 234 | TraversalType curTraversal = ROOT; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 235 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 236 | while (tempNode != null) { |
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 (curTraversal != PARENT) { |
| 239 | close(tempNode); |
| 240 | } |
| 241 | if (curTraversal != PARENT && tempNode.getChild() != null) { |
| 242 | curTraversal = CHILD; |
| 243 | tempNode = tempNode.getChild(); |
| 244 | } else if (tempNode.getNextSibling() != null) { |
| 245 | curTraversal = SIBILING; |
| 246 | tempNode = tempNode.getNextSibling(); |
| 247 | } else { |
| 248 | curTraversal = PARENT; |
| 249 | tempNode = tempNode.getParent(); |
| 250 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 251 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 252 | |
| 253 | freeRestResources(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 254 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 258 | * Closes all the current open file handles of node and delete all generated |
| 259 | * files. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 260 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 261 | * @param node current YANG node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 262 | * @throws IOException when fails to do IO operations |
| 263 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 264 | private static void close(YangNode node) |
| 265 | throws IOException { |
Vidyashree Rama | 5daea74 | 2016-05-20 16:29:25 +0530 | [diff] [blame] | 266 | if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node) |
| 267 | .getTempJavaCodeFragmentFiles() != null) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 268 | ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |