blob: 5c06f42d5895c106787785065a337d8532966404 [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +05303 *
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
17package org.onosproject.yangutils.translator.tojava;
18
19import java.io.IOException;
20
21import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
janani bde4ffab2016-04-15 16:18:30 +053023import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053024
25import static org.onosproject.yangutils.translator.tojava.TraversalType.CHILD;
26import static org.onosproject.yangutils.translator.tojava.TraversalType.PARENT;
27import static org.onosproject.yangutils.translator.tojava.TraversalType.ROOT;
28import static org.onosproject.yangutils.translator.tojava.TraversalType.SIBILING;
Vinod Kumar S38046502016-03-23 15:30:27 +053029
30/**
Vidyashree Rama74453712016-04-18 12:29:39 +053031 * Representation of java code generator based on application schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053032 */
33public final class JavaCodeGeneratorUtil {
34
35 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053036 * Current YANG node.
37 */
38 private static YangNode curNode;
39
40 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053041 * Creates a java code generator utility object.
Vinod Kumar S38046502016-03-23 15:30:27 +053042 */
43 private JavaCodeGeneratorUtil() {
44 }
45
46 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053047 * 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 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +053060 public static void setCurNode(YangNode node) {
61 curNode = node;
62 }
63
64 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053065 * Generates Java code files corresponding to the YANG schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053066 *
67 * @param rootNode root node of the data model tree
janani bde4ffab2016-04-15 16:18:30 +053068 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053069 * @throws TranslatorException when fails to generate java code file the current
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053070 * node
Vinod Kumar S38046502016-03-23 15:30:27 +053071 */
Bharat saraswal33dfa012016-05-17 19:59:16 +053072 public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053073 throws TranslatorException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053074
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075 YangNode codeGenNode = rootNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053076 TraversalType curTraversal = ROOT;
Vinod Kumar S38046502016-03-23 15:30:27 +053077
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053078 while (codeGenNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053079 if (curTraversal != PARENT) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053080 if (codeGenNode instanceof JavaCodeGenerator) {
81 setCurNode(codeGenNode);
Bharat saraswal33dfa012016-05-17 19:59:16 +053082 generateCodeEntry(codeGenNode, yangPlugin);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053083 } else {
84 /*
85 * For grouping and uses, there is no code generation, skip the generation for the child.
86 */
87 if (codeGenNode.getNextSibling() != null) {
88 curTraversal = SIBILING;
89 codeGenNode = codeGenNode.getNextSibling();
90 } else {
91 curTraversal = PARENT;
92 codeGenNode = codeGenNode.getParent();
93 }
94 continue;
95 }
96
Vinod Kumar S38046502016-03-23 15:30:27 +053097 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053098 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053099 curTraversal = CHILD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530100 codeGenNode = codeGenNode.getChild();
101 } else if (codeGenNode.getNextSibling() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530102 generateCodeExit(codeGenNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530103 curTraversal = SIBILING;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530104 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 } else {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530106 generateCodeExit(codeGenNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530107 curTraversal = PARENT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530108 codeGenNode = codeGenNode.getParent();
Vinod Kumar S38046502016-03-23 15:30:27 +0530109 }
110 }
111 }
112
113 /**
114 * Generates the current nodes code snippet.
115 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530116 * @param codeGenNode current data model node for which the code needs to be
117 * generated
janani bde4ffab2016-04-15 16:18:30 +0530118 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530119 * @throws TranslatorException when fails to generate java code file the current
120 * node
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530122 private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530123 throws TranslatorException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530124
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530125 if (codeGenNode instanceof JavaCodeGenerator) {
126 ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +0530127 } else {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530128 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530129 "Generated data model node cannot be translated to target language code");
Vinod Kumar S38046502016-03-23 15:30:27 +0530130 }
131 }
132
133 /**
134 * Generates the current nodes code target code from the snippet.
135 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 * @param codeGenNode current data model node for which the code needs to be
137 * generated
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530138 * @throws TranslatorException when fails to generate java code file the current
139 * node
Vinod Kumar S38046502016-03-23 15:30:27 +0530140 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530141 private static void generateCodeExit(YangNode codeGenNode) throws TranslatorException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530142
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530143 if (codeGenNode instanceof JavaCodeGenerator) {
144 ((JavaCodeGenerator) codeGenNode).generateCodeExit();
Vinod Kumar S38046502016-03-23 15:30:27 +0530145 } else {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530146 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530147 "Generated data model node cannot be translated to target language code");
148 }
149 }
150
151 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 * Free other YANG nodes of data-model tree when error occurs while file
153 * generation of current node.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530154 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530155 private static void freeRestResources() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530156
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157 YangNode freedNode = getCurNode();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530158 if (getCurNode() != null) {
159 YangNode tempNode = freedNode;
160 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530161
Bharat saraswal33dfa012016-05-17 19:59:16 +0530162 while (freedNode != tempNode.getParent()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530163
Bharat saraswal33dfa012016-05-17 19:59:16 +0530164 if (curTraversal != PARENT && freedNode.getChild() != null) {
165 curTraversal = CHILD;
166 freedNode = freedNode.getChild();
167 } else if (freedNode.getNextSibling() != null) {
168 curTraversal = SIBILING;
169 if (freedNode != tempNode) {
170 free(freedNode);
171 }
172 freedNode = freedNode.getNextSibling();
173 } else {
174 curTraversal = PARENT;
175 if (freedNode != tempNode) {
176 free(freedNode);
177 }
178 freedNode = freedNode.getParent();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530179 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530180 }
181 }
182 }
183
184 /**
185 * Free the current node.
186 *
187 * @param node YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530188 */
Vidyashree Rama74453712016-04-18 12:29:39 +0530189 private static void free(YangNode node) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530190
191 YangNode parent = node.getParent();
192 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530193
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530194 if (node.getNextSibling() != null) {
195 parent.setChild(node.getNextSibling());
196 } else if (node.getPreviousSibling() != null) {
197 parent.setChild(node.getPreviousSibling());
198 }
199 node = null;
200 }
201
202 /**
203 * Delete Java code files corresponding to the YANG schema.
204 *
205 * @param rootNode root node of data-model tree
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530206 * @throws IOException when fails to delete java code file the current node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530207 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530208 public static void translatorErrorHandler(YangNode rootNode)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530209 throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530210
Bharat saraswal33dfa012016-05-17 19:59:16 +0530211 if (rootNode != null) {
212 /**
213 * Free other resources where translator has failed.
214 */
215 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530216
Bharat saraswal33dfa012016-05-17 19:59:16 +0530217 /**
218 * Start removing all open files.
219 */
220 YangNode tempNode = rootNode;
221 setCurNode(tempNode.getChild());
222 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530223
Bharat saraswal33dfa012016-05-17 19:59:16 +0530224 while (tempNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530225
Bharat saraswal33dfa012016-05-17 19:59:16 +0530226 if (curTraversal != PARENT) {
227 close(tempNode);
228 }
229 if (curTraversal != PARENT && tempNode.getChild() != null) {
230 curTraversal = CHILD;
231 tempNode = tempNode.getChild();
232 } else if (tempNode.getNextSibling() != null) {
233 curTraversal = SIBILING;
234 tempNode = tempNode.getNextSibling();
235 } else {
236 curTraversal = PARENT;
237 tempNode = tempNode.getParent();
238 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530239 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530240
241 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530242 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530243 }
244
245 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530246 * Closes all the current open file handles of node and delete all generated
247 * files.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530248 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530249 * @param node current YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530250 * @throws IOException when fails to do IO operations
251 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530252 private static void close(YangNode node)
253 throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530254
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530255 if (((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530256 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
Vinod Kumar S38046502016-03-23 15:30:27 +0530257 }
258 }
259}