blob: eb02fd7354739c0a66cc8275f0f0e12fdefd2451 [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;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053022import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswal8beac342016-08-04 02:00:03 +053023import org.onosproject.yangutils.datamodel.YangAtomicPath;
Bharat saraswal039f59c2016-07-14 21:57:13 +053024import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswale2bc60d2016-04-16 02:28:25 +053025import org.onosproject.yangutils.datamodel.YangCase;
26import org.onosproject.yangutils.datamodel.YangChoice;
Gaurav Agrawal26390042016-04-12 13:30:16 +053027import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Bharat saraswal8beac342016-08-04 02:00:03 +053028import org.onosproject.yangutils.datamodel.YangModule;
Gaurav Agrawal26390042016-04-12 13:30:16 +053029import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal039f59c2016-07-14 21:57:13 +053030import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Bharat saraswal8beac342016-08-04 02:00:03 +053031import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswal039f59c2016-07-14 21:57:13 +053032import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053033import org.onosproject.yangutils.datamodel.YangTypeHolder;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053034import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053035import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053036import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugmentTranslator;
37import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumerationTranslator;
38import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
39import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
Gaurav Agrawale5057f02016-08-22 17:14:33 +053040import org.onosproject.yangutils.utils.io.YangPluginConfig;
Gaurav Agrawal26390042016-04-12 13:30:16 +053041
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053042import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
43import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053044import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswald14cbe82016-07-14 13:26:18 +053045import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswal039f59c2016-07-14 21:57:13 +053046import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053047import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
48import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
49import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
50import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Bharat saraswal8beac342016-08-04 02:00:03 +053051import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Gaurav Agrawal26390042016-04-12 13:30:16 +053052
53/**
54 * Represents utility class for YANG java model.
55 */
56public final class YangJavaModelUtils {
57
58 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +053059 * Creates an instance of YANG java model utility.
Gaurav Agrawal26390042016-04-12 13:30:16 +053060 */
61 private YangJavaModelUtils() {
62 }
63
64 /**
65 * Updates YANG java file package information.
66 *
Vinod Kumar S79a374b2016-04-30 21:09:15 +053067 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053068 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +053069 * @throws IOException IO operations fails
70 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053071 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +053072 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +053073 throws IOException {
Shankara-Huaweib7564772016-08-02 18:13:13 +053074 if (javaCodeGeneratorInfo instanceof YangJavaAugmentTranslator) {
Bharat saraswal8beac342016-08-04 02:00:03 +053075 updatePackageForAugmentInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswald14cbe82016-07-14 13:26:18 +053076 } else {
77 javaCodeGeneratorInfo.getJavaFileInfo()
78 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
79 yangPluginConfig.getConflictResolver()));
Bharat saraswal8beac342016-08-04 02:00:03 +053080 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
Bharat saraswald14cbe82016-07-14 13:26:18 +053081 }
Bharat saraswal8beac342016-08-04 02:00:03 +053082 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
83 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
84
85 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
86 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
87
88 }
89
90 /**
91 * Updates YANG java file package information.
92 *
93 * @param javaCodeGeneratorInfo YANG java file info node
94 * @param yangPluginConfig YANG plugin config
95 * @throws IOException IO operations fails
96 */
97 private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
98 YangPluginConfig yangPluginConfig)
99 throws IOException {
100 javaCodeGeneratorInfo.getJavaFileInfo()
101 .setJavaName(getAugmentClassName((YangJavaAugmentTranslator) javaCodeGeneratorInfo,
102 yangPluginConfig));
103 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(
104 getAugmentsNodePackage((YangNode) javaCodeGeneratorInfo, yangPluginConfig));
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530105 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
106 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530107 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530108 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530109 }
110
111 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530112 * Returns package for augment node.
113 *
114 * @param yangNode augment node
115 * @param yangPluginConfig plugin configurations
116 * @return package for augment node
117 */
118 private static String getAugmentsNodePackage(YangNode yangNode, YangPluginConfig yangPluginConfig) {
119 YangAugment augment = (YangAugment) yangNode;
120 StringBuilder augmentPkg = new StringBuilder();
121 augmentPkg.append(getCurNodePackage(augment));
122
123 String pkg = PERIOD;
124 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
125 pkg = pkg + getCamelCase(atomicPath.getNodeIdentifier().getName(), yangPluginConfig.getConflictResolver())
126 + PERIOD;
127 }
128 pkg = trimAtLast(pkg, PERIOD);
129 augmentPkg.append(pkg.toLowerCase());
130 return augmentPkg.toString();
131 }
132
133 /**
Gaurav Agrawal26390042016-04-12 13:30:16 +0530134 * Updates YANG java file package information for specified package.
135 *
136 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530137 * @param yangPlugin YANG plugin config
Gaurav Agrawal26390042016-04-12 13:30:16 +0530138 * @throws IOException IO operations fails
139 */
janani b1c6acc42016-04-15 16:18:30 +0530140 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530141 String pkg) throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530142 javaCodeGeneratorInfo.getJavaFileInfo()
janani b703cfe42016-05-17 13:12:22 +0530143 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
144 yangPlugin.getConflictResolver()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530145 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
146 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
147 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
Bharat saraswal8beac342016-08-04 02:00:03 +0530148 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530149 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530150 }
151
152 /**
153 * Updates temporary java code fragment files.
154 *
155 * @param javaCodeGeneratorInfo YANG java file info node
156 * @throws IOException IO operations fails
157 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530158 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
159 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530160 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530161 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal26390042016-04-12 13:30:16 +0530162 }
163
164 /**
165 * Updates leaf information in temporary java code fragment files.
166 *
167 * @param javaCodeGeneratorInfo YANG java file info node
168 * @throws IOException IO operations fails
169 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530170 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530171 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530172 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530173
174 if (javaCodeGeneratorInfo instanceof YangModule
175 || javaCodeGeneratorInfo instanceof YangSubModule) {
176 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().setRooNode(true);
177 }
178
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530179 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530180 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530181 * Module / sub module node code generation.
182 */
Gaurav Agrawal26390042016-04-12 13:30:16 +0530183 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530184 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530185 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweib7564772016-08-02 18:13:13 +0530186 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
187 if (!((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
188 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530189 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530190 } else if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
191 if (!((YangJavaSubModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
192 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530193 }
194 }
195
Bharat saraswal64e7e232016-07-14 23:33:55 +0530196 }
197 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530198 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530199 * Container
200 * Case
201 * Grouping
202 * Input
203 * List
204 * Notification
205 * Output
206 */
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530207 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530208 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530209 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530210 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530211 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530212 * Typedef
213 * Union
214 */
215 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530216 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweib7564772016-08-02 18:13:13 +0530217 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumerationTranslator) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530218 /*
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530219 * Enumeration
220 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530221 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530222 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
223
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530224 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
225 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal26390042016-04-12 13:30:16 +0530226 } else {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530227 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530228 }
229 }
230
231 /**
232 * Process generate code entry of YANG node.
233 *
234 * @param javaCodeGeneratorInfo YANG java file info node
Bharat saraswalaf413b82016-07-14 15:18:20 +0530235 * @param yangPluginConfig plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530236 * @throws IOException IO operations fails
237 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530238 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +0530239 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530240 throws IOException {
241 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530242 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530243 }
244 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530245 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530246
247 }
248
249 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530250 * Updates notification node info in service temporary file.
251 *
252 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530253 * @param yangPluginConfig plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530254 * @throws IOException when fails to do IO operations
255 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530256 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530257 YangPluginConfig yangPluginConfig)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530258 throws IOException {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530259 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
260 for (YangNode notification : ((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes()) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530261 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal039f59c2016-07-14 21:57:13 +0530262 .getEventFragmentFiles()
Shankara-Huaweib7564772016-08-02 18:13:13 +0530263 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530264 }
265 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530266 if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
267 for (YangNode notification : ((YangJavaSubModuleTranslator) javaCodeGeneratorInfo)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530268 .getNotificationNodes()) {
269 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal039f59c2016-07-14 21:57:13 +0530270 .getEventFragmentFiles()
Shankara-Huaweib7564772016-08-02 18:13:13 +0530271 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530272 }
273 }
274 }
275
276 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530277 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530278 *
279 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530280 * @param yangPlugin YANG plugin config
281 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530282 * @throws IOException IO operations fails
283 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530284 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530285 YangPluginConfig yangPlugin, boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530286 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530287 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530288 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530289 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530290
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530291 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530292 * Generate the Java files corresponding to the current node.
293 */
294 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
295
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530296 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530297 * Update the current nodes info in its parent nodes generated files.
298 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530299 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530300 }
301
302 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530303 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530304 *
305 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530306 * @param yangPlugin YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530307 * @throws IOException IO operations fails
308 */
309 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530310 YangPluginConfig yangPlugin)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530311 throws IOException {
312 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
313 throw new TranslatorException("invalid node for translation");
Vidyashree Rama13960652016-04-26 15:06:06 +0530314 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530315
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530316 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530317 TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530318
Vidyashree Rama13960652016-04-26 15:06:06 +0530319 if (javaCodeGeneratorInfo instanceof YangCase) {
320 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswale50edca2016-08-05 01:58:25 +0530321 JavaQualifiedTypeInfoTranslator parentsInfo = getQualifierInfoForCasesParent(parent, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530322 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalaf413b82016-07-14 15:18:20 +0530323 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo,
324 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530325
326 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530327 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530328
Vidyashree Rama13960652016-04-26 15:06:06 +0530329 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530330 }
331
332 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530333 * Returns cases parent's qualified info.
334 *
335 * @param parent parent node
336 * @param yangPluginConfig plugin configuration
337 * @return cases parent's qualified info
338 */
339 public static JavaQualifiedTypeInfoTranslator getQualifierInfoForCasesParent(YangNode parent,
340 YangPluginConfig yangPluginConfig) {
341 String parentName;
342 String parentPkg;
343 JavaQualifiedTypeInfoTranslator parentsInfo = new JavaQualifiedTypeInfoTranslator();
344 JavaFileInfoTranslator parentInfo;
345 if (parent instanceof YangChoice) {
346 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
347 } else {
348 parent = ((YangAugment) parent).getAugmentedNode();
349 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
350 }
351 if (parentInfo.getPackage() != null) {
352 parentName = getCapitalCase(parentInfo.getJavaName());
353 parentPkg = parentInfo.getPackage();
354 } else {
355 parentName = getCapitalCase(getCamelCase(parent.getName(), yangPluginConfig.getConflictResolver()));
356 parentPkg = getNodesPackage(parent, yangPluginConfig);
357 }
358 parentsInfo.setClassInfo(parentName);
359 parentsInfo.setPkgInfo(parentPkg);
360 return parentsInfo;
361
362 }
363
364 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530365 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530366 *
367 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530368 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530369 * @throws IOException IO operations fails
370 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530371 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530372 YangPluginConfig yangPluginConfig)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530373 throws IOException {
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530374 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hana60bf4c2016-05-30 20:13:13 +0900375 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530376 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530377 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
378 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530379 }
380
381 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530382 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530383 *
384 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530385 * @param yangPluginConfig YANG plugin config
386 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530387 * @throws IOException IO operations fails
388 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530389 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530390 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530391 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530392 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hana60bf4c2016-05-30 20:13:13 +0900393 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal26390042016-04-12 13:30:16 +0530394 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530395 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
396
397 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
398 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
399 }
400
401 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530402 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530403
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530404 /**
405 * Returns the node package string.
406 *
407 * @param curNode current java node whose package string needs to be set
408 * @return returns the root package string
409 */
410 public static String getCurNodePackage(YangNode curNode) {
411
412 String pkg;
413 if (!(curNode instanceof JavaFileInfoContainer)
414 || curNode.getParent() == null) {
415 throw new TranslatorException("missing parent node to get current node's package");
416 }
417
418 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
419 if (!(parentNode instanceof JavaFileInfoContainer)) {
420 throw new TranslatorException("missing parent java node to get current node's package");
421 }
Bharat saraswale50edca2016-08-05 01:58:25 +0530422 JavaFileInfoTranslator parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530423 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
424 return pkg.toLowerCase();
425 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530426
427 /**
428 * Returns true if root node contains any data node.
429 *
430 * @param node root YANG node
431 * @return true if root node contains any data node
432 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530433 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530434
435 List<YangNode> childNodes = new ArrayList<>();
Bharat saraswal8beac342016-08-04 02:00:03 +0530436 YangNode tempNode = node.getChild();
437 while (tempNode != null) {
438 childNodes.add(tempNode);
439 tempNode = tempNode.getNextSibling();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530440 }
441
442 if (childNodes.size() == 0) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530443 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
444 return !leavesHolder.getListOfLeaf().isEmpty() || !leavesHolder.getListOfLeafList().isEmpty();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530445 } else if (childNodes.size() == 1) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530446 return !(childNodes.get(0) instanceof YangTranslatorOperatorNode);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530447 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530448 List<Boolean> booleanData = new ArrayList<>();
449 for (YangNode child : childNodes) {
450 if (child instanceof YangTranslatorOperatorNode) {
451 booleanData.add(false);
452 } else {
453 booleanData.add(true);
454 }
455 }
456 return booleanData.contains(true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530457 }
458
459 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530460 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530461 *
Bharat saraswal8beac342016-08-04 02:00:03 +0530462 * @param node YANG node
Bharat saraswalaf413b82016-07-14 15:18:20 +0530463 * @param yangPluginConfig plugin config
464 * @return java package
465 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530466 public static String getNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530467
468 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530469 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530470 if (node instanceof YangJavaAugmentTranslator) {
471 clsInfo.add(getAugmentClassName((YangAugment) node, yangPluginConfig));
472 } else {
473 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530474 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530475 node = node.getParent();
476 }
477
478 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530479 if (node instanceof YangJavaModuleTranslator) {
480 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530481 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace(), module
Bharat saraswalaf413b82016-07-14 15:18:20 +0530482 .getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530483 } else if (node instanceof YangJavaSubModuleTranslator) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530484 YangJavaSubModuleTranslator subModule = (YangJavaSubModuleTranslator) node;
485 pkg.append(getRootPackage(subModule.getVersion(),
486 subModule.getNameSpaceFromModule(subModule.getBelongsTo()),
487 subModule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530488 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530489 String concat = "";
Bharat saraswalaf413b82016-07-14 15:18:20 +0530490 for (int i = 1; i <= clsInfo.size(); i++) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530491 concat = concat + "." + clsInfo.get(clsInfo.size() - i);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530492 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530493 pkg.append(concat);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530494 return pkg.toString().toLowerCase();
495
496 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530497
498 /**
499 * Returns augment class name.
500 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530501 * @param augment YANG augment
Bharat saraswal039f59c2016-07-14 21:57:13 +0530502 * @param yangPluginConfig plugin configurations
503 * @return augment class name
504 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530505 static String getAugmentClassName(YangAugment augment, YangPluginConfig yangPluginConfig) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530506 YangNodeIdentifier yangNodeIdentifier = augment.getTargetNode().get(augment.getTargetNode().size() - 1)
507 .getNodeIdentifier();
508 String name = getCapitalCase(getCamelCase(yangNodeIdentifier.getName(), yangPluginConfig
509 .getConflictResolver()));
510 if (yangNodeIdentifier.getPrefix() != null) {
511 return AUGMENTED + getCapitalCase(yangNodeIdentifier.getPrefix()) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530512 } else {
513 return AUGMENTED + name;
514 }
515 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530516
Gaurav Agrawal26390042016-04-12 13:30:16 +0530517}