blob: 0a433ee7b76a1a3f1932783ee9af9f7215fcf0b6 [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
Bharat saraswal94844d62016-10-13 13:28:03 +053041import java.io.File;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053042import java.io.IOException;
43import java.util.ArrayList;
44import java.util.List;
45
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053046import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
47import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053048import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswal94844d62016-10-13 13:28:03 +053049import static org.onosproject.yangutils.translator.tojava.utils.IndentationType.FOUR_SPACE;
50import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
Bharat saraswald14cbe82016-07-14 13:26:18 +053051import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswal94844d62016-10-13 13:28:03 +053052import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.methodClose;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053053import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_NODE;
54import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_PARENT_NODE;
55import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_TRANSLATION_NODE;
56import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getBeanFiles;
57import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsg;
58import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsgForCodeGenerator;
Bharat saraswal039f59c2016-07-14 21:57:13 +053059import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053060import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal94844d62016-10-13 13:28:03 +053061import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
janani b3c396f02016-09-27 18:45:23 +053062import static org.onosproject.yangutils.utils.UtilConstants.INPUT_KEYWORD;
63import static org.onosproject.yangutils.utils.UtilConstants.OUTPUT_KEYWORD;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053064import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswal94844d62016-10-13 13:28:03 +053065import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053066import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
67import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
68import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Bharat saraswal94844d62016-10-13 13:28:03 +053069import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
Bharat saraswal8beac342016-08-04 02:00:03 +053070import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Bharat saraswal94844d62016-10-13 13:28:03 +053071import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.validateLineLength;
Gaurav Agrawal26390042016-04-12 13:30:16 +053072
73/**
74 * Represents utility class for YANG java model.
75 */
76public final class YangJavaModelUtils {
77
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053078 // No instantiation.
Gaurav Agrawal26390042016-04-12 13:30:16 +053079 private YangJavaModelUtils() {
80 }
81
82 /**
83 * Updates YANG java file package information.
84 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053085 * @param info YANG java file info node
86 * @param config YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +053087 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053088 public static void updatePackageInfo(JavaCodeGeneratorInfo info,
Bharat saraswal9fab16b2016-09-23 23:27:24 +053089 YangPluginConfig config) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053090
91 JavaFileInfoTranslator translator = info.getJavaFileInfo();
92
93 if (info instanceof YangJavaAugmentTranslator) {
94 updatePackageForAugmentInfo(info, config);
Bharat saraswald14cbe82016-07-14 13:26:18 +053095 } else {
janani b3c396f02016-09-27 18:45:23 +053096 setNodeJavaName(info, config);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053097 translator.setJavaAttributeName(info.getJavaFileInfo()
98 .getJavaName());
99 translator.setPackage(getCurNodePackage((YangNode) info));
Bharat saraswald14cbe82016-07-14 13:26:18 +0530100 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530101 updateCommonPackageInfo(translator, info, config);
Bharat saraswal8beac342016-08-04 02:00:03 +0530102 }
103
104 /**
janani b3c396f02016-09-27 18:45:23 +0530105 * The java name for input, output is prefixed with rpc name and other
106 * nodes are set by taking its own name from YANG.
107 *
108 * @param info YANG java file info node
109 * @param config YANG plugin config
110 */
111 private static void setNodeJavaName(JavaCodeGeneratorInfo info,
112 YangPluginConfig config) {
113 String javaGenName;
114 if (info instanceof YangJavaInputTranslator) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530115 javaGenName = ((YangJavaInputTranslator) info).getParent().getName() +
janani b3c396f02016-09-27 18:45:23 +0530116 INPUT_KEYWORD;
117 } else if (info instanceof YangJavaOutputTranslator) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530118 javaGenName = ((YangJavaOutputTranslator) info).getParent().getName() +
janani b3c396f02016-09-27 18:45:23 +0530119 OUTPUT_KEYWORD;
120 } else {
121 javaGenName = ((YangNode) info).getName();
122 }
123 info.getJavaFileInfo().setJavaName(getCamelCase(
124 javaGenName, config.getConflictResolver()));
125 }
126
127 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530128 * Updates YANG java file package information.
129 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530130 * @param info YANG java file info node
131 * @param config YANG plugin config
Bharat saraswal8beac342016-08-04 02:00:03 +0530132 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530133 private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo info,
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530134 YangPluginConfig config) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530135
136 JavaFileInfoTranslator translator = info.getJavaFileInfo();
137
138 translator.setJavaName(getAugmentClassName(
139 (YangJavaAugmentTranslator) info, config));
140 translator.setPackage(getAugmentsNodePackage((YangNode) info, config));
141 updateCommonPackageInfo(translator, info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530142 }
143
144 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530145 * Returns package for augment node.
146 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530147 * @param yangNode augment node
148 * @param config plugin configurations
Bharat saraswal8beac342016-08-04 02:00:03 +0530149 * @return package for augment node
150 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530151 private static String getAugmentsNodePackage(YangNode yangNode,
152 YangPluginConfig config) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530153 YangAugment augment = (YangAugment) yangNode;
154 StringBuilder augmentPkg = new StringBuilder();
155 augmentPkg.append(getCurNodePackage(augment));
156
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530157 StringBuilder pkg = new StringBuilder();
158 pkg.append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530159 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530160 pkg.append(getCamelCase(atomicPath.getNodeIdentifier().getName(),
161 config.getConflictResolver()))
162 .append(PERIOD);
Bharat saraswal8beac342016-08-04 02:00:03 +0530163 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530164 augmentPkg.append(trimAtLast(pkg.toString(), PERIOD).toLowerCase());
Bharat saraswal8beac342016-08-04 02:00:03 +0530165 return augmentPkg.toString();
166 }
167
168 /**
Gaurav Agrawal26390042016-04-12 13:30:16 +0530169 * Updates YANG java file package information for specified package.
170 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530171 * @param info YANG java file info node
172 * @param config YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +0530173 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530174 private static void updatePackageInfo(JavaCodeGeneratorInfo info,
175 YangPluginConfig config,
176 String pkg) {
177
178 JavaFileInfoTranslator translator = info.getJavaFileInfo();
179 translator.setJavaName(getCamelCase(((YangNode) info).getName(),
180 config.getConflictResolver()));
181 translator.setPackage(pkg);
182 updateCommonPackageInfo(translator, info, config);
183 }
184
185 /**
186 * Updates common package information.
187 *
188 * @param translator JAVA file info translator
189 * @param info YANG java file info node
190 * @param config YANG plugin config
191 */
192 private static void updateCommonPackageInfo(JavaFileInfoTranslator translator,
193 JavaCodeGeneratorInfo info,
194 YangPluginConfig config) {
195 translator.setPackageFilePath(getPackageDirPathFromJavaJPackage(
196 info.getJavaFileInfo().getPackage()));
197 translator.setBaseCodeGenPath(config.getCodeGenDir());
198 translator.setPluginConfig(config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530199 }
200
201 /**
202 * Updates temporary java code fragment files.
203 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530204 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530205 * @throws IOException IO operations fails
206 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530207 private static void createTempFragmentFile(JavaCodeGeneratorInfo info)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530208 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530209 info.setTempJavaCodeFragmentFiles(
210 new TempJavaCodeFragmentFiles(info.getJavaFileInfo()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530211 }
212
213 /**
214 * Updates leaf information in temporary java code fragment files.
215 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530216 * @param info YANG java file info node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530217 * @throws IOException IO operations fails
218 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530219 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo info,
220 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530221 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530222
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530223 TempJavaCodeFragmentFiles translator =
224 info.getTempJavaCodeFragmentFiles();
225
226 if (info instanceof RpcNotificationContainer) {
227 getBeanFiles(info).setRootNode(true);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530228 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530229 * Module / sub module node code generation.
230 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530231 if (info instanceof YangJavaModuleTranslator) {
232 if (!((YangJavaModuleTranslator) info).getNotificationNodes()
233 .isEmpty()) {
234 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530235 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530236 } else if (info instanceof YangJavaSubModuleTranslator) {
237 if (!((YangJavaSubModuleTranslator) info).getNotificationNodes()
238 .isEmpty()) {
239 updateNotificationNodeInfo(info, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530240 }
241 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530242 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530243 if (info instanceof YangLeavesHolder) {
Bharat saraswal94844d62016-10-13 13:28:03 +0530244 YangLeavesHolder holder = (YangLeavesHolder) info;
245 boolean isLeafPresent = holder.getListOfLeaf() != null && !holder
246 .getListOfLeaf().isEmpty();
247 boolean isLeafListPresent = holder.getListOfLeafList() != null &&
248 !holder.getListOfLeafList().isEmpty();
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530249 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530250 * Container
251 * Case
252 * Grouping
253 * Input
254 * List
255 * Notification
256 * Output
257 */
Bharat saraswal94844d62016-10-13 13:28:03 +0530258 if (isLeafPresent || isLeafListPresent) {
259 getBeanFiles(info).addCurNodeLeavesInfoToTempFiles((YangNode) info,
260 config);
261 }
262 //Add value leaf flag attribute to temp file.
263 if (isLeafPresent) {
264 getBeanFiles(info).addValueLeafFlag(config, (YangNode) info);
265 }
266 if (((YangNode) info).isOpTypeReq()) {
267 // Add operation type as an attribute.
268 getBeanFiles(info).addOperationTypeToTempFiles((YangNode) info,
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530269 config);
Bharat saraswal94844d62016-10-13 13:28:03 +0530270 if (isLeafPresent) {
271 //Add select leaf flag attribute to temp file.
272 getBeanFiles(info).addSelectLeafFlag(config);
273 }
274 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530275 } else if (info instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530276 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530277 * Typedef
278 * Union
279 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530280 translator.addTypeInfoToTempFiles((YangTypeHolder) info, config);
281 } else if (info instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530282 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530283 * Enumeration
284 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530285 translator.getEnumTempFiles()
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530286 .addEnumAttributeToTempFiles((YangNode) info, config);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530287 } else if (!(info instanceof YangChoice)) {
288 /*Do nothing, only the interface needs to be generated for choice*/
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530289 throw new TranslatorException(
290 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
291 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530292 }
293 }
294
295 /**
296 * Process generate code entry of YANG node.
297 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530298 * @param info YANG java file info node
299 * @param config plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530300 * @throws IOException IO operations fails
301 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530302 private static void generateTempFiles(JavaCodeGeneratorInfo info,
303 YangPluginConfig config)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530304 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530305 if (!(info instanceof YangNode)) {
306 throw new TranslatorException(
307 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
308 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530309 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530310 createTempFragmentFile(info);
311 updateTempFragmentFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530312 }
313
314 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530315 * Updates notification node info in service temporary file.
316 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530317 * @param info java code generator info
318 * @param config plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530319 * @throws IOException when fails to do IO operations
320 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530321 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo info,
322 YangPluginConfig config)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530323 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530324 TempJavaCodeFragmentFiles translator =
325 info.getTempJavaCodeFragmentFiles();
326 if (info instanceof YangJavaModuleTranslator) {
327 for (YangNode notification : ((YangJavaModuleTranslator) info)
328 .getNotificationNodes()) {
329 translator.getEventFragmentFiles()
330 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530331 }
332 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530333 if (info instanceof YangJavaSubModuleTranslator) {
334 for (YangNode notification : ((YangJavaSubModuleTranslator) info)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530335 .getNotificationNodes()) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530336 translator.getEventFragmentFiles()
337 .addJavaSnippetOfEvent(notification, config);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530338 }
339 }
340 }
341
342 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530343 * Generates code for the current ata model node and adds itself as an
344 * attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530345 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530346 * @param info YANG java file info node
347 * @param config YANG plugin config
348 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530349 * @throws IOException IO operations fails
350 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530351 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo info,
352 YangPluginConfig config,
353 boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530354 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530355 if (!(info instanceof YangNode)) {
356 throw new TranslatorException(
357 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
358 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530359 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530360
Gaurav Agrawald30f79b2016-10-06 00:58:57 +0530361 /*
362 * For second level and below cloned nodes code shouldn't be
363 * generated also they needn't be added in parent, since
364 * generated code will be under grouping, cloned node is only
365 * used for YANG namespace.
366 */
367 YangNode n = (YangNode) info;
368 if (n.getReferredSchema() != null &&
369 !(((YangNode) n.getReferredSchema()).getParent() instanceof
370 YangGrouping)) {
371 return;
372 }
373 /*
374 * If first level cloned node, then it needs to be imported in the
375 * generated code. In case uses under grouping, it would further have
376 * second level uses, hence needn't add in parent grouping.
377 */
378 YangSchemaNode rn = getRefSchema(info);
379 if (rn != null) {
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530380 YangNode parent = ((YangNode) info).getParent();
381 if (!(parent instanceof YangGrouping)) {
Gaurav Agrawald30f79b2016-10-06 00:58:57 +0530382 addCurNodeInfoInParentTempFile((YangNode) rn, isMultiInstance,
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530383 config, parent);
384 }
385 return;
386 }
387
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530388 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530389 * Generate the Java files corresponding to the current node.
390 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530391 generateCodeOfAugmentableNode(info, config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530392
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530393 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530394 * Update the current nodes info in its parent nodes generated files.
395 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530396 addCurNodeInfoInParentTempFile((YangNode) info, isMultiInstance,
397 config);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530398 }
399
400 /**
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530401 * Returns referred schema node in case of grouping uses.
402 *
403 * @param info YANG java file info node
404 * @return referred schema node
405 */
406 private static YangSchemaNode getRefSchema(JavaCodeGeneratorInfo info) {
407
Bharat saraswal94844d62016-10-13 13:28:03 +0530408 YangSchemaNode node = (YangSchemaNode) info;
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530409 if (node.getReferredSchema() == null) {
410 return null;
411 }
412
413 /*
414 * Obtain last referred node in case grouping is embedded inside
415 * another grouping.
416 */
417 while (node.getReferredSchema() != null) {
418 node = node.getReferredSchema();
419 }
420 return node;
421 }
422
423 /**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530424 * Generates code for the current data model node and adds support for it to
425 * be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530426 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530427 * @param info YANG java file info node
428 * @param config YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530429 * @throws IOException IO operations fails
430 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530431 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo info,
432 YangPluginConfig config)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530433 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530434 if (!(info instanceof YangNode)) {
435 throw new TranslatorException(
436 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
437 info));
Vidyashree Rama13960652016-04-26 15:06:06 +0530438 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530439
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530440 generateCodeOfNode(info, config);
441 TempJavaCodeFragmentFiles tempFiles =
442 info.getTempJavaCodeFragmentFiles();
Bharat saraswal94844d62016-10-13 13:28:03 +0530443 if (!(info instanceof YangChoice)) {
444 getBeanFiles(info).addYangAugmentedMap(config);
445 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530446 if (info instanceof YangCase) {
447 YangNode parent = ((YangCase) info).getParent();
448 JavaQualifiedTypeInfoTranslator typeInfo =
449 getQualifierInfoForCasesParent(parent, config);
450 getBeanFiles(info).getJavaExtendsListHolder()
451 .addToExtendsList(typeInfo, (YangNode) info,
452 tempFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530453
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530454 getBeanFiles(info).addParentInfoInCurNodeTempFile((YangNode) info,
455 config);
Vidyashree Rama13960652016-04-26 15:06:06 +0530456 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530457 }
458
459 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530460 * Returns cases parent's qualified info.
461 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530462 * @param parent parent node
463 * @param config plugin configuration
Bharat saraswale50edca2016-08-05 01:58:25 +0530464 * @return cases parent's qualified info
465 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530466 public static JavaQualifiedTypeInfoTranslator
467 getQualifierInfoForCasesParent(YangNode parent,
468 YangPluginConfig config) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530469 String parentName;
470 String parentPkg;
Bharat saraswale50edca2016-08-05 01:58:25 +0530471 JavaFileInfoTranslator parentInfo;
472 if (parent instanceof YangChoice) {
473 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
474 } else {
475 parent = ((YangAugment) parent).getAugmentedNode();
476 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
477 }
478 if (parentInfo.getPackage() != null) {
479 parentName = getCapitalCase(parentInfo.getJavaName());
480 parentPkg = parentInfo.getPackage();
481 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530482 parentName = getCapitalCase(getCamelCase(parent.getName(),
483 config.getConflictResolver()));
484 parentPkg = getNodesPackage(parent, config);
Bharat saraswale50edca2016-08-05 01:58:25 +0530485 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530486 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo =
487 new JavaQualifiedTypeInfoTranslator();
488 qualifiedTypeInfo.setClassInfo(parentName);
489 qualifiedTypeInfo.setPkgInfo(parentPkg);
490 return qualifiedTypeInfo;
Bharat saraswale50edca2016-08-05 01:58:25 +0530491 }
492
493 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530494 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530495 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530496 * @param info YANG java file info node
497 * @param config YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530498 * @throws IOException IO operations fails
499 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530500 public static void generateCodeOfNode(JavaCodeGeneratorInfo info,
501 YangPluginConfig config)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530502 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530503 if (!(info instanceof YangNode)) {
504 throw new TranslatorException(
505 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
506 info));
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530507 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530508 updatePackageInfo(info, config);
509 generateTempFiles(info, config);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530510 }
511
512 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530513 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530514 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530515 * @param info YANG java file info node
516 * @param config YANG plugin config
517 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530518 * @throws IOException IO operations fails
519 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530520 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo info,
521 YangPluginConfig config,
522 String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530523 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530524 if (!(info instanceof YangNode)) {
525 throw new TranslatorException(
526 getErrorMsgForCodeGenerator(INVALID_TRANSLATION_NODE,
527 info));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530528 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530529 updatePackageInfo(info, config, rootPkg);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530530
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530531 if (isRpcChildNodePresent((YangNode) info)) {
532 info.getJavaFileInfo()
533 .addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530534 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530535 generateTempFiles(info, config);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530536 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530537
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530538 /**
539 * Returns the node package string.
540 *
541 * @param curNode current java node whose package string needs to be set
542 * @return returns the root package string
543 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530544 public static String getCurNodePackage(YangNode curNode)
545 throws TranslatorException {
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530546
547 String pkg;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530548 if (!(curNode instanceof JavaFileInfoContainer) ||
549 curNode.getParent() == null) {
550 throw new TranslatorException(getErrorMsg(INVALID_NODE, curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530551 }
552
553 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
554 if (!(parentNode instanceof JavaFileInfoContainer)) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530555 throw new TranslatorException(getErrorMsg(INVALID_PARENT_NODE,
556 curNode));
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530557 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530558 JavaFileInfoTranslator handle =
559 ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
560 pkg = handle.getPackage() + PERIOD + handle.getJavaName();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530561 return pkg.toLowerCase();
562 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530563
564 /**
565 * Returns true if root node contains any data node.
566 *
567 * @param node root YANG node
568 * @return true if root node contains any data node
569 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530570 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530571 return isNodeCodeGenRequired(node, true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530572 }
573
574 /**
Bharat saraswal1edde622016-09-06 10:18:04 +0530575 * Returns true if get/set method of root node are required.
Bharat saraswale3175d32016-08-31 17:50:11 +0530576 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530577 * @param node root node
Bharat saraswal1edde622016-09-06 10:18:04 +0530578 * @return true if get/set method of root node are required
Bharat saraswale3175d32016-08-31 17:50:11 +0530579 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530580 public static boolean isGetSetOfRootNodeRequired(YangNode node) {
581 return isNodeCodeGenRequired(node, false);
582 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530583
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530584 /**
585 * Returns true if either get/set method of root node are required or root
586 * node contains any data node. This check is done depending on the
587 * boolean parameter indicating whether check to be performed for root
588 * node code generation or get/set method generation.
589 *
590 * @param node root node
591 * @param rootNodeGen flag indicating check type
592 * @return true if check pass, false otherwise
593 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530594 private static boolean isNodeCodeGenRequired(YangNode node,
595 boolean rootNodeGen) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530596 YangLeavesHolder holder = (YangLeavesHolder) node;
597
598 if (!holder.getListOfLeaf().isEmpty()) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530599 return true;
Bharat saraswale3175d32016-08-31 17:50:11 +0530600 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530601 if (!holder.getListOfLeafList().isEmpty()) {
602 return true;
603 }
604 node = node.getChild();
605 if (node == null) {
606 return false;
607 }
608
609 if (rootNodeGen) {
610 while (node != null) {
611 if (!(node instanceof YangTranslatorOperatorNode)) {
612 return true;
613 }
614 node = node.getNextSibling();
615 }
616 return false;
617 }
618 while (node != null) {
619 if (!(node instanceof YangAugment)) {
620 return true;
621 }
622 node = node.getNextSibling();
623 }
624 return false;
Bharat saraswale3175d32016-08-31 17:50:11 +0530625 }
626
627 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530628 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530629 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530630 * @param node YANG node
631 * @param config plugin config
Bharat saraswalaf413b82016-07-14 15:18:20 +0530632 * @return java package
633 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530634 public static String getNodesPackage(YangNode node,
635 YangPluginConfig config) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530636
637 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530638 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530639 if (node instanceof YangJavaAugmentTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530640 clsInfo.add(getAugmentClassName((YangAugment) node,
641 config));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530642 } else {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530643 clsInfo.add(getCamelCase(node.getName(), config
644 .getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530645 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530646 node = node.getParent();
647 }
648
649 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530650 if (node instanceof YangJavaModuleTranslator) {
651 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawal493e0a82016-09-28 18:30:08 +0530652 pkg.append(getRootPackage(module.getVersion(),
653 module.getModuleNamespace(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530654 module.getRevision(),
655 config.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530656 } else if (node instanceof YangJavaSubModuleTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530657 YangJavaSubModuleTranslator subModule =
658 (YangJavaSubModuleTranslator) node;
Bharat saraswal8beac342016-08-04 02:00:03 +0530659 pkg.append(getRootPackage(subModule.getVersion(),
Bharat saraswal1edde622016-09-06 10:18:04 +0530660 subModule.getNameSpaceFromModule(),
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530661 subModule.getRevision(),
662 config.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530663 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530664 pkg.append(EMPTY_STRING);
665 int size = clsInfo.size();
666 for (int i = size - 1; i >= 0; i--) {
667 pkg.append(PERIOD).append(clsInfo.get(i));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530668 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530669 return pkg.toString().toLowerCase();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530670 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530671
672 /**
673 * Returns augment class name.
674 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530675 * @param augment YANG augment
676 * @param config plugin configurations
Bharat saraswal039f59c2016-07-14 21:57:13 +0530677 * @return augment class name
678 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530679 private static String getAugmentClassName(YangAugment augment,
680 YangPluginConfig config) {
681 YangNodeIdentifier identifier =
682 augment.getTargetNode().get(augment.getTargetNode().size() - 1)
683 .getNodeIdentifier();
684 String name = getCapitalCase(getCamelCase(identifier.getName(),
685 config.getConflictResolver()));
686 if (identifier.getPrefix() != null) {
Bharat saraswal94844d62016-10-13 13:28:03 +0530687 return getCapitalCase(getCamelCase(AUGMENTED + HYPHEN + identifier
688 .getPrefix(),
689 config.getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530690 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530691 return AUGMENTED + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530692 }
Gaurav Agrawald30f79b2016-10-06 00:58:57 +0530693
694 /**
695 * Generated java code during exit.
696 *
697 * @param type generated file type
698 * @param node current YANG node
699 * @throws IOException when fails to generate java files
700 */
Bharat saraswal94844d62016-10-13 13:28:03 +0530701 public static void generateJava(int type, YangNode node)
Gaurav Agrawald30f79b2016-10-06 00:58:57 +0530702 throws IOException {
703 /*
704 * Call for file generation if node is not under uses.
705 */
Bharat saraswal94844d62016-10-13 13:28:03 +0530706 if (node.getReferredSchema() == null) {
Gaurav Agrawald30f79b2016-10-06 00:58:57 +0530707 ((TempJavaCodeFragmentFilesContainer) node)
Bharat saraswal94844d62016-10-13 13:28:03 +0530708 .getTempJavaCodeFragmentFiles().generateJavaFile(type, node);
Gaurav Agrawald30f79b2016-10-06 00:58:57 +0530709 }
710 }
Bharat saraswal94844d62016-10-13 13:28:03 +0530711
712 /**
713 * Generates interface file for those yang file which contains only any
714 * of these grouping, typedef and identity.
715 *
716 * @param rootNode root node
717 * @throws IOException when fails to do IO operations
718 */
719 public static void generateInterfaceFileForNonDataNodes(YangNode rootNode) throws
720 IOException {
721 JavaCodeGeneratorInfo info = (JavaCodeGeneratorInfo) rootNode;
722 TempJavaCodeFragmentFiles tempFile = info
723 .getTempJavaCodeFragmentFiles();
724 JavaFileInfoTranslator fileInfo = info.getJavaFileInfo();
725 File filePath = new File(fileInfo.getBaseCodeGenPath() + fileInfo
726 .getPackageFilePath());
727 String name = getCapitalCase(fileInfo.getJavaName());
728 //Add package info file for this.
729 addPackageInfo(filePath, name, fileInfo.getPackage(), false);
730 //Generate file handle for this.
731 File interFace = tempFile.getBeanTempFiles().getJavaFileHandle(
732 name);
733 //generate java code for interface file.
734 validateLineLength(generateInterfaceFile(interFace, null, rootNode,
735 false));
736 insertDataIntoJavaFile(interFace, methodClose(FOUR_SPACE));
737 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530738}