blob: 0a08db344390168c559a9cc3b4ccee3e8dfdd3c5 [file] [log] [blame]
Gaurav Agrawal1c8f80c2016-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
17package org.onosproject.yangutils.translator.tojava.utils;
18
19import java.io.IOException;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053021import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053022import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053023import org.onosproject.yangutils.datamodel.YangAugmentationHolder;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053024import org.onosproject.yangutils.datamodel.YangCase;
25import org.onosproject.yangutils.datamodel.YangChoice;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053026import org.onosproject.yangutils.datamodel.YangLeavesHolder;
27import org.onosproject.yangutils.datamodel.YangNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028import org.onosproject.yangutils.datamodel.YangTypeHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053029import org.onosproject.yangutils.translator.exception.TranslatorException;
30import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053031import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053032import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
33import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
Bharat saraswald72411a2016-04-19 01:00:16 +053034import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053035
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053036import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
37import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053038import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053039import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053040import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
41import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053042import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053043import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053044import static org.onosproject.yangutils.utils.UtilConstants.PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053045
46/**
47 * Represents utility class for YANG java model.
48 */
49public final class YangJavaModelUtils {
50
51 /**
52 * Creates YANG java model utility.
53 */
54 private YangJavaModelUtils() {
55 }
56
57 /**
58 * Updates YANG java file package information.
59 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053060 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053061 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053062 * @throws IOException IO operations fails
63 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053064 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
65 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053066 throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +053068 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
69 yangPluginConfig.getConflictResolver()));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053070 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
71 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
72 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053073 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053074 }
75
76 /**
77 * Updates YANG java file package information for specified package.
78 *
79 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053081 * @throws IOException IO operations fails
82 */
janani bde4ffab2016-04-15 16:18:30 +053083 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053084 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053085 throws IOException {
86 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +053087 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
88 yangPlugin.getConflictResolver()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053089 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
90 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
91 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053092 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053093 }
94
95 /**
96 * Updates temporary java code fragment files.
97 *
98 * @param javaCodeGeneratorInfo YANG java file info node
99 * @throws IOException IO operations fails
100 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530101 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
102 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530103 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530104 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530105 }
106
107 /**
108 * Updates leaf information in temporary java code fragment files.
109 *
110 * @param javaCodeGeneratorInfo YANG java file info node
111 * @throws IOException IO operations fails
112 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530113 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
114 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530115 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530116 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530117 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530118 * Module / sub module node code generation.
119 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530120 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530121 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530122 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530123 } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530124 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530125 * Container
126 * Case
127 * Grouping
128 * Input
129 * List
130 * Notification
131 * Output
132 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530133 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530134 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530135 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530136 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530137 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530138 * Typedef
139 * Union
140 */
141 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
142 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo);
Bharat saraswald72411a2016-04-19 01:00:16 +0530143 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530144 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530145 * Enumeration
146 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530147 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswald72411a2016-04-19 01:00:16 +0530148 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530149 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
150 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530151 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530153 }
154 }
155
156 /**
157 * Process generate code entry of YANG node.
158 *
159 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530160 * @throws IOException IO operations fails
161 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530162 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
163 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530164 throws IOException {
165 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530166 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530167 }
168 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530169 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530170
171 }
172
173 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530174 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530175 *
176 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530177 * @param yangPlugin YANG plugin config
178 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530179 * @throws IOException IO operations fails
180 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530181 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
182 YangPluginConfig yangPlugin, boolean isMultiInstance)
183 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530184 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530185 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530186 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530187
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530188 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530189 * Generate the Java files corresponding to the current node.
190 */
191 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
192
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530193 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530194 * Update the current nodes info in its parent nodes generated files.
195 */
196 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
197 }
198
199 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530200 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530201 *
202 * @param javaCodeGeneratorInfo YANG java file info node
203 * @param yangPlugin YANG plugin config
204 * @throws IOException IO operations fails
205 */
206 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
207 YangPluginConfig yangPlugin)
208 throws IOException {
209 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
210 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530211 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530212
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530213 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
214
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530215 /*
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530216 * For augmentation of nodes.
217 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530218 if (javaCodeGeneratorInfo instanceof YangAugmentationHolder) {
219 JavaQualifiedTypeInfo augmentationHoldersInfo = new JavaQualifiedTypeInfo();
220 augmentationHoldersInfo.setClassInfo(AUGMENTATION_HOLDER);
221 augmentationHoldersInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG);
222 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
223 .addToExtendsList(augmentationHoldersInfo, (YangNode) javaCodeGeneratorInfo);
224
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530225 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530226 JavaQualifiedTypeInfo augmentedInfo = new JavaQualifiedTypeInfo();
227 augmentedInfo.setClassInfo(AUGMENTED_INFO);
228 augmentedInfo.setPkgInfo(PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG);
229 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
230 .addToExtendsList(augmentedInfo, (YangNode) javaCodeGeneratorInfo);
231
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530232 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530233
234 if (javaCodeGeneratorInfo instanceof YangCase) {
235 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530236 JavaQualifiedTypeInfo parentsInfo = new JavaQualifiedTypeInfo();
237 String parentName = ((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName();
238 String parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
239 parentsInfo.setClassInfo(parentName);
240 parentsInfo.setPkgInfo(parentPkg);
241 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
242 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo);
243
244 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
245 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo);
246
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530247 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530248 }
249
250 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530251 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530252 *
253 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530254 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530255 * @throws IOException IO operations fails
256 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530257 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
258 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530259 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530260 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
261 // TODO:throw exception
262 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530263 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
264 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530265 }
266
267 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530268 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530269 *
270 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530271 * @param yangPluginConfig YANG plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530272 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530273 * @throws IOException IO operations fails
274 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530275 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
276 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530277 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530278 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
279 // TODO:throw exception
280 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530281 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
282
283 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
284 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
285 }
286
287 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530288 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530289
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530290}