blob: 08a6d9b2aa940202fe5f28f65876f8e4460aca24 [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 {
Bharat saraswale3175d32016-08-31 17:50:11 +0530230 throw new TranslatorException("Unsupported Node Translation "
231 + javaCodeGeneratorInfo.getLineNumber() + " at " +
232 javaCodeGeneratorInfo.getCharPosition()
233 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal26390042016-04-12 13:30:16 +0530234 }
235 }
236
237 /**
238 * Process generate code entry of YANG node.
239 *
240 * @param javaCodeGeneratorInfo YANG java file info node
Bharat saraswalaf413b82016-07-14 15:18:20 +0530241 * @param yangPluginConfig plugin configurations
Gaurav Agrawal26390042016-04-12 13:30:16 +0530242 * @throws IOException IO operations fails
243 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530244 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +0530245 YangPluginConfig yangPluginConfig)
Gaurav Agrawal26390042016-04-12 13:30:16 +0530246 throws IOException {
247 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530248 throw new TranslatorException("translation is not supported for the node "
249 + javaCodeGeneratorInfo.getLineNumber() + " at " +
250 javaCodeGeneratorInfo.getCharPosition()
251 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal26390042016-04-12 13:30:16 +0530252 }
253 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530254 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530255
256 }
257
258 /**
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530259 * Updates notification node info in service temporary file.
260 *
261 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530262 * @param yangPluginConfig plugin configurations
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530263 * @throws IOException when fails to do IO operations
264 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530265 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530266 YangPluginConfig yangPluginConfig)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530267 throws IOException {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530268 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
269 for (YangNode notification : ((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes()) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530270 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 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530275 if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
276 for (YangNode notification : ((YangJavaSubModuleTranslator) javaCodeGeneratorInfo)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530277 .getNotificationNodes()) {
278 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal039f59c2016-07-14 21:57:13 +0530279 .getEventFragmentFiles()
Shankara-Huaweib7564772016-08-02 18:13:13 +0530280 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530281 }
282 }
283 }
284
285 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530286 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530287 *
288 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530289 * @param yangPlugin YANG plugin config
290 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal26390042016-04-12 13:30:16 +0530291 * @throws IOException IO operations fails
292 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530293 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530294 YangPluginConfig yangPlugin, boolean isMultiInstance)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530295 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530296 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530297 throw new TranslatorException("Invalid node for translation " +
298 javaCodeGeneratorInfo.getLineNumber() + " at " +
299 javaCodeGeneratorInfo.getCharPosition()
300 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal26390042016-04-12 13:30:16 +0530301 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530302
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530303 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530304 * Generate the Java files corresponding to the current node.
305 */
306 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
307
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530308 /*
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530309 * Update the current nodes info in its parent nodes generated files.
310 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530311 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530312 }
313
314 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530315 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530316 *
317 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530318 * @param yangPlugin YANG plugin config
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530319 * @throws IOException IO operations fails
320 */
321 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530322 YangPluginConfig yangPlugin)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530323 throws IOException {
324 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530325 throw new TranslatorException("invalid node for translation " +
326 javaCodeGeneratorInfo.getLineNumber() + " at " +
327 javaCodeGeneratorInfo.getCharPosition()
328 + " in " + javaCodeGeneratorInfo.getFileName());
Vidyashree Rama13960652016-04-26 15:06:06 +0530329 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530330
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530331 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530332 TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530333
Vidyashree Rama13960652016-04-26 15:06:06 +0530334 if (javaCodeGeneratorInfo instanceof YangCase) {
335 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswale50edca2016-08-05 01:58:25 +0530336 JavaQualifiedTypeInfoTranslator parentsInfo = getQualifierInfoForCasesParent(parent, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530337 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalaf413b82016-07-14 15:18:20 +0530338 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo,
339 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530340
341 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530342 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530343
Vidyashree Rama13960652016-04-26 15:06:06 +0530344 }
Gaurav Agrawal26390042016-04-12 13:30:16 +0530345 }
346
347 /**
Bharat saraswale50edca2016-08-05 01:58:25 +0530348 * Returns cases parent's qualified info.
349 *
350 * @param parent parent node
351 * @param yangPluginConfig plugin configuration
352 * @return cases parent's qualified info
353 */
354 public static JavaQualifiedTypeInfoTranslator getQualifierInfoForCasesParent(YangNode parent,
355 YangPluginConfig yangPluginConfig) {
356 String parentName;
357 String parentPkg;
358 JavaQualifiedTypeInfoTranslator parentsInfo = new JavaQualifiedTypeInfoTranslator();
359 JavaFileInfoTranslator parentInfo;
360 if (parent instanceof YangChoice) {
361 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
362 } else {
363 parent = ((YangAugment) parent).getAugmentedNode();
364 parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
365 }
366 if (parentInfo.getPackage() != null) {
367 parentName = getCapitalCase(parentInfo.getJavaName());
368 parentPkg = parentInfo.getPackage();
369 } else {
370 parentName = getCapitalCase(getCamelCase(parent.getName(), yangPluginConfig.getConflictResolver()));
371 parentPkg = getNodesPackage(parent, yangPluginConfig);
372 }
373 parentsInfo.setClassInfo(parentName);
374 parentsInfo.setPkgInfo(parentPkg);
375 return parentsInfo;
376
377 }
378
379 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530380 * Generates code for the current data model node.
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530381 *
382 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530383 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530384 * @throws IOException IO operations fails
385 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530386 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswald14cbe82016-07-14 13:26:18 +0530387 YangPluginConfig yangPluginConfig)
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530388 throws IOException {
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530389 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530390 throw new TranslatorException("invalid node for translation "
391 + javaCodeGeneratorInfo.getLineNumber() + " at " +
392 javaCodeGeneratorInfo.getCharPosition()
393 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530394 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530395 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
396 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530397 }
398
399 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530400 * Generates code for the root module/sub-module node.
Gaurav Agrawal26390042016-04-12 13:30:16 +0530401 *
402 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530403 * @param yangPluginConfig YANG plugin config
404 * @param rootPkg package of the root node
Gaurav Agrawal26390042016-04-12 13:30:16 +0530405 * @throws IOException IO operations fails
406 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530407 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530408 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530409 throws IOException {
Gaurav Agrawal26390042016-04-12 13:30:16 +0530410 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530411 throw new TranslatorException("invalid node for translation " + javaCodeGeneratorInfo.getLineNumber()
412 + " at " +
413 javaCodeGeneratorInfo.getCharPosition()
414 + " in " + javaCodeGeneratorInfo.getFileName());
Gaurav Agrawal26390042016-04-12 13:30:16 +0530415 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530416 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
417
418 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
419 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
420 }
421
422 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal26390042016-04-12 13:30:16 +0530423 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530424
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530425 /**
426 * Returns the node package string.
427 *
428 * @param curNode current java node whose package string needs to be set
429 * @return returns the root package string
430 */
431 public static String getCurNodePackage(YangNode curNode) {
432
433 String pkg;
434 if (!(curNode instanceof JavaFileInfoContainer)
435 || curNode.getParent() == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530436 throw new TranslatorException("missing parent node to get current node's package " +
437 curNode.getName() + " in " +
438 curNode.getLineNumber() + " at " +
439 curNode.getCharPosition()
440 + " in " + curNode.getFileName());
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530441 }
442
443 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
444 if (!(parentNode instanceof JavaFileInfoContainer)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530445 throw new TranslatorException("missing parent java node to get current node's package " +
446 curNode.getName() + " in " +
447 curNode.getLineNumber() + " at " +
448 curNode.getCharPosition()
449 + " in " + curNode.getFileName());
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530450 }
Bharat saraswale50edca2016-08-05 01:58:25 +0530451 JavaFileInfoTranslator parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530452 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
453 return pkg.toLowerCase();
454 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530455
456 /**
457 * Returns true if root node contains any data node.
458 *
459 * @param node root YANG node
460 * @return true if root node contains any data node
461 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530462 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530463
464 List<YangNode> childNodes = new ArrayList<>();
Bharat saraswal8beac342016-08-04 02:00:03 +0530465 YangNode tempNode = node.getChild();
466 while (tempNode != null) {
467 childNodes.add(tempNode);
468 tempNode = tempNode.getNextSibling();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530469 }
470
471 if (childNodes.size() == 0) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530472 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
473 return !leavesHolder.getListOfLeaf().isEmpty() || !leavesHolder.getListOfLeafList().isEmpty();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530474 } else if (childNodes.size() == 1) {
Bharat saraswal039f59c2016-07-14 21:57:13 +0530475 return !(childNodes.get(0) instanceof YangTranslatorOperatorNode);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530476 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530477 List<Boolean> booleanData = new ArrayList<>();
478 for (YangNode child : childNodes) {
479 if (child instanceof YangTranslatorOperatorNode) {
480 booleanData.add(false);
481 } else {
482 booleanData.add(true);
483 }
484 }
485 return booleanData.contains(true);
Bharat saraswald14cbe82016-07-14 13:26:18 +0530486 }
487
488 /**
Bharat saraswale3175d32016-08-31 17:50:11 +0530489 * Returns true if only augment nodes present in module.
490 *
491 * @param curNode root node
492 * @return true if only augment nodes present in module
493 */
494 public static boolean isOnlyAugmentNodeInRoot(YangNode curNode) {
495 List<Boolean> booleanData = new ArrayList<>();
496 curNode = curNode.getChild();
497 while (curNode != null) {
498 if (curNode instanceof YangAugment) {
499 booleanData.add(true);
500 } else {
501 booleanData.add(false);
502 }
503 curNode = curNode.getNextSibling();
504 }
505 return !booleanData.contains(false);
506 }
507
508 /**
Bharat saraswal8beac342016-08-04 02:00:03 +0530509 * Returns nodes package.
Bharat saraswald14cbe82016-07-14 13:26:18 +0530510 *
Bharat saraswal8beac342016-08-04 02:00:03 +0530511 * @param node YANG node
Bharat saraswalaf413b82016-07-14 15:18:20 +0530512 * @param yangPluginConfig plugin config
513 * @return java package
514 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530515 public static String getNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530516
517 List<String> clsInfo = new ArrayList<>();
Bharat saraswal039f59c2016-07-14 21:57:13 +0530518 while (node.getParent() != null) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530519 if (node instanceof YangJavaAugmentTranslator) {
520 clsInfo.add(getAugmentClassName((YangAugment) node, yangPluginConfig));
521 } else {
522 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530523 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530524 node = node.getParent();
525 }
526
527 StringBuilder pkg = new StringBuilder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530528 if (node instanceof YangJavaModuleTranslator) {
529 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530530 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace(), module
Bharat saraswalaf413b82016-07-14 15:18:20 +0530531 .getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Shankara-Huaweib7564772016-08-02 18:13:13 +0530532 } else if (node instanceof YangJavaSubModuleTranslator) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530533 YangJavaSubModuleTranslator subModule = (YangJavaSubModuleTranslator) node;
534 pkg.append(getRootPackage(subModule.getVersion(),
535 subModule.getNameSpaceFromModule(subModule.getBelongsTo()),
536 subModule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530537 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530538 String concat = "";
Bharat saraswalaf413b82016-07-14 15:18:20 +0530539 for (int i = 1; i <= clsInfo.size(); i++) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530540 concat = concat + "." + clsInfo.get(clsInfo.size() - i);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530541 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530542 pkg.append(concat);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530543 return pkg.toString().toLowerCase();
544
545 }
Bharat saraswal039f59c2016-07-14 21:57:13 +0530546
547 /**
548 * Returns augment class name.
549 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530550 * @param augment YANG augment
Bharat saraswal039f59c2016-07-14 21:57:13 +0530551 * @param yangPluginConfig plugin configurations
552 * @return augment class name
553 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530554 static String getAugmentClassName(YangAugment augment, YangPluginConfig yangPluginConfig) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530555 YangNodeIdentifier yangNodeIdentifier = augment.getTargetNode().get(augment.getTargetNode().size() - 1)
556 .getNodeIdentifier();
557 String name = getCapitalCase(getCamelCase(yangNodeIdentifier.getName(), yangPluginConfig
558 .getConflictResolver()));
559 if (yangNodeIdentifier.getPrefix() != null) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530560 return AUGMENTED + getCapitalCase(getCamelCase(yangNodeIdentifier.getPrefix(), yangPluginConfig
561 .getConflictResolver())) + name;
Bharat saraswal039f59c2016-07-14 21:57:13 +0530562 } else {
563 return AUGMENTED + name;
564 }
565 }
Bharat saraswal8beac342016-08-04 02:00:03 +0530566
Gaurav Agrawal26390042016-04-12 13:30:16 +0530567}