blob: ff28b66a02944d5b5aee47be9e75a37500973dd8 [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 saraswald50c6382016-07-14 21:57:13 +053025import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053026import org.onosproject.yangutils.datamodel.YangCase;
27import org.onosproject.yangutils.datamodel.YangChoice;
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 saraswald50c6382016-07-14 21:57:13 +053031import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Bharat saraswalb551aae2016-07-14 15:18:20 +053032import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswald50c6382016-07-14 21:57:13 +053033import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053035import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053036import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswalb551aae2016-07-14 15:18:20 +053037import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment;
38import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
39import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
40import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053041import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053042
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053043import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
44import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053045import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053046import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswald50c6382016-07-14 21:57:13 +053047import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053048import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053049import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswalb551aae2016-07-14 15:18:20 +053050import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO;
51import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053052import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
53import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
54import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053055
56/**
57 * Represents utility class for YANG java model.
58 */
59public final class YangJavaModelUtils {
60
61 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053062 * Creates an instance of YANG java model utility.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053063 */
64 private YangJavaModelUtils() {
65 }
66
67 /**
68 * Updates YANG java file package information.
69 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053070 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053071 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053072 * @throws IOException IO operations fails
73 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053074 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +053075 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053076 throws IOException {
Bharat saraswalb1170bd2016-07-14 13:26:18 +053077 if (javaCodeGeneratorInfo instanceof YangJavaAugment) {
78 javaCodeGeneratorInfo.getJavaFileInfo()
Bharat saraswald50c6382016-07-14 21:57:13 +053079 .setJavaName(getAugmentClassName((YangJavaAugment) javaCodeGeneratorInfo,
80 yangPluginConfig));
Bharat saraswalb1170bd2016-07-14 13:26:18 +053081 } else {
82 javaCodeGeneratorInfo.getJavaFileInfo()
83 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
84 yangPluginConfig.getConflictResolver()));
85 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
87 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
88 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053089 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +053090 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053091 }
92
93 /**
94 * Updates YANG java file package information for specified package.
95 *
96 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053097 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053098 * @throws IOException IO operations fails
99 */
janani bde4ffab2016-04-15 16:18:30 +0530100 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530101 String pkg) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530102 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +0530103 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
104 yangPlugin.getConflictResolver()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530105 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
106 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
107 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530108 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getManagerCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530109 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530110 }
111
112 /**
113 * Updates temporary java code fragment files.
114 *
115 * @param javaCodeGeneratorInfo YANG java file info node
116 * @throws IOException IO operations fails
117 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
119 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530120 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530121 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530122 }
123
124 /**
125 * Updates leaf information in temporary java code fragment files.
126 *
127 * @param javaCodeGeneratorInfo YANG java file info node
128 * @throws IOException IO operations fails
129 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530130 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530131 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530132 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530133 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530134 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530135 * Module / sub module node code generation.
136 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530137 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530138 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530139 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530140 if (javaCodeGeneratorInfo instanceof YangJavaModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530141 if (!((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
142 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
143 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530144 } else if (javaCodeGeneratorInfo instanceof YangJavaSubModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530145 if (!((YangJavaSubModule) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
146 updateNotificaitonNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
147 }
148 }
149
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530150 } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530151 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530152 * Container
153 * Case
154 * Grouping
155 * Input
156 * List
157 * Notification
158 * Output
159 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530160 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530161 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530162 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530163 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530164 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530165 * Typedef
166 * Union
167 */
168 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530169 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswald72411a2016-04-19 01:00:16 +0530170 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530171 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530172 * Enumeration
173 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530174 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530175 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
176
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530177 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
178 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530179 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530180 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530181 }
182 }
183
184 /**
185 * Process generate code entry of YANG node.
186 *
187 * @param javaCodeGeneratorInfo YANG java file info node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530188 * @param yangPluginConfig plugin configurations
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 saraswald50c6382016-07-14 21:57:13 +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 saraswalb551aae2016-07-14 15:18:20 +0530212 if (javaCodeGeneratorInfo instanceof YangJavaModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530213 for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) {
214 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswald50c6382016-07-14 21:57:13 +0530215 .getEventFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530216 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
217 }
218 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530219 if (javaCodeGeneratorInfo instanceof YangJavaSubModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530220 for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo)
221 .getNotificationNodes()) {
222 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswald50c6382016-07-14 21:57:13 +0530223 .getEventFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530224 .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);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530270 TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530271 if (javaCodeGeneratorInfo instanceof YangJavaAugment) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530272
Bharat saraswalb551aae2016-07-14 15:18:20 +0530273 JavaQualifiedTypeInfo yangAugmentedInfo = new JavaQualifiedTypeInfo();
274 yangAugmentedInfo.setClassInfo(YANG_AUGMENTED_INFO);
275 yangAugmentedInfo.setPkgInfo(YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG);
276 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
277 .addToExtendsList(yangAugmentedInfo, (YangNode) javaCodeGeneratorInfo,
278 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530279 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530280 if (javaCodeGeneratorInfo instanceof YangCase) {
281 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530282 JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo();
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530283 String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530284 String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
285 parentsInfo.setClassInfo(parentName);
286 parentsInfo.setPkgInfo(parentPkg);
287 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530288 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo,
289 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530290
291 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530292 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530293
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530294 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530295 }
296
297 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530298 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530299 *
300 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530301 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530302 * @throws IOException IO operations fails
303 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530304 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530305 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530306 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530307 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900308 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530309 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530310 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
311 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530312 }
313
314 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530315 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530316 *
317 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530318 * @param yangPluginConfig YANG plugin config
319 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530320 * @throws IOException IO operations fails
321 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530322 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530323 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530324 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530325 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900326 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530327 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530328 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
329
330 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
331 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
332 }
333
334 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530335 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530336
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530337 /**
338 * Returns the node package string.
339 *
340 * @param curNode current java node whose package string needs to be set
341 * @return returns the root package string
342 */
343 public static String getCurNodePackage(YangNode curNode) {
344
345 String pkg;
346 if (!(curNode instanceof JavaFileInfoContainer)
347 || curNode.getParent() == null) {
348 throw new TranslatorException("missing parent node to get current node's package");
349 }
350
351 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
352 if (!(parentNode instanceof JavaFileInfoContainer)) {
353 throw new TranslatorException("missing parent java node to get current node's package");
354 }
355 JavaFileInfo parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
356 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
357 return pkg.toLowerCase();
358 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530359
360 /**
361 * Returns true if root node contains any data node.
362 *
363 * @param node root YANG node
364 * @return true if root node contains any data node
365 */
366 public static boolean isManagerCodeGenRequired(YangNode node) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530367
368 List<YangNode> childNodes = new ArrayList<>();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530369 node = node.getChild();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530370 while (node != null) {
371 childNodes.add(node);
372 node = node.getNextSibling();
373 }
374
375 if (childNodes.size() == 0) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530376 return false;
Bharat saraswalb551aae2016-07-14 15:18:20 +0530377 } else if (childNodes.size() == 1) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530378 return !(childNodes.get(0) instanceof YangTranslatorOperatorNode);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530379 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530380 List<Boolean> booleanData = new ArrayList<>();
381 for (YangNode child : childNodes) {
382 if (child instanceof YangTranslatorOperatorNode) {
383 booleanData.add(false);
384 } else {
385 booleanData.add(true);
386 }
387 }
388 return booleanData.contains(true);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530389 }
390
391 /**
392 * Return false if files are already present.
393 *
394 * @param info java file info
395 * @return false if files already present
396 */
397 public static boolean isGenerationOfCodeReq(JavaFileInfo info) {
398 File codeGenDir = new File(info.getBaseCodeGenPath()
399 + info.getPackageFilePath());
400 File[] files = codeGenDir.listFiles();
401 if (files.length >= 1) {
402 for (File file : files) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530403 if (file.getName().contentEquals(getCapitalCase(info.getJavaName() + MANAGER + ".java"))) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530404 return false;
405 }
406 }
407 }
408 return true;
409 }
410
Bharat saraswalb551aae2016-07-14 15:18:20 +0530411 /**
412 * Returns augmented nodes package.
413 *
414 * @param node augmented node
415 * @param yangPluginConfig plugin config
416 * @return java package
417 */
418 public static String getAugmentedNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
419
420 List<String> clsInfo = new ArrayList<>();
Bharat saraswald50c6382016-07-14 21:57:13 +0530421 while (node.getParent() != null) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530422 if (!(node instanceof YangModule)
423 || !(node instanceof YangSubModule)) {
424 if (node instanceof YangJavaAugment) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530425 clsInfo.add(getAugmentClassName((YangAugment) node, yangPluginConfig));
Bharat saraswalb551aae2016-07-14 15:18:20 +0530426 } else {
427 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
428 }
429 }
430 if (node instanceof YangJavaModule
431 || node instanceof YangJavaSubModule) {
432 break;
433 }
434 node = node.getParent();
435 }
436
437 StringBuilder pkg = new StringBuilder();
438 if (node instanceof YangJavaModule) {
439 YangJavaModule module = (YangJavaModule) node;
440 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
441 .getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
442 } else if (node instanceof YangJavaSubModule) {
443 YangJavaSubModule submodule = (YangJavaSubModule) node;
444 pkg.append(getRootPackage(submodule.getVersion(),
445 submodule.getNameSpaceFromModule(submodule.getBelongsTo()),
446 submodule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
447 }
448 for (int i = 1; i <= clsInfo.size(); i++) {
449 pkg.append("." + clsInfo.get(clsInfo.size() - i));
450 }
451
452 return pkg.toString().toLowerCase();
453
454 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530455
456 /**
457 * Returns augment class name.
458 *
459 * @param augment YANG augment
460 * @param yangPluginConfig plugin configurations
461 * @return augment class name
462 */
463 public static String getAugmentClassName(YangAugment augment, YangPluginConfig yangPluginConfig) {
464 YangNodeIdentifier nodeId = augment.getTargetNode().get(augment.getTargetNode().size() - 1).getNodeIdentifier();
465 String name = getCapitalCase(getCamelCase(nodeId.getName(), yangPluginConfig.getConflictResolver()));
466 if (nodeId.getPrefix() != null) {
467 return AUGMENTED + getCapitalCase(nodeId.getPrefix()) + name;
468 } else {
469 return AUGMENTED + name;
470 }
471 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530472}