blob: 3f378c7a1b20de9263b20ecd38150ab94d5a7d46 [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.YangCase;
23import org.onosproject.yangutils.datamodel.YangChoice;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053024import org.onosproject.yangutils.datamodel.YangLeavesHolder;
25import org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053026import org.onosproject.yangutils.datamodel.YangTypeHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053027import org.onosproject.yangutils.translator.exception.TranslatorException;
28import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053029import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053030import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
31import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
Bharat saraswald72411a2016-04-19 01:00:16 +053032import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
Bharat saraswal33dfa012016-05-17 19:59:16 +053033import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
34import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053035
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053036import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
Bharat saraswal33dfa012016-05-17 19:59:16 +053037import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
38import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
39import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053040import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053041import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053042import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
Vidyashree Rama210c01d2016-05-20 16:29:25 +053043import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
45import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053046
47/**
48 * Represents utility class for YANG java model.
49 */
50public final class YangJavaModelUtils {
51
52 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053053 * Creates an instance of YANG java model utility.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053054 */
55 private YangJavaModelUtils() {
56 }
57
58 /**
59 * Updates YANG java file package information.
60 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053061 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053062 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053063 * @throws IOException IO operations fails
64 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053065 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
66 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053067 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +053068
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053069 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +053070 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
71 yangPluginConfig.getConflictResolver()));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053072 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
73 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
74 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053075 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +053076 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053077 }
78
79 /**
80 * Updates YANG java file package information for specified package.
81 *
82 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053083 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053084 * @throws IOException IO operations fails
85 */
janani bde4ffab2016-04-15 16:18:30 +053086 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053088 throws IOException {
89 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +053090 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
91 yangPlugin.getConflictResolver()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053092 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
93 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
94 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053095 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +053096 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053097 }
98
99 /**
100 * Updates temporary java code fragment files.
101 *
102 * @param javaCodeGeneratorInfo YANG java file info node
103 * @throws IOException IO operations fails
104 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530105 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
106 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530107 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530108 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530109 }
110
111 /**
112 * Updates leaf information in temporary java code fragment files.
113 *
114 * @param javaCodeGeneratorInfo YANG java file info node
115 * @throws IOException IO operations fails
116 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530117 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
118 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530119 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530120 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530121 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530122 * Module / sub module node code generation.
123 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530124 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530125 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530126 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530127 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
128 if (!((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
129 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
130 }
131 } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
132 if (!((YangJavaSubModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
133 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
134 }
135 }
136
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530137 } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530138 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530139 * Container
140 * Case
141 * Grouping
142 * Input
143 * List
144 * Notification
145 * Output
146 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530147 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530148 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530149 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530150 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530151 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530152 * Typedef
153 * Union
154 */
155 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530156 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswald72411a2016-04-19 01:00:16 +0530157 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530158 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530159 * Enumeration
160 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530161 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530162 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
163
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530164 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
165 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530166 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530167 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530168 }
169 }
170
171 /**
172 * Process generate code entry of YANG node.
173 *
174 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530175 * @throws IOException IO operations fails
176 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530177 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
178 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530179 throws IOException {
180 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530181 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530182 }
183 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530184 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530185
186 }
187
188 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530189 * Updates notification node info in service temporary file.
190 *
191 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530192 * @param yangPluginConfig plugin configurations
Bharat saraswal33dfa012016-05-17 19:59:16 +0530193 * @throws IOException when fails to do IO operations
194 */
195 private static void updateNotificaitonNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530196 YangPluginConfig yangPluginConfig)
197 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530198 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
199 for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) {
200 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
201 .getServiceTempFiles()
202 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
203 }
204 }
205 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
206 for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo)
207 .getNotificationNodes()) {
208 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
209 .getServiceTempFiles()
210 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
211 }
212 }
213 }
214
215 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530216 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530217 *
218 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530219 * @param yangPlugin YANG plugin config
220 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530221 * @throws IOException IO operations fails
222 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530223 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
224 YangPluginConfig yangPlugin, boolean isMultiInstance)
225 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530226 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530227 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530228 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530229
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530230 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530231 * Generate the Java files corresponding to the current node.
232 */
233 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
234
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530235 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530236 * Update the current nodes info in its parent nodes generated files.
237 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530238 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530239 }
240
241 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530242 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530243 *
244 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530245 * @param yangPlugin YANG plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530246 * @throws IOException IO operations fails
247 */
248 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
249 YangPluginConfig yangPlugin)
250 throws IOException {
251 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
252 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530253 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530254
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530255 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
256
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530257 /*
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530258 TODO: Need to use this, when augmentation is added in YMS
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530259 * For augmentation of nodes.
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530260
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530261 if (javaCodeGeneratorInfo instanceof YangAugmentationHolder) {
262 JavaQualifiedTypeInfo augmentationHoldersInfo = new JavaQualifiedTypeInfo();
263 augmentationHoldersInfo.setClassInfo(AUGMENTATION_HOLDER);
264 augmentationHoldersInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG);
265 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
266 .addToExtendsList(augmentationHoldersInfo, (YangNode) javaCodeGeneratorInfo);
267
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530268 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530269 JavaQualifiedTypeInfo augmentedInfo = new JavaQualifiedTypeInfo();
270 augmentedInfo.setClassInfo(AUGMENTED_INFO);
271 augmentedInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG);
272 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
273 .addToExtendsList(augmentedInfo, (YangNode) javaCodeGeneratorInfo);
274
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530275 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530276 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530277 if (javaCodeGeneratorInfo instanceof YangCase) {
278 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530279 JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo();
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530280 String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530281 String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
282 parentsInfo.setClassInfo(parentName);
283 parentsInfo.setPkgInfo(parentPkg);
284 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
285 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo);
286
287 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530288 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530289
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530290 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530291 }
292
293 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530294 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530295 *
296 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530297 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530298 * @throws IOException IO operations fails
299 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530300 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
301 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530302 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530303 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
304 // TODO:throw exception
305 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530306 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
307 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530308 }
309
310 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530311 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530312 *
313 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530314 * @param yangPluginConfig YANG plugin config
315 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530316 * @throws IOException IO operations fails
317 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530318 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
319 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530320 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530321 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
322 // TODO:throw exception
323 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530324 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
325
326 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
327 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
328 }
329
Bharat saraswal33dfa012016-05-17 19:59:16 +0530330 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
331 if (!((YangJavaModule) javaCodeGeneratorInfo)
332 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
333 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
334 }
335 } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
336 if (!((YangJavaSubModule) javaCodeGeneratorInfo)
337 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
338 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
339 }
340 }
341
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530342 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530343 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530344
Bharat saraswal33dfa012016-05-17 19:59:16 +0530345 /*Updates java code generator info with events info.*/
346 private static void updateCodeGenInfoForEvent(JavaCodeGeneratorInfo javaCodeGeneratorInfo) {
347 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_SUBJECT_CLASS);
348 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_CLASS);
349 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_LISTENER_INTERFACE);
350 }
351
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530352}