blob: 064229a68a3d110c404ca6d44b32fa71dacee394 [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;
janani b3c396f02016-09-27 18:45:23 +053024import org.onosproject.yangutils.datamodel.YangInput;
Gaurav Agrawal26390042016-04-12 13:30:16 +053025import org.onosproject.yangutils.datamodel.YangLeavesHolder;
26import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal039f59c2016-07-14 21:57:13 +053027import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
janani b3c396f02016-09-27 18:45:23 +053028import org.onosproject.yangutils.datamodel.YangOutput;
Bharat saraswal039f59c2016-07-14 21:57:13 +053029import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053030import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053031import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053032import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053033import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugmentTranslator;
34import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumerationTranslator;
janani b3c396f02016-09-27 18:45:23 +053035import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaInputTranslator;
Shankara-Huaweib7564772016-08-02 18:13:13 +053036import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
janani b3c396f02016-09-27 18:45:23 +053037import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaOutputTranslator;
Shankara-Huaweib7564772016-08-02 18:13:13 +053038import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053039import org.onosproject.yangutils.utils.io.YangPluginConfig;
Gaurav Agrawal26390042016-04-12 13:30:16 +053040
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053041import java.io.IOException;
42import java.util.ArrayList;
43import java.util.List;
44
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053045import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
46import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053047import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswald14cbe82016-07-14 13:26:18 +053048import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053049import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_NODE;
50import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_PARENT_NODE;
51import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_TRANSLATION_NODE;
52import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getBeanFiles;
53import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsg;
54import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsgForCodeGenerator;
Bharat saraswal039f59c2016-07-14 21:57:13 +053055import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053056import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
janani b3c396f02016-09-27 18:45:23 +053057import static org.onosproject.yangutils.utils.UtilConstants.INPUT_KEYWORD;
58import static org.onosproject.yangutils.utils.UtilConstants.OUTPUT_KEYWORD;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053059import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
60import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
61import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
62import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053063import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Gaurav Agrawal26390042016-04-12 13:30:16 +053064
65/**
66 * Represents utility class for YANG java model.
67 */
68public final class YangJavaModelUtils {
69
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053070 // No instantiation.
Gaurav Agrawal26390042016-04-12 13:30:16 +053071 private YangJavaModelUtils() {
72 }
73
74 /**
75 * Updates YANG java file package information.
76 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053077 * @param info YANG java file info node
78 * @param config YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +053079 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053080 public static void updatePackageInfo(JavaCodeGeneratorInfo info,
Bharat saraswal9fab16b2016-09-23 23:27:24 +053081 YangPluginConfig config) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053082
83 JavaFileInfoTranslator translator = info.getJavaFileInfo();
84
85 if (info instanceof YangJavaAugmentTranslator) {
86 updatePackageForAugmentInfo(info, config);
Bharat saraswald14cbe82016-07-14 13:26:18 +053087 } else {
janani b3c396f02016-09-27 18:45:23 +053088 setNodeJavaName(info, config);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053089 translator.setJavaAttributeName(info.getJavaFileInfo()
90 .getJavaName());
91 translator.setPackage(getCurNodePackage((YangNode) info));
Bharat saraswald14cbe82016-07-14 13:26:18 +053092 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053093 updateCommonPackageInfo(translator, info, config);
Bharat saraswal8beac342016-08-04 02:00:03 +053094 }
95
96 /**
janani b3c396f02016-09-27 18:45:23 +053097 * The java name for input, output is prefixed with rpc name and other
98 * nodes are set by taking its own name from YANG.
99 *
100 * @param info YANG java file info node
101 * @param config YANG plugin config
102 */
103 private static void setNodeJavaName(JavaCodeGeneratorInfo info,
104 YangPluginConfig config) {
105 String javaGenName;
106 if (info instanceof YangJavaInputTranslator) {
107 javaGenName = ((YangInput) info).getParent().getName() +
108 INPUT_KEYWORD;
109 } else if (info instanceof YangJavaOutputTranslator) {
110 javaGenName = ((YangOutput) info).getParent().getName() +
111 OUTPUT_KEYWORD;
112 } else {
113 javaGenName = ((YangNode) info).getName();
114 }
115 info.getJavaFileInfo().setJavaName(getCamelCase(
116 javaGenName, config.getConflictResolver()));
117 }
118
119 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530120 * Updates YANG java file package information.
121 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530122 * @param info YANG java file info node
123 * @param config YANG plugin config
Bharat saraswal8beac342016-08-04 02:00:03 +0530124 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530125 private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo info,
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530126 YangPluginConfig config) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530127
128 JavaFileInfoTranslator translator = info.getJavaFileInfo();
129
130 translator.setJavaName(getAugmentClassName(
131 (YangJavaAugmentTranslator) info, config));
132 translator.setPackage(getAugmentsNodePackage((YangNode) info, config));
133 updateCommonPackageInfo(translator, info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530134 }
135
136 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530137 * Returns package for augment node.
138 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530139 * @param yangNode augment node
140 * @param config plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +0530141 * @return package for augment node
142 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530143 private static String getAugmentsNodePackage(YangNode yangNode,
144 YangPluginConfig config) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530145 YangAugment augment = (YangAugment) yangNode;
146 StringBuilder augmentPkg = new StringBuilder();
147 augmentPkg.append(getCurNodePackage(augment));
148
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530149 StringBuilder pkg = new StringBuilder();
150 pkg.append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530151 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530152 pkg.append(getCamelCase(atomicPath.getNodeIdentifier().getName(),
153 config.getConflictResolver()))
154 .append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530155 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530156 augmentPkg.append(trimAtLast(pkg.toString(), PERIOD).toLowerCase());
Bharat saraswal8beac342016-08-04 02:00:03 +0530157 return augmentPkg.toString();
158 }
159
160 /**
Gaurav Agrawal26390042016-04-12 13:30:16 +0530161 * Updates YANG java file package information for specified package.
162 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530163 * @param info YANG java file info node
164 * @param config YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +0530165 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530166 private static void updatePackageInfo(JavaCodeGeneratorInfo info,
167 YangPluginConfig config,
168 String pkg) {
169
170 JavaFileInfoTranslator translator = info.getJavaFileInfo();
171 translator.setJavaName(getCamelCase(((YangNode) info).getName(),
172 config.getConflictResolver()));
173 translator.setPackage(pkg);
174 updateCommonPackageInfo(translator, info, config);
175 }
176
177 /**
178 * Updates common package information.
179 *
180 * @param translator JAVA file info translator
181 * @param info YANG java file info node
182 * @param config YANG plugin config
183 */
184 private static void updateCommonPackageInfo(JavaFileInfoTranslator translator,
185 JavaCodeGeneratorInfo info,
186 YangPluginConfig config) {
187 translator.setPackageFilePath(getPackageDirPathFromJavaJPackage(
188 info.getJavaFileInfo().getPackage()));
189 translator.setBaseCodeGenPath(config.getCodeGenDir());
190 translator.setPluginConfig(config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530191 }
192
193 /**
194 * Updates temporary java code fragment files.
195 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530196 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530197 * @throws IOException IO operations fails
198 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530199 private static void createTempFragmentFile(JavaCodeGeneratorInfo info)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530200 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530201 info.setTempJavaCodeFragmentFiles(
202 new TempJavaCodeFragmentFiles(info.getJavaFileInfo()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530203 }
204
205 /**
206 * Updates leaf information in temporary java code fragment files.
207 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530208 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530209 * @throws IOException IO operations fails
210 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530211 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo info,
212 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530213 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530214
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530215 TempJavaCodeFragmentFiles translator =
216 info.getTempJavaCodeFragmentFiles();
217
218 if (info instanceof RpcNotificationContainer) {
219 getBeanFiles(info).setRootNode(true);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530220 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530221 * Module / sub module node code generation.
222 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530223 translator.getServiceTempFiles()
224 .addCurNodeLeavesInfoToTempFiles((YangNode) info, config);
225 if (info instanceof YangJavaModuleTranslator) {
226 if (!((YangJavaModuleTranslator) info).getNotificationNodes()
227 .isEmpty()) {
228 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530229 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530230 } else if (info instanceof YangJavaSubModuleTranslator) {
231 if (!((YangJavaSubModuleTranslator) info).getNotificationNodes()
232 .isEmpty()) {
233 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530234 }
235 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530236 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530237 if (info instanceof YangLeavesHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530238 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530239 * Container
240 * Case
241 * Grouping
242 * Input
243 * List
244 * Notification
245 * Output
246 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530247 getBeanFiles(info).addCurNodeLeavesInfoToTempFiles((YangNode) info,
248 config);
249
250 // Add operation type as an attribute.
251 getBeanFiles(info).addOperationTypeToTempFiles((YangNode) info,
252 config);
253 } else if (info instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530254 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530255 * Typedef
256 * Union
257 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530258 translator.addTypeInfoToTempFiles((YangTypeHolder) info, config);
259 } else if (info instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530260 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530261 * Enumeration
262 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530263 translator.getEnumTempFiles()
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530264 .addEnumAttributeToTempFiles((YangNode) info, config);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530265 } else if (!(info instanceof YangChoice)) {
266 /*Do nothing, only the interface needs to be generated for choice*/
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530267 throw new TranslatorException(
268 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
269 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530270 }
271 }
272
273 /**
274 * Process generate code entry of YANG node.
275 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530276 * @param info YANG java file info node
277 * @param config plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530278 * @throws IOException IO operations fails
279 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530280 private static void generateTempFiles(JavaCodeGeneratorInfo info,
281 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530282 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530283 if (!(info instanceof YangNode)) {
284 throw new TranslatorException(
285 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
286 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530287 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530288 createTempFragmentFile(info);
289 updateTempFragmentFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530290 }
291
292 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530293 * Updates notification node info in service temporary file.
294 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530295 * @param info java code generator info
296 * @param config plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530297 * @throws IOException when fails to do IO operations
298 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530299 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo info,
300 YangPluginConfig config)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530301 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530302 TempJavaCodeFragmentFiles translator =
303 info.getTempJavaCodeFragmentFiles();
304 if (info instanceof YangJavaModuleTranslator) {
305 for (YangNode notification : ((YangJavaModuleTranslator) info)
306 .getNotificationNodes()) {
307 translator.getEventFragmentFiles()
308 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530309 }
310 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530311 if (info instanceof YangJavaSubModuleTranslator) {
312 for (YangNode notification : ((YangJavaSubModuleTranslator) info)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530313 .getNotificationNodes()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530314 translator.getEventFragmentFiles()
315 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530316 }
317 }
318 }
319
320 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530321 * Generates code for the current ata model node and adds itself as an
322 * attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530323 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530324 * @param info YANG java file info node
325 * @param config YANG plugin config
326 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530327 * @throws IOException IO operations fails
328 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530329 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo info,
330 YangPluginConfig config,
331 boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530332 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530333 if (!(info instanceof YangNode)) {
334 throw new TranslatorException(
335 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
336 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530337 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530338
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530339 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530340 * Generate the Java files corresponding to the current node.
341 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530342 generateCodeOfAugmentableNode(info, config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530343
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530344 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530345 * Update the current nodes info in its parent nodes generated files.
346 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530347 addCurNodeInfoInParentTempFile((YangNode) info, isMultiInstance,
348 config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530349 }
350
351 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530352 * Generates code for the current data model node and adds support for it to
353 * be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530354 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530355 * @param info YANG java file info node
356 * @param config YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530357 * @throws IOException IO operations fails
358 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530359 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo info,
360 YangPluginConfig config)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530361 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530362 if (!(info instanceof YangNode)) {
363 throw new TranslatorException(
364 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
365 info));
Vidyashree Rama13960652016-04-26 15:06:06 +0530366 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530367
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530368 generateCodeOfNode(info, config);
369 TempJavaCodeFragmentFiles tempFiles =
370 info.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530371
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530372 if (info instanceof YangCase) {
373 YangNode parent = ((YangCase) info).getParent();
374 JavaQualifiedTypeInfoTranslator typeInfo =
375 getQualifierInfoForCasesParent(parent, config);
376 getBeanFiles(info).getJavaExtendsListHolder()
377 .addToExtendsList(typeInfo, (YangNode) info,
378 tempFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530379
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530380 getBeanFiles(info).addParentInfoInCurNodeTempFile((YangNode) info,
381 config);
Vidyashree Rama13960652016-04-26 15:06:06 +0530382 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530383 }
384
385 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530386 * Returns cases parent's qualified info.
387 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530388 * @param parent parent node
389 * @param config plugin configuration
Bharat saraswale50edca2016-08-05 01:58:25 +0530390 * @return cases parent's qualified info
391 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530392 public static JavaQualifiedTypeInfoTranslator
393 getQualifierInfoForCasesParent(YangNode parent,
394 YangPluginConfig config) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530395 String parentName;
396 String parentPkg;
Bharat saraswale50edca2016-08-05 01:58:25 +0530397 JavaFileInfoTranslator parentInfo;
398 if (parent instanceof YangChoice) {
399 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
400 } else {
401 parent = ((YangAugment) parent).getAugmentedNode();
402 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
403 }
404 if (parentInfo.getPackage() != null) {
405 parentName = getCapitalCase(parentInfo.getJavaName());
406 parentPkg = parentInfo.getPackage();
407 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530408 parentName = getCapitalCase(getCamelCase(parent.getName(),
409 config.getConflictResolver()));
410 parentPkg = getNodesPackage(parent, config);
Bharat saraswale50edca2016-08-05 01:58:25 +0530411 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530412 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo =
413 new JavaQualifiedTypeInfoTranslator();
414 qualifiedTypeInfo.setClassInfo(parentName);
415 qualifiedTypeInfo.setPkgInfo(parentPkg);
416 return qualifiedTypeInfo;
Bharat saraswale50edca2016-08-05 01:58:25 +0530417 }
418
419 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530420 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530421 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530422 * @param info YANG java file info node
423 * @param config YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530424 * @throws IOException IO operations fails
425 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530426 public static void generateCodeOfNode(JavaCodeGeneratorInfo info,
427 YangPluginConfig config)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530428 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530429 if (!(info instanceof YangNode)) {
430 throw new TranslatorException(
431 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
432 info));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530433 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530434 updatePackageInfo(info, config);
435 generateTempFiles(info, config);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530436 }
437
438 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530439 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530440 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530441 * @param info YANG java file info node
442 * @param config YANG plugin config
443 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530444 * @throws IOException IO operations fails
445 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530446 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo info,
447 YangPluginConfig config,
448 String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530449 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530450 if (!(info instanceof YangNode)) {
451 throw new TranslatorException(
452 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
453 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530454 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530455 updatePackageInfo(info, config, rootPkg);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530456
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530457 if (isRpcChildNodePresent((YangNode) info)) {
458 info.getJavaFileInfo()
459 .addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530460 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530461 generateTempFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530462 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530463
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530464 /**
465 * Returns the node package string.
466 *
467 * @param curNode current java node whose package string needs to be set
468 * @return returns the root package string
469 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530470 public static String getCurNodePackage(YangNode curNode)
471 throws TranslatorException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530472
473 String pkg;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530474 if (!(curNode instanceof JavaFileInfoContainer) ||
475 curNode.getParent() == null) {
476 throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530477 }
478
479 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
480 if (!(parentNode instanceof JavaFileInfoContainer)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530481 throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE,
482 curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530483 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530484 JavaFileInfoTranslator handle =
485 ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
486 pkg = handle.getPackage() + PERIOD + handle.getJavaName();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530487 return pkg.toLowerCase();
488 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530489
490 /**
491 * Returns true if root node contains any data node.
492 *
493 * @param node root YANG node
494 * @return true if root node contains any data node
495 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530496 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530497 return isNodeCodeGenRequired(node, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530498 }
499
500 /**
Bharat saraswal1edde622016-09-06 10:18:04 +0530501 * Returns true if get/set method of root node are required.
Bharat saraswale3175d32016-08-31 17:50:11 +0530502 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530503 * @param node root node
Bharat saraswal1edde622016-09-06 10:18:04 +0530504 * @return true if get/set method of root node are required
Bharat saraswale3175d32016-08-31 17:50:11 +0530505 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530506 public static boolean isGetSetOfRootNodeRequired(YangNode node) {
507 return isNodeCodeGenRequired(node, false);
508 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530509
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530510 /**
511 * Returns true if either get/set method of root node are required or root
512 * node contains any data node. This check is done depending on the
513 * boolean parameter indicating whether check to be performed for root
514 * node code generation or get/set method generation.
515 *
516 * @param node root node
517 * @param rootNodeGen flag indicating check type
518 * @return true if check pass, false otherwise
519 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530520 private static boolean isNodeCodeGenRequired(YangNode node,
521 boolean rootNodeGen) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530522 YangLeavesHolder holder = (YangLeavesHolder) node;
523
524 if (!holder.getListOfLeaf().isEmpty()) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530525 return true;
Bharat saraswale3175d32016-08-31 17:50:11 +0530526 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530527 if (!holder.getListOfLeafList().isEmpty()) {
528 return true;
529 }
530 node = node.getChild();
531 if (node == null) {
532 return false;
533 }
534
535 if (rootNodeGen) {
536 while (node != null) {
537 if (!(node instanceof YangTranslatorOperatorNode)) {
538 return true;
539 }
540 node = node.getNextSibling();
541 }
542 return false;
543 }
544 while (node != null) {
545 if (!(node instanceof YangAugment)) {
546 return true;
547 }
548 node = node.getNextSibling();
549 }
550 return false;
Bharat saraswale3175d32016-08-31 17:50:11 +0530551 }
552
553 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530554 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530555 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530556 * @param node YANG node
557 * @param config plugin config
Bharat saraswalaf413b82016-07-14 15:18:20 +0530558 * @return java package
559 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530560 public static String getNodesPackage(YangNode node,
561 YangPluginConfig config) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530562
563 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530564 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530565 if (node instanceof YangJavaAugmentTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530566 clsInfo.add(getAugmentClassName((YangAugment) node,
567 config));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530568 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530569 clsInfo.add(getCamelCase(node.getName(), config
570 .getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530571 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530572 node = node.getParent();
573 }
574
575 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530576 if (node instanceof YangJavaModuleTranslator) {
577 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawal493e0a82016-09-28 18:30:08 +0530578 pkg.append(getRootPackage(module.getVersion(),
579 module.getModuleNamespace(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530580 module.getRevision(),
581 config.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530582 } else if (node instanceof YangJavaSubModuleTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530583 YangJavaSubModuleTranslator subModule =
584 (YangJavaSubModuleTranslator) node;
Bharat saraswal8beac342016-08-04 02:00:03 +0530585 pkg.append(getRootPackage(subModule.getVersion(),
Bharat saraswal1edde622016-09-06 10:18:04 +0530586 subModule.getNameSpaceFromModule(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530587 subModule.getRevision(),
588 config.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530589 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530590 pkg.append(EMPTY_STRING);
591 int size = clsInfo.size();
592 for (int i = size - 1; i >= 0; i--) {
593 pkg.append(PERIOD).append(clsInfo.get(i));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530594 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530595 return pkg.toString().toLowerCase();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530596 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530597
598 /**
599 * Returns augment class name.
600 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530601 * @param augment YANG augment
602 * @param config plugin configurations
Bharat saraswal039f59c2016-07-14 21:57:13 +0530603 * @return augment class name
604 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530605 private static String getAugmentClassName(YangAugment augment,
606 YangPluginConfig config) {
607 YangNodeIdentifier identifier =
608 augment.getTargetNode().get(augment.getTargetNode().size() - 1)
609 .getNodeIdentifier();
610 String name = getCapitalCase(getCamelCase(identifier.getName(),
611 config.getConflictResolver()));
612 if (identifier.getPrefix() != null) {
613 return AUGMENTED + getCapitalCase(
614 getCamelCase(identifier.getPrefix(),
615 config.getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530616 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530617 return AUGMENTED + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530618 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530619}