blob: add5504d417d8a557540dc872b42f483513d1ba7 [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.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530135 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530136 public static void freeRestResources() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530137
138 YangNode curNode = getCurNode();
139 YangNode tempNode = curNode;
140 TraversalType curTraversal = ROOT;
141
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530142 while (curNode != tempNode.getParent()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530143
144 if (curTraversal != PARENT && curNode.getChild() != null) {
145 curTraversal = CHILD;
146 curNode = curNode.getChild();
147 } else if (curNode.getNextSibling() != null) {
148 curTraversal = SIBILING;
149 if (curNode != tempNode) {
150 free(curNode);
151 }
152 curNode = curNode.getNextSibling();
153 } else {
154 curTraversal = PARENT;
155 if (curNode != tempNode) {
156 free(curNode);
157 }
158 curNode = curNode.getParent();
159 }
160 }
161 }
162
163 /**
164 * Free the current node.
165 *
166 * @param node YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530167 */
Vidyashree Rama74453712016-04-18 12:29:39 +0530168 private static void free(YangNode node) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530169
170 YangNode parent = node.getParent();
171 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530172
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530173 if (node.getNextSibling() != null) {
174 parent.setChild(node.getNextSibling());
175 } else if (node.getPreviousSibling() != null) {
176 parent.setChild(node.getPreviousSibling());
177 }
178 node = null;
179 }
180
181 /**
182 * Delete Java code files corresponding to the YANG schema.
183 *
184 * @param rootNode root node of data-model tree
185 * @throws IOException when fails to delete java code file the current node
186 * @throws DataModelException when fails to do datamodel operations
187 */
188 public static void translatorErrorHandler(YangNode rootNode) throws IOException, DataModelException {
189
190 /**
191 * Free other resources where translator has failed.
192 */
193 freeRestResources();
194
195 /**
196 * Start removing all open files.
197 */
198 YangNode curNode = rootNode;
199 setCurNode(curNode.getChild());
200 TraversalType curTraversal = ROOT;
201
Vidyashree Rama74453712016-04-18 12:29:39 +0530202 while (curNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530203
204 if (curTraversal != PARENT) {
205 close(curNode);
206 }
207 if (curTraversal != PARENT && curNode.getChild() != null) {
208 curTraversal = CHILD;
209 curNode = curNode.getChild();
210 } else if (curNode.getNextSibling() != null) {
211 curTraversal = SIBILING;
212 curNode = curNode.getNextSibling();
213 } else {
214 curTraversal = PARENT;
215 curNode = curNode.getParent();
216 }
217 }
218
219 freeRestResources();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530220 }
221
222 /**
223 * Closes all the current open file handles of node and delete all generated files.
224 *
225 * @param curNode current YANG node
226 * @throws IOException when fails to do IO operations
227 */
228 private static void close(YangNode curNode) throws IOException {
229
230 if (((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles() != null) {
231 ((HasTempJavaCodeFragmentFiles) curNode).getTempJavaCodeFragmentFiles().close(true);
Vinod Kumar S38046502016-03-23 15:30:27 +0530232 }
233 }
234}