blob: 64575962de9fd6a1cc0527469c8802f5f1ddfef1 [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) {
Bharat saraswalcad0e652016-05-26 23:48:38 +053080 setCurNode(codeGenNode);
81 generateCodeEntry(codeGenNode, yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +053082 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053083 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053084 curTraversal = CHILD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085 codeGenNode = codeGenNode.getChild();
86 } else if (codeGenNode.getNextSibling() != null) {
Bharat saraswal33dfa012016-05-17 19:59:16 +053087 generateCodeExit(codeGenNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053088 curTraversal = SIBILING;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053089 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S38046502016-03-23 15:30:27 +053090 } else {
Bharat saraswal33dfa012016-05-17 19:59:16 +053091 generateCodeExit(codeGenNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053092 curTraversal = PARENT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053093 codeGenNode = codeGenNode.getParent();
Vinod Kumar S38046502016-03-23 15:30:27 +053094 }
95 }
96 }
97
98 /**
99 * Generates the current nodes code snippet.
100 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530101 * @param codeGenNode current data model node for which the code needs to be
102 * generated
janani bde4ffab2016-04-15 16:18:30 +0530103 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530104 * @throws TranslatorException when fails to generate java code file the current
105 * node
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530107 private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530108 throws TranslatorException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530109
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530110 if (codeGenNode instanceof JavaCodeGenerator) {
111 ((JavaCodeGenerator) codeGenNode).generateCodeEntry(yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 } else {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530113 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530114 "Generated data model node cannot be translated to target language code");
Vinod Kumar S38046502016-03-23 15:30:27 +0530115 }
116 }
117
118 /**
119 * Generates the current nodes code target code from the snippet.
120 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530121 * @param codeGenNode current data model node for which the code needs to be
122 * generated
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530123 * @throws TranslatorException when fails to generate java code file the current
124 * node
Vinod Kumar S38046502016-03-23 15:30:27 +0530125 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530126 private static void generateCodeExit(YangNode codeGenNode) throws TranslatorException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530127
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530128 if (codeGenNode instanceof JavaCodeGenerator) {
129 ((JavaCodeGenerator) codeGenNode).generateCodeExit();
Vinod Kumar S38046502016-03-23 15:30:27 +0530130 } else {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530131 throw new TranslatorException(
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530132 "Generated data model node cannot be translated to target language code");
133 }
134 }
135
136 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530137 * Free other YANG nodes of data-model tree when error occurs while file
138 * generation of current node.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530139 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530140 private static void freeRestResources() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530141
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 YangNode freedNode = getCurNode();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530143 if (getCurNode() != null) {
144 YangNode tempNode = freedNode;
145 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530146
Bharat saraswal33dfa012016-05-17 19:59:16 +0530147 while (freedNode != tempNode.getParent()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530148
Bharat saraswal33dfa012016-05-17 19:59:16 +0530149 if (curTraversal != PARENT && freedNode.getChild() != null) {
150 curTraversal = CHILD;
151 freedNode = freedNode.getChild();
152 } else if (freedNode.getNextSibling() != null) {
153 curTraversal = SIBILING;
154 if (freedNode != tempNode) {
155 free(freedNode);
156 }
157 freedNode = freedNode.getNextSibling();
158 } else {
159 curTraversal = PARENT;
160 if (freedNode != tempNode) {
161 free(freedNode);
162 }
163 freedNode = freedNode.getParent();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530164 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530165 }
166 }
167 }
168
169 /**
170 * Free the current node.
171 *
172 * @param node YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530173 */
Vidyashree Rama74453712016-04-18 12:29:39 +0530174 private static void free(YangNode node) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530175
176 YangNode parent = node.getParent();
177 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530178
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530179 if (node.getNextSibling() != null) {
180 parent.setChild(node.getNextSibling());
181 } else if (node.getPreviousSibling() != null) {
182 parent.setChild(node.getPreviousSibling());
183 }
184 node = null;
185 }
186
187 /**
188 * Delete Java code files corresponding to the YANG schema.
189 *
190 * @param rootNode root node of data-model tree
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530191 * @throws IOException when fails to delete java code file the current node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530192 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530193 public static void translatorErrorHandler(YangNode rootNode)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530194 throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530195
Bharat saraswal33dfa012016-05-17 19:59:16 +0530196 if (rootNode != null) {
197 /**
198 * Free other resources where translator has failed.
199 */
200 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530201
Bharat saraswal33dfa012016-05-17 19:59:16 +0530202 /**
203 * Start removing all open files.
204 */
205 YangNode tempNode = rootNode;
206 setCurNode(tempNode.getChild());
207 TraversalType curTraversal = ROOT;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530208
Bharat saraswal33dfa012016-05-17 19:59:16 +0530209 while (tempNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530210
Bharat saraswal33dfa012016-05-17 19:59:16 +0530211 if (curTraversal != PARENT) {
212 close(tempNode);
213 }
214 if (curTraversal != PARENT && tempNode.getChild() != null) {
215 curTraversal = CHILD;
216 tempNode = tempNode.getChild();
217 } else if (tempNode.getNextSibling() != null) {
218 curTraversal = SIBILING;
219 tempNode = tempNode.getNextSibling();
220 } else {
221 curTraversal = PARENT;
222 tempNode = tempNode.getParent();
223 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530224 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530225
226 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530227 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530228 }
229
230 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530231 * Closes all the current open file handles of node and delete all generated
232 * files.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530233 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530234 * @param node current YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530235 * @throws IOException when fails to do IO operations
236 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530237 private static void close(YangNode node)
238 throws IOException {
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530239 if (node instanceof JavaCodeGenerator && ((TempJavaCodeFragmentFilesContainer) node)
240 .getTempJavaCodeFragmentFiles() != null) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530241 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
Vinod Kumar S38046502016-03-23 15:30:27 +0530242 }
243 }
244}