blob: f89ad3d57466f044a01b20457a15bcaa512c9b7a [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;
Bharat saraswal8beac342016-08-04 02:00:03 +053029import org.onosproject.yangutils.datamodel.YangModule;
Gaurav Agrawal26390042016-04-12 13:30:16 +053030import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal039f59c2016-07-14 21:57:13 +053031import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Bharat saraswal8beac342016-08-04 02:00:03 +053032import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswal039f59c2016-07-14 21:57:13 +053033import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053034import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053035import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053036import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053037import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugmentTranslator;
38import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumerationTranslator;
39import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
40import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053041import org.onosproject.yangutils.utils.io.YangPluginConfig;
Gaurav Agrawal26390042016-04-12 13:30:16 +053042
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053043import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
44import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053045import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswald14cbe82016-07-14 13:26:18 +053046import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswal039f59c2016-07-14 21:57:13 +053047import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053048import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
49import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
50import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
51import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053052import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Gaurav Agrawal26390042016-04-12 13:30:16 +053053
54/**
55 * Represents utility class for YANG java model.
56 */
57public final class YangJavaModelUtils {
58
59 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +053060 * Creates an instance of YANG java model utility.
Gaurav Agrawal26390042016-04-12 13:30:16 +053061 */
62 private YangJavaModelUtils() {
63 }
64
65 /**
66 * Updates YANG java file package information.
67 *
Vinod Kumar S79a374b2016-04-30 21:09:15 +053068 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053069 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +053070 * @throws IOException IO operations fails
71 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053072 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +053073 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +053074 throws IOException {
Shankara-Huaweib7564772016-08-02 18:13:13 +053075 if (javaCodeGeneratorInfo instanceof YangJavaAugmentTranslator) {
Bharat saraswal8beac342016-08-04 02:00:03 +053076 updatePackageForAugmentInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswald14cbe82016-07-14 13:26:18 +053077 } else {
78 javaCodeGeneratorInfo.getJavaFileInfo()
79 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
80 yangPluginConfig.getConflictResolver()));
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053081 javaCodeGeneratorInfo.getJavaFileInfo().setJavaAttributeName(javaCodeGeneratorInfo
82 .getJavaFileInfo().getJavaName());
Bharat saraswal8beac342016-08-04 02:00:03 +053083 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
Bharat saraswald14cbe82016-07-14 13:26:18 +053084 }
Bharat saraswal8beac342016-08-04 02:00:03 +053085 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
86 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
87
88 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
89 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
90
91 }
92
93 /**
94 * Updates YANG java file package information.
95 *
96 * @param javaCodeGeneratorInfo YANG java file info node
97 * @param yangPluginConfig YANG plugin config
98 * @throws IOException IO operations fails
99 */
100 private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
101 YangPluginConfig yangPluginConfig)
102 throws IOException {
103 javaCodeGeneratorInfo.getJavaFileInfo()
104 .setJavaName(getAugmentClassName((YangJavaAugmentTranslator) javaCodeGeneratorInfo,
105 yangPluginConfig));
106 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(
107 getAugmentsNodePackage((YangNode) javaCodeGeneratorInfo, yangPluginConfig));
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530108 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
109 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530110 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530111 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530112 }
113
114 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530115 * Returns package for augment node.
116 *
117 * @param yangNode augment node
118 * @param yangPluginConfig plugin configurations
119 * @return package for augment node
120 */
121 private static String getAugmentsNodePackage(YangNode yangNode, YangPluginConfig yangPluginConfig) {
122 YangAugment augment = (YangAugment) yangNode;
123 StringBuilder augmentPkg = new StringBuilder();
124 augmentPkg.append(getCurNodePackage(augment));
125
126 String pkg = PERIOD;
127 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
128 pkg = pkg + getCamelCase(atomicPath.getNodeIdentifier().getName(), yangPluginConfig.getConflictResolver())
129 + PERIOD;
130 }
131 pkg = trimAtLast(pkg, PERIOD);
132 augmentPkg.append(pkg.toLowerCase());
133 return augmentPkg.toString();
134 }
135
136 /**
Gaurav Agrawal26390042016-04-12 13:30:16 +0530137 * Updates YANG java file package information for specified package.
138 *
139 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530140 * @param yangPlugin YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +0530141 * @throws IOException IO operations fails
142 */
janani b1c6acc42016-04-15 16:18:30 +0530143 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530144 String pkg) throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530145 javaCodeGeneratorInfo.getJavaFileInfo()
janani b703cfe42016-05-17 13:12:22 +0530146 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
147 yangPlugin.getConflictResolver()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530148 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
149 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
150 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
Bharat saraswal8beac342016-08-04 02:00:03 +0530151 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530152 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530153 }
154
155 /**
156 * Updates temporary java code fragment files.
157 *
158 * @param javaCodeGeneratorInfo YANG java file info node
159 * @throws IOException IO operations fails
160 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530161 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
162 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530163 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530164 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530165 }
166
167 /**
168 * Updates leaf information in temporary java code fragment files.
169 *
170 * @param javaCodeGeneratorInfo YANG java file info node
171 * @throws IOException IO operations fails
172 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530173 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530174 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530175 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530176
177 if (javaCodeGeneratorInfo instanceof YangModule
178 || javaCodeGeneratorInfo instanceof YangSubModule) {
179 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().setRooNode(true);
180 }
181
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530182 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530183 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530184 * Module / sub module node code generation.
185 */
Gaurav Agrawal26390042016-04-12 13:30:16 +0530186 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530187 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530188 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweib7564772016-08-02 18:13:13 +0530189 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
190 if (!((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
191 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530192 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530193 } else if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
194 if (!((YangJavaSubModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
195 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530196 }
197 }
198
Bharat saraswal64e7e232016-07-14 23:33:55 +0530199 }
200 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530201 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530202 * Container
203 * Case
204 * Grouping
205 * Input
206 * List
207 * Notification
208 * Output
209 */
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530210 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530211 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530212 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530213 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530214 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530215 * Typedef
216 * Union
217 */
218 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530219 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweib7564772016-08-02 18:13:13 +0530220 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530221 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530222 * Enumeration
223 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530224 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530225 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
226
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530227 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
228 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal26390042016-04-12 13:30:16 +0530229 } else {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530230 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530231 }
232 }
233
234 /**
235 * Process generate code entry of YANG node.
236 *
237 * @param javaCodeGeneratorInfo YANG java file info node
Bharat saraswalaf413b82016-07-14 15:18:20 +0530238 * @param yangPluginConfig plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530239 * @throws IOException IO operations fails
240 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530241 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +0530242 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530243 throws IOException {
244 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530245 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530246 }
247 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530248 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530249
250 }
251
252 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530253 * Updates notification node info in service temporary file.
254 *
255 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530256 * @param yangPluginConfig plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530257 * @throws IOException when fails to do IO operations
258 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530259 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530260 YangPluginConfig yangPluginConfig)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530261 throws IOException {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530262 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
263 for (YangNode notification : ((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes()) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530264 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal039f59c2016-07-14 21:57:13 +0530265 .getEventFragmentFiles()
Shankara-Huaweib7564772016-08-02 18:13:13 +0530266 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530267 }
268 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530269 if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
270 for (YangNode notification : ((YangJavaSubModuleTranslator) javaCodeGeneratorInfo)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530271 .getNotificationNodes()) {
272 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal039f59c2016-07-14 21:57:13 +0530273 .getEventFragmentFiles()
Shankara-Huaweib7564772016-08-02 18:13:13 +0530274 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530275 }
276 }
277 }
278
279 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530280 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530281 *
282 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530283 * @param yangPlugin YANG plugin config
284 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530285 * @throws IOException IO operations fails
286 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530287 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530288 YangPluginConfig yangPlugin, boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530289 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530290 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530291 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530292 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530293
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530294 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530295 * Generate the Java files corresponding to the current node.
296 */
297 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
298
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530299 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530300 * Update the current nodes info in its parent nodes generated files.
301 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530302 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530303 }
304
305 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530306 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530307 *
308 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530309 * @param yangPlugin YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530310 * @throws IOException IO operations fails
311 */
312 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530313 YangPluginConfig yangPlugin)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530314 throws IOException {
315 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
316 throw new TranslatorException("invalid node for translation");
Vidyashree Rama13960652016-04-26 15:06:06 +0530317 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530318
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530319 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530320 TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530321
Vidyashree Rama13960652016-04-26 15:06:06 +0530322 if (javaCodeGeneratorInfo instanceof YangCase) {
323 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswale50edca2016-08-05 01:58:25 +0530324 JavaQualifiedTypeInfoTranslator parentsInfo = getQualifierInfoForCasesParent(parent, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530325 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalaf413b82016-07-14 15:18:20 +0530326 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo,
327 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530328
329 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530330 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530331
Vidyashree Rama13960652016-04-26 15:06:06 +0530332 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530333 }
334
335 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530336 * Returns cases parent's qualified info.
337 *
338 * @param parent parent node
339 * @param yangPluginConfig plugin configuration
340 * @return cases parent's qualified info
341 */
342 public static JavaQualifiedTypeInfoTranslator getQualifierInfoForCasesParent(YangNode parent,
343 YangPluginConfig yangPluginConfig) {
344 String parentName;
345 String parentPkg;
346 JavaQualifiedTypeInfoTranslator parentsInfo = new JavaQualifiedTypeInfoTranslator();
347 JavaFileInfoTranslator parentInfo;
348 if (parent instanceof YangChoice) {
349 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
350 } else {
351 parent = ((YangAugment) parent).getAugmentedNode();
352 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
353 }
354 if (parentInfo.getPackage() != null) {
355 parentName = getCapitalCase(parentInfo.getJavaName());
356 parentPkg = parentInfo.getPackage();
357 } else {
358 parentName = getCapitalCase(getCamelCase(parent.getName(), yangPluginConfig.getConflictResolver()));
359 parentPkg = getNodesPackage(parent, yangPluginConfig);
360 }
361 parentsInfo.setClassInfo(parentName);
362 parentsInfo.setPkgInfo(parentPkg);
363 return parentsInfo;
364
365 }
366
367 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530368 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530369 *
370 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530371 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530372 * @throws IOException IO operations fails
373 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530374 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530375 YangPluginConfig yangPluginConfig)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530376 throws IOException {
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530377 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hana60bf4c2016-05-30 20:13:13 +0900378 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530379 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530380 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
381 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530382 }
383
384 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530385 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530386 *
387 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530388 * @param yangPluginConfig YANG plugin config
389 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530390 * @throws IOException IO operations fails
391 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530392 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530393 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530394 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530395 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hana60bf4c2016-05-30 20:13:13 +0900396 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530397 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530398 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
399
400 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
401 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
402 }
403
404 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530405 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530406
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530407 /**
408 * Returns the node package string.
409 *
410 * @param curNode current java node whose package string needs to be set
411 * @return returns the root package string
412 */
413 public static String getCurNodePackage(YangNode curNode) {
414
415 String pkg;
416 if (!(curNode instanceof JavaFileInfoContainer)
417 || curNode.getParent() == null) {
418 throw new TranslatorException("missing parent node to get current node's package");
419 }
420
421 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
422 if (!(parentNode instanceof JavaFileInfoContainer)) {
423 throw new TranslatorException("missing parent java node to get current node's package");
424 }
Bharat saraswale50edca2016-08-05 01:58:25 +0530425 JavaFileInfoTranslator parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530426 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
427 return pkg.toLowerCase();
428 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530429
430 /**
431 * Returns true if root node contains any data node.
432 *
433 * @param node root YANG node
434 * @return true if root node contains any data node
435 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530436 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530437
438 List<YangNode> childNodes = new ArrayList<>();
Bharat saraswal8beac342016-08-04 02:00:03 +0530439 YangNode tempNode = node.getChild();
440 while (tempNode != null) {
441 childNodes.add(tempNode);
442 tempNode = tempNode.getNextSibling();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530443 }
444
445 if (childNodes.size() == 0) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530446 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
447 return !leavesHolder.getListOfLeaf().isEmpty() || !leavesHolder.getListOfLeafList().isEmpty();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530448 } else if (childNodes.size() == 1) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530449 return !(childNodes.get(0) instanceof YangTranslatorOperatorNode);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530450 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530451 List<Boolean> booleanData = new ArrayList<>();
452 for (YangNode child : childNodes) {
453 if (child instanceof YangTranslatorOperatorNode) {
454 booleanData.add(false);
455 } else {
456 booleanData.add(true);
457 }
458 }
459 return booleanData.contains(true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530460 }
461
462 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530463 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530464 *
Bharat saraswal8beac342016-08-04 02:00:03 +0530465 * @param node YANG node
Bharat saraswalaf413b82016-07-14 15:18:20 +0530466 * @param yangPluginConfig plugin config
467 * @return java package
468 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530469 public static String getNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530470
471 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530472 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530473 if (node instanceof YangJavaAugmentTranslator) {
474 clsInfo.add(getAugmentClassName((YangAugment) node, yangPluginConfig));
475 } else {
476 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530477 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530478 node = node.getParent();
479 }
480
481 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530482 if (node instanceof YangJavaModuleTranslator) {
483 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530484 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace(), module
Bharat saraswalaf413b82016-07-14 15:18:20 +0530485 .getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530486 } else if (node instanceof YangJavaSubModuleTranslator) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530487 YangJavaSubModuleTranslator subModule = (YangJavaSubModuleTranslator) node;
488 pkg.append(getRootPackage(subModule.getVersion(),
489 subModule.getNameSpaceFromModule(subModule.getBelongsTo()),
490 subModule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530491 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530492 String concat = "";
Bharat saraswalaf413b82016-07-14 15:18:20 +0530493 for (int i = 1; i <= clsInfo.size(); i++) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530494 concat = concat + "." + clsInfo.get(clsInfo.size() - i);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530495 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530496 pkg.append(concat);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530497 return pkg.toString().toLowerCase();
498
499 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530500
501 /**
502 * Returns augment class name.
503 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530504 * @param augment YANG augment
Bharat saraswal039f59c2016-07-14 21:57:13 +0530505 * @param yangPluginConfig plugin configurations
506 * @return augment class name
507 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530508 static String getAugmentClassName(YangAugment augment, YangPluginConfig yangPluginConfig) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530509 YangNodeIdentifier yangNodeIdentifier = augment.getTargetNode().get(augment.getTargetNode().size() - 1)
510 .getNodeIdentifier();
511 String name = getCapitalCase(getCamelCase(yangNodeIdentifier.getName(), yangPluginConfig
512 .getConflictResolver()));
513 if (yangNodeIdentifier.getPrefix() != null) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530514 return AUGMENTED + getCapitalCase(getCamelCase(yangNodeIdentifier.getPrefix(), yangPluginConfig
515 .getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530516 } else {
517 return AUGMENTED + name;
518 }
519 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530520
Gaurav Agrawal26390042016-04-12 13:30:16 +0530521}