blob: d285351192465565c7b695176551222ddb212b5e [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;
23import org.onosproject.yangutils.datamodel.YangCase;
24import org.onosproject.yangutils.datamodel.YangChoice;
25import org.onosproject.yangutils.datamodel.YangContainer;
26import org.onosproject.yangutils.datamodel.YangInput;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053027import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053028import org.onosproject.yangutils.datamodel.YangList;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053029import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053030import org.onosproject.yangutils.datamodel.YangNotification;
31import org.onosproject.yangutils.datamodel.YangOutput;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032import org.onosproject.yangutils.datamodel.YangTypeHolder;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053033import org.onosproject.yangutils.translator.exception.TranslatorException;
34import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053035import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
36import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
Bharat saraswald72411a2016-04-19 01:00:16 +053037import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053038
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053039import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
40import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053041import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053042import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
43import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
44import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
45import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053046import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
47import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053048import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053049
50/**
51 * Represents utility class for YANG java model.
52 */
53public final class YangJavaModelUtils {
54
55 /**
56 * Creates YANG java model utility.
57 */
58 private YangJavaModelUtils() {
59 }
60
61 /**
62 * Updates YANG java file package information.
63 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053064 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053065 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053066 * @throws IOException IO operations fails
67 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053068 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
69 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053070 throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053072 .setJavaName(getCaptialCase(
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053073 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
74 yangPluginConfig.getConflictResolver())));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
76 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
77 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053078 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053079 }
80
81 /**
82 * Updates YANG java file package information for specified package.
83 *
84 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053086 * @throws IOException IO operations fails
87 */
janani bde4ffab2016-04-15 16:18:30 +053088 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053089 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053090 throws IOException {
91 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053092 .setJavaName(getCaptialCase(
93 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053094 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
95 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
96 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053097 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053098 }
99
100 /**
101 * Updates temporary java code fragment files.
102 *
103 * @param javaCodeGeneratorInfo YANG java file info node
104 * @throws IOException IO operations fails
105 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530106 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
107 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530108 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530109 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530110 }
111
112 /**
113 * Updates leaf information in temporary java code fragment files.
114 *
115 * @param javaCodeGeneratorInfo YANG java file info node
116 * @throws IOException IO operations fails
117 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530118 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
119 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530120 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530121 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
122 /**
123 * Module / sub module node code generation.
124 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530125 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530126 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
127 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
128 } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
129 /**
130 * Container
131 * Case
132 * Grouping
133 * Input
134 * List
135 * Notification
136 * Output
137 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530138 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530139 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
140 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
141 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
142 /**
143 * Typedef
144 * Union
145 */
146 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
147 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo);
Bharat saraswald72411a2016-04-19 01:00:16 +0530148 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530149 /**
150 * Enumeration
151 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530152 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswald72411a2016-04-19 01:00:16 +0530153 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530154 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
155 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530156 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530158 }
159 }
160
161 /**
162 * Process generate code entry of YANG node.
163 *
164 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530165 * @throws IOException IO operations fails
166 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530167 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
168 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530169 throws IOException {
170 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530171 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530172 }
173 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530174 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530175
176 }
177
178 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530179 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530180 *
181 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530182 * @param yangPlugin YANG plugin config
183 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530184 * @throws IOException IO operations fails
185 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530186 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
187 YangPluginConfig yangPlugin, boolean isMultiInstance)
188 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530189 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530190 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530191 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530192
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530193 /**
194 * Generate the Java files corresponding to the current node.
195 */
196 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
197
198 /**
199 * Update the current nodes info in its parent nodes generated files.
200 */
201 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
202 }
203
204 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530205 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530206 *
207 * @param javaCodeGeneratorInfo YANG java file info node
208 * @param yangPlugin YANG plugin config
209 * @throws IOException IO operations fails
210 */
211 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
212 YangPluginConfig yangPlugin)
213 throws IOException {
214 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
215 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530216 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530217
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530218 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
219
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530220 /**
221 * For augmentation of nodes.
222 */
223 if (javaCodeGeneratorInfo instanceof YangContainer
224 || javaCodeGeneratorInfo instanceof YangCase
225 || javaCodeGeneratorInfo instanceof YangChoice
226 || javaCodeGeneratorInfo instanceof YangInput
227 || javaCodeGeneratorInfo instanceof YangList
228 || javaCodeGeneratorInfo instanceof YangNotification
229 || javaCodeGeneratorInfo instanceof YangOutput) {
230 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION);
231 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
232 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
233 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530234
235 if (javaCodeGeneratorInfo instanceof YangCase) {
236 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
237 String curNodeName = ((YangCase) javaCodeGeneratorInfo).getName();
238 if (!parent.getName().equals(curNodeName)) {
239 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(getCaptialCase(getCamelCase(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 parent.getName(), null)));
241 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
242 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530243 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530244 String parentPackage = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530245 String caseExtendInfo = parentPackage + PERIOD + parent.getName();
246 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(caseExtendInfo);
247 }
248 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530249 }
250
251 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530252 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530253 *
254 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530255 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530256 * @throws IOException IO operations fails
257 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530258 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
259 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530260 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530261 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
262 // TODO:throw exception
263 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530264 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
265 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530266 }
267
268 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530269 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530270 *
271 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530272 * @param yangPluginConfig YANG plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530273 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530274 * @throws IOException IO operations fails
275 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530276 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
277 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530278 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530279 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
280 // TODO:throw exception
281 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530282 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
283
284 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
285 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
286 }
287
288 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530289 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530290
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530291}