blob: 2c019f6577e9b46fedeed7efaf9c623a1d3a2afe [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 Agrawald30f79b2016-10-06 00:58:57 +0530337 /*
338 * For second level and below cloned nodes code shouldn't be
339 * generated also they needn't be added in parent, since
340 * generated code will be under grouping, cloned node is only
341 * used for YANG namespace.
342 */
343 YangNode n = (YangNode) info;
344 if (n.getReferredSchema() != null &&
345 !(((YangNode) n.getReferredSchema()).getParent() instanceof
346 YangGrouping)) {
347 return;
348 }
349 /*
350 * If first level cloned node, then it needs to be imported in the
351 * generated code. In case uses under grouping, it would further have
352 * second level uses, hence needn't add in parent grouping.
353 */
354 YangSchemaNode rn = getRefSchema(info);
355 if (rn != null) {
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530356 YangNode parent = ((YangNode) info).getParent();
357 if (!(parent instanceof YangGrouping)) {
Gaurav Agrawald30f79b2016-10-06 00:58:57 +0530358 addCurNodeInfoInParentTempFile((YangNode) rn, isMultiInstance,
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530359 config, parent);
360 }
361 return;
362 }
363
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530364 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530365 * Generate the Java files corresponding to the current node.
366 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530367 generateCodeOfAugmentableNode(info, config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530368
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530369 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530370 * Update the current nodes info in its parent nodes generated files.
371 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530372 addCurNodeInfoInParentTempFile((YangNode) info, isMultiInstance,
373 config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530374 }
375
376 /**
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530377 * Returns referred schema node in case of grouping uses.
378 *
379 * @param info YANG java file info node
380 * @return referred schema node
381 */
382 private static YangSchemaNode getRefSchema(JavaCodeGeneratorInfo info) {
383
384 YangSchemaNode node = ((YangSchemaNode) info);
385 if (node.getReferredSchema() == null) {
386 return null;
387 }
388
389 /*
390 * Obtain last referred node in case grouping is embedded inside
391 * another grouping.
392 */
393 while (node.getReferredSchema() != null) {
394 node = node.getReferredSchema();
395 }
396 return node;
397 }
398
399 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530400 * Generates code for the current data model node and adds support for it to
401 * be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530402 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530403 * @param info YANG java file info node
404 * @param config YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530405 * @throws IOException IO operations fails
406 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530407 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo info,
408 YangPluginConfig config)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530409 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530410 if (!(info instanceof YangNode)) {
411 throw new TranslatorException(
412 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
413 info));
Vidyashree Rama13960652016-04-26 15:06:06 +0530414 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530415
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530416 generateCodeOfNode(info, config);
417 TempJavaCodeFragmentFiles tempFiles =
418 info.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530419
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530420 if (info instanceof YangCase) {
421 YangNode parent = ((YangCase) info).getParent();
422 JavaQualifiedTypeInfoTranslator typeInfo =
423 getQualifierInfoForCasesParent(parent, config);
424 getBeanFiles(info).getJavaExtendsListHolder()
425 .addToExtendsList(typeInfo, (YangNode) info,
426 tempFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530427
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530428 getBeanFiles(info).addParentInfoInCurNodeTempFile((YangNode) info,
429 config);
Vidyashree Rama13960652016-04-26 15:06:06 +0530430 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530431 }
432
433 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530434 * Returns cases parent's qualified info.
435 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530436 * @param parent parent node
437 * @param config plugin configuration
Bharat saraswale50edca2016-08-05 01:58:25 +0530438 * @return cases parent's qualified info
439 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530440 public static JavaQualifiedTypeInfoTranslator
441 getQualifierInfoForCasesParent(YangNode parent,
442 YangPluginConfig config) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530443 String parentName;
444 String parentPkg;
Bharat saraswale50edca2016-08-05 01:58:25 +0530445 JavaFileInfoTranslator parentInfo;
446 if (parent instanceof YangChoice) {
447 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
448 } else {
449 parent = ((YangAugment) parent).getAugmentedNode();
450 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
451 }
452 if (parentInfo.getPackage() != null) {
453 parentName = getCapitalCase(parentInfo.getJavaName());
454 parentPkg = parentInfo.getPackage();
455 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530456 parentName = getCapitalCase(getCamelCase(parent.getName(),
457 config.getConflictResolver()));
458 parentPkg = getNodesPackage(parent, config);
Bharat saraswale50edca2016-08-05 01:58:25 +0530459 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530460 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo =
461 new JavaQualifiedTypeInfoTranslator();
462 qualifiedTypeInfo.setClassInfo(parentName);
463 qualifiedTypeInfo.setPkgInfo(parentPkg);
464 return qualifiedTypeInfo;
Bharat saraswale50edca2016-08-05 01:58:25 +0530465 }
466
467 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530468 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530469 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530470 * @param info YANG java file info node
471 * @param config YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530472 * @throws IOException IO operations fails
473 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530474 public static void generateCodeOfNode(JavaCodeGeneratorInfo info,
475 YangPluginConfig config)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530476 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530477 if (!(info instanceof YangNode)) {
478 throw new TranslatorException(
479 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
480 info));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530481 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530482 updatePackageInfo(info, config);
483 generateTempFiles(info, config);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530484 }
485
486 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530487 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530488 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530489 * @param info YANG java file info node
490 * @param config YANG plugin config
491 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530492 * @throws IOException IO operations fails
493 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530494 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo info,
495 YangPluginConfig config,
496 String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530497 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530498 if (!(info instanceof YangNode)) {
499 throw new TranslatorException(
500 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
501 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530502 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530503 updatePackageInfo(info, config, rootPkg);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530504
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530505 if (isRpcChildNodePresent((YangNode) info)) {
506 info.getJavaFileInfo()
507 .addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530508 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530509 generateTempFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530510 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530511
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530512 /**
513 * Returns the node package string.
514 *
515 * @param curNode current java node whose package string needs to be set
516 * @return returns the root package string
517 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530518 public static String getCurNodePackage(YangNode curNode)
519 throws TranslatorException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530520
521 String pkg;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530522 if (!(curNode instanceof JavaFileInfoContainer) ||
523 curNode.getParent() == null) {
524 throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530525 }
526
527 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
528 if (!(parentNode instanceof JavaFileInfoContainer)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530529 throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE,
530 curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530531 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530532 JavaFileInfoTranslator handle =
533 ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
534 pkg = handle.getPackage() + PERIOD + handle.getJavaName();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530535 return pkg.toLowerCase();
536 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530537
538 /**
539 * Returns true if root node contains any data node.
540 *
541 * @param node root YANG node
542 * @return true if root node contains any data node
543 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530544 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530545 return isNodeCodeGenRequired(node, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530546 }
547
548 /**
Bharat saraswal1edde622016-09-06 10:18:04 +0530549 * Returns true if get/set method of root node are required.
Bharat saraswale3175d32016-08-31 17:50:11 +0530550 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530551 * @param node root node
Bharat saraswal1edde622016-09-06 10:18:04 +0530552 * @return true if get/set method of root node are required
Bharat saraswale3175d32016-08-31 17:50:11 +0530553 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530554 public static boolean isGetSetOfRootNodeRequired(YangNode node) {
555 return isNodeCodeGenRequired(node, false);
556 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530557
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530558 /**
559 * Returns true if either get/set method of root node are required or root
560 * node contains any data node. This check is done depending on the
561 * boolean parameter indicating whether check to be performed for root
562 * node code generation or get/set method generation.
563 *
564 * @param node root node
565 * @param rootNodeGen flag indicating check type
566 * @return true if check pass, false otherwise
567 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530568 private static boolean isNodeCodeGenRequired(YangNode node,
569 boolean rootNodeGen) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530570 YangLeavesHolder holder = (YangLeavesHolder) node;
571
572 if (!holder.getListOfLeaf().isEmpty()) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530573 return true;
Bharat saraswale3175d32016-08-31 17:50:11 +0530574 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530575 if (!holder.getListOfLeafList().isEmpty()) {
576 return true;
577 }
578 node = node.getChild();
579 if (node == null) {
580 return false;
581 }
582
583 if (rootNodeGen) {
584 while (node != null) {
585 if (!(node instanceof YangTranslatorOperatorNode)) {
586 return true;
587 }
588 node = node.getNextSibling();
589 }
590 return false;
591 }
592 while (node != null) {
593 if (!(node instanceof YangAugment)) {
594 return true;
595 }
596 node = node.getNextSibling();
597 }
598 return false;
Bharat saraswale3175d32016-08-31 17:50:11 +0530599 }
600
601 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530602 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530603 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530604 * @param node YANG node
605 * @param config plugin config
Bharat saraswalaf413b82016-07-14 15:18:20 +0530606 * @return java package
607 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530608 public static String getNodesPackage(YangNode node,
609 YangPluginConfig config) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530610
611 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530612 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530613 if (node instanceof YangJavaAugmentTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530614 clsInfo.add(getAugmentClassName((YangAugment) node,
615 config));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530616 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530617 clsInfo.add(getCamelCase(node.getName(), config
618 .getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530619 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530620 node = node.getParent();
621 }
622
623 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530624 if (node instanceof YangJavaModuleTranslator) {
625 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawal493e0a82016-09-28 18:30:08 +0530626 pkg.append(getRootPackage(module.getVersion(),
627 module.getModuleNamespace(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530628 module.getRevision(),
629 config.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530630 } else if (node instanceof YangJavaSubModuleTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530631 YangJavaSubModuleTranslator subModule =
632 (YangJavaSubModuleTranslator) node;
Bharat saraswal8beac342016-08-04 02:00:03 +0530633 pkg.append(getRootPackage(subModule.getVersion(),
Bharat saraswal1edde622016-09-06 10:18:04 +0530634 subModule.getNameSpaceFromModule(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530635 subModule.getRevision(),
636 config.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530637 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530638 pkg.append(EMPTY_STRING);
639 int size = clsInfo.size();
640 for (int i = size - 1; i >= 0; i--) {
641 pkg.append(PERIOD).append(clsInfo.get(i));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530642 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530643 return pkg.toString().toLowerCase();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530644 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530645
646 /**
647 * Returns augment class name.
648 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530649 * @param augment YANG augment
650 * @param config plugin configurations
Bharat saraswal039f59c2016-07-14 21:57:13 +0530651 * @return augment class name
652 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530653 private static String getAugmentClassName(YangAugment augment,
654 YangPluginConfig config) {
655 YangNodeIdentifier identifier =
656 augment.getTargetNode().get(augment.getTargetNode().size() - 1)
657 .getNodeIdentifier();
658 String name = getCapitalCase(getCamelCase(identifier.getName(),
659 config.getConflictResolver()));
660 if (identifier.getPrefix() != null) {
661 return AUGMENTED + getCapitalCase(
662 getCamelCase(identifier.getPrefix(),
663 config.getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530664 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530665 return AUGMENTED + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530666 }
Gaurav Agrawald30f79b2016-10-06 00:58:57 +0530667
668 /**
669 * Generated java code during exit.
670 *
671 * @param type generated file type
672 * @param node current YANG node
673 * @throws IOException when fails to generate java files
674 */
675 public static void generateJava(int type, YangNode node)
676 throws IOException {
677 /*
678 * Call for file generation if node is not under uses.
679 */
680 if(node.getReferredSchema() == null) {
681 ((TempJavaCodeFragmentFilesContainer) node)
682 .getTempJavaCodeFragmentFiles().generateJavaFile(type, node);
683 }
684 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530685}