blob: 7e010dbd6f51c544a131aea20d13453cffc8ba1f [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;
janani bde4ffab2016-04-15 16:18:30 +053024import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053025
26import static org.onosproject.yangutils.translator.tojava.TraversalType.CHILD;
27import static org.onosproject.yangutils.translator.tojava.TraversalType.PARENT;
28import static org.onosproject.yangutils.translator.tojava.TraversalType.ROOT;
29import static org.onosproject.yangutils.translator.tojava.TraversalType.SIBILING;
Vinod Kumar S38046502016-03-23 15:30:27 +053030
31/**
Vidyashree Rama74453712016-04-18 12:29:39 +053032 * Representation of java code generator based on application schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053033 */
34public final class JavaCodeGeneratorUtil {
35
36 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053037 * Current YANG node.
38 */
39 private static YangNode curNode;
40
41 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053042 * Creates a java code generator util object.
Vinod Kumar S38046502016-03-23 15:30:27 +053043 */
44 private JavaCodeGeneratorUtil() {
45 }
46
47 /**
Bharat saraswal6ef0b762016-04-05 12:45:45 +053048 * 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 saraswal6ef0b762016-04-05 12:45:45 +053061 public static void setCurNode(YangNode node) {
62 curNode = node;
63 }
64
65 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Generates Java code files corresponding to the YANG schema.
Vinod Kumar S38046502016-03-23 15:30:27 +053067 *
68 * @param rootNode root node of the data model tree
janani bde4ffab2016-04-15 16:18:30 +053069 * @param yangPlugin YANG plugin config
Vinod Kumar S38046502016-03-23 15:30:27 +053070 * @throws IOException when fails to generate java code file the current
71 * node
72 */
janani bde4ffab2016-04-15 16:18:30 +053073 public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin) throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053074
Vinod Kumar S38046502016-03-23 15:30:27 +053075 YangNode curNode = rootNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053076 TraversalType curTraversal = ROOT;
Vinod Kumar S38046502016-03-23 15:30:27 +053077
Vidyashree Rama74453712016-04-18 12:29:39 +053078 while (curNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053079 if (curTraversal != PARENT) {
80 setCurNode(curNode);
janani bde4ffab2016-04-15 16:18:30 +053081 generateCodeEntry(curNode, yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +053082 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +053083 if (curTraversal != PARENT && curNode.getChild() != null) {
84 curTraversal = CHILD;
Vinod Kumar S38046502016-03-23 15:30:27 +053085 curNode = curNode.getChild();
86 } else if (curNode.getNextSibling() != null) {
87 generateCodeExit(curNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053088 curTraversal = SIBILING;
Vinod Kumar S38046502016-03-23 15:30:27 +053089 curNode = curNode.getNextSibling();
90 } else {
91 generateCodeExit(curNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053092 curTraversal = PARENT;
Vinod Kumar S38046502016-03-23 15:30:27 +053093 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 bde4ffab2016-04-15 16:18:30 +0530103 * @param yangPlugin YANG plugin config
Vinod Kumar S38046502016-03-23 15:30:27 +0530104 * @throws IOException IO operation exception
105 */
janani bde4ffab2016-04-15 16:18:30 +0530106 private static void generateCodeEntry(YangNode curNode, YangPluginConfig yangPlugin) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530107
108 if (curNode instanceof JavaCodeGenerator) {
janani bde4ffab2016-04-15 16:18:30 +0530109 ((JavaCodeGenerator) curNode).generateCodeEntry(yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 } 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
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530169 */
Vidyashree Rama74453712016-04-18 12:29:39 +0530170 private static void free(YangNode node) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530171
172 YangNode parent = node.getParent();
173 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530174
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530175 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 Rama74453712016-04-18 12:29:39 +0530204 while (curNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530205
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 saraswal6ef0b762016-04-05 12:45:45 +0530222 }
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 S38046502016-03-23 15:30:27 +0530234 }
235 }
236}