blob: 8687115680a103972e6989bbd7b18ba03d262d48 [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
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053017package org.onosproject.yangutils.translator.tojava.javamodel;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053018
19import java.io.IOException;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053020import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053021import org.onosproject.yangutils.datamodel.YangCase;
22import org.onosproject.yangutils.datamodel.YangChoice;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053023import org.onosproject.yangutils.datamodel.YangLeavesHolder;
24import org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053025import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053026import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053027import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053028import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
29import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053030import 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;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053033import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053034
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053035import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
Bharat saraswal33dfa012016-05-17 19:59:16 +053036import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
37import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
38import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053040import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053041import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
42import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
43import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
44import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053045
46/**
47 * Represents utility class for YANG java model.
48 */
49public final class YangJavaModelUtils {
50
51 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053052 * Creates an instance of YANG java model utility.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053053 */
54 private YangJavaModelUtils() {
55 }
56
57 /**
58 * Updates YANG java file package information.
59 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053060 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053061 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053062 * @throws IOException IO operations fails
63 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053064 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
65 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053066 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +053067
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053068 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +053069 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
70 yangPluginConfig.getConflictResolver()));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
72 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
73 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053074 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +053075 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053076 }
77
78 /**
79 * Updates YANG java file package information for specified package.
80 *
81 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053082 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053083 * @throws IOException IO operations fails
84 */
janani bde4ffab2016-04-15 16:18:30 +053085 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053087 throws IOException {
88 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +053089 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
90 yangPlugin.getConflictResolver()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053091 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
92 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
93 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053094 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +053095 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053096 }
97
98 /**
99 * Updates temporary java code fragment files.
100 *
101 * @param javaCodeGeneratorInfo YANG java file info node
102 * @throws IOException IO operations fails
103 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530104 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
105 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530106 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530108 }
109
110 /**
111 * Updates leaf information in temporary java code fragment files.
112 *
113 * @param javaCodeGeneratorInfo YANG java file info node
114 * @throws IOException IO operations fails
115 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530116 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
117 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530118 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530119 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530120 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530121 * Module / sub module node code generation.
122 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530123 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530124 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530125 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530126 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
127 if (!((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
128 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
129 }
130 } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
131 if (!((YangJavaSubModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
132 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
133 }
134 }
135
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530136 } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530137 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530138 * Container
139 * Case
140 * Grouping
141 * Input
142 * List
143 * Notification
144 * Output
145 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530146 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530147 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530148 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530149 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530150 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530151 * Typedef
152 * Union
153 */
154 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530155 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswald72411a2016-04-19 01:00:16 +0530156 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530157 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530158 * Enumeration
159 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530160 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530161 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
162
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530163 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
164 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530165 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530166 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530167 }
168 }
169
170 /**
171 * Process generate code entry of YANG node.
172 *
173 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530174 * @throws IOException IO operations fails
175 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530176 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
177 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530178 throws IOException {
179 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530181 }
182 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530183 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530184
185 }
186
187 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530188 * Updates notification node info in service temporary file.
189 *
190 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530191 * @param yangPluginConfig plugin configurations
Bharat saraswal33dfa012016-05-17 19:59:16 +0530192 * @throws IOException when fails to do IO operations
193 */
194 private static void updateNotificaitonNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530195 YangPluginConfig yangPluginConfig)
196 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530197 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
198 for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) {
199 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
200 .getServiceTempFiles()
201 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
202 }
203 }
204 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
205 for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo)
206 .getNotificationNodes()) {
207 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
208 .getServiceTempFiles()
209 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
210 }
211 }
212 }
213
214 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530215 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530216 *
217 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530218 * @param yangPlugin YANG plugin config
219 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530220 * @throws IOException IO operations fails
221 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530222 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
223 YangPluginConfig yangPlugin, boolean isMultiInstance)
224 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530225 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530226 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530227 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530228
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530229 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530230 * Generate the Java files corresponding to the current node.
231 */
232 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
233
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530234 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530235 * Update the current nodes info in its parent nodes generated files.
236 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530237 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530238 }
239
240 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530241 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530242 *
243 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530244 * @param yangPlugin YANG plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530245 * @throws IOException IO operations fails
246 */
247 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
248 YangPluginConfig yangPlugin)
249 throws IOException {
250 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
251 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530252 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530253
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530254 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
255
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530256 /*
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530257 TODO: Need to use this, when augmentation is added in YMS
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530258 * For augmentation of nodes.
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530259
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530260 if (javaCodeGeneratorInfo instanceof YangAugmentationHolder) {
261 JavaQualifiedTypeInfo augmentationHoldersInfo = new JavaQualifiedTypeInfo();
262 augmentationHoldersInfo.setClassInfo(AUGMENTATION_HOLDER);
263 augmentationHoldersInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG);
264 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
265 .addToExtendsList(augmentationHoldersInfo, (YangNode) javaCodeGeneratorInfo);
266
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530267 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530268 JavaQualifiedTypeInfo augmentedInfo = new JavaQualifiedTypeInfo();
269 augmentedInfo.setClassInfo(AUGMENTED_INFO);
270 augmentedInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG);
271 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
272 .addToExtendsList(augmentedInfo, (YangNode) javaCodeGeneratorInfo);
273
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530274 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530275 */
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530276 if (javaCodeGeneratorInfo instanceof YangCase) {
277 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530278 JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo();
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530279 String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530280 String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
281 parentsInfo.setClassInfo(parentName);
282 parentsInfo.setPkgInfo(parentPkg);
283 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
284 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo);
285
286 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530287 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530288
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530289 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530290 }
291
292 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530293 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530294 *
295 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530296 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530297 * @throws IOException IO operations fails
298 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530299 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
300 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530301 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530302 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900303 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530304 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530305 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
306 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530307 }
308
309 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530310 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530311 *
312 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530313 * @param yangPluginConfig YANG plugin config
314 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530315 * @throws IOException IO operations fails
316 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530317 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530318 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530319 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530320 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900321 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530322 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530323 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
324
325 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
326 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
327 }
328
Bharat saraswal33dfa012016-05-17 19:59:16 +0530329 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
330 if (!((YangJavaModule) javaCodeGeneratorInfo)
331 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
332 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
333 }
334 } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
335 if (!((YangJavaSubModule) javaCodeGeneratorInfo)
336 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
337 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
338 }
339 }
340
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530341 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530342 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530343
Bharat saraswal33dfa012016-05-17 19:59:16 +0530344 /*Updates java code generator info with events info.*/
345 private static void updateCodeGenInfoForEvent(JavaCodeGeneratorInfo javaCodeGeneratorInfo) {
346 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_SUBJECT_CLASS);
347 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_CLASS);
348 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_LISTENER_INTERFACE);
349 }
350
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530351 /**
352 * Returns the node package string.
353 *
354 * @param curNode current java node whose package string needs to be set
355 * @return returns the root package string
356 */
357 public static String getCurNodePackage(YangNode curNode) {
358
359 String pkg;
360 if (!(curNode instanceof JavaFileInfoContainer)
361 || curNode.getParent() == null) {
362 throw new TranslatorException("missing parent node to get current node's package");
363 }
364
365 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
366 if (!(parentNode instanceof JavaFileInfoContainer)) {
367 throw new TranslatorException("missing parent java node to get current node's package");
368 }
369 JavaFileInfo parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
370 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
371 return pkg.toLowerCase();
372 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530373}