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