blob: 3489eea2359f66004603a8d6b7870b4926151fee [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
Bharat saraswale707f032016-07-14 23:33:55 +0530150 }
151 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
Bharat saraswalb551aae2016-07-14 15:18:20 +0530189 * @param yangPluginConfig plugin configurations
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530190 * @throws IOException IO operations fails
191 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530192 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswale707f032016-07-14 23:33:55 +0530193 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530194 throws IOException {
195 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530196 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530197 }
198 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530199 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530200
201 }
202
203 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530204 * Updates notification node info in service temporary file.
205 *
206 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530207 * @param yangPluginConfig plugin configurations
Bharat saraswal33dfa012016-05-17 19:59:16 +0530208 * @throws IOException when fails to do IO operations
209 */
210 private static void updateNotificaitonNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530211 YangPluginConfig yangPluginConfig)
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530212 throws IOException {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530213 if (javaCodeGeneratorInfo instanceof YangJavaModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530214 for (YangNode notificaiton : ((YangJavaModule) javaCodeGeneratorInfo).getNotificationNodes()) {
215 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswald50c6382016-07-14 21:57:13 +0530216 .getEventFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530217 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
218 }
219 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530220 if (javaCodeGeneratorInfo instanceof YangJavaSubModule) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530221 for (YangNode notificaiton : ((YangJavaSubModule) javaCodeGeneratorInfo)
222 .getNotificationNodes()) {
223 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswald50c6382016-07-14 21:57:13 +0530224 .getEventFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530225 .addJavaSnippetOfEvent(notificaiton, yangPluginConfig);
226 }
227 }
228 }
229
230 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530231 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530232 *
233 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530234 * @param yangPlugin YANG plugin config
235 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530236 * @throws IOException IO operations fails
237 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530238 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530239 YangPluginConfig yangPlugin, boolean isMultiInstance)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530241 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530242 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530243 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530244
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530245 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530246 * Generate the Java files corresponding to the current node.
247 */
248 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
249
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530250 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530251 * Update the current nodes info in its parent nodes generated files.
252 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530253 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530254 }
255
256 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530257 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530258 *
259 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530260 * @param yangPlugin YANG plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530261 * @throws IOException IO operations fails
262 */
263 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530264 YangPluginConfig yangPlugin)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530265 throws IOException {
266 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
267 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530268 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530269
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530270 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530271 TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530272 if (javaCodeGeneratorInfo instanceof YangJavaAugment) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530273
Bharat saraswalb551aae2016-07-14 15:18:20 +0530274 JavaQualifiedTypeInfo yangAugmentedInfo = new JavaQualifiedTypeInfo();
275 yangAugmentedInfo.setClassInfo(YANG_AUGMENTED_INFO);
276 yangAugmentedInfo.setPkgInfo(YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG);
277 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
278 .addToExtendsList(yangAugmentedInfo, (YangNode) javaCodeGeneratorInfo,
279 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530280 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530281 if (javaCodeGeneratorInfo instanceof YangCase) {
282 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530283 JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo();
Vidyashree Rama210c01d2016-05-20 16:29:25 +0530284 String parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530285 String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
286 parentsInfo.setClassInfo(parentName);
287 parentsInfo.setPkgInfo(parentPkg);
288 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530289 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo,
290 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530291
292 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530293 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530294
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530295 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530296 }
297
298 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530299 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530300 *
301 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530302 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530303 * @throws IOException IO operations fails
304 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530305 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530306 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530307 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530308 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900309 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530310 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530311 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
312 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530313 }
314
315 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530316 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530317 *
318 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530319 * @param yangPluginConfig YANG plugin config
320 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530321 * @throws IOException IO operations fails
322 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530323 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530324 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530325 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530326 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900327 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530328 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530329 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
330
331 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
332 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
333 }
334
335 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530336 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530337
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530338 /**
339 * Returns the node package string.
340 *
341 * @param curNode current java node whose package string needs to be set
342 * @return returns the root package string
343 */
344 public static String getCurNodePackage(YangNode curNode) {
345
346 String pkg;
347 if (!(curNode instanceof JavaFileInfoContainer)
348 || curNode.getParent() == null) {
349 throw new TranslatorException("missing parent node to get current node's package");
350 }
351
352 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
353 if (!(parentNode instanceof JavaFileInfoContainer)) {
354 throw new TranslatorException("missing parent java node to get current node's package");
355 }
356 JavaFileInfo parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
357 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
358 return pkg.toLowerCase();
359 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530360
361 /**
362 * Returns true if root node contains any data node.
363 *
364 * @param node root YANG node
365 * @return true if root node contains any data node
366 */
367 public static boolean isManagerCodeGenRequired(YangNode node) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530368
369 List<YangNode> childNodes = new ArrayList<>();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530370 node = node.getChild();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530371 while (node != null) {
372 childNodes.add(node);
373 node = node.getNextSibling();
374 }
375
376 if (childNodes.size() == 0) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530377 return false;
Bharat saraswalb551aae2016-07-14 15:18:20 +0530378 } else if (childNodes.size() == 1) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530379 return !(childNodes.get(0) instanceof YangTranslatorOperatorNode);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530380 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530381 List<Boolean> booleanData = new ArrayList<>();
382 for (YangNode child : childNodes) {
383 if (child instanceof YangTranslatorOperatorNode) {
384 booleanData.add(false);
385 } else {
386 booleanData.add(true);
387 }
388 }
389 return booleanData.contains(true);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530390 }
391
392 /**
393 * Return false if files are already present.
394 *
395 * @param info java file info
396 * @return false if files already present
397 */
398 public static boolean isGenerationOfCodeReq(JavaFileInfo info) {
399 File codeGenDir = new File(info.getBaseCodeGenPath()
400 + info.getPackageFilePath());
401 File[] files = codeGenDir.listFiles();
402 if (files.length >= 1) {
403 for (File file : files) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530404 if (file.getName().contentEquals(getCapitalCase(info.getJavaName() + MANAGER + ".java"))) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530405 return false;
406 }
407 }
408 }
409 return true;
410 }
411
Bharat saraswalb551aae2016-07-14 15:18:20 +0530412 /**
413 * Returns augmented nodes package.
414 *
415 * @param node augmented node
416 * @param yangPluginConfig plugin config
417 * @return java package
418 */
419 public static String getAugmentedNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
420
421 List<String> clsInfo = new ArrayList<>();
Bharat saraswald50c6382016-07-14 21:57:13 +0530422 while (node.getParent() != null) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530423 if (!(node instanceof YangModule)
424 || !(node instanceof YangSubModule)) {
425 if (node instanceof YangJavaAugment) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530426 clsInfo.add(getAugmentClassName((YangAugment) node, yangPluginConfig));
Bharat saraswalb551aae2016-07-14 15:18:20 +0530427 } else {
428 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
429 }
430 }
431 if (node instanceof YangJavaModule
432 || node instanceof YangJavaSubModule) {
433 break;
434 }
435 node = node.getParent();
436 }
437
438 StringBuilder pkg = new StringBuilder();
439 if (node instanceof YangJavaModule) {
440 YangJavaModule module = (YangJavaModule) node;
441 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
442 .getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
443 } else if (node instanceof YangJavaSubModule) {
444 YangJavaSubModule submodule = (YangJavaSubModule) node;
445 pkg.append(getRootPackage(submodule.getVersion(),
446 submodule.getNameSpaceFromModule(submodule.getBelongsTo()),
447 submodule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
448 }
449 for (int i = 1; i <= clsInfo.size(); i++) {
450 pkg.append("." + clsInfo.get(clsInfo.size() - i));
451 }
452
453 return pkg.toString().toLowerCase();
454
455 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530456
457 /**
458 * Returns augment class name.
459 *
Bharat saraswale707f032016-07-14 23:33:55 +0530460 * @param augment YANG augment
Bharat saraswald50c6382016-07-14 21:57:13 +0530461 * @param yangPluginConfig plugin configurations
462 * @return augment class name
463 */
464 public static String getAugmentClassName(YangAugment augment, YangPluginConfig yangPluginConfig) {
465 YangNodeIdentifier nodeId = augment.getTargetNode().get(augment.getTargetNode().size() - 1).getNodeIdentifier();
466 String name = getCapitalCase(getCamelCase(nodeId.getName(), yangPluginConfig.getConflictResolver()));
467 if (nodeId.getPrefix() != null) {
468 return AUGMENTED + getCapitalCase(nodeId.getPrefix()) + name;
469 } else {
470 return AUGMENTED + name;
471 }
472 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530473}