blob: 582b9b50e8692bf8f030ba8193ab512169c6c291 [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
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;
29import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053030import org.onosproject.yangutils.datamodel.YangTypeDef;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053031import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053032import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053033import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053034import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
35import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053036import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053037import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053038import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053039import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053040
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053041import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
Bharat saraswal33dfa012016-05-17 19:59:16 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
43import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
44import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053045import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053047import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
48import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
49import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
50import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053051import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053052import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053053import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
54import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
55import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053056
57/**
58 * Represents utility class for YANG java model.
59 */
60public final class YangJavaModelUtils {
61
62 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053063 * Creates an instance of YANG java model utility.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053064 */
65 private YangJavaModelUtils() {
66 }
67
68 /**
69 * Updates YANG java file package information.
70 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053072 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053073 * @throws IOException IO operations fails
74 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053075 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +053076 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053077 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +053078
Bharat saraswalb1170bd2016-07-14 13:26:18 +053079 if (javaCodeGeneratorInfo instanceof YangJavaAugment) {
80 javaCodeGeneratorInfo.getJavaFileInfo()
81 .setJavaName(((YangJavaAugment) javaCodeGeneratorInfo).getAugmentClassName());
82 } else {
83 javaCodeGeneratorInfo.getJavaFileInfo()
84 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
85 yangPluginConfig.getConflictResolver()));
86 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
88 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
89 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053090 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +053091 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053092 }
93
94 /**
95 * Updates YANG java file package information for specified package.
96 *
97 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053098 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053099 * @throws IOException IO operations fails
100 */
janani bde4ffab2016-04-15 16:18:30 +0530101 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530102 String pkg) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530103 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +0530104 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
105 yangPlugin.getConflictResolver()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530106 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
107 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
108 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530109 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getManagerCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530110 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530111 }
112
113 /**
114 * Updates temporary java code fragment files.
115 *
116 * @param javaCodeGeneratorInfo YANG java file info node
117 * @throws IOException IO operations fails
118 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530119 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
120 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530121 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530122 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530123 }
124
125 /**
126 * Updates leaf information in temporary java code fragment files.
127 *
128 * @param javaCodeGeneratorInfo YANG java file info node
129 * @throws IOException IO operations fails
130 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530131 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530132 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530133 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530134 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530135 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530136 * Module / sub module node code generation.
137 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530138 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530139 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530140 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530141 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
142 if (!((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
143 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
144 }
145 } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
146 if (!((YangJavaSubModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
147 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
148 }
149 }
150
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530151 } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530152 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530153 * Container
154 * Case
155 * Grouping
156 * Input
157 * List
158 * Notification
159 * Output
160 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530161 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530162 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530163 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530164 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530165 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530166 * Typedef
167 * Union
168 */
169 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530170 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswald72411a2016-04-19 01:00:16 +0530171 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530172 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530173 * Enumeration
174 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530175 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530176 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
177
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530178 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
179 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530180 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530181 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530182 }
183 }
184
185 /**
186 * Process generate code entry of YANG node.
187 *
188 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530189 * @throws IOException IO operations fails
190 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530191 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530192 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530193 throws IOException {
194 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530195 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530196 }
197 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530198 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530199
200 }
201
202 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530203 * Updates notification node info in service temporary file.
204 *
205 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530206 * @param yangPluginConfig plugin configurations
Bharat saraswal33dfa012016-05-17 19:59:16 +0530207 * @throws IOException when fails to do IO operations
208 */
209 private static void updateNotificaitonNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530210 YangPluginConfig yangPluginConfig)
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530211 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530212 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
213 for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) {
214 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
215 .getServiceTempFiles()
216 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
217 }
218 }
219 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
220 for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo)
221 .getNotificationNodes()) {
222 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
223 .getServiceTempFiles()
224 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
225 }
226 }
227 }
228
229 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530230 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530231 *
232 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530233 * @param yangPlugin YANG plugin config
234 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530235 * @throws IOException IO operations fails
236 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530237 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530238 YangPluginConfig yangPlugin, boolean isMultiInstance)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530239 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530240 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530241 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530242 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530243
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530244 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530245 * Generate the Java files corresponding to the current node.
246 */
247 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
248
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530249 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530250 * Update the current nodes info in its parent nodes generated files.
251 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530252 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530253 }
254
255 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530256 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530257 *
258 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530259 * @param yangPlugin YANG plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530260 * @throws IOException IO operations fails
261 */
262 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530263 YangPluginConfig yangPlugin)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530264 throws IOException {
265 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
266 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530267 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530268
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530269 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
270
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530271 if (javaCodeGeneratorInfo instanceof YangJavaAugment) {
272 JavaQualifiedTypeInfo augmentedBuilderInfo = new JavaQualifiedTypeInfo();
273 JavaQualifiedTypeInfo augmentedBuilderClassInfo = new JavaQualifiedTypeInfo();
274 JavaQualifiedTypeInfo augmentedClassInfo = new JavaQualifiedTypeInfo();
275 JavaQualifiedTypeInfo augmentedImplInfo = new JavaQualifiedTypeInfo();
276 YangNode augmentedNode = ((YangJavaAugment) javaCodeGeneratorInfo).getAugmentedNode();
277 String name = null;
278 JavaFileInfo augmentedfileInfo = ((JavaFileInfoContainer) augmentedNode).getJavaFileInfo();
279 if (augmentedfileInfo.getJavaName() != null) {
280 name = getCapitalCase(augmentedfileInfo.getJavaName());
281 augmentedClassInfo.setClassInfo(name);
282 augmentedClassInfo.setPkgInfo(augmentedfileInfo.getPackage());
283 } else {
284 name = getCapitalCase(getCamelCase(augmentedNode.getName(), yangPlugin.getConflictResolver()));
285 augmentedClassInfo.setClassInfo(name);
286 augmentedClassInfo.setPkgInfo(getAugmentedNodesPackage(augmentedNode, yangPlugin));
287 }
288 augmentedBuilderInfo.setClassInfo(name + PERIOD + name + BUILDER);
289 augmentedBuilderInfo.setPkgInfo(augmentedClassInfo.getPkgInfo());
290 augmentedBuilderClassInfo.setClassInfo(getCapitalCase(DEFAULT) + name + PERIOD + name + BUILDER);
291 augmentedBuilderClassInfo.setPkgInfo(augmentedClassInfo.getPkgInfo());
292 augmentedImplInfo.setClassInfo(getCapitalCase(DEFAULT) + name);
293 augmentedImplInfo.setPkgInfo(augmentedBuilderInfo.getPkgInfo());
294 ((YangJavaAugment) javaCodeGeneratorInfo).addToExtendedClassInfo(augmentedBuilderInfo);
295 ((YangJavaAugment) javaCodeGeneratorInfo).addToExtendedClassInfo(augmentedImplInfo);
296 ((YangJavaAugment) javaCodeGeneratorInfo).addToExtendedClassInfo(augmentedBuilderClassInfo);
297 ((YangJavaAugment) javaCodeGeneratorInfo).addToExtendedClassInfo(augmentedClassInfo);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530298 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530299 .addToExtendsList(augmentedClassInfo, (YangNode) javaCodeGeneratorInfo);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530300 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530301 .addToExtendsList(augmentedBuilderInfo, (YangNode) javaCodeGeneratorInfo);
302 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
303 .addToExtendsList(augmentedImplInfo, (YangNode) javaCodeGeneratorInfo);
304 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
305 .addToExtendsList(augmentedBuilderClassInfo, (YangNode) javaCodeGeneratorInfo);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530306
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530307 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530308
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530309 if (javaCodeGeneratorInfo instanceof YangCase) {
310 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530311 JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo();
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530312 String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530313 String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
314 parentsInfo.setClassInfo(parentName);
315 parentsInfo.setPkgInfo(parentPkg);
316 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
317 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo);
318
319 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530320 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530321
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530322 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530323 }
324
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530325 private static String getAugmentedNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
326
327 List<String> clsInfo = new ArrayList<>();
328 node = node.getParent();
329 while (node != null) {
330 if (!(node instanceof YangJavaModule)
331 || !(node instanceof YangJavaSubModule)) {
332 if (node instanceof YangJavaAugment) {
333 clsInfo.add(((YangJavaAugment) node).getAugmentClassName());
334 } else {
335 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
336 }
337 }
338 if (node instanceof YangJavaModule
339 || node instanceof YangJavaSubModule) {
340 break;
341 }
342 node = node.getParent();
343 }
344
345 StringBuilder pkg = new StringBuilder();
346 if (node instanceof YangJavaModule) {
347 YangJavaModule module = (YangJavaModule) node;
348 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
349 .getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
350 } else if (node instanceof YangJavaSubModule) {
351 YangJavaSubModule submodule = (YangJavaSubModule) node;
352 pkg.append(getRootPackage(submodule.getVersion(),
353 submodule.getNameSpaceFromModule(submodule.getBelongsTo()),
354 submodule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
355 }
356 for (int i = 1; i <= clsInfo.size(); i++) {
357 pkg.append("." + clsInfo.get(clsInfo.size() - i));
358 }
359
360 return pkg.toString().toLowerCase();
361
362 }
363
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530364 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530365 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530366 *
367 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530368 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530369 * @throws IOException IO operations fails
370 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530371 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530372 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530373 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530374 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900375 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530376 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530377 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
378 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530379 }
380
381 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530382 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530383 *
384 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530385 * @param yangPluginConfig YANG plugin config
386 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530387 * @throws IOException IO operations fails
388 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530389 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530390 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530391 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530392 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900393 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530394 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530395 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
396
397 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
398 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
399 }
400
Bharat saraswal33dfa012016-05-17 19:59:16 +0530401 if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaModule) {
402 if (!((YangJavaModule) javaCodeGeneratorInfo)
403 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
404 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
405 }
406 } else if ((YangNode) javaCodeGeneratorInfo instanceof YangJavaSubModule) {
407 if (!((YangJavaSubModule) javaCodeGeneratorInfo)
408 .isNotificationChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
409 updateCodeGenInfoForEvent(javaCodeGeneratorInfo);
410 }
411 }
412
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530413 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530414 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530415
Bharat saraswal33dfa012016-05-17 19:59:16 +0530416 /*Updates java code generator info with events info.*/
417 private static void updateCodeGenInfoForEvent(JavaCodeGeneratorInfo javaCodeGeneratorInfo) {
418 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_SUBJECT_CLASS);
419 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_CLASS);
420 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_EVENT_LISTENER_INTERFACE);
421 }
422
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530423 /**
424 * Returns the node package string.
425 *
426 * @param curNode current java node whose package string needs to be set
427 * @return returns the root package string
428 */
429 public static String getCurNodePackage(YangNode curNode) {
430
431 String pkg;
432 if (!(curNode instanceof JavaFileInfoContainer)
433 || curNode.getParent() == null) {
434 throw new TranslatorException("missing parent node to get current node's package");
435 }
436
437 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
438 if (!(parentNode instanceof JavaFileInfoContainer)) {
439 throw new TranslatorException("missing parent java node to get current node's package");
440 }
441 JavaFileInfo parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
442 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
443 return pkg.toLowerCase();
444 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530445
446 /**
447 * Returns true if root node contains any data node.
448 *
449 * @param node root YANG node
450 * @return true if root node contains any data node
451 */
452 public static boolean isManagerCodeGenRequired(YangNode node) {
453 YangLeavesHolder holder = (YangLeavesHolder) node;
454
455 if (holder.getListOfLeaf() != null && !holder.getListOfLeaf().isEmpty()) {
456 return true;
457 } else if (holder.getListOfLeafList() != null && !holder.getListOfLeafList().isEmpty()) {
458 return true;
459 }
460 node = node.getChild();
461 return node != null && !(node instanceof YangTypeDef) && !(node instanceof YangGrouping);
462 }
463
464 /**
465 * Return false if files are already present.
466 *
467 * @param info java file info
468 * @return false if files already present
469 */
470 public static boolean isGenerationOfCodeReq(JavaFileInfo info) {
471 File codeGenDir = new File(info.getBaseCodeGenPath()
472 + info.getPackageFilePath());
473 File[] files = codeGenDir.listFiles();
474 if (files.length >= 1) {
475 for (File file : files) {
476 if (file.getName().contentEquals(getCapitalCase(info.getJavaName() + MANAGER + ".java"))
477 || file.getName().contentEquals(getCapitalCase(info.getJavaName() + SERVICE + ".java"))) {
478 return false;
479 }
480 }
481 }
482 return true;
483 }
484
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530485}