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