blob: ed37a3610a9d377c94a3dfc4d2145d3672b0e9e7 [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 * @throws IOException IO operations fails
74 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053075 public static void updatePackageInfo(JavaCodeGeneratorInfo info,
76 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +053077 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053078
79 JavaFileInfoTranslator translator = info.getJavaFileInfo();
80
81 if (info instanceof YangJavaAugmentTranslator) {
82 updatePackageForAugmentInfo(info, config);
Bharat saraswald14cbe82016-07-14 13:26:18 +053083 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053084 translator.setJavaName(getCamelCase(((YangNode) info).getName(),
85 config.getConflictResolver()));
86 translator.setJavaAttributeName(info.getJavaFileInfo()
87 .getJavaName());
88 translator.setPackage(getCurNodePackage((YangNode) info));
Bharat saraswald14cbe82016-07-14 13:26:18 +053089 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053090 updateCommonPackageInfo(translator, info, config);
Bharat saraswal8beac342016-08-04 02:00:03 +053091 }
92
93 /**
94 * Updates YANG java file package information.
95 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053096 * @param info YANG java file info node
97 * @param config YANG plugin config
Bharat saraswal8beac342016-08-04 02:00:03 +053098 * @throws IOException IO operations fails
99 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530100 private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo info,
101 YangPluginConfig config)
Bharat saraswal8beac342016-08-04 02:00:03 +0530102 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530103
104 JavaFileInfoTranslator translator = info.getJavaFileInfo();
105
106 translator.setJavaName(getAugmentClassName(
107 (YangJavaAugmentTranslator) info, config));
108 translator.setPackage(getAugmentsNodePackage((YangNode) info, config));
109 updateCommonPackageInfo(translator, info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530110 }
111
112 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530113 * Returns package for augment node.
114 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530115 * @param yangNode augment node
116 * @param config plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +0530117 * @return package for augment node
118 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530119 private static String getAugmentsNodePackage(YangNode yangNode,
120 YangPluginConfig config) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530121 YangAugment augment = (YangAugment) yangNode;
122 StringBuilder augmentPkg = new StringBuilder();
123 augmentPkg.append(getCurNodePackage(augment));
124
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530125 StringBuilder pkg = new StringBuilder();
126 pkg.append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530127 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530128 pkg.append(getCamelCase(atomicPath.getNodeIdentifier().getName(),
129 config.getConflictResolver()))
130 .append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530131 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530132 augmentPkg.append(trimAtLast(pkg.toString(), PERIOD).toLowerCase());
Bharat saraswal8beac342016-08-04 02:00:03 +0530133 return augmentPkg.toString();
134 }
135
136 /**
Gaurav Agrawal26390042016-04-12 13:30:16 +0530137 * Updates YANG java file package information for specified package.
138 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530139 * @param info YANG java file info node
140 * @param config YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +0530141 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530142 private static void updatePackageInfo(JavaCodeGeneratorInfo info,
143 YangPluginConfig config,
144 String pkg) {
145
146 JavaFileInfoTranslator translator = info.getJavaFileInfo();
147 translator.setJavaName(getCamelCase(((YangNode) info).getName(),
148 config.getConflictResolver()));
149 translator.setPackage(pkg);
150 updateCommonPackageInfo(translator, info, config);
151 }
152
153 /**
154 * Updates common package information.
155 *
156 * @param translator JAVA file info translator
157 * @param info YANG java file info node
158 * @param config YANG plugin config
159 */
160 private static void updateCommonPackageInfo(JavaFileInfoTranslator translator,
161 JavaCodeGeneratorInfo info,
162 YangPluginConfig config) {
163 translator.setPackageFilePath(getPackageDirPathFromJavaJPackage(
164 info.getJavaFileInfo().getPackage()));
165 translator.setBaseCodeGenPath(config.getCodeGenDir());
166 translator.setPluginConfig(config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530167 }
168
169 /**
170 * Updates temporary java code fragment files.
171 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530172 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530173 * @throws IOException IO operations fails
174 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530175 private static void createTempFragmentFile(JavaCodeGeneratorInfo info)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530176 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530177 info.setTempJavaCodeFragmentFiles(
178 new TempJavaCodeFragmentFiles(info.getJavaFileInfo()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530179 }
180
181 /**
182 * Updates leaf information in temporary java code fragment files.
183 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530184 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530185 * @throws IOException IO operations fails
186 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530187 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo info,
188 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530189 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530190
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530191 TempJavaCodeFragmentFiles translator =
192 info.getTempJavaCodeFragmentFiles();
193
194 if (info instanceof RpcNotificationContainer) {
195 getBeanFiles(info).setRootNode(true);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530196 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530197 * Module / sub module node code generation.
198 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530199 translator.getServiceTempFiles()
200 .addCurNodeLeavesInfoToTempFiles((YangNode) info, config);
201 if (info instanceof YangJavaModuleTranslator) {
202 if (!((YangJavaModuleTranslator) info).getNotificationNodes()
203 .isEmpty()) {
204 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530205 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530206 } else if (info instanceof YangJavaSubModuleTranslator) {
207 if (!((YangJavaSubModuleTranslator) info).getNotificationNodes()
208 .isEmpty()) {
209 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530210 }
211 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530212 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530213 if (info instanceof YangLeavesHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530214 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530215 * Container
216 * Case
217 * Grouping
218 * Input
219 * List
220 * Notification
221 * Output
222 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530223 getBeanFiles(info).addCurNodeLeavesInfoToTempFiles((YangNode) info,
224 config);
225
226 // Add operation type as an attribute.
227 getBeanFiles(info).addOperationTypeToTempFiles((YangNode) info,
228 config);
229 } else if (info instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530230 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530231 * Typedef
232 * Union
233 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530234 translator.addTypeInfoToTempFiles((YangTypeHolder) info, config);
235 } else if (info instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530236 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530237 * Enumeration
238 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530239 translator.getEnumerationTempFiles()
240 .addEnumAttributeToTempFiles((YangNode) info, config);
241 } else if (info instanceof YangChoice) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530242 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal26390042016-04-12 13:30:16 +0530243 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530244 throw new TranslatorException(
245 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
246 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530247 }
248 }
249
250 /**
251 * Process generate code entry of YANG node.
252 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530253 * @param info YANG java file info node
254 * @param config plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530255 * @throws IOException IO operations fails
256 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530257 private static void generateTempFiles(JavaCodeGeneratorInfo info,
258 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530259 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530260 if (!(info instanceof YangNode)) {
261 throw new TranslatorException(
262 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
263 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530264 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530265 createTempFragmentFile(info);
266 updateTempFragmentFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530267 }
268
269 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530270 * Updates notification node info in service temporary file.
271 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530272 * @param info java code generator info
273 * @param config plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530274 * @throws IOException when fails to do IO operations
275 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530276 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo info,
277 YangPluginConfig config)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530278 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530279 TempJavaCodeFragmentFiles translator =
280 info.getTempJavaCodeFragmentFiles();
281 if (info instanceof YangJavaModuleTranslator) {
282 for (YangNode notification : ((YangJavaModuleTranslator) info)
283 .getNotificationNodes()) {
284 translator.getEventFragmentFiles()
285 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530286 }
287 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530288 if (info instanceof YangJavaSubModuleTranslator) {
289 for (YangNode notification : ((YangJavaSubModuleTranslator) info)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530290 .getNotificationNodes()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530291 translator.getEventFragmentFiles()
292 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530293 }
294 }
295 }
296
297 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530298 * Generates code for the current ata model node and adds itself as an
299 * attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530300 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530301 * @param info YANG java file info node
302 * @param config YANG plugin config
303 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530304 * @throws IOException IO operations fails
305 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530306 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo info,
307 YangPluginConfig config,
308 boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530309 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530310 if (!(info instanceof YangNode)) {
311 throw new TranslatorException(
312 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
313 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530314 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530315
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530316 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530317 * Generate the Java files corresponding to the current node.
318 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530319 generateCodeOfAugmentableNode(info, config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530320
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530321 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530322 * Update the current nodes info in its parent nodes generated files.
323 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530324 addCurNodeInfoInParentTempFile((YangNode) info, isMultiInstance,
325 config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530326 }
327
328 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530329 * Generates code for the current data model node and adds support for it to
330 * be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530331 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530332 * @param info YANG java file info node
333 * @param config YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530334 * @throws IOException IO operations fails
335 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530336 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo info,
337 YangPluginConfig config)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530338 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530339 if (!(info instanceof YangNode)) {
340 throw new TranslatorException(
341 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
342 info));
Vidyashree Rama13960652016-04-26 15:06:06 +0530343 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530344
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530345 generateCodeOfNode(info, config);
346 TempJavaCodeFragmentFiles tempFiles =
347 info.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530348
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530349 if (info instanceof YangCase) {
350 YangNode parent = ((YangCase) info).getParent();
351 JavaQualifiedTypeInfoTranslator typeInfo =
352 getQualifierInfoForCasesParent(parent, config);
353 getBeanFiles(info).getJavaExtendsListHolder()
354 .addToExtendsList(typeInfo, (YangNode) info,
355 tempFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530356
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530357 getBeanFiles(info).addParentInfoInCurNodeTempFile((YangNode) info,
358 config);
Vidyashree Rama13960652016-04-26 15:06:06 +0530359 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530360 }
361
362 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530363 * Returns cases parent's qualified info.
364 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530365 * @param parent parent node
366 * @param config plugin configuration
Bharat saraswale50edca2016-08-05 01:58:25 +0530367 * @return cases parent's qualified info
368 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530369 public static JavaQualifiedTypeInfoTranslator
370 getQualifierInfoForCasesParent(YangNode parent,
371 YangPluginConfig config) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530372 String parentName;
373 String parentPkg;
Bharat saraswale50edca2016-08-05 01:58:25 +0530374 JavaFileInfoTranslator parentInfo;
375 if (parent instanceof YangChoice) {
376 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
377 } else {
378 parent = ((YangAugment) parent).getAugmentedNode();
379 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
380 }
381 if (parentInfo.getPackage() != null) {
382 parentName = getCapitalCase(parentInfo.getJavaName());
383 parentPkg = parentInfo.getPackage();
384 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530385 parentName = getCapitalCase(getCamelCase(parent.getName(),
386 config.getConflictResolver()));
387 parentPkg = getNodesPackage(parent, config);
Bharat saraswale50edca2016-08-05 01:58:25 +0530388 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530389 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo =
390 new JavaQualifiedTypeInfoTranslator();
391 qualifiedTypeInfo.setClassInfo(parentName);
392 qualifiedTypeInfo.setPkgInfo(parentPkg);
393 return qualifiedTypeInfo;
Bharat saraswale50edca2016-08-05 01:58:25 +0530394 }
395
396 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530397 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530398 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530399 * @param info YANG java file info node
400 * @param config YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530401 * @throws IOException IO operations fails
402 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530403 public static void generateCodeOfNode(JavaCodeGeneratorInfo info,
404 YangPluginConfig config)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530405 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530406 if (!(info instanceof YangNode)) {
407 throw new TranslatorException(
408 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
409 info));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530410 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530411 updatePackageInfo(info, config);
412 generateTempFiles(info, config);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530413 }
414
415 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530416 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530417 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530418 * @param info YANG java file info node
419 * @param config YANG plugin config
420 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530421 * @throws IOException IO operations fails
422 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530423 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo info,
424 YangPluginConfig config,
425 String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530426 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530427 if (!(info instanceof YangNode)) {
428 throw new TranslatorException(
429 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
430 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530431 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530432 updatePackageInfo(info, config, rootPkg);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530433
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530434 if (isRpcChildNodePresent((YangNode) info)) {
435 info.getJavaFileInfo()
436 .addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530437 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530438 generateTempFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530439 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530440
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530441 /**
442 * Returns the node package string.
443 *
444 * @param curNode current java node whose package string needs to be set
445 * @return returns the root package string
446 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530447 public static String getCurNodePackage(YangNode curNode)
448 throws TranslatorException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530449
450 String pkg;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530451 if (!(curNode instanceof JavaFileInfoContainer) ||
452 curNode.getParent() == null) {
453 throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530454 }
455
456 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
457 if (!(parentNode instanceof JavaFileInfoContainer)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530458 throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE,
459 curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530460 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530461 JavaFileInfoTranslator handle =
462 ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
463 pkg = handle.getPackage() + PERIOD + handle.getJavaName();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530464 return pkg.toLowerCase();
465 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530466
467 /**
468 * Returns true if root node contains any data node.
469 *
470 * @param node root YANG node
471 * @return true if root node contains any data node
472 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530473 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530474 return isNodeCodeGenRequired(node, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530475 }
476
477 /**
Bharat saraswal1edde622016-09-06 10:18:04 +0530478 * Returns true if get/set method of root node are required.
Bharat saraswale3175d32016-08-31 17:50:11 +0530479 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530480 * @param node root node
Bharat saraswal1edde622016-09-06 10:18:04 +0530481 * @return true if get/set method of root node are required
Bharat saraswale3175d32016-08-31 17:50:11 +0530482 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530483 public static boolean isGetSetOfRootNodeRequired(YangNode node) {
484 return isNodeCodeGenRequired(node, false);
485 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530486
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530487 /**
488 * Returns true if either get/set method of root node are required or root
489 * node contains any data node. This check is done depending on the
490 * boolean parameter indicating whether check to be performed for root
491 * node code generation or get/set method generation.
492 *
493 * @param node root node
494 * @param rootNodeGen flag indicating check type
495 * @return true if check pass, false otherwise
496 */
497 public static boolean isNodeCodeGenRequired(YangNode node,
498 boolean rootNodeGen) {
499 YangLeavesHolder holder = (YangLeavesHolder) node;
500
501 if (!holder.getListOfLeaf().isEmpty()) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530502 return true;
Bharat saraswale3175d32016-08-31 17:50:11 +0530503 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530504 if (!holder.getListOfLeafList().isEmpty()) {
505 return true;
506 }
507 node = node.getChild();
508 if (node == null) {
509 return false;
510 }
511
512 if (rootNodeGen) {
513 while (node != null) {
514 if (!(node instanceof YangTranslatorOperatorNode)) {
515 return true;
516 }
517 node = node.getNextSibling();
518 }
519 return false;
520 }
521 while (node != null) {
522 if (!(node instanceof YangAugment)) {
523 return true;
524 }
525 node = node.getNextSibling();
526 }
527 return false;
Bharat saraswale3175d32016-08-31 17:50:11 +0530528 }
529
530 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530531 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530532 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530533 * @param node YANG node
534 * @param config plugin config
Bharat saraswalaf413b82016-07-14 15:18:20 +0530535 * @return java package
536 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530537 public static String getNodesPackage(YangNode node,
538 YangPluginConfig config) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530539
540 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530541 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530542 if (node instanceof YangJavaAugmentTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530543 clsInfo.add(getAugmentClassName((YangAugment) node,
544 config));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530545 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530546 clsInfo.add(getCamelCase(node.getName(), config
547 .getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530548 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530549 node = node.getParent();
550 }
551
552 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530553 if (node instanceof YangJavaModuleTranslator) {
554 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530555 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace(),
556 module.getRevision(),
557 config.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530558 } else if (node instanceof YangJavaSubModuleTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530559 YangJavaSubModuleTranslator subModule =
560 (YangJavaSubModuleTranslator) node;
Bharat saraswal8beac342016-08-04 02:00:03 +0530561 pkg.append(getRootPackage(subModule.getVersion(),
Bharat saraswal1edde622016-09-06 10:18:04 +0530562 subModule.getNameSpaceFromModule(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530563 subModule.getRevision(),
564 config.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530565 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530566 pkg.append(EMPTY_STRING);
567 int size = clsInfo.size();
568 for (int i = size - 1; i >= 0; i--) {
569 pkg.append(PERIOD).append(clsInfo.get(i));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530570 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530571 return pkg.toString().toLowerCase();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530572 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530573
574 /**
575 * Returns augment class name.
576 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530577 * @param augment YANG augment
578 * @param config plugin configurations
Bharat saraswal039f59c2016-07-14 21:57:13 +0530579 * @return augment class name
580 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530581 private static String getAugmentClassName(YangAugment augment,
582 YangPluginConfig config) {
583 YangNodeIdentifier identifier =
584 augment.getTargetNode().get(augment.getTargetNode().size() - 1)
585 .getNodeIdentifier();
586 String name = getCapitalCase(getCamelCase(identifier.getName(),
587 config.getConflictResolver()));
588 if (identifier.getPrefix() != null) {
589 return AUGMENTED + getCapitalCase(
590 getCamelCase(identifier.getPrefix(),
591 config.getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530592 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530593 return AUGMENTED + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530594 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530595}