blob: 136fad56a7d0275925b44f02a5d234593c8bd919 [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 Agrawal9381ebb2016-09-29 19:00:33 +053024import org.onosproject.yangutils.datamodel.YangGrouping;
janani b3c396f02016-09-27 18:45:23 +053025import org.onosproject.yangutils.datamodel.YangInput;
Gaurav Agrawal26390042016-04-12 13:30:16 +053026import org.onosproject.yangutils.datamodel.YangLeavesHolder;
27import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal039f59c2016-07-14 21:57:13 +053028import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
janani b3c396f02016-09-27 18:45:23 +053029import org.onosproject.yangutils.datamodel.YangOutput;
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +053030import org.onosproject.yangutils.datamodel.YangSchemaNode;
Bharat saraswal039f59c2016-07-14 21:57:13 +053031import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053032import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053033import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053034import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053035import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugmentTranslator;
36import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumerationTranslator;
janani b3c396f02016-09-27 18:45:23 +053037import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaInputTranslator;
Shankara-Huaweib7564772016-08-02 18:13:13 +053038import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
janani b3c396f02016-09-27 18:45:23 +053039import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaOutputTranslator;
Shankara-Huaweib7564772016-08-02 18:13:13 +053040import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053041import org.onosproject.yangutils.utils.io.YangPluginConfig;
Gaurav Agrawal26390042016-04-12 13:30:16 +053042
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053043import java.io.IOException;
44import java.util.ArrayList;
45import java.util.List;
46
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053047import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
48import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053049import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswald14cbe82016-07-14 13:26:18 +053050import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053051import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_NODE;
52import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_PARENT_NODE;
53import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_TRANSLATION_NODE;
54import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getBeanFiles;
55import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsg;
56import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsgForCodeGenerator;
Bharat saraswal039f59c2016-07-14 21:57:13 +053057import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053058import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
janani b3c396f02016-09-27 18:45:23 +053059import static org.onosproject.yangutils.utils.UtilConstants.INPUT_KEYWORD;
60import static org.onosproject.yangutils.utils.UtilConstants.OUTPUT_KEYWORD;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053061import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
62import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
63import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
64import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053065import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Gaurav Agrawal26390042016-04-12 13:30:16 +053066
67/**
68 * Represents utility class for YANG java model.
69 */
70public final class YangJavaModelUtils {
71
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053072 // No instantiation.
Gaurav Agrawal26390042016-04-12 13:30:16 +053073 private YangJavaModelUtils() {
74 }
75
76 /**
77 * Updates YANG java file package information.
78 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053079 * @param info YANG java file info node
80 * @param config YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +053081 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053082 public static void updatePackageInfo(JavaCodeGeneratorInfo info,
Bharat saraswal9fab16b2016-09-23 23:27:24 +053083 YangPluginConfig config) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053084
85 JavaFileInfoTranslator translator = info.getJavaFileInfo();
86
87 if (info instanceof YangJavaAugmentTranslator) {
88 updatePackageForAugmentInfo(info, config);
Bharat saraswald14cbe82016-07-14 13:26:18 +053089 } else {
janani b3c396f02016-09-27 18:45:23 +053090 setNodeJavaName(info, config);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053091 translator.setJavaAttributeName(info.getJavaFileInfo()
92 .getJavaName());
93 translator.setPackage(getCurNodePackage((YangNode) info));
Bharat saraswald14cbe82016-07-14 13:26:18 +053094 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053095 updateCommonPackageInfo(translator, info, config);
Bharat saraswal8beac342016-08-04 02:00:03 +053096 }
97
98 /**
janani b3c396f02016-09-27 18:45:23 +053099 * The java name for input, output is prefixed with rpc name and other
100 * nodes are set by taking its own name from YANG.
101 *
102 * @param info YANG java file info node
103 * @param config YANG plugin config
104 */
105 private static void setNodeJavaName(JavaCodeGeneratorInfo info,
106 YangPluginConfig config) {
107 String javaGenName;
108 if (info instanceof YangJavaInputTranslator) {
109 javaGenName = ((YangInput) info).getParent().getName() +
110 INPUT_KEYWORD;
111 } else if (info instanceof YangJavaOutputTranslator) {
112 javaGenName = ((YangOutput) info).getParent().getName() +
113 OUTPUT_KEYWORD;
114 } else {
115 javaGenName = ((YangNode) info).getName();
116 }
117 info.getJavaFileInfo().setJavaName(getCamelCase(
118 javaGenName, config.getConflictResolver()));
119 }
120
121 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530122 * Updates YANG java file package information.
123 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530124 * @param info YANG java file info node
125 * @param config YANG plugin config
Bharat saraswal8beac342016-08-04 02:00:03 +0530126 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530127 private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo info,
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530128 YangPluginConfig config) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530129
130 JavaFileInfoTranslator translator = info.getJavaFileInfo();
131
132 translator.setJavaName(getAugmentClassName(
133 (YangJavaAugmentTranslator) info, config));
134 translator.setPackage(getAugmentsNodePackage((YangNode) info, config));
135 updateCommonPackageInfo(translator, info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530136 }
137
138 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530139 * Returns package for augment node.
140 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530141 * @param yangNode augment node
142 * @param config plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +0530143 * @return package for augment node
144 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530145 private static String getAugmentsNodePackage(YangNode yangNode,
146 YangPluginConfig config) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530147 YangAugment augment = (YangAugment) yangNode;
148 StringBuilder augmentPkg = new StringBuilder();
149 augmentPkg.append(getCurNodePackage(augment));
150
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530151 StringBuilder pkg = new StringBuilder();
152 pkg.append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530153 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530154 pkg.append(getCamelCase(atomicPath.getNodeIdentifier().getName(),
155 config.getConflictResolver()))
156 .append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530157 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530158 augmentPkg.append(trimAtLast(pkg.toString(), PERIOD).toLowerCase());
Bharat saraswal8beac342016-08-04 02:00:03 +0530159 return augmentPkg.toString();
160 }
161
162 /**
Gaurav Agrawal26390042016-04-12 13:30:16 +0530163 * Updates YANG java file package information for specified package.
164 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530165 * @param info YANG java file info node
166 * @param config YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +0530167 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530168 private static void updatePackageInfo(JavaCodeGeneratorInfo info,
169 YangPluginConfig config,
170 String pkg) {
171
172 JavaFileInfoTranslator translator = info.getJavaFileInfo();
173 translator.setJavaName(getCamelCase(((YangNode) info).getName(),
174 config.getConflictResolver()));
175 translator.setPackage(pkg);
176 updateCommonPackageInfo(translator, info, config);
177 }
178
179 /**
180 * Updates common package information.
181 *
182 * @param translator JAVA file info translator
183 * @param info YANG java file info node
184 * @param config YANG plugin config
185 */
186 private static void updateCommonPackageInfo(JavaFileInfoTranslator translator,
187 JavaCodeGeneratorInfo info,
188 YangPluginConfig config) {
189 translator.setPackageFilePath(getPackageDirPathFromJavaJPackage(
190 info.getJavaFileInfo().getPackage()));
191 translator.setBaseCodeGenPath(config.getCodeGenDir());
192 translator.setPluginConfig(config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530193 }
194
195 /**
196 * Updates temporary java code fragment files.
197 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530198 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530199 * @throws IOException IO operations fails
200 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530201 private static void createTempFragmentFile(JavaCodeGeneratorInfo info)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530202 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530203 info.setTempJavaCodeFragmentFiles(
204 new TempJavaCodeFragmentFiles(info.getJavaFileInfo()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530205 }
206
207 /**
208 * Updates leaf information in temporary java code fragment files.
209 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530210 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530211 * @throws IOException IO operations fails
212 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530213 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo info,
214 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530215 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530216
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530217 TempJavaCodeFragmentFiles translator =
218 info.getTempJavaCodeFragmentFiles();
219
220 if (info instanceof RpcNotificationContainer) {
221 getBeanFiles(info).setRootNode(true);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530222 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530223 * Module / sub module node code generation.
224 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530225 translator.getServiceTempFiles()
226 .addCurNodeLeavesInfoToTempFiles((YangNode) info, config);
227 if (info instanceof YangJavaModuleTranslator) {
228 if (!((YangJavaModuleTranslator) info).getNotificationNodes()
229 .isEmpty()) {
230 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530231 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530232 } else if (info instanceof YangJavaSubModuleTranslator) {
233 if (!((YangJavaSubModuleTranslator) info).getNotificationNodes()
234 .isEmpty()) {
235 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530236 }
237 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530238 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530239 if (info instanceof YangLeavesHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530240 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530241 * Container
242 * Case
243 * Grouping
244 * Input
245 * List
246 * Notification
247 * Output
248 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530249 getBeanFiles(info).addCurNodeLeavesInfoToTempFiles((YangNode) info,
250 config);
251
252 // Add operation type as an attribute.
253 getBeanFiles(info).addOperationTypeToTempFiles((YangNode) info,
254 config);
255 } else if (info instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530256 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530257 * Typedef
258 * Union
259 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530260 translator.addTypeInfoToTempFiles((YangTypeHolder) info, config);
261 } else if (info instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530262 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530263 * Enumeration
264 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530265 translator.getEnumTempFiles()
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530266 .addEnumAttributeToTempFiles((YangNode) info, config);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530267 } else if (!(info instanceof YangChoice)) {
268 /*Do nothing, only the interface needs to be generated for choice*/
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530269 throw new TranslatorException(
270 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
271 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530272 }
273 }
274
275 /**
276 * Process generate code entry of YANG node.
277 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530278 * @param info YANG java file info node
279 * @param config plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530280 * @throws IOException IO operations fails
281 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530282 private static void generateTempFiles(JavaCodeGeneratorInfo info,
283 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530284 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530285 if (!(info instanceof YangNode)) {
286 throw new TranslatorException(
287 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
288 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530289 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530290 createTempFragmentFile(info);
291 updateTempFragmentFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530292 }
293
294 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530295 * Updates notification node info in service temporary file.
296 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530297 * @param info java code generator info
298 * @param config plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530299 * @throws IOException when fails to do IO operations
300 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530301 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo info,
302 YangPluginConfig config)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530303 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530304 TempJavaCodeFragmentFiles translator =
305 info.getTempJavaCodeFragmentFiles();
306 if (info instanceof YangJavaModuleTranslator) {
307 for (YangNode notification : ((YangJavaModuleTranslator) info)
308 .getNotificationNodes()) {
309 translator.getEventFragmentFiles()
310 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530311 }
312 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530313 if (info instanceof YangJavaSubModuleTranslator) {
314 for (YangNode notification : ((YangJavaSubModuleTranslator) info)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530315 .getNotificationNodes()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530316 translator.getEventFragmentFiles()
317 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530318 }
319 }
320 }
321
322 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530323 * Generates code for the current ata model node and adds itself as an
324 * attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530325 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530326 * @param info YANG java file info node
327 * @param config YANG plugin config
328 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530329 * @throws IOException IO operations fails
330 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530331 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo info,
332 YangPluginConfig config,
333 boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530334 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530335 if (!(info instanceof YangNode)) {
336 throw new TranslatorException(
337 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
338 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530339 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530340
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530341 YangSchemaNode node = getRefSchema(info);
342 if (node != null) {
343 YangNode parent = ((YangNode) info).getParent();
344 if (!(parent instanceof YangGrouping)) {
345 addCurNodeInfoInParentTempFile((YangNode) node, isMultiInstance,
346 config, parent);
347 }
348 return;
349 }
350
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530351 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530352 * Generate the Java files corresponding to the current node.
353 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530354 generateCodeOfAugmentableNode(info, config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530355
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530356 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530357 * Update the current nodes info in its parent nodes generated files.
358 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530359 addCurNodeInfoInParentTempFile((YangNode) info, isMultiInstance,
360 config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530361 }
362
363 /**
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530364 * Returns referred schema node in case of grouping uses.
365 *
366 * @param info YANG java file info node
367 * @return referred schema node
368 */
369 private static YangSchemaNode getRefSchema(JavaCodeGeneratorInfo info) {
370
371 YangSchemaNode node = ((YangSchemaNode) info);
372 if (node.getReferredSchema() == null) {
373 return null;
374 }
375
376 /*
377 * Obtain last referred node in case grouping is embedded inside
378 * another grouping.
379 */
380 while (node.getReferredSchema() != null) {
381 node = node.getReferredSchema();
382 }
383 return node;
384 }
385
386 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530387 * Generates code for the current data model node and adds support for it to
388 * be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530389 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530390 * @param info YANG java file info node
391 * @param config YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530392 * @throws IOException IO operations fails
393 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530394 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo info,
395 YangPluginConfig config)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530396 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530397 if (!(info instanceof YangNode)) {
398 throw new TranslatorException(
399 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
400 info));
Vidyashree Rama13960652016-04-26 15:06:06 +0530401 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530402
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530403 generateCodeOfNode(info, config);
404 TempJavaCodeFragmentFiles tempFiles =
405 info.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530406
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530407 if (info instanceof YangCase) {
408 YangNode parent = ((YangCase) info).getParent();
409 JavaQualifiedTypeInfoTranslator typeInfo =
410 getQualifierInfoForCasesParent(parent, config);
411 getBeanFiles(info).getJavaExtendsListHolder()
412 .addToExtendsList(typeInfo, (YangNode) info,
413 tempFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530414
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530415 getBeanFiles(info).addParentInfoInCurNodeTempFile((YangNode) info,
416 config);
Vidyashree Rama13960652016-04-26 15:06:06 +0530417 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530418 }
419
420 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530421 * Returns cases parent's qualified info.
422 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530423 * @param parent parent node
424 * @param config plugin configuration
Bharat saraswale50edca2016-08-05 01:58:25 +0530425 * @return cases parent's qualified info
426 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530427 public static JavaQualifiedTypeInfoTranslator
428 getQualifierInfoForCasesParent(YangNode parent,
429 YangPluginConfig config) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530430 String parentName;
431 String parentPkg;
Bharat saraswale50edca2016-08-05 01:58:25 +0530432 JavaFileInfoTranslator parentInfo;
433 if (parent instanceof YangChoice) {
434 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
435 } else {
436 parent = ((YangAugment) parent).getAugmentedNode();
437 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
438 }
439 if (parentInfo.getPackage() != null) {
440 parentName = getCapitalCase(parentInfo.getJavaName());
441 parentPkg = parentInfo.getPackage();
442 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530443 parentName = getCapitalCase(getCamelCase(parent.getName(),
444 config.getConflictResolver()));
445 parentPkg = getNodesPackage(parent, config);
Bharat saraswale50edca2016-08-05 01:58:25 +0530446 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530447 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo =
448 new JavaQualifiedTypeInfoTranslator();
449 qualifiedTypeInfo.setClassInfo(parentName);
450 qualifiedTypeInfo.setPkgInfo(parentPkg);
451 return qualifiedTypeInfo;
Bharat saraswale50edca2016-08-05 01:58:25 +0530452 }
453
454 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530455 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530456 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530457 * @param info YANG java file info node
458 * @param config YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530459 * @throws IOException IO operations fails
460 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530461 public static void generateCodeOfNode(JavaCodeGeneratorInfo info,
462 YangPluginConfig config)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530463 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530464 if (!(info instanceof YangNode)) {
465 throw new TranslatorException(
466 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
467 info));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530468 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530469 updatePackageInfo(info, config);
470 generateTempFiles(info, config);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530471 }
472
473 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530474 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530475 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530476 * @param info YANG java file info node
477 * @param config YANG plugin config
478 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530479 * @throws IOException IO operations fails
480 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530481 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo info,
482 YangPluginConfig config,
483 String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530484 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530485 if (!(info instanceof YangNode)) {
486 throw new TranslatorException(
487 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
488 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530489 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530490 updatePackageInfo(info, config, rootPkg);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530491
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530492 if (isRpcChildNodePresent((YangNode) info)) {
493 info.getJavaFileInfo()
494 .addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530495 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530496 generateTempFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530497 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530498
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530499 /**
500 * Returns the node package string.
501 *
502 * @param curNode current java node whose package string needs to be set
503 * @return returns the root package string
504 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530505 public static String getCurNodePackage(YangNode curNode)
506 throws TranslatorException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530507
508 String pkg;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530509 if (!(curNode instanceof JavaFileInfoContainer) ||
510 curNode.getParent() == null) {
511 throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530512 }
513
514 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
515 if (!(parentNode instanceof JavaFileInfoContainer)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530516 throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE,
517 curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530518 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530519 JavaFileInfoTranslator handle =
520 ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
521 pkg = handle.getPackage() + PERIOD + handle.getJavaName();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530522 return pkg.toLowerCase();
523 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530524
525 /**
526 * Returns true if root node contains any data node.
527 *
528 * @param node root YANG node
529 * @return true if root node contains any data node
530 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530531 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530532 return isNodeCodeGenRequired(node, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530533 }
534
535 /**
Bharat saraswal1edde622016-09-06 10:18:04 +0530536 * Returns true if get/set method of root node are required.
Bharat saraswale3175d32016-08-31 17:50:11 +0530537 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530538 * @param node root node
Bharat saraswal1edde622016-09-06 10:18:04 +0530539 * @return true if get/set method of root node are required
Bharat saraswale3175d32016-08-31 17:50:11 +0530540 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530541 public static boolean isGetSetOfRootNodeRequired(YangNode node) {
542 return isNodeCodeGenRequired(node, false);
543 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530544
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530545 /**
546 * Returns true if either get/set method of root node are required or root
547 * node contains any data node. This check is done depending on the
548 * boolean parameter indicating whether check to be performed for root
549 * node code generation or get/set method generation.
550 *
551 * @param node root node
552 * @param rootNodeGen flag indicating check type
553 * @return true if check pass, false otherwise
554 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530555 private static boolean isNodeCodeGenRequired(YangNode node,
556 boolean rootNodeGen) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530557 YangLeavesHolder holder = (YangLeavesHolder) node;
558
559 if (!holder.getListOfLeaf().isEmpty()) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530560 return true;
Bharat saraswale3175d32016-08-31 17:50:11 +0530561 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530562 if (!holder.getListOfLeafList().isEmpty()) {
563 return true;
564 }
565 node = node.getChild();
566 if (node == null) {
567 return false;
568 }
569
570 if (rootNodeGen) {
571 while (node != null) {
572 if (!(node instanceof YangTranslatorOperatorNode)) {
573 return true;
574 }
575 node = node.getNextSibling();
576 }
577 return false;
578 }
579 while (node != null) {
580 if (!(node instanceof YangAugment)) {
581 return true;
582 }
583 node = node.getNextSibling();
584 }
585 return false;
Bharat saraswale3175d32016-08-31 17:50:11 +0530586 }
587
588 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530589 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530590 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530591 * @param node YANG node
592 * @param config plugin config
Bharat saraswalaf413b82016-07-14 15:18:20 +0530593 * @return java package
594 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530595 public static String getNodesPackage(YangNode node,
596 YangPluginConfig config) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530597
598 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530599 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530600 if (node instanceof YangJavaAugmentTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530601 clsInfo.add(getAugmentClassName((YangAugment) node,
602 config));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530603 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530604 clsInfo.add(getCamelCase(node.getName(), config
605 .getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530606 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530607 node = node.getParent();
608 }
609
610 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530611 if (node instanceof YangJavaModuleTranslator) {
612 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawal493e0a82016-09-28 18:30:08 +0530613 pkg.append(getRootPackage(module.getVersion(),
614 module.getModuleNamespace(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530615 module.getRevision(),
616 config.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530617 } else if (node instanceof YangJavaSubModuleTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530618 YangJavaSubModuleTranslator subModule =
619 (YangJavaSubModuleTranslator) node;
Bharat saraswal8beac342016-08-04 02:00:03 +0530620 pkg.append(getRootPackage(subModule.getVersion(),
Bharat saraswal1edde622016-09-06 10:18:04 +0530621 subModule.getNameSpaceFromModule(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530622 subModule.getRevision(),
623 config.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530624 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530625 pkg.append(EMPTY_STRING);
626 int size = clsInfo.size();
627 for (int i = size - 1; i >= 0; i--) {
628 pkg.append(PERIOD).append(clsInfo.get(i));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530629 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530630 return pkg.toString().toLowerCase();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530631 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530632
633 /**
634 * Returns augment class name.
635 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530636 * @param augment YANG augment
637 * @param config plugin configurations
Bharat saraswal039f59c2016-07-14 21:57:13 +0530638 * @return augment class name
639 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530640 private static String getAugmentClassName(YangAugment augment,
641 YangPluginConfig config) {
642 YangNodeIdentifier identifier =
643 augment.getTargetNode().get(augment.getTargetNode().size() - 1)
644 .getNodeIdentifier();
645 String name = getCapitalCase(getCamelCase(identifier.getName(),
646 config.getConflictResolver()));
647 if (identifier.getPrefix() != null) {
648 return AUGMENTED + getCapitalCase(
649 getCamelCase(identifier.getPrefix(),
650 config.getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530651 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530652 return AUGMENTED + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530653 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530654}