blob: 5e9891956d116b72cf6219546aa16881e9a37baa [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;
22
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;
Bharat saraswale50edca2016-08-05 01:58:25 +053035import org.onosproject.yangutils.utils.io.YangPluginConfig;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053036import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053037import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053038import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugmentTranslator;
39import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumerationTranslator;
40import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
41import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
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()));
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,
103 yangPluginConfig));
104 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 saraswald14cbe82016-07-14 13:26:18 +0530142 String pkg) throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530143 javaCodeGeneratorInfo.getJavaFileInfo()
janani b703cfe42016-05-17 13:12:22 +0530144 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
145 yangPlugin.getConflictResolver()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530146 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
147 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
148 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
Bharat saraswal8beac342016-08-04 02:00:03 +0530149 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530150 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530151 }
152
153 /**
154 * Updates temporary java code fragment files.
155 *
156 * @param javaCodeGeneratorInfo YANG java file info node
157 * @throws IOException IO operations fails
158 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530159 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
160 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530161 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530162 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530163 }
164
165 /**
166 * Updates leaf information in temporary java code fragment files.
167 *
168 * @param javaCodeGeneratorInfo YANG java file info node
169 * @throws IOException IO operations fails
170 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530171 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530172 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530173 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530174
175 if (javaCodeGeneratorInfo instanceof YangModule
176 || javaCodeGeneratorInfo instanceof YangSubModule) {
177 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().setRooNode(true);
178 }
179
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530180 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530181 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530182 * Module / sub module node code generation.
183 */
Gaurav Agrawal26390042016-04-12 13:30:16 +0530184 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530185 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530186 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweib7564772016-08-02 18:13:13 +0530187 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
188 if (!((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
189 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530190 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530191 } else if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
192 if (!((YangJavaSubModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
193 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530194 }
195 }
196
Bharat saraswal64e7e232016-07-14 23:33:55 +0530197 }
198 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530199 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530200 * Container
201 * Case
202 * Grouping
203 * Input
204 * List
205 * Notification
206 * Output
207 */
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530208 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530209 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530210 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530211 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530212 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530213 * Typedef
214 * Union
215 */
216 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530217 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweib7564772016-08-02 18:13:13 +0530218 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530219 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530220 * Enumeration
221 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530222 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530223 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
224
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530225 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
226 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal26390042016-04-12 13:30:16 +0530227 } else {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530228 throw new TranslatorException("Unsupported Node Translation");
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)) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530243 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530244 }
245 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530246 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530247
248 }
249
250 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530251 * Updates notification node info in service temporary file.
252 *
253 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530254 * @param yangPluginConfig plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530255 * @throws IOException when fails to do IO operations
256 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530257 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530258 YangPluginConfig yangPluginConfig)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530259 throws IOException {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530260 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
261 for (YangNode notification : ((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes()) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530262 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal039f59c2016-07-14 21:57:13 +0530263 .getEventFragmentFiles()
Shankara-Huaweib7564772016-08-02 18:13:13 +0530264 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530265 }
266 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530267 if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
268 for (YangNode notification : ((YangJavaSubModuleTranslator) javaCodeGeneratorInfo)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530269 .getNotificationNodes()) {
270 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal039f59c2016-07-14 21:57:13 +0530271 .getEventFragmentFiles()
Shankara-Huaweib7564772016-08-02 18:13:13 +0530272 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530273 }
274 }
275 }
276
277 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530278 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530279 *
280 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530281 * @param yangPlugin YANG plugin config
282 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530283 * @throws IOException IO operations fails
284 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530285 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530286 YangPluginConfig yangPlugin, boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530287 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530288 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530289 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530290 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530291
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530292 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530293 * Generate the Java files corresponding to the current node.
294 */
295 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
296
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530297 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530298 * Update the current nodes info in its parent nodes generated files.
299 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530300 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530301 }
302
303 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530304 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530305 *
306 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530307 * @param yangPlugin YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530308 * @throws IOException IO operations fails
309 */
310 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530311 YangPluginConfig yangPlugin)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530312 throws IOException {
313 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
314 throw new TranslatorException("invalid node for translation");
Vidyashree Rama13960652016-04-26 15:06:06 +0530315 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530316
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530317 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530318 TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530319
Vidyashree Rama13960652016-04-26 15:06:06 +0530320 if (javaCodeGeneratorInfo instanceof YangCase) {
321 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswale50edca2016-08-05 01:58:25 +0530322 JavaQualifiedTypeInfoTranslator parentsInfo = getQualifierInfoForCasesParent(parent, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530323 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalaf413b82016-07-14 15:18:20 +0530324 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo,
325 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530326
327 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530328 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530329
Vidyashree Rama13960652016-04-26 15:06:06 +0530330 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530331 }
332
333 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530334 * Returns cases parent's qualified info.
335 *
336 * @param parent parent node
337 * @param yangPluginConfig plugin configuration
338 * @return cases parent's qualified info
339 */
340 public static JavaQualifiedTypeInfoTranslator getQualifierInfoForCasesParent(YangNode parent,
341 YangPluginConfig yangPluginConfig) {
342 String parentName;
343 String parentPkg;
344 JavaQualifiedTypeInfoTranslator parentsInfo = new JavaQualifiedTypeInfoTranslator();
345 JavaFileInfoTranslator parentInfo;
346 if (parent instanceof YangChoice) {
347 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
348 } else {
349 parent = ((YangAugment) parent).getAugmentedNode();
350 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
351 }
352 if (parentInfo.getPackage() != null) {
353 parentName = getCapitalCase(parentInfo.getJavaName());
354 parentPkg = parentInfo.getPackage();
355 } else {
356 parentName = getCapitalCase(getCamelCase(parent.getName(), yangPluginConfig.getConflictResolver()));
357 parentPkg = getNodesPackage(parent, yangPluginConfig);
358 }
359 parentsInfo.setClassInfo(parentName);
360 parentsInfo.setPkgInfo(parentPkg);
361 return parentsInfo;
362
363 }
364
365 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530366 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530367 *
368 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530369 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530370 * @throws IOException IO operations fails
371 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530372 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530373 YangPluginConfig yangPluginConfig)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530374 throws IOException {
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530375 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hana60bf4c2016-05-30 20:13:13 +0900376 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530377 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530378 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
379 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530380 }
381
382 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530383 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530384 *
385 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530386 * @param yangPluginConfig YANG plugin config
387 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530388 * @throws IOException IO operations fails
389 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530390 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530391 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530392 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530393 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hana60bf4c2016-05-30 20:13:13 +0900394 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530395 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530396 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
397
398 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
399 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
400 }
401
402 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530403 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530404
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530405 /**
406 * Returns the node package string.
407 *
408 * @param curNode current java node whose package string needs to be set
409 * @return returns the root package string
410 */
411 public static String getCurNodePackage(YangNode curNode) {
412
413 String pkg;
414 if (!(curNode instanceof JavaFileInfoContainer)
415 || curNode.getParent() == null) {
416 throw new TranslatorException("missing parent node to get current node's package");
417 }
418
419 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
420 if (!(parentNode instanceof JavaFileInfoContainer)) {
421 throw new TranslatorException("missing parent java node to get current node's package");
422 }
Bharat saraswale50edca2016-08-05 01:58:25 +0530423 JavaFileInfoTranslator parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530424 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
425 return pkg.toLowerCase();
426 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530427
428 /**
429 * Returns true if root node contains any data node.
430 *
431 * @param node root YANG node
432 * @return true if root node contains any data node
433 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530434 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530435
436 List<YangNode> childNodes = new ArrayList<>();
Bharat saraswal8beac342016-08-04 02:00:03 +0530437 YangNode tempNode = node.getChild();
438 while (tempNode != null) {
439 childNodes.add(tempNode);
440 tempNode = tempNode.getNextSibling();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530441 }
442
443 if (childNodes.size() == 0) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530444 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
445 return !leavesHolder.getListOfLeaf().isEmpty() || !leavesHolder.getListOfLeafList().isEmpty();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530446 } else if (childNodes.size() == 1) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530447 return !(childNodes.get(0) instanceof YangTranslatorOperatorNode);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530448 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530449 List<Boolean> booleanData = new ArrayList<>();
450 for (YangNode child : childNodes) {
451 if (child instanceof YangTranslatorOperatorNode) {
452 booleanData.add(false);
453 } else {
454 booleanData.add(true);
455 }
456 }
457 return booleanData.contains(true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530458 }
459
460 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530461 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530462 *
Bharat saraswal8beac342016-08-04 02:00:03 +0530463 * @param node YANG node
Bharat saraswalaf413b82016-07-14 15:18:20 +0530464 * @param yangPluginConfig plugin config
465 * @return java package
466 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530467 public static String getNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530468
469 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530470 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530471 if (node instanceof YangJavaAugmentTranslator) {
472 clsInfo.add(getAugmentClassName((YangAugment) node, yangPluginConfig));
473 } else {
474 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530475 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530476 node = node.getParent();
477 }
478
479 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530480 if (node instanceof YangJavaModuleTranslator) {
481 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Bharat saraswalaf413b82016-07-14 15:18:20 +0530482 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
483 .getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530484 } else if (node instanceof YangJavaSubModuleTranslator) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530485 YangJavaSubModuleTranslator subModule = (YangJavaSubModuleTranslator) node;
486 pkg.append(getRootPackage(subModule.getVersion(),
487 subModule.getNameSpaceFromModule(subModule.getBelongsTo()),
488 subModule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530489 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530490 String concat = "";
Bharat saraswalaf413b82016-07-14 15:18:20 +0530491 for (int i = 1; i <= clsInfo.size(); i++) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530492 concat = concat + "." + clsInfo.get(clsInfo.size() - i);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530493 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530494 pkg.append(concat);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530495 return pkg.toString().toLowerCase();
496
497 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530498
499 /**
500 * Returns augment class name.
501 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530502 * @param augment YANG augment
Bharat saraswal039f59c2016-07-14 21:57:13 +0530503 * @param yangPluginConfig plugin configurations
504 * @return augment class name
505 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530506 static String getAugmentClassName(YangAugment augment, YangPluginConfig yangPluginConfig) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530507 YangNodeIdentifier yangNodeIdentifier = augment.getTargetNode().get(augment.getTargetNode().size() - 1)
508 .getNodeIdentifier();
509 String name = getCapitalCase(getCamelCase(yangNodeIdentifier.getName(), yangPluginConfig
510 .getConflictResolver()));
511 if (yangNodeIdentifier.getPrefix() != null) {
512 return AUGMENTED + getCapitalCase(yangNodeIdentifier.getPrefix()) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530513 } else {
514 return AUGMENTED + name;
515 }
516 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530517
Gaurav Agrawal26390042016-04-12 13:30:16 +0530518}