blob: 19314e2f6993b805c5fe0e215d44867d1e3dce0e [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
Bharat saraswalb551aae2016-07-14 15:18:20 +053017package org.onosproject.yangutils.translator.tojava;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053018
Bharat saraswalb1170bd2016-07-14 13:26:18 +053019import java.io.File;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053020import java.io.IOException;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053021import java.util.ArrayList;
22import java.util.List;
23
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053024import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053025import org.onosproject.yangutils.datamodel.YangCase;
26import org.onosproject.yangutils.datamodel.YangChoice;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053027import org.onosproject.yangutils.datamodel.YangGrouping;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053028import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Bharat saraswalb551aae2016-07-14 15:18:20 +053029import org.onosproject.yangutils.datamodel.YangModule;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053030import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalb551aae2016-07-14 15:18:20 +053031import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053032import org.onosproject.yangutils.datamodel.YangTypeDef;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053033import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053034import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053035import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswalb551aae2016-07-14 15:18:20 +053036import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment;
37import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
38import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
39import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053040import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053041
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053042import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
Bharat saraswal33dfa012016-05-17 19:59:16 +053043import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
44import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
45import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053046import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053048import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
49import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
50import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
51import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053052import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053053import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Bharat saraswalb551aae2016-07-14 15:18:20 +053054import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO;
55import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053056import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
57import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
58import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053059
60/**
61 * Represents utility class for YANG java model.
62 */
63public final class YangJavaModelUtils {
64
65 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053066 * Creates an instance of YANG java model utility.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053067 */
68 private YangJavaModelUtils() {
69 }
70
71 /**
72 * Updates YANG java file package information.
73 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053074 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053075 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053076 * @throws IOException IO operations fails
77 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053078 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +053079 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053080 throws IOException {
Bharat saraswalb1170bd2016-07-14 13:26:18 +053081 if (javaCodeGeneratorInfo instanceof YangJavaAugment) {
82 javaCodeGeneratorInfo.getJavaFileInfo()
83 .setJavaName(((YangJavaAugment) javaCodeGeneratorInfo).getAugmentClassName());
84 } else {
85 javaCodeGeneratorInfo.getJavaFileInfo()
86 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
87 yangPluginConfig.getConflictResolver()));
88 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053089 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
90 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
91 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053092 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +053093 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053094 }
95
96 /**
97 * Updates YANG java file package information for specified package.
98 *
99 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530100 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530101 * @throws IOException IO operations fails
102 */
janani bde4ffab2016-04-15 16:18:30 +0530103 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530104 String pkg) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530105 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +0530106 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
107 yangPlugin.getConflictResolver()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530108 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
109 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
110 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530111 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getManagerCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530112 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530113 }
114
115 /**
116 * Updates temporary java code fragment files.
117 *
118 * @param javaCodeGeneratorInfo YANG java file info node
119 * @throws IOException IO operations fails
120 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530121 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
122 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530123 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530124 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530125 }
126
127 /**
128 * Updates leaf information in temporary java code fragment files.
129 *
130 * @param javaCodeGeneratorInfo YANG java file info node
131 * @throws IOException IO operations fails
132 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530133 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530134 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530135 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530136 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530137 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530138 * Module / sub module node code generation.
139 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530140 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530141 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530142 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530143 if (javaCodeGeneratorInfo instanceof YangJavaModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530144 if (!((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
145 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
146 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530147 } else if (javaCodeGeneratorInfo instanceof YangJavaSubModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530148 if (!((YangJavaSubModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
149 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
150 }
151 }
152
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530153 } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530154 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530155 * Container
156 * Case
157 * Grouping
158 * Input
159 * List
160 * Notification
161 * Output
162 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530163 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530164 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530165 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530166 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530167 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530168 * Typedef
169 * Union
170 */
171 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530172 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswald72411a2016-04-19 01:00:16 +0530173 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530174 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530175 * Enumeration
176 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530177 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530178 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
179
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
181 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530182 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530183 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530184 }
185 }
186
187 /**
188 * Process generate code entry of YANG node.
189 *
190 * @param javaCodeGeneratorInfo YANG java file info node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530191 * @param yangPluginConfig plugin configurations
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530192 * @throws IOException IO operations fails
193 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530194 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530195 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530196 throws IOException {
197 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530198 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530199 }
200 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530201 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530202
203 }
204
205 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530206 * Updates notification node info in service temporary file.
207 *
208 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530209 * @param yangPluginConfig plugin configurations
Bharat saraswal33dfa012016-05-17 19:59:16 +0530210 * @throws IOException when fails to do IO operations
211 */
212 private static void updateNotificaitonNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530213 YangPluginConfig yangPluginConfig)
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530214 throws IOException {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530215 if (javaCodeGeneratorInfo instanceof YangJavaModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530216 for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) {
217 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
218 .getServiceTempFiles()
219 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
220 }
221 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530222 if (javaCodeGeneratorInfo instanceof YangJavaSubModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530223 for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo)
224 .getNotificationNodes()) {
225 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
226 .getServiceTempFiles()
227 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
228 }
229 }
230 }
231
232 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530233 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530234 *
235 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530236 * @param yangPlugin YANG plugin config
237 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530238 * @throws IOException IO operations fails
239 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530241 YangPluginConfig yangPlugin, boolean isMultiInstance)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530242 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530243 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530244 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530245 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530246
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530247 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530248 * Generate the Java files corresponding to the current node.
249 */
250 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
251
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530252 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530253 * Update the current nodes info in its parent nodes generated files.
254 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530255 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530256 }
257
258 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530259 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530260 *
261 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530262 * @param yangPlugin YANG plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530263 * @throws IOException IO operations fails
264 */
265 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530266 YangPluginConfig yangPlugin)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530267 throws IOException {
268 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
269 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530270 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530271
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530272 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530273 TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530274 if (javaCodeGeneratorInfo instanceof YangJavaAugment) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530275
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530276 JavaQualifiedTypeInfo augmentedBuilderInfo = new JavaQualifiedTypeInfo();
277 JavaQualifiedTypeInfo augmentedBuilderClassInfo = new JavaQualifiedTypeInfo();
278 JavaQualifiedTypeInfo augmentedClassInfo = new JavaQualifiedTypeInfo();
279 JavaQualifiedTypeInfo augmentedImplInfo = new JavaQualifiedTypeInfo();
280 YangNode augmentedNode = ((YangJavaAugment) javaCodeGeneratorInfo).getAugmentedNode();
281 String name = null;
282 JavaFileInfo augmentedfileInfo = ((JavaFileInfoContainer) augmentedNode).getJavaFileInfo();
283 if (augmentedfileInfo.getJavaName() != null) {
284 name = getCapitalCase(augmentedfileInfo.getJavaName());
285 augmentedClassInfo.setClassInfo(name);
286 augmentedClassInfo.setPkgInfo(augmentedfileInfo.getPackage());
287 } else {
288 name = getCapitalCase(getCamelCase(augmentedNode.getName(), yangPlugin.getConflictResolver()));
289 augmentedClassInfo.setClassInfo(name);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530290 augmentedClassInfo.setPkgInfo(getAugmentedNodesPackage(augmentedNode,
291 yangPlugin));
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530292 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530293
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530294 augmentedBuilderInfo.setClassInfo(name + PERIOD + name + BUILDER);
295 augmentedBuilderInfo.setPkgInfo(augmentedClassInfo.getPkgInfo());
296 augmentedBuilderClassInfo.setClassInfo(getCapitalCase(DEFAULT) + name + PERIOD + name + BUILDER);
297 augmentedBuilderClassInfo.setPkgInfo(augmentedClassInfo.getPkgInfo());
298 augmentedImplInfo.setClassInfo(getCapitalCase(DEFAULT) + name);
299 augmentedImplInfo.setPkgInfo(augmentedBuilderInfo.getPkgInfo());
300 ((YangJavaAugment) javaCodeGeneratorInfo).addToExtendedClassInfo(augmentedBuilderInfo);
301 ((YangJavaAugment) javaCodeGeneratorInfo).addToExtendedClassInfo(augmentedImplInfo);
302 ((YangJavaAugment) javaCodeGeneratorInfo).addToExtendedClassInfo(augmentedBuilderClassInfo);
303 ((YangJavaAugment) javaCodeGeneratorInfo).addToExtendedClassInfo(augmentedClassInfo);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530304 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530305 .addToExtendsList(augmentedClassInfo, (YangNode) javaCodeGeneratorInfo,
306 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530307 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530308 .addToExtendsList(augmentedBuilderInfo, (YangNode) javaCodeGeneratorInfo,
309 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530310 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530311 .addToExtendsList(augmentedImplInfo, (YangNode) javaCodeGeneratorInfo,
312 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530313 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530314 .addToExtendsList(augmentedBuilderClassInfo, (YangNode) javaCodeGeneratorInfo,
315 tempJavaCodeFragmentFiles.getBeanTempFiles());
316
317 JavaQualifiedTypeInfo yangAugmentedInfo = new JavaQualifiedTypeInfo();
318 yangAugmentedInfo.setClassInfo(YANG_AUGMENTED_INFO);
319 yangAugmentedInfo.setPkgInfo(YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG);
320 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
321 .addToExtendsList(yangAugmentedInfo, (YangNode) javaCodeGeneratorInfo,
322 tempJavaCodeFragmentFiles.getBeanTempFiles());
323
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530324
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530325 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530326
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530327 if (javaCodeGeneratorInfo instanceof YangCase) {
328 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530329 JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo();
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530330 String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530331 String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
332 parentsInfo.setClassInfo(parentName);
333 parentsInfo.setPkgInfo(parentPkg);
334 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530335 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo,
336 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530337
338 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530339 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530340
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530341 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530342 }
343
344 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530345 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530346 *
347 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530348 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530349 * @throws IOException IO operations fails
350 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530351 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530352 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530353 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530354 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900355 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530356 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530357 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
358 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530359 }
360
361 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530362 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530363 *
364 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530365 * @param yangPluginConfig YANG plugin config
366 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530367 * @throws IOException IO operations fails
368 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530369 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530370 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530371 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530372 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900373 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530374 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530375 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
376
377 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
378 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
379 }
380
Bharat saraswalb551aae2016-07-14 15:18:20 +0530381 if (javaCodeGeneratorInfo instanceof YangJavaModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530382 if (!((YangJavaModule) javaCodeGeneratorInfo)
383 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
384 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
385 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530386 } else if (javaCodeGeneratorInfo instanceof YangJavaSubModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530387 if (!((YangJavaSubModule) javaCodeGeneratorInfo)
388 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
389 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
390 }
391 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530392 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530393 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530394
Bharat saraswal33dfa012016-05-17 19:59:16 +0530395 /*Updates java code generator info with events info.*/
396 private static void updateCodeGenInfoForEvent(JavaCodeGeneratorInfo javaCodeGeneratorInfo) {
397 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_SUBJECT_CLASS);
398 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_CLASS);
399 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_LISTENER_INTERFACE);
400 }
401
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530402 /**
403 * Returns the node package string.
404 *
405 * @param curNode current java node whose package string needs to be set
406 * @return returns the root package string
407 */
408 public static String getCurNodePackage(YangNode curNode) {
409
410 String pkg;
411 if (!(curNode instanceof JavaFileInfoContainer)
412 || curNode.getParent() == null) {
413 throw new TranslatorException("missing parent node to get current node's package");
414 }
415
416 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
417 if (!(parentNode instanceof JavaFileInfoContainer)) {
418 throw new TranslatorException("missing parent java node to get current node's package");
419 }
420 JavaFileInfo parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
421 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
422 return pkg.toLowerCase();
423 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530424
425 /**
426 * Returns true if root node contains any data node.
427 *
428 * @param node root YANG node
429 * @return true if root node contains any data node
430 */
431 public static boolean isManagerCodeGenRequired(YangNode node) {
432 YangLeavesHolder holder = (YangLeavesHolder) node;
433
Bharat saraswalb551aae2016-07-14 15:18:20 +0530434 boolean isCodeReq = true;
435
436 List<YangNode> childNodes = new ArrayList<>();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530437 node = node.getChild();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530438 while (node != null) {
439 childNodes.add(node);
440 node = node.getNextSibling();
441 }
442
443 if (childNodes.size() == 0) {
444 if (holder.getListOfLeaf() != null && !holder.getListOfLeaf().isEmpty()) {
445 isCodeReq = true;
446 } else if (holder.getListOfLeafList() != null && !holder.getListOfLeafList().isEmpty()) {
447 isCodeReq = true;
448 }
449 return isCodeReq;
450 } else if (childNodes.size() == 1) {
451 return childNodes.get(0) instanceof YangTypeDef && childNodes.get(0) instanceof YangGrouping;
452 }
453 return isCodeReq;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530454 }
455
456 /**
457 * Return false if files are already present.
458 *
459 * @param info java file info
460 * @return false if files already present
461 */
462 public static boolean isGenerationOfCodeReq(JavaFileInfo info) {
463 File codeGenDir = new File(info.getBaseCodeGenPath()
464 + info.getPackageFilePath());
465 File[] files = codeGenDir.listFiles();
466 if (files.length >= 1) {
467 for (File file : files) {
468 if (file.getName().contentEquals(getCapitalCase(info.getJavaName() + MANAGER + ".java"))
469 || file.getName().contentEquals(getCapitalCase(info.getJavaName() + SERVICE + ".java"))) {
470 return false;
471 }
472 }
473 }
474 return true;
475 }
476
Bharat saraswalb551aae2016-07-14 15:18:20 +0530477 /**
478 * Returns augmented nodes package.
479 *
480 * @param node augmented node
481 * @param yangPluginConfig plugin config
482 * @return java package
483 */
484 public static String getAugmentedNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
485
486 List<String> clsInfo = new ArrayList<>();
487 node = node.getParent();
488 while (node != null) {
489 if (!(node instanceof YangModule)
490 || !(node instanceof YangSubModule)) {
491 if (node instanceof YangJavaAugment) {
492 clsInfo.add(((YangJavaAugment) node).getAugmentClassName());
493 } else {
494 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
495 }
496 }
497 if (node instanceof YangJavaModule
498 || node instanceof YangJavaSubModule) {
499 break;
500 }
501 node = node.getParent();
502 }
503
504 StringBuilder pkg = new StringBuilder();
505 if (node instanceof YangJavaModule) {
506 YangJavaModule module = (YangJavaModule) node;
507 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
508 .getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
509 } else if (node instanceof YangJavaSubModule) {
510 YangJavaSubModule submodule = (YangJavaSubModule) node;
511 pkg.append(getRootPackage(submodule.getVersion(),
512 submodule.getNameSpaceFromModule(submodule.getBelongsTo()),
513 submodule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
514 }
515 for (int i = 1; i <= clsInfo.size(); i++) {
516 pkg.append("." + clsInfo.get(clsInfo.size() - i));
517 }
518
519 return pkg.toString().toLowerCase();
520
521 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530522}