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; |
| 20 | |
| 21 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 23 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 25 | |
| 26 | import static org.onosproject.yangutils.translator.tojava.TraversalType.CHILD; |
| 27 | import static org.onosproject.yangutils.translator.tojava.TraversalType.PARENT; |
| 28 | import static org.onosproject.yangutils.translator.tojava.TraversalType.ROOT; |
| 29 | import static org.onosproject.yangutils.translator.tojava.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 | * |
| 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 |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 70 | * @throws IOException when fails to generate java code file the current |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 71 | * node |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 72 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 73 | public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin) |
| 74 | throws IOException { |
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) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 81 | setCurNode(codeGenNode); |
| 82 | generateCodeEntry(codeGenNode, yangPlugin); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 83 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 84 | if (curTraversal != PARENT && codeGenNode.getChild() != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 85 | curTraversal = CHILD; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 86 | codeGenNode = codeGenNode.getChild(); |
| 87 | } else if (codeGenNode.getNextSibling() != null) { |
| 88 | generateCodeExit(codeGenNode); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 89 | curTraversal = SIBILING; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 90 | codeGenNode = codeGenNode.getNextSibling(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 91 | } else { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 92 | generateCodeExit(codeGenNode); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 93 | curTraversal = PARENT; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 94 | codeGenNode = codeGenNode.getParent(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Generates the current nodes code snippet. |
| 101 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 102 | * @param codeGenNode current data model node for which the code needs to be |
| 103 | * generated |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 104 | * @param yangPlugin YANG plugin config |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 105 | * @throws IOException IO operation exception |
| 106 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 107 | private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin) |
| 108 | throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 109 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 110 | if (codeGenNode instanceof JavaCodeGenerator) { |
| 111 | ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 112 | } else { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 113 | throw new TranslatorException( |
| 114 | "Generated data model node cannot be translated to target language code"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Generates the current nodes code target code from the snippet. |
| 120 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 121 | * @param codeGenNode current data model node for which the code needs to be |
| 122 | * generated |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 123 | * @throws IOException IO operation exception |
| 124 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 125 | private static void generateCodeExit(YangNode codeGenNode) |
| 126 | throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 127 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 128 | if (codeGenNode instanceof JavaCodeGenerator) { |
| 129 | ((JavaCodeGenerator) codeGenNode).generateCodeExit(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 130 | } else { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 131 | throw new TranslatorException( |
| 132 | "Generated data model node cannot be translated to target language code"); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 137 | * Free other YANG nodes of data-model tree when error occurs while file |
| 138 | * generation of current node. |
| 139 | * |
| 140 | * @throws DataModelException when fails to do datamodel operations |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 141 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 142 | private static void freeRestResources() |
| 143 | throws DataModelException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 144 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 145 | YangNode freedNode = getCurNode(); |
| 146 | YangNode tempNode = freedNode; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 147 | TraversalType curTraversal = ROOT; |
| 148 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 149 | while (freedNode != tempNode.getParent()) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 150 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 151 | if (curTraversal != PARENT && freedNode.getChild() != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 152 | curTraversal = CHILD; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 153 | freedNode = freedNode.getChild(); |
| 154 | } else if (freedNode.getNextSibling() != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 155 | curTraversal = SIBILING; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 156 | if (freedNode != tempNode) { |
| 157 | free(freedNode); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 158 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 159 | freedNode = freedNode.getNextSibling(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 160 | } else { |
| 161 | curTraversal = PARENT; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 162 | if (freedNode != tempNode) { |
| 163 | free(freedNode); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 164 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 165 | freedNode = freedNode.getParent(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Free the current node. |
| 172 | * |
| 173 | * @param node YANG node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 174 | */ |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 175 | private static void free(YangNode node) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 176 | |
| 177 | YangNode parent = node.getParent(); |
| 178 | parent.setChild(null); |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 179 | |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 180 | if (node.getNextSibling() != null) { |
| 181 | parent.setChild(node.getNextSibling()); |
| 182 | } else if (node.getPreviousSibling() != null) { |
| 183 | parent.setChild(node.getPreviousSibling()); |
| 184 | } |
| 185 | node = null; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Delete Java code files corresponding to the YANG schema. |
| 190 | * |
| 191 | * @param rootNode root node of data-model tree |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 192 | * @throws IOException when fails to delete java code file the current node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 193 | * @throws DataModelException when fails to do datamodel operations |
| 194 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 195 | public static void translatorErrorHandler(YangNode rootNode) |
| 196 | throws IOException, DataModelException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * Free other resources where translator has failed. |
| 200 | */ |
| 201 | freeRestResources(); |
| 202 | |
| 203 | /** |
| 204 | * Start removing all open files. |
| 205 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 206 | YangNode tempNode = rootNode; |
| 207 | setCurNode(tempNode.getChild()); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 208 | TraversalType curTraversal = ROOT; |
| 209 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 210 | while (tempNode != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 211 | |
| 212 | if (curTraversal != PARENT) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 213 | close(tempNode); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 214 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 215 | if (curTraversal != PARENT && tempNode.getChild() != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 216 | curTraversal = CHILD; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 217 | tempNode = tempNode.getChild(); |
| 218 | } else if (tempNode.getNextSibling() != null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 219 | curTraversal = SIBILING; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 220 | tempNode = tempNode.getNextSibling(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 221 | } else { |
| 222 | curTraversal = PARENT; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 223 | tempNode = tempNode.getParent(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | freeRestResources(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 231 | * Closes all the current open file handles of node and delete all generated |
| 232 | * files. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 233 | * |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 234 | * @param node current YANG node |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 235 | * @throws IOException when fails to do IO operations |
| 236 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 237 | private static void close(YangNode node) |
| 238 | throws IOException { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 239 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 240 | if (((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles() != null) { |
| 241 | ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().close(true); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |