blob: 3c41605f60baa76eb7c208f7a717a7c5a1b9bc9a [file] [log] [blame]
Gaurav Agrawal26390042016-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 saraswalaf413b82016-07-14 15:18:20 +053017package org.onosproject.yangutils.translator.tojava;
Gaurav Agrawal26390042016-04-12 13:30:16 +053018
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053019import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswal8beac342016-08-04 02:00:03 +053020import org.onosproject.yangutils.datamodel.YangAtomicPath;
Bharat saraswal039f59c2016-07-14 21:57:13 +053021import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswale2bc60d2016-04-16 02:28:25 +053022import org.onosproject.yangutils.datamodel.YangCase;
23import org.onosproject.yangutils.datamodel.YangChoice;
Gaurav Agrawal26390042016-04-12 13:30:16 +053024import org.onosproject.yangutils.datamodel.YangLeavesHolder;
25import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal039f59c2016-07-14 21:57:13 +053026import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Bharat saraswal039f59c2016-07-14 21:57:13 +053027import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053028import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053029import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053030import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053031import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugmentTranslator;
32import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumerationTranslator;
33import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
34import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053035import org.onosproject.yangutils.utils.io.YangPluginConfig;
Gaurav Agrawal26390042016-04-12 13:30:16 +053036
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053037import java.io.IOException;
38import java.util.ArrayList;
39import java.util.List;
40
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053041import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
42import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053043import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswald14cbe82016-07-14 13:26:18 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053045import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_NODE;
46import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_PARENT_NODE;
47import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_TRANSLATION_NODE;
48import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getBeanFiles;
49import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsg;
50import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsgForCodeGenerator;
Bharat saraswal039f59c2016-07-14 21:57:13 +053051import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053052import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053053import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
54import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
55import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
56import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053057import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Gaurav Agrawal26390042016-04-12 13:30:16 +053058
59/**
60 * Represents utility class for YANG java model.
61 */
62public final class YangJavaModelUtils {
63
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053064 // No instantiation.
Gaurav Agrawal26390042016-04-12 13:30:16 +053065 private YangJavaModelUtils() {
66 }
67
68 /**
69 * Updates YANG java file package information.
70 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053071 * @param info YANG java file info node
72 * @param config YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +053073 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053074 public static void updatePackageInfo(JavaCodeGeneratorInfo info,
Bharat saraswal9fab16b2016-09-23 23:27:24 +053075 YangPluginConfig config) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053076
77 JavaFileInfoTranslator translator = info.getJavaFileInfo();
78
79 if (info instanceof YangJavaAugmentTranslator) {
80 updatePackageForAugmentInfo(info, config);
Bharat saraswald14cbe82016-07-14 13:26:18 +053081 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053082 translator.setJavaName(getCamelCase(((YangNode) info).getName(),
83 config.getConflictResolver()));
84 translator.setJavaAttributeName(info.getJavaFileInfo()
85 .getJavaName());
86 translator.setPackage(getCurNodePackage((YangNode) info));
Bharat saraswald14cbe82016-07-14 13:26:18 +053087 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053088 updateCommonPackageInfo(translator, info, config);
Bharat saraswal8beac342016-08-04 02:00:03 +053089 }
90
91 /**
92 * Updates YANG java file package information.
93 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053094 * @param info YANG java file info node
95 * @param config YANG plugin config
Bharat saraswal8beac342016-08-04 02:00:03 +053096 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053097 private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo info,
Bharat saraswal9fab16b2016-09-23 23:27:24 +053098 YangPluginConfig config) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053099
100 JavaFileInfoTranslator translator = info.getJavaFileInfo();
101
102 translator.setJavaName(getAugmentClassName(
103 (YangJavaAugmentTranslator) info, config));
104 translator.setPackage(getAugmentsNodePackage((YangNode) info, config));
105 updateCommonPackageInfo(translator, info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530106 }
107
108 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530109 * Returns package for augment node.
110 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530111 * @param yangNode augment node
112 * @param config plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +0530113 * @return package for augment node
114 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530115 private static String getAugmentsNodePackage(YangNode yangNode,
116 YangPluginConfig config) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530117 YangAugment augment = (YangAugment) yangNode;
118 StringBuilder augmentPkg = new StringBuilder();
119 augmentPkg.append(getCurNodePackage(augment));
120
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530121 StringBuilder pkg = new StringBuilder();
122 pkg.append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530123 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530124 pkg.append(getCamelCase(atomicPath.getNodeIdentifier().getName(),
125 config.getConflictResolver()))
126 .append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530127 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530128 augmentPkg.append(trimAtLast(pkg.toString(), PERIOD).toLowerCase());
Bharat saraswal8beac342016-08-04 02:00:03 +0530129 return augmentPkg.toString();
130 }
131
132 /**
Gaurav Agrawal26390042016-04-12 13:30:16 +0530133 * Updates YANG java file package information for specified package.
134 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530135 * @param info YANG java file info node
136 * @param config YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +0530137 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530138 private static void updatePackageInfo(JavaCodeGeneratorInfo info,
139 YangPluginConfig config,
140 String pkg) {
141
142 JavaFileInfoTranslator translator = info.getJavaFileInfo();
143 translator.setJavaName(getCamelCase(((YangNode) info).getName(),
144 config.getConflictResolver()));
145 translator.setPackage(pkg);
146 updateCommonPackageInfo(translator, info, config);
147 }
148
149 /**
150 * Updates common package information.
151 *
152 * @param translator JAVA file info translator
153 * @param info YANG java file info node
154 * @param config YANG plugin config
155 */
156 private static void updateCommonPackageInfo(JavaFileInfoTranslator translator,
157 JavaCodeGeneratorInfo info,
158 YangPluginConfig config) {
159 translator.setPackageFilePath(getPackageDirPathFromJavaJPackage(
160 info.getJavaFileInfo().getPackage()));
161 translator.setBaseCodeGenPath(config.getCodeGenDir());
162 translator.setPluginConfig(config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530163 }
164
165 /**
166 * Updates temporary java code fragment files.
167 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530168 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530169 * @throws IOException IO operations fails
170 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530171 private static void createTempFragmentFile(JavaCodeGeneratorInfo info)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530172 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530173 info.setTempJavaCodeFragmentFiles(
174 new TempJavaCodeFragmentFiles(info.getJavaFileInfo()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530175 }
176
177 /**
178 * Updates leaf information in temporary java code fragment files.
179 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530180 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530181 * @throws IOException IO operations fails
182 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530183 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo info,
184 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530185 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530186
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530187 TempJavaCodeFragmentFiles translator =
188 info.getTempJavaCodeFragmentFiles();
189
190 if (info instanceof RpcNotificationContainer) {
191 getBeanFiles(info).setRootNode(true);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530192 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530193 * Module / sub module node code generation.
194 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530195 translator.getServiceTempFiles()
196 .addCurNodeLeavesInfoToTempFiles((YangNode) info, config);
197 if (info instanceof YangJavaModuleTranslator) {
198 if (!((YangJavaModuleTranslator) info).getNotificationNodes()
199 .isEmpty()) {
200 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530201 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530202 } else if (info instanceof YangJavaSubModuleTranslator) {
203 if (!((YangJavaSubModuleTranslator) info).getNotificationNodes()
204 .isEmpty()) {
205 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530206 }
207 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530208 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530209 if (info instanceof YangLeavesHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530210 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530211 * Container
212 * Case
213 * Grouping
214 * Input
215 * List
216 * Notification
217 * Output
218 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530219 getBeanFiles(info).addCurNodeLeavesInfoToTempFiles((YangNode) info,
220 config);
221
222 // Add operation type as an attribute.
223 getBeanFiles(info).addOperationTypeToTempFiles((YangNode) info,
224 config);
225 } else if (info instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530226 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530227 * Typedef
228 * Union
229 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530230 translator.addTypeInfoToTempFiles((YangTypeHolder) info, config);
231 } else if (info instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530232 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530233 * Enumeration
234 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530235 translator.getEnumTempFiles()
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530236 .addEnumAttributeToTempFiles((YangNode) info, config);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530237 } else if (!(info instanceof YangChoice)) {
238 /*Do nothing, only the interface needs to be generated for choice*/
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530239 throw new TranslatorException(
240 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
241 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530242 }
243 }
244
245 /**
246 * Process generate code entry of YANG node.
247 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530248 * @param info YANG java file info node
249 * @param config plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530250 * @throws IOException IO operations fails
251 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530252 private static void generateTempFiles(JavaCodeGeneratorInfo info,
253 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530254 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530255 if (!(info instanceof YangNode)) {
256 throw new TranslatorException(
257 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
258 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530259 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530260 createTempFragmentFile(info);
261 updateTempFragmentFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530262 }
263
264 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530265 * Updates notification node info in service temporary file.
266 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530267 * @param info java code generator info
268 * @param config plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530269 * @throws IOException when fails to do IO operations
270 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530271 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo info,
272 YangPluginConfig config)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530273 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530274 TempJavaCodeFragmentFiles translator =
275 info.getTempJavaCodeFragmentFiles();
276 if (info instanceof YangJavaModuleTranslator) {
277 for (YangNode notification : ((YangJavaModuleTranslator) info)
278 .getNotificationNodes()) {
279 translator.getEventFragmentFiles()
280 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530281 }
282 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530283 if (info instanceof YangJavaSubModuleTranslator) {
284 for (YangNode notification : ((YangJavaSubModuleTranslator) info)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530285 .getNotificationNodes()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530286 translator.getEventFragmentFiles()
287 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530288 }
289 }
290 }
291
292 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530293 * Generates code for the current ata model node and adds itself as an
294 * attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530295 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530296 * @param info YANG java file info node
297 * @param config YANG plugin config
298 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530299 * @throws IOException IO operations fails
300 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530301 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo info,
302 YangPluginConfig config,
303 boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530304 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530305 if (!(info instanceof YangNode)) {
306 throw new TranslatorException(
307 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
308 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530309 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530310
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530311 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530312 * Generate the Java files corresponding to the current node.
313 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530314 generateCodeOfAugmentableNode(info, config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530315
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530316 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530317 * Update the current nodes info in its parent nodes generated files.
318 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530319 addCurNodeInfoInParentTempFile((YangNode) info, isMultiInstance,
320 config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530321 }
322
323 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530324 * Generates code for the current data model node and adds support for it to
325 * be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530326 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530327 * @param info YANG java file info node
328 * @param config YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530329 * @throws IOException IO operations fails
330 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530331 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo info,
332 YangPluginConfig config)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530333 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530334 if (!(info instanceof YangNode)) {
335 throw new TranslatorException(
336 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
337 info));
Vidyashree Rama13960652016-04-26 15:06:06 +0530338 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530339
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530340 generateCodeOfNode(info, config);
341 TempJavaCodeFragmentFiles tempFiles =
342 info.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530343
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530344 if (info instanceof YangCase) {
345 YangNode parent = ((YangCase) info).getParent();
346 JavaQualifiedTypeInfoTranslator typeInfo =
347 getQualifierInfoForCasesParent(parent, config);
348 getBeanFiles(info).getJavaExtendsListHolder()
349 .addToExtendsList(typeInfo, (YangNode) info,
350 tempFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530351
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530352 getBeanFiles(info).addParentInfoInCurNodeTempFile((YangNode) info,
353 config);
Vidyashree Rama13960652016-04-26 15:06:06 +0530354 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530355 }
356
357 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530358 * Returns cases parent's qualified info.
359 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530360 * @param parent parent node
361 * @param config plugin configuration
Bharat saraswale50edca2016-08-05 01:58:25 +0530362 * @return cases parent's qualified info
363 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530364 public static JavaQualifiedTypeInfoTranslator
365 getQualifierInfoForCasesParent(YangNode parent,
366 YangPluginConfig config) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530367 String parentName;
368 String parentPkg;
Bharat saraswale50edca2016-08-05 01:58:25 +0530369 JavaFileInfoTranslator parentInfo;
370 if (parent instanceof YangChoice) {
371 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
372 } else {
373 parent = ((YangAugment) parent).getAugmentedNode();
374 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
375 }
376 if (parentInfo.getPackage() != null) {
377 parentName = getCapitalCase(parentInfo.getJavaName());
378 parentPkg = parentInfo.getPackage();
379 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530380 parentName = getCapitalCase(getCamelCase(parent.getName(),
381 config.getConflictResolver()));
382 parentPkg = getNodesPackage(parent, config);
Bharat saraswale50edca2016-08-05 01:58:25 +0530383 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530384 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo =
385 new JavaQualifiedTypeInfoTranslator();
386 qualifiedTypeInfo.setClassInfo(parentName);
387 qualifiedTypeInfo.setPkgInfo(parentPkg);
388 return qualifiedTypeInfo;
Bharat saraswale50edca2016-08-05 01:58:25 +0530389 }
390
391 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530392 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530393 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530394 * @param info YANG java file info node
395 * @param config YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530396 * @throws IOException IO operations fails
397 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530398 public static void generateCodeOfNode(JavaCodeGeneratorInfo info,
399 YangPluginConfig config)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530400 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530401 if (!(info instanceof YangNode)) {
402 throw new TranslatorException(
403 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
404 info));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530405 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530406 updatePackageInfo(info, config);
407 generateTempFiles(info, config);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530408 }
409
410 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530411 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530412 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530413 * @param info YANG java file info node
414 * @param config YANG plugin config
415 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530416 * @throws IOException IO operations fails
417 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530418 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo info,
419 YangPluginConfig config,
420 String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530421 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530422 if (!(info instanceof YangNode)) {
423 throw new TranslatorException(
424 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
425 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530426 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530427 updatePackageInfo(info, config, rootPkg);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530428
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530429 if (isRpcChildNodePresent((YangNode) info)) {
430 info.getJavaFileInfo()
431 .addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530432 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530433 generateTempFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530434 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530435
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530436 /**
437 * Returns the node package string.
438 *
439 * @param curNode current java node whose package string needs to be set
440 * @return returns the root package string
441 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530442 public static String getCurNodePackage(YangNode curNode)
443 throws TranslatorException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530444
445 String pkg;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530446 if (!(curNode instanceof JavaFileInfoContainer) ||
447 curNode.getParent() == null) {
448 throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530449 }
450
451 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
452 if (!(parentNode instanceof JavaFileInfoContainer)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530453 throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE,
454 curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530455 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530456 JavaFileInfoTranslator handle =
457 ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
458 pkg = handle.getPackage() + PERIOD + handle.getJavaName();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530459 return pkg.toLowerCase();
460 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530461
462 /**
463 * Returns true if root node contains any data node.
464 *
465 * @param node root YANG node
466 * @return true if root node contains any data node
467 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530468 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530469 return isNodeCodeGenRequired(node, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530470 }
471
472 /**
Bharat saraswal1edde622016-09-06 10:18:04 +0530473 * Returns true if get/set method of root node are required.
Bharat saraswale3175d32016-08-31 17:50:11 +0530474 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530475 * @param node root node
Bharat saraswal1edde622016-09-06 10:18:04 +0530476 * @return true if get/set method of root node are required
Bharat saraswale3175d32016-08-31 17:50:11 +0530477 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530478 public static boolean isGetSetOfRootNodeRequired(YangNode node) {
479 return isNodeCodeGenRequired(node, false);
480 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530481
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530482 /**
483 * Returns true if either get/set method of root node are required or root
484 * node contains any data node. This check is done depending on the
485 * boolean parameter indicating whether check to be performed for root
486 * node code generation or get/set method generation.
487 *
488 * @param node root node
489 * @param rootNodeGen flag indicating check type
490 * @return true if check pass, false otherwise
491 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530492 private static boolean isNodeCodeGenRequired(YangNode node,
493 boolean rootNodeGen) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530494 YangLeavesHolder holder = (YangLeavesHolder) node;
495
496 if (!holder.getListOfLeaf().isEmpty()) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530497 return true;
Bharat saraswale3175d32016-08-31 17:50:11 +0530498 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530499 if (!holder.getListOfLeafList().isEmpty()) {
500 return true;
501 }
502 node = node.getChild();
503 if (node == null) {
504 return false;
505 }
506
507 if (rootNodeGen) {
508 while (node != null) {
509 if (!(node instanceof YangTranslatorOperatorNode)) {
510 return true;
511 }
512 node = node.getNextSibling();
513 }
514 return false;
515 }
516 while (node != null) {
517 if (!(node instanceof YangAugment)) {
518 return true;
519 }
520 node = node.getNextSibling();
521 }
522 return false;
Bharat saraswale3175d32016-08-31 17:50:11 +0530523 }
524
525 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530526 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530527 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530528 * @param node YANG node
529 * @param config plugin config
Bharat saraswalaf413b82016-07-14 15:18:20 +0530530 * @return java package
531 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530532 public static String getNodesPackage(YangNode node,
533 YangPluginConfig config) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530534
535 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530536 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530537 if (node instanceof YangJavaAugmentTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530538 clsInfo.add(getAugmentClassName((YangAugment) node,
539 config));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530540 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530541 clsInfo.add(getCamelCase(node.getName(), config
542 .getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530543 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530544 node = node.getParent();
545 }
546
547 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530548 if (node instanceof YangJavaModuleTranslator) {
549 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530550 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace(),
551 module.getRevision(),
552 config.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530553 } else if (node instanceof YangJavaSubModuleTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530554 YangJavaSubModuleTranslator subModule =
555 (YangJavaSubModuleTranslator) node;
Bharat saraswal8beac342016-08-04 02:00:03 +0530556 pkg.append(getRootPackage(subModule.getVersion(),
Bharat saraswal1edde622016-09-06 10:18:04 +0530557 subModule.getNameSpaceFromModule(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530558 subModule.getRevision(),
559 config.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530560 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530561 pkg.append(EMPTY_STRING);
562 int size = clsInfo.size();
563 for (int i = size - 1; i >= 0; i--) {
564 pkg.append(PERIOD).append(clsInfo.get(i));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530565 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530566 return pkg.toString().toLowerCase();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530567 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530568
569 /**
570 * Returns augment class name.
571 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530572 * @param augment YANG augment
573 * @param config plugin configurations
Bharat saraswal039f59c2016-07-14 21:57:13 +0530574 * @return augment class name
575 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530576 private static String getAugmentClassName(YangAugment augment,
577 YangPluginConfig config) {
578 YangNodeIdentifier identifier =
579 augment.getTargetNode().get(augment.getTargetNode().size() - 1)
580 .getNodeIdentifier();
581 String name = getCapitalCase(getCamelCase(identifier.getName(),
582 config.getConflictResolver()));
583 if (identifier.getPrefix() != null) {
584 return AUGMENTED + getCapitalCase(
585 getCamelCase(identifier.getPrefix(),
586 config.getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530587 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530588 return AUGMENTED + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530589 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530590}