blob: f0e700f2ff188dc8a413e833803909e5911dfa01 [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.datamodel.exceptions.DataModelException;
23import org.onosproject.yangutils.translator.exception.TranslatorException;
24
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/**
Bharat saraswald9822e92016-04-05 15:13:44 +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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053041 * Creates a java code generator util 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
68 * @param codeGenDir code generation directory
69 * @throws IOException when fails to generate java code file the current
70 * node
71 */
72 public static void generateJavaCode(YangNode rootNode, String codeGenDir) throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053073
Vinod Kumar S38046502016-03-23 15:30:27 +053074 YangNode curNode = rootNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053075 TraversalType curTraversal = ROOT;
Vinod Kumar S38046502016-03-23 15:30:27 +053076
77 while (!(curNode == null)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053078 if (curTraversal != PARENT) {
79 setCurNode(curNode);
Vinod Kumar S38046502016-03-23 15:30:27 +053080 generateCodeEntry(curNode, codeGenDir);
81 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +053082 if (curTraversal != PARENT && curNode.getChild() != null) {
83 curTraversal = CHILD;
Vinod Kumar S38046502016-03-23 15:30:27 +053084 curNode = curNode.getChild();
85 } else if (curNode.getNextSibling() != null) {
86 generateCodeExit(curNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053087 curTraversal = SIBILING;
Vinod Kumar S38046502016-03-23 15:30:27 +053088 curNode = curNode.getNextSibling();
89 } else {
90 generateCodeExit(curNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053091 curTraversal = PARENT;
Vinod Kumar S38046502016-03-23 15:30:27 +053092 curNode = curNode.getParent();
93 }
94 }
95 }
96
97 /**
98 * Generates the current nodes code snippet.
99 *
100 * @param curNode current data model node for which the code needs to be
101 * generated
102 * @param codeGenDir the base directory where the code needs to be generated
103 * @throws IOException IO operation exception
104 */
105 private static void generateCodeEntry(YangNode curNode,
106 String codeGenDir) throws IOException {
107
108 if (curNode instanceof JavaCodeGenerator) {
109 ((JavaCodeGenerator) curNode).generateCodeEntry(codeGenDir);
110 } else {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530111 throw new TranslatorException(
112 "Generated data model node cannot be translated to target language code");
Vinod Kumar S38046502016-03-23 15:30:27 +0530113 }
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 saraswal6ef0b762016-04-05 12:45:45 +0530128 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
169 * @throws DataModelException when fails to do datamodel operations
170 */
171 private static void free(YangNode node) throws DataModelException {
172
173 YangNode parent = node.getParent();
174 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530175
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530176 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 S38046502016-03-23 15:30:27 +0530236 }
237 }
238}