blob: 6d05ba6458916df32570c8fca14dd4e160feddb4 [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 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042 * Creates a java code generator utility 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
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 * node
Vinod Kumar S38046502016-03-23 15:30:27 +053072 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053073 public static void generateJavaCode(YangNode rootNode, YangPluginConfig yangPlugin)
74 throws IOException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053075
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076 YangNode codeGenNode = rootNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053077 TraversalType curTraversal = ROOT;
Vinod Kumar S38046502016-03-23 15:30:27 +053078
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079 while (codeGenNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053080 if (curTraversal != PARENT) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053081 setCurNode(codeGenNode);
82 generateCodeEntry(codeGenNode, yangPlugin);
Vinod Kumar S38046502016-03-23 15:30:27 +053083 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053084 if (curTraversal != PARENT && codeGenNode.getChild() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053085 curTraversal = CHILD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086 codeGenNode = codeGenNode.getChild();
87 } else if (codeGenNode.getNextSibling() != null) {
88 generateCodeExit(codeGenNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053089 curTraversal = SIBILING;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053090 codeGenNode = codeGenNode.getNextSibling();
Vinod Kumar S38046502016-03-23 15:30:27 +053091 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053092 generateCodeExit(codeGenNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +053093 curTraversal = PARENT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053094 codeGenNode = codeGenNode.getParent();
Vinod Kumar S38046502016-03-23 15:30:27 +053095 }
96 }
97 }
98
99 /**
100 * Generates the current nodes code snippet.
101 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530102 * @param codeGenNode current data model node for which the code needs to be
103 * generated
janani bde4ffab2016-04-15 16:18:30 +0530104 * @param yangPlugin YANG plugin config
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 * @throws IOException IO operation exception
106 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107 private static void generateCodeEntry(YangNode codeGenNode, YangPluginConfig yangPlugin)
108 throws IOException {
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 saraswal6ef0b762016-04-05 12:45:45 +0530113 throw new TranslatorException(
114 "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
Vinod Kumar S38046502016-03-23 15:30:27 +0530123 * @throws IOException IO operation exception
124 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530125 private static void generateCodeExit(YangNode codeGenNode)
126 throws IOException {
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 saraswal6ef0b762016-04-05 12:45:45 +0530131 throw new TranslatorException(
132 "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.
139 *
140 * @throws DataModelException when fails to do datamodel operations
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530141 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530142 private static void freeRestResources()
143 throws DataModelException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530144
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530145 YangNode freedNode = getCurNode();
146 YangNode tempNode = freedNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530147 TraversalType curTraversal = ROOT;
148
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530149 while (freedNode != tempNode.getParent()) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530150
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530151 if (curTraversal != PARENT && freedNode.getChild() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530152 curTraversal = CHILD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530153 freedNode = freedNode.getChild();
154 } else if (freedNode.getNextSibling() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530155 curTraversal = SIBILING;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530156 if (freedNode != tempNode) {
157 free(freedNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530158 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530159 freedNode = freedNode.getNextSibling();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530160 } else {
161 curTraversal = PARENT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530162 if (freedNode != tempNode) {
163 free(freedNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530164 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530165 freedNode = freedNode.getParent();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530166 }
167 }
168 }
169
170 /**
171 * Free the current node.
172 *
173 * @param node YANG node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530174 */
Vidyashree Rama74453712016-04-18 12:29:39 +0530175 private static void free(YangNode node) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530176
177 YangNode parent = node.getParent();
178 parent.setChild(null);
Bharat saraswald9822e92016-04-05 15:13:44 +0530179
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530180 if (node.getNextSibling() != null) {
181 parent.setChild(node.getNextSibling());
182 } else if (node.getPreviousSibling() != null) {
183 parent.setChild(node.getPreviousSibling());
184 }
185 node = null;
186 }
187
188 /**
189 * Delete Java code files corresponding to the YANG schema.
190 *
191 * @param rootNode root node of data-model tree
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530192 * @throws IOException when fails to delete java code file the current node
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530193 * @throws DataModelException when fails to do datamodel operations
194 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530195 public static void translatorErrorHandler(YangNode rootNode)
196 throws IOException, DataModelException {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530197
198 /**
199 * Free other resources where translator has failed.
200 */
201 freeRestResources();
202
203 /**
204 * Start removing all open files.
205 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530206 YangNode tempNode = rootNode;
207 setCurNode(tempNode.getChild());
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530208 TraversalType curTraversal = ROOT;
209
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530210 while (tempNode != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530211
212 if (curTraversal != PARENT) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530213 close(tempNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530214 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530215 if (curTraversal != PARENT && tempNode.getChild() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530216 curTraversal = CHILD;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530217 tempNode = tempNode.getChild();
218 } else if (tempNode.getNextSibling() != null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530219 curTraversal = SIBILING;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530220 tempNode = tempNode.getNextSibling();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530221 } else {
222 curTraversal = PARENT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530223 tempNode = tempNode.getParent();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530224 }
225 }
226
227 freeRestResources();
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 {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530239
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 if (((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles() != null) {
241 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().close(true);
Vinod Kumar S38046502016-03-23 15:30:27 +0530242 }
243 }
244}