blob: 8e020824db2dd10495735d1501fec42e2c5abb21 [file] [log] [blame]
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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.utils;
18
19import java.io.IOException;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053021import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053022import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053023import org.onosproject.yangutils.datamodel.YangAugmentationHolder;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053024import org.onosproject.yangutils.datamodel.YangCase;
25import org.onosproject.yangutils.datamodel.YangChoice;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053026import org.onosproject.yangutils.datamodel.YangLeavesHolder;
27import org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028import org.onosproject.yangutils.datamodel.YangTypeHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029import org.onosproject.yangutils.translator.exception.TranslatorException;
30import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053031import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053032import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
33import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
Bharat saraswald72411a2016-04-19 01:00:16 +053034import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
Bharat saraswal33dfa012016-05-17 19:59:16 +053035import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
36import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053037
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053038import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
Bharat saraswal33dfa012016-05-17 19:59:16 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
40import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
41import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053043import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053045import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053046import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
47import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053048import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053049import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053050import static org.onosproject.yangutils.utils.UtilConstants.PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053051
52/**
53 * Represents utility class for YANG java model.
54 */
55public final class YangJavaModelUtils {
56
57 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053058 * Creates an instance of YANG java model utility.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053059 */
60 private YangJavaModelUtils() {
61 }
62
63 /**
64 * Updates YANG java file package information.
65 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053066 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053067 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053068 * @throws IOException IO operations fails
69 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053070 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
71 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053072 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +053073
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053074 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +053075 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
76 yangPluginConfig.getConflictResolver()));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053077 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
78 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
79 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053080 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +053081 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053082 }
83
84 /**
85 * Updates YANG java file package information for specified package.
86 *
87 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053088 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053089 * @throws IOException IO operations fails
90 */
janani bde4ffab2016-04-15 16:18:30 +053091 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053092 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053093 throws IOException {
94 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +053095 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
96 yangPlugin.getConflictResolver()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053097 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
98 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
99 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +0530100 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530101 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530102 }
103
104 /**
105 * Updates temporary java code fragment files.
106 *
107 * @param javaCodeGeneratorInfo YANG java file info node
108 * @throws IOException IO operations fails
109 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530110 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
111 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530112 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530113 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530114 }
115
116 /**
117 * Updates leaf information in temporary java code fragment files.
118 *
119 * @param javaCodeGeneratorInfo YANG java file info node
120 * @throws IOException IO operations fails
121 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530122 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
123 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530124 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530125 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530126 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530127 * Module / sub module node code generation.
128 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530129 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530130 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530131 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530132 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
133 if (!((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
134 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
135 }
136 } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
137 if (!((YangJavaSubModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
138 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
139 }
140 }
141
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530142 } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530143 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530144 * Container
145 * Case
146 * Grouping
147 * Input
148 * List
149 * Notification
150 * Output
151 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530152 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530153 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530154 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530155 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530156 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530157 * Typedef
158 * Union
159 */
160 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530161 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswald72411a2016-04-19 01:00:16 +0530162 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530163 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530164 * Enumeration
165 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530166 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530167 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
168
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530169 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
170 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530171 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530172 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530173 }
174 }
175
176 /**
177 * Process generate code entry of YANG node.
178 *
179 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530180 * @throws IOException IO operations fails
181 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530182 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
183 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530184 throws IOException {
185 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530186 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530187 }
188 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530189 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530190
191 }
192
193 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530194 * Updates notification node info in service temporary file.
195 *
196 * @param javaCodeGeneratorInfo java code generator info
197 * @param yangPluginConfig plugin configurations
198 * @throws IOException when fails to do IO operations
199 */
200 private static void updateNotificaitonNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
201 YangPluginConfig yangPluginConfig) throws IOException {
202 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
203 for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) {
204 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
205 .getServiceTempFiles()
206 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
207 }
208 }
209 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
210 for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo)
211 .getNotificationNodes()) {
212 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
213 .getServiceTempFiles()
214 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
215 }
216 }
217 }
218
219 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530220 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530221 *
222 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530223 * @param yangPlugin YANG plugin config
224 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530225 * @throws IOException IO operations fails
226 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530227 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
228 YangPluginConfig yangPlugin, boolean isMultiInstance)
229 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530230 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530231 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530232 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530233
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530234 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530235 * Generate the Java files corresponding to the current node.
236 */
237 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
238
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530239 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 * Update the current nodes info in its parent nodes generated files.
241 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530242 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530243 }
244
245 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530246 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530247 *
248 * @param javaCodeGeneratorInfo YANG java file info node
249 * @param yangPlugin YANG plugin config
250 * @throws IOException IO operations fails
251 */
252 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
253 YangPluginConfig yangPlugin)
254 throws IOException {
255 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
256 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530257 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530258
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530259 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
260
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530261 /*
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530262 * For augmentation of nodes.
263 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530264 if (javaCodeGeneratorInfo instanceof YangAugmentationHolder) {
265 JavaQualifiedTypeInfo augmentationHoldersInfo = new JavaQualifiedTypeInfo();
266 augmentationHoldersInfo.setClassInfo(AUGMENTATION_HOLDER);
267 augmentationHoldersInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG);
268 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
269 .addToExtendsList(augmentationHoldersInfo, (YangNode) javaCodeGeneratorInfo);
270
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530271 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530272 JavaQualifiedTypeInfo augmentedInfo = new JavaQualifiedTypeInfo();
273 augmentedInfo.setClassInfo(AUGMENTED_INFO);
274 augmentedInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG);
275 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
276 .addToExtendsList(augmentedInfo, (YangNode) javaCodeGeneratorInfo);
277
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530278 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530279
280 if (javaCodeGeneratorInfo instanceof YangCase) {
281 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530282 JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo();
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530283 String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530284 String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
285 parentsInfo.setClassInfo(parentName);
286 parentsInfo.setPkgInfo(parentPkg);
287 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
288 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo);
289
290 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530291 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530292
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530293 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530294 }
295
296 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530297 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530298 *
299 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530300 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530301 * @throws IOException IO operations fails
302 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530303 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
304 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530305 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530306 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
307 // TODO:throw exception
308 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530309 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
310 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530311 }
312
313 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530314 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530315 *
316 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530317 * @param yangPluginConfig YANG plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530318 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530319 * @throws IOException IO operations fails
320 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530321 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
322 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530323 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530324 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
325 // TODO:throw exception
326 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530327 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
328
329 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
330 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
331 }
332
Bharat saraswal33dfa012016-05-17 19:59:16 +0530333 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
334 if (!((YangJavaModule) javaCodeGeneratorInfo)
335 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
336 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
337 }
338 } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
339 if (!((YangJavaSubModule) javaCodeGeneratorInfo)
340 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
341 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
342 }
343 }
344
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530345 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530346 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530347
Bharat saraswal33dfa012016-05-17 19:59:16 +0530348 /*Updates java code generator info with events info.*/
349 private static void updateCodeGenInfoForEvent(JavaCodeGeneratorInfo javaCodeGeneratorInfo) {
350 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_SUBJECT_CLASS);
351 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_CLASS);
352 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_LISTENER_INTERFACE);
353 }
354
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530355}