blob: 2a1c8b47363181b161649333a49463efbb23f24a [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;
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;
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +053028import org.onosproject.yangutils.datamodel.YangSchemaNode;
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) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530107 javaGenName = ((YangJavaInputTranslator) info).getParent().getName() +
janani b3c396f02016-09-27 18:45:23 +0530108 INPUT_KEYWORD;
109 } else if (info instanceof YangJavaOutputTranslator) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530110 javaGenName = ((YangJavaOutputTranslator) info).getParent().getName() +
janani b3c396f02016-09-27 18:45:23 +0530111 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 if (info instanceof YangJavaModuleTranslator) {
224 if (!((YangJavaModuleTranslator) info).getNotificationNodes()
225 .isEmpty()) {
226 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530227 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530228 } else if (info instanceof YangJavaSubModuleTranslator) {
229 if (!((YangJavaSubModuleTranslator) info).getNotificationNodes()
230 .isEmpty()) {
231 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530232 }
233 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530234 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530235 if (info instanceof YangLeavesHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530236 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530237 * Container
238 * Case
239 * Grouping
240 * Input
241 * List
242 * Notification
243 * Output
244 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530245 getBeanFiles(info).addCurNodeLeavesInfoToTempFiles((YangNode) info,
246 config);
247
248 // Add operation type as an attribute.
249 getBeanFiles(info).addOperationTypeToTempFiles((YangNode) info,
250 config);
251 } else if (info instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530252 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530253 * Typedef
254 * Union
255 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530256 translator.addTypeInfoToTempFiles((YangTypeHolder) info, config);
257 } else if (info instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530258 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530259 * Enumeration
260 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530261 translator.getEnumTempFiles()
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530262 .addEnumAttributeToTempFiles((YangNode) info, config);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530263 } else if (!(info instanceof YangChoice)) {
264 /*Do nothing, only the interface needs to be generated for choice*/
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530265 throw new TranslatorException(
266 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
267 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530268 }
269 }
270
271 /**
272 * Process generate code entry of YANG node.
273 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530274 * @param info YANG java file info node
275 * @param config plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530276 * @throws IOException IO operations fails
277 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530278 private static void generateTempFiles(JavaCodeGeneratorInfo info,
279 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530280 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530281 if (!(info instanceof YangNode)) {
282 throw new TranslatorException(
283 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
284 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530285 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530286 createTempFragmentFile(info);
287 updateTempFragmentFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530288 }
289
290 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530291 * Updates notification node info in service temporary file.
292 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530293 * @param info java code generator info
294 * @param config plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530295 * @throws IOException when fails to do IO operations
296 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530297 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo info,
298 YangPluginConfig config)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530299 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530300 TempJavaCodeFragmentFiles translator =
301 info.getTempJavaCodeFragmentFiles();
302 if (info instanceof YangJavaModuleTranslator) {
303 for (YangNode notification : ((YangJavaModuleTranslator) info)
304 .getNotificationNodes()) {
305 translator.getEventFragmentFiles()
306 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530307 }
308 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530309 if (info instanceof YangJavaSubModuleTranslator) {
310 for (YangNode notification : ((YangJavaSubModuleTranslator) info)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530311 .getNotificationNodes()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530312 translator.getEventFragmentFiles()
313 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530314 }
315 }
316 }
317
318 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530319 * Generates code for the current ata model node and adds itself as an
320 * attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530321 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530322 * @param info YANG java file info node
323 * @param config YANG plugin config
324 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530325 * @throws IOException IO operations fails
326 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530327 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo info,
328 YangPluginConfig config,
329 boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530330 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530331 if (!(info instanceof YangNode)) {
332 throw new TranslatorException(
333 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
334 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530335 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530336
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530337 YangSchemaNode node = getRefSchema(info);
338 if (node != null) {
339 YangNode parent = ((YangNode) info).getParent();
340 if (!(parent instanceof YangGrouping)) {
341 addCurNodeInfoInParentTempFile((YangNode) node, isMultiInstance,
342 config, parent);
343 }
344 return;
345 }
346
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530347 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530348 * Generate the Java files corresponding to the current node.
349 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530350 generateCodeOfAugmentableNode(info, config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530351
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530352 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530353 * Update the current nodes info in its parent nodes generated files.
354 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530355 addCurNodeInfoInParentTempFile((YangNode) info, isMultiInstance,
356 config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530357 }
358
359 /**
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530360 * Returns referred schema node in case of grouping uses.
361 *
362 * @param info YANG java file info node
363 * @return referred schema node
364 */
365 private static YangSchemaNode getRefSchema(JavaCodeGeneratorInfo info) {
366
367 YangSchemaNode node = ((YangSchemaNode) info);
368 if (node.getReferredSchema() == null) {
369 return null;
370 }
371
372 /*
373 * Obtain last referred node in case grouping is embedded inside
374 * another grouping.
375 */
376 while (node.getReferredSchema() != null) {
377 node = node.getReferredSchema();
378 }
379 return node;
380 }
381
382 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530383 * Generates code for the current data model node and adds support for it to
384 * be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530385 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530386 * @param info YANG java file info node
387 * @param config YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530388 * @throws IOException IO operations fails
389 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530390 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo info,
391 YangPluginConfig config)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530392 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530393 if (!(info instanceof YangNode)) {
394 throw new TranslatorException(
395 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
396 info));
Vidyashree Rama13960652016-04-26 15:06:06 +0530397 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530398
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530399 generateCodeOfNode(info, config);
400 TempJavaCodeFragmentFiles tempFiles =
401 info.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530402
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530403 if (info instanceof YangCase) {
404 YangNode parent = ((YangCase) info).getParent();
405 JavaQualifiedTypeInfoTranslator typeInfo =
406 getQualifierInfoForCasesParent(parent, config);
407 getBeanFiles(info).getJavaExtendsListHolder()
408 .addToExtendsList(typeInfo, (YangNode) info,
409 tempFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530410
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530411 getBeanFiles(info).addParentInfoInCurNodeTempFile((YangNode) info,
412 config);
Vidyashree Rama13960652016-04-26 15:06:06 +0530413 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530414 }
415
416 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530417 * Returns cases parent's qualified info.
418 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530419 * @param parent parent node
420 * @param config plugin configuration
Bharat saraswale50edca2016-08-05 01:58:25 +0530421 * @return cases parent's qualified info
422 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530423 public static JavaQualifiedTypeInfoTranslator
424 getQualifierInfoForCasesParent(YangNode parent,
425 YangPluginConfig config) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530426 String parentName;
427 String parentPkg;
Bharat saraswale50edca2016-08-05 01:58:25 +0530428 JavaFileInfoTranslator parentInfo;
429 if (parent instanceof YangChoice) {
430 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
431 } else {
432 parent = ((YangAugment) parent).getAugmentedNode();
433 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
434 }
435 if (parentInfo.getPackage() != null) {
436 parentName = getCapitalCase(parentInfo.getJavaName());
437 parentPkg = parentInfo.getPackage();
438 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530439 parentName = getCapitalCase(getCamelCase(parent.getName(),
440 config.getConflictResolver()));
441 parentPkg = getNodesPackage(parent, config);
Bharat saraswale50edca2016-08-05 01:58:25 +0530442 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530443 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo =
444 new JavaQualifiedTypeInfoTranslator();
445 qualifiedTypeInfo.setClassInfo(parentName);
446 qualifiedTypeInfo.setPkgInfo(parentPkg);
447 return qualifiedTypeInfo;
Bharat saraswale50edca2016-08-05 01:58:25 +0530448 }
449
450 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530451 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530452 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530453 * @param info YANG java file info node
454 * @param config YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530455 * @throws IOException IO operations fails
456 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530457 public static void generateCodeOfNode(JavaCodeGeneratorInfo info,
458 YangPluginConfig config)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530459 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530460 if (!(info instanceof YangNode)) {
461 throw new TranslatorException(
462 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
463 info));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530464 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530465 updatePackageInfo(info, config);
466 generateTempFiles(info, config);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530467 }
468
469 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530470 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530471 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530472 * @param info YANG java file info node
473 * @param config YANG plugin config
474 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530475 * @throws IOException IO operations fails
476 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530477 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo info,
478 YangPluginConfig config,
479 String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530480 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530481 if (!(info instanceof YangNode)) {
482 throw new TranslatorException(
483 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
484 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530485 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530486 updatePackageInfo(info, config, rootPkg);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530487
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530488 if (isRpcChildNodePresent((YangNode) info)) {
489 info.getJavaFileInfo()
490 .addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530491 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530492 generateTempFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530493 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530494
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530495 /**
496 * Returns the node package string.
497 *
498 * @param curNode current java node whose package string needs to be set
499 * @return returns the root package string
500 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530501 public static String getCurNodePackage(YangNode curNode)
502 throws TranslatorException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530503
504 String pkg;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530505 if (!(curNode instanceof JavaFileInfoContainer) ||
506 curNode.getParent() == null) {
507 throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530508 }
509
510 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
511 if (!(parentNode instanceof JavaFileInfoContainer)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530512 throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE,
513 curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530514 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530515 JavaFileInfoTranslator handle =
516 ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
517 pkg = handle.getPackage() + PERIOD + handle.getJavaName();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530518 return pkg.toLowerCase();
519 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530520
521 /**
522 * Returns true if root node contains any data node.
523 *
524 * @param node root YANG node
525 * @return true if root node contains any data node
526 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530527 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530528 return isNodeCodeGenRequired(node, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530529 }
530
531 /**
Bharat saraswal1edde622016-09-06 10:18:04 +0530532 * Returns true if get/set method of root node are required.
Bharat saraswale3175d32016-08-31 17:50:11 +0530533 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530534 * @param node root node
Bharat saraswal1edde622016-09-06 10:18:04 +0530535 * @return true if get/set method of root node are required
Bharat saraswale3175d32016-08-31 17:50:11 +0530536 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530537 public static boolean isGetSetOfRootNodeRequired(YangNode node) {
538 return isNodeCodeGenRequired(node, false);
539 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530540
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530541 /**
542 * Returns true if either get/set method of root node are required or root
543 * node contains any data node. This check is done depending on the
544 * boolean parameter indicating whether check to be performed for root
545 * node code generation or get/set method generation.
546 *
547 * @param node root node
548 * @param rootNodeGen flag indicating check type
549 * @return true if check pass, false otherwise
550 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530551 private static boolean isNodeCodeGenRequired(YangNode node,
552 boolean rootNodeGen) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530553 YangLeavesHolder holder = (YangLeavesHolder) node;
554
555 if (!holder.getListOfLeaf().isEmpty()) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530556 return true;
Bharat saraswale3175d32016-08-31 17:50:11 +0530557 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530558 if (!holder.getListOfLeafList().isEmpty()) {
559 return true;
560 }
561 node = node.getChild();
562 if (node == null) {
563 return false;
564 }
565
566 if (rootNodeGen) {
567 while (node != null) {
568 if (!(node instanceof YangTranslatorOperatorNode)) {
569 return true;
570 }
571 node = node.getNextSibling();
572 }
573 return false;
574 }
575 while (node != null) {
576 if (!(node instanceof YangAugment)) {
577 return true;
578 }
579 node = node.getNextSibling();
580 }
581 return false;
Bharat saraswale3175d32016-08-31 17:50:11 +0530582 }
583
584 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530585 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530586 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530587 * @param node YANG node
588 * @param config plugin config
Bharat saraswalaf413b82016-07-14 15:18:20 +0530589 * @return java package
590 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530591 public static String getNodesPackage(YangNode node,
592 YangPluginConfig config) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530593
594 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530595 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530596 if (node instanceof YangJavaAugmentTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530597 clsInfo.add(getAugmentClassName((YangAugment) node,
598 config));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530599 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530600 clsInfo.add(getCamelCase(node.getName(), config
601 .getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530602 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530603 node = node.getParent();
604 }
605
606 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530607 if (node instanceof YangJavaModuleTranslator) {
608 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawal493e0a82016-09-28 18:30:08 +0530609 pkg.append(getRootPackage(module.getVersion(),
610 module.getModuleNamespace(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530611 module.getRevision(),
612 config.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530613 } else if (node instanceof YangJavaSubModuleTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530614 YangJavaSubModuleTranslator subModule =
615 (YangJavaSubModuleTranslator) node;
Bharat saraswal8beac342016-08-04 02:00:03 +0530616 pkg.append(getRootPackage(subModule.getVersion(),
Bharat saraswal1edde622016-09-06 10:18:04 +0530617 subModule.getNameSpaceFromModule(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530618 subModule.getRevision(),
619 config.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530620 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530621 pkg.append(EMPTY_STRING);
622 int size = clsInfo.size();
623 for (int i = size - 1; i >= 0; i--) {
624 pkg.append(PERIOD).append(clsInfo.get(i));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530625 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530626 return pkg.toString().toLowerCase();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530627 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530628
629 /**
630 * Returns augment class name.
631 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530632 * @param augment YANG augment
633 * @param config plugin configurations
Bharat saraswal039f59c2016-07-14 21:57:13 +0530634 * @return augment class name
635 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530636 private static String getAugmentClassName(YangAugment augment,
637 YangPluginConfig config) {
638 YangNodeIdentifier identifier =
639 augment.getTargetNode().get(augment.getTargetNode().size() - 1)
640 .getNodeIdentifier();
641 String name = getCapitalCase(getCamelCase(identifier.getName(),
642 config.getConflictResolver()));
643 if (identifier.getPrefix() != null) {
644 return AUGMENTED + getCapitalCase(
645 getCamelCase(identifier.getPrefix(),
646 config.getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530647 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530648 return AUGMENTED + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530649 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530650}