blob: 7891b087110d11ee3e4a2ce2890ee2c795cf934f [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
19import java.io.IOException;
Bharat saraswald14cbe82016-07-14 13:26:18 +053020import java.util.ArrayList;
21import java.util.List;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053022
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053023import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswal8beac342016-08-04 02:00:03 +053024import org.onosproject.yangutils.datamodel.YangAtomicPath;
Bharat saraswal039f59c2016-07-14 21:57:13 +053025import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswale2bc60d2016-04-16 02:28:25 +053026import org.onosproject.yangutils.datamodel.YangCase;
27import org.onosproject.yangutils.datamodel.YangChoice;
Gaurav Agrawal26390042016-04-12 13:30:16 +053028import org.onosproject.yangutils.datamodel.YangLeavesHolder;
29import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal039f59c2016-07-14 21:57:13 +053030import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Bharat saraswal039f59c2016-07-14 21:57:13 +053031import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053032import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053033import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053034import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053035import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugmentTranslator;
36import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumerationTranslator;
37import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
38import 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
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053041import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
42import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053043import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswald14cbe82016-07-14 13:26:18 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswal039f59c2016-07-14 21:57:13 +053045import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053046import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
47import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
48import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
49import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053050import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Gaurav Agrawal26390042016-04-12 13:30:16 +053051
52/**
53 * Represents utility class for YANG java model.
54 */
55public final class YangJavaModelUtils {
56
57 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +053058 * Creates an instance of YANG java model utility.
Gaurav Agrawal26390042016-04-12 13:30:16 +053059 */
60 private YangJavaModelUtils() {
61 }
62
63 /**
64 * Updates YANG java file package information.
65 *
Vinod Kumar S79a374b2016-04-30 21:09:15 +053066 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053067 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +053068 * @throws IOException IO operations fails
69 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053070 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +053071 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +053072 throws IOException {
Shankara-Huaweib7564772016-08-02 18:13:13 +053073 if (javaCodeGeneratorInfo instanceof YangJavaAugmentTranslator) {
Bharat saraswal8beac342016-08-04 02:00:03 +053074 updatePackageForAugmentInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswald14cbe82016-07-14 13:26:18 +053075 } else {
76 javaCodeGeneratorInfo.getJavaFileInfo()
77 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
Bharat saraswal1edde622016-09-06 10:18:04 +053078 yangPluginConfig.getConflictResolver()));
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053079 javaCodeGeneratorInfo.getJavaFileInfo().setJavaAttributeName(javaCodeGeneratorInfo
Bharat saraswal1edde622016-09-06 10:18:04 +053080 .getJavaFileInfo().getJavaName());
Bharat saraswal8beac342016-08-04 02:00:03 +053081 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
Bharat saraswald14cbe82016-07-14 13:26:18 +053082 }
Bharat saraswal8beac342016-08-04 02:00:03 +053083 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
84 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
85
86 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
87 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
88
89 }
90
91 /**
92 * Updates YANG java file package information.
93 *
94 * @param javaCodeGeneratorInfo YANG java file info node
95 * @param yangPluginConfig YANG plugin config
96 * @throws IOException IO operations fails
97 */
98 private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
99 YangPluginConfig yangPluginConfig)
100 throws IOException {
101 javaCodeGeneratorInfo.getJavaFileInfo()
102 .setJavaName(getAugmentClassName((YangJavaAugmentTranslator) javaCodeGeneratorInfo,
Bharat saraswal1edde622016-09-06 10:18:04 +0530103 yangPluginConfig));
Bharat saraswal8beac342016-08-04 02:00:03 +0530104 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(
105 getAugmentsNodePackage((YangNode) javaCodeGeneratorInfo, yangPluginConfig));
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530106 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
107 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530108 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530109 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530110 }
111
112 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530113 * Returns package for augment node.
114 *
115 * @param yangNode augment node
116 * @param yangPluginConfig plugin configurations
117 * @return package for augment node
118 */
119 private static String getAugmentsNodePackage(YangNode yangNode, YangPluginConfig yangPluginConfig) {
120 YangAugment augment = (YangAugment) yangNode;
121 StringBuilder augmentPkg = new StringBuilder();
122 augmentPkg.append(getCurNodePackage(augment));
123
124 String pkg = PERIOD;
125 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
126 pkg = pkg + getCamelCase(atomicPath.getNodeIdentifier().getName(), yangPluginConfig.getConflictResolver())
127 + PERIOD;
128 }
129 pkg = trimAtLast(pkg, PERIOD);
130 augmentPkg.append(pkg.toLowerCase());
131 return augmentPkg.toString();
132 }
133
134 /**
Gaurav Agrawal26390042016-04-12 13:30:16 +0530135 * Updates YANG java file package information for specified package.
136 *
137 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530138 * @param yangPlugin YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +0530139 * @throws IOException IO operations fails
140 */
janani b1c6acc42016-04-15 16:18:30 +0530141 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Bharat saraswal1edde622016-09-06 10:18:04 +0530142 String pkg)
143 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530144 javaCodeGeneratorInfo.getJavaFileInfo()
janani b703cfe42016-05-17 13:12:22 +0530145 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
Bharat saraswal1edde622016-09-06 10:18:04 +0530146 yangPlugin.getConflictResolver()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530147 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
148 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
149 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
Bharat saraswal8beac342016-08-04 02:00:03 +0530150 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530151 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530152 }
153
154 /**
155 * Updates temporary java code fragment files.
156 *
157 * @param javaCodeGeneratorInfo YANG java file info node
158 * @throws IOException IO operations fails
159 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530160 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
161 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530162 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530163 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530164 }
165
166 /**
167 * Updates leaf information in temporary java code fragment files.
168 *
169 * @param javaCodeGeneratorInfo YANG java file info node
170 * @throws IOException IO operations fails
171 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530172 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530173 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530174 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530175
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530176 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530177 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().setRooNode(true);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530178 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530179 * Module / sub module node code generation.
180 */
Gaurav Agrawal26390042016-04-12 13:30:16 +0530181 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530182 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530183 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweib7564772016-08-02 18:13:13 +0530184 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
185 if (!((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
186 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530187 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530188 } else if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
189 if (!((YangJavaSubModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
190 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530191 }
192 }
193
Bharat saraswal64e7e232016-07-14 23:33:55 +0530194 }
195 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530196 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530197 * Container
198 * Case
199 * Grouping
200 * Input
201 * List
202 * Notification
203 * Output
204 */
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530205 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530206 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530207 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530208 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530209 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530210 * Typedef
211 * Union
212 */
213 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530214 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweib7564772016-08-02 18:13:13 +0530215 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530216 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530217 * Enumeration
218 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530219 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530220 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
221
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530222 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
223 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal26390042016-04-12 13:30:16 +0530224 } else {
Bharat saraswale3175d32016-08-31 17:50:11 +0530225 throw new TranslatorException("Unsupported Node Translation "
Bharat saraswal1edde622016-09-06 10:18:04 +0530226 + javaCodeGeneratorInfo.getLineNumber() + " at " +
227 javaCodeGeneratorInfo.getCharPosition()
228 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal26390042016-04-12 13:30:16 +0530229 }
230 }
231
232 /**
233 * Process generate code entry of YANG node.
234 *
235 * @param javaCodeGeneratorInfo YANG java file info node
Bharat saraswalaf413b82016-07-14 15:18:20 +0530236 * @param yangPluginConfig plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530237 * @throws IOException IO operations fails
238 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530239 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +0530240 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530241 throws IOException {
242 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530243 throw new TranslatorException("translation is not supported for the node "
Bharat saraswal1edde622016-09-06 10:18:04 +0530244 + javaCodeGeneratorInfo.getLineNumber() + " at " +
245 javaCodeGeneratorInfo.getCharPosition()
246 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal26390042016-04-12 13:30:16 +0530247 }
248 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530249 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530250
251 }
252
253 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530254 * Updates notification node info in service temporary file.
255 *
256 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530257 * @param yangPluginConfig plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530258 * @throws IOException when fails to do IO operations
259 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530260 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530261 YangPluginConfig yangPluginConfig)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530262 throws IOException {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530263 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
264 for (YangNode notification : ((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes()) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530265 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal039f59c2016-07-14 21:57:13 +0530266 .getEventFragmentFiles()
Shankara-Huaweib7564772016-08-02 18:13:13 +0530267 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530268 }
269 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530270 if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
271 for (YangNode notification : ((YangJavaSubModuleTranslator) javaCodeGeneratorInfo)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530272 .getNotificationNodes()) {
273 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal039f59c2016-07-14 21:57:13 +0530274 .getEventFragmentFiles()
Shankara-Huaweib7564772016-08-02 18:13:13 +0530275 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530276 }
277 }
278 }
279
280 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530281 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530282 *
283 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530284 * @param yangPlugin YANG plugin config
285 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530286 * @throws IOException IO operations fails
287 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530288 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530289 YangPluginConfig yangPlugin, boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530290 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530291 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530292 throw new TranslatorException("Invalid node for translation " +
Bharat saraswal1edde622016-09-06 10:18:04 +0530293 javaCodeGeneratorInfo.getLineNumber() + " at " +
294 javaCodeGeneratorInfo.getCharPosition()
295 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal26390042016-04-12 13:30:16 +0530296 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530297
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530298 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530299 * Generate the Java files corresponding to the current node.
300 */
301 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
302
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530303 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530304 * Update the current nodes info in its parent nodes generated files.
305 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530306 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530307 }
308
309 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530310 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530311 *
312 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530313 * @param yangPlugin YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530314 * @throws IOException IO operations fails
315 */
316 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530317 YangPluginConfig yangPlugin)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530318 throws IOException {
319 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530320 throw new TranslatorException("invalid node for translation " +
Bharat saraswal1edde622016-09-06 10:18:04 +0530321 javaCodeGeneratorInfo.getLineNumber() + " at " +
322 javaCodeGeneratorInfo.getCharPosition()
323 + " in " + javaCodeGeneratorInfo.getFileName());
Vidyashree Rama13960652016-04-26 15:06:06 +0530324 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530325
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530326 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530327 TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530328
Vidyashree Rama13960652016-04-26 15:06:06 +0530329 if (javaCodeGeneratorInfo instanceof YangCase) {
330 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswale50edca2016-08-05 01:58:25 +0530331 JavaQualifiedTypeInfoTranslator parentsInfo = getQualifierInfoForCasesParent(parent, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530332 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalaf413b82016-07-14 15:18:20 +0530333 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo,
Bharat saraswal1edde622016-09-06 10:18:04 +0530334 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530335
336 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530337 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530338
Vidyashree Rama13960652016-04-26 15:06:06 +0530339 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530340 }
341
342 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530343 * Returns cases parent's qualified info.
344 *
345 * @param parent parent node
346 * @param yangPluginConfig plugin configuration
347 * @return cases parent's qualified info
348 */
349 public static JavaQualifiedTypeInfoTranslator getQualifierInfoForCasesParent(YangNode parent,
350 YangPluginConfig yangPluginConfig) {
351 String parentName;
352 String parentPkg;
353 JavaQualifiedTypeInfoTranslator parentsInfo = new JavaQualifiedTypeInfoTranslator();
354 JavaFileInfoTranslator parentInfo;
355 if (parent instanceof YangChoice) {
356 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
357 } else {
358 parent = ((YangAugment) parent).getAugmentedNode();
359 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
360 }
361 if (parentInfo.getPackage() != null) {
362 parentName = getCapitalCase(parentInfo.getJavaName());
363 parentPkg = parentInfo.getPackage();
364 } else {
365 parentName = getCapitalCase(getCamelCase(parent.getName(), yangPluginConfig.getConflictResolver()));
366 parentPkg = getNodesPackage(parent, yangPluginConfig);
367 }
368 parentsInfo.setClassInfo(parentName);
369 parentsInfo.setPkgInfo(parentPkg);
370 return parentsInfo;
371
372 }
373
374 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530375 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530376 *
377 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530378 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530379 * @throws IOException IO operations fails
380 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530381 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530382 YangPluginConfig yangPluginConfig)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530383 throws IOException {
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530384 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530385 throw new TranslatorException("invalid node for translation "
Bharat saraswal1edde622016-09-06 10:18:04 +0530386 + javaCodeGeneratorInfo.getLineNumber() + " at " +
387 javaCodeGeneratorInfo.getCharPosition()
388 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530389 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530390 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
391 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530392 }
393
394 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530395 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530396 *
397 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530398 * @param yangPluginConfig YANG plugin config
399 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530400 * @throws IOException IO operations fails
401 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530402 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530403 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530404 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530405 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530406 throw new TranslatorException("invalid node for translation " + javaCodeGeneratorInfo.getLineNumber()
Bharat saraswal1edde622016-09-06 10:18:04 +0530407 + " at " +
408 javaCodeGeneratorInfo.getCharPosition()
409 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal26390042016-04-12 13:30:16 +0530410 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530411 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
412
413 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
414 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
415 }
416
417 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530418 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530419
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530420 /**
421 * Returns the node package string.
422 *
423 * @param curNode current java node whose package string needs to be set
424 * @return returns the root package string
425 */
426 public static String getCurNodePackage(YangNode curNode) {
427
428 String pkg;
429 if (!(curNode instanceof JavaFileInfoContainer)
430 || curNode.getParent() == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530431 throw new TranslatorException("missing parent node to get current node's package " +
Bharat saraswal1edde622016-09-06 10:18:04 +0530432 curNode.getName() + " in " +
433 curNode.getLineNumber() + " at " +
434 curNode.getCharPosition()
435 + " in " + curNode.getFileName());
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530436 }
437
438 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
439 if (!(parentNode instanceof JavaFileInfoContainer)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530440 throw new TranslatorException("missing parent java node to get current node's package " +
Bharat saraswal1edde622016-09-06 10:18:04 +0530441 curNode.getName() + " in " +
442 curNode.getLineNumber() + " at " +
443 curNode.getCharPosition()
444 + " in " + curNode.getFileName());
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530445 }
Bharat saraswale50edca2016-08-05 01:58:25 +0530446 JavaFileInfoTranslator parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530447 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
448 return pkg.toLowerCase();
449 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530450
451 /**
452 * Returns true if root node contains any data node.
453 *
454 * @param node root YANG node
455 * @return true if root node contains any data node
456 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530457 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Bharat saraswal1edde622016-09-06 10:18:04 +0530458 YangLeavesHolder holder = (YangLeavesHolder) node;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530459
Bharat saraswal1edde622016-09-06 10:18:04 +0530460 if (holder.getListOfLeaf().isEmpty()) {
461 if (holder.getListOfLeafList().isEmpty()) {
462 YangNode tempNode = node.getChild();
463 if (tempNode == null) {
464 return false;
465 }
466 while (tempNode != null) {
467 if (!(tempNode instanceof YangTranslatorOperatorNode)) {
468 return true;
469 }
470 tempNode = tempNode.getNextSibling();
471 }
472 return false;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530473 } else {
Bharat saraswal1edde622016-09-06 10:18:04 +0530474 return true;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530475 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530476 } else {
477 return true;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530478 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530479 }
480
481 /**
Bharat saraswal1edde622016-09-06 10:18:04 +0530482 * Returns true if get/set method of root node are required.
Bharat saraswale3175d32016-08-31 17:50:11 +0530483 *
484 * @param curNode root node
Bharat saraswal1edde622016-09-06 10:18:04 +0530485 * @return true if get/set method of root node are required
Bharat saraswale3175d32016-08-31 17:50:11 +0530486 */
Bharat saraswal1edde622016-09-06 10:18:04 +0530487 public static boolean isGetSetOfRootNodeRequired(YangNode curNode) {
488 YangLeavesHolder holder = (YangLeavesHolder) curNode;
489
490 if (holder.getListOfLeaf().isEmpty()) {
491 if (holder.getListOfLeafList().isEmpty()) {
492 curNode = curNode.getChild();
493 if (curNode == null) {
494 return false;
495 }
496 while (curNode != null) {
497 if (!(curNode instanceof YangAugment)) {
498 return true;
499 }
500 curNode = curNode.getNextSibling();
501 }
502 return false;
Bharat saraswale3175d32016-08-31 17:50:11 +0530503 } else {
Bharat saraswal1edde622016-09-06 10:18:04 +0530504 return true;
Bharat saraswale3175d32016-08-31 17:50:11 +0530505 }
Bharat saraswal1edde622016-09-06 10:18:04 +0530506 } else {
507 return true;
Bharat saraswale3175d32016-08-31 17:50:11 +0530508 }
Bharat saraswale3175d32016-08-31 17:50:11 +0530509 }
510
511 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530512 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530513 *
Bharat saraswal8beac342016-08-04 02:00:03 +0530514 * @param node YANG node
Bharat saraswalaf413b82016-07-14 15:18:20 +0530515 * @param yangPluginConfig plugin config
516 * @return java package
517 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530518 public static String getNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530519
520 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530521 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530522 if (node instanceof YangJavaAugmentTranslator) {
523 clsInfo.add(getAugmentClassName((YangAugment) node, yangPluginConfig));
524 } else {
525 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530526 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530527 node = node.getParent();
528 }
529
530 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530531 if (node instanceof YangJavaModuleTranslator) {
532 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530533 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace(), module
Bharat saraswal1edde622016-09-06 10:18:04 +0530534 .getRevision(), yangPluginConfig.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530535 } else if (node instanceof YangJavaSubModuleTranslator) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530536 YangJavaSubModuleTranslator subModule = (YangJavaSubModuleTranslator) node;
537 pkg.append(getRootPackage(subModule.getVersion(),
Bharat saraswal1edde622016-09-06 10:18:04 +0530538 subModule.getNameSpaceFromModule(),
539 subModule.getRevision(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530540 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530541 String concat = "";
Bharat saraswalaf413b82016-07-14 15:18:20 +0530542 for (int i = 1; i <= clsInfo.size(); i++) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530543 concat = concat + "." + clsInfo.get(clsInfo.size() - i);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530544 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530545 pkg.append(concat);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530546 return pkg.toString().toLowerCase();
547
548 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530549
550 /**
551 * Returns augment class name.
552 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530553 * @param augment YANG augment
Bharat saraswal039f59c2016-07-14 21:57:13 +0530554 * @param yangPluginConfig plugin configurations
555 * @return augment class name
556 */
Bharat saraswal1edde622016-09-06 10:18:04 +0530557 private static String getAugmentClassName(YangAugment augment, YangPluginConfig yangPluginConfig) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530558 YangNodeIdentifier yangNodeIdentifier = augment.getTargetNode().get(augment.getTargetNode().size() - 1)
559 .getNodeIdentifier();
560 String name = getCapitalCase(getCamelCase(yangNodeIdentifier.getName(), yangPluginConfig
561 .getConflictResolver()));
562 if (yangNodeIdentifier.getPrefix() != null) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530563 return AUGMENTED + getCapitalCase(getCamelCase(yangNodeIdentifier.getPrefix(), yangPluginConfig
564 .getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530565 } else {
566 return AUGMENTED + name;
567 }
568 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530569
Gaurav Agrawal26390042016-04-12 13:30:16 +0530570}