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; |
| 24 | |
| 25 | import static org.onosproject.yangutils.translator.tojava.TraversalType.CHILD; |
| 26 | import static org.onosproject.yangutils.translator.tojava.TraversalType.PARENT; |
| 27 | import static org.onosproject.yangutils.translator.tojava.TraversalType.ROOT; |
| 28 | import static org.onosproject.yangutils.translator.tojava.TraversalType.SIBILING; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Implementation of Java code generator based on application schema. |
| 32 | */ |
| 33 | public final class JavaCodeGeneratorUtil { |
| 34 | |
| 35 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 36 | * Current YANG node. |
| 37 | */ |
| 38 | private static YangNode curNode; |
| 39 | |
| 40 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 41 | * Default constructor. |
| 42 | */ |
| 43 | private JavaCodeGeneratorUtil() { |
| 44 | } |
| 45 | |
| 46 | /** |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 47 | * Returns current YANG node. |
| 48 | * |
| 49 | * @return current YANG node |
| 50 | */ |
| 51 | public static YangNode getCurNode() { |
| 52 | return curNode; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets current YANG node. |
| 57 | * |
| 58 | * @param node current YANG node |
| 59 | */ |
| 60 | |
| 61 | public static void setCurNode(YangNode node) { |
| 62 | curNode = node; |
| 63 | } |
| 64 | |
| 65 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 66 | * Generate Java code files corresponding to the YANG schema. |
| 67 | * |
| 68 | * @param rootNode root node of the data model tree |
| 69 | * @param codeGenDir code generation directory |
| 70 | * @throws IOException when fails to generate java code file the current |
| 71 | * node |
| 72 | */ |
| 73 | public static void generateJavaCode(YangNode rootNode, String codeGenDir) 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 | |
| 78 | while (!(curNode == null)) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 79 | if (curTraversal != PARENT) { |
| 80 | setCurNode(curNode); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 81 | generateCodeEntry(curNode, codeGenDir); |
| 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 |
| 103 | * @param codeGenDir the base directory where the code needs to be generated |
| 104 | * @throws IOException IO operation exception |
| 105 | */ |
| 106 | private static void generateCodeEntry(YangNode curNode, |
| 107 | String codeGenDir) throws IOException { |
| 108 | |
| 109 | if (curNode instanceof JavaCodeGenerator) { |
| 110 | ((JavaCodeGenerator) curNode).generateCodeEntry(codeGenDir); |
| 111 | } else { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 112 | throw new TranslatorException( |
| 113 | "Generated data model node cannot be translated to target language code"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Generates the current nodes code target code from the snippet. |
| 119 | * |
| 120 | * @param curNode current data model node for which the code needs to be |
| 121 | * generated |
| 122 | * @throws IOException IO operation exception |
| 123 | */ |
| 124 | private static void generateCodeExit(YangNode curNode) throws IOException { |
| 125 | |
| 126 | if (curNode instanceof JavaCodeGenerator) { |
| 127 | ((JavaCodeGenerator) curNode).generateCodeExit(); |
| 128 | } else { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 129 | throw new TranslatorException( |
| 130 | "Generated data model node cannot be translated to target language code"); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Free other YANG nodes of data-model tree when error occurs while file generation of current node. |
| 136 | * |
| 137 | * @throws DataModelException when fails to do datamodel operations |
| 138 | */ |
| 139 | public static void freeRestResources() throws DataModelException { |
| 140 | |
| 141 | YangNode curNode = getCurNode(); |
| 142 | YangNode tempNode = curNode; |
| 143 | TraversalType curTraversal = ROOT; |
| 144 | |
| 145 | while (!(curNode == tempNode.getParent())) { |
| 146 | |
| 147 | if (curTraversal != PARENT && curNode.getChild() != null) { |
| 148 | curTraversal = CHILD; |
| 149 | curNode = curNode.getChild(); |
| 150 | } else if (curNode.getNextSibling() != null) { |
| 151 | curTraversal = SIBILING; |
| 152 | if (curNode != tempNode) { |
| 153 | free(curNode); |
| 154 | } |
| 155 | curNode = curNode.getNextSibling(); |
| 156 | } else { |
| 157 | curTraversal = PARENT; |
| 158 | if (curNode != tempNode) { |
| 159 | free(curNode); |
| 160 | } |
| 161 | curNode = curNode.getParent(); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Free the current node. |
| 168 | * |
| 169 | * @param node YANG node |
| 170 | * @throws DataModelException when fails to do datamodel operations |
| 171 | */ |
| 172 | private static void free(YangNode node) throws DataModelException { |
| 173 | |
| 174 | YangNode parent = node.getParent(); |
| 175 | parent.setChild(null); |
| 176 | if (node.getNextSibling() != null) { |
| 177 | parent.setChild(node.getNextSibling()); |
| 178 | } else if (node.getPreviousSibling() != null) { |
| 179 | parent.setChild(node.getPreviousSibling()); |
| 180 | } |
| 181 | node = null; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Delete Java code files corresponding to the YANG schema. |
| 186 | * |
| 187 | * @param rootNode root node of data-model tree |
| 188 | * @throws IOException when fails to delete java code file the current node |
| 189 | * @throws DataModelException when fails to do datamodel operations |
| 190 | */ |
| 191 | public static void translatorErrorHandler(YangNode rootNode) throws IOException, DataModelException { |
| 192 | |
| 193 | /** |
| 194 | * Free other resources where translator has failed. |
| 195 | */ |
| 196 | freeRestResources(); |
| 197 | |
| 198 | /** |
| 199 | * Start removing all open files. |
| 200 | */ |
| 201 | YangNode curNode = rootNode; |
| 202 | setCurNode(curNode.getChild()); |
| 203 | TraversalType curTraversal = ROOT; |
| 204 | |
| 205 | while (!(curNode == null)) { |
| 206 | |
| 207 | if (curTraversal != PARENT) { |
| 208 | close(curNode); |
| 209 | } |
| 210 | if (curTraversal != PARENT && curNode.getChild() != null) { |
| 211 | curTraversal = CHILD; |
| 212 | curNode = curNode.getChild(); |
| 213 | } else if (curNode.getNextSibling() != null) { |
| 214 | curTraversal = SIBILING; |
| 215 | curNode = curNode.getNextSibling(); |
| 216 | } else { |
| 217 | curTraversal = PARENT; |
| 218 | curNode = curNode.getParent(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | freeRestResources(); |
| 223 | curNode = null; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Closes all the current open file handles of node and delete all generated files. |
| 228 | * |
| 229 | * @param curNode current YANG node |
| 230 | * @throws IOException when fails to do IO operations |
| 231 | */ |
| 232 | private static void close(YangNode curNode) throws IOException { |
| 233 | |
| 234 | if (((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles() != null) { |
| 235 | ((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().close(true); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |