blob: e92b963d20cc18b2ed9efe79e933dc2a2e52018e [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;
janani b4a6711a2016-05-17 13:12:22 +053043import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053044import 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 b4a6711a2016-05-17 13:12:22 +053072 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
73 yangPluginConfig.getConflictResolver()));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053074 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
75 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
76 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053077 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053078 }
79
80 /**
81 * Updates YANG java file package information for specified package.
82 *
83 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053084 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053085 * @throws IOException IO operations fails
86 */
janani bde4ffab2016-04-15 16:18:30 +053087 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053088 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053089 throws IOException {
90 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +053091 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
92 yangPlugin.getConflictResolver()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053093 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
94 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
95 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053096 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053097 }
98
99 /**
100 * Updates temporary java code fragment files.
101 *
102 * @param javaCodeGeneratorInfo YANG java file info node
103 * @throws IOException IO operations fails
104 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530105 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
106 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530107 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530108 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530109 }
110
111 /**
112 * Updates leaf information in temporary java code fragment files.
113 *
114 * @param javaCodeGeneratorInfo YANG java file info node
115 * @throws IOException IO operations fails
116 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530117 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
118 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530119 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530120 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
121 /**
122 * Module / sub module node code generation.
123 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530124 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530125 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
126 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
127 } else if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
128 /**
129 * Container
130 * Case
131 * Grouping
132 * Input
133 * List
134 * Notification
135 * Output
136 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530137 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530138 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
139 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
140 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
141 /**
142 * Typedef
143 * Union
144 */
145 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
146 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo);
Bharat saraswald72411a2016-04-19 01:00:16 +0530147 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530148 /**
149 * Enumeration
150 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530151 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswald72411a2016-04-19 01:00:16 +0530152 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530153 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
154 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530155 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530156 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530157 }
158 }
159
160 /**
161 * Process generate code entry of YANG node.
162 *
163 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530164 * @throws IOException IO operations fails
165 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530166 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
167 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530168 throws IOException {
169 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530170 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530171 }
172 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530173 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530174
175 }
176
177 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530178 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530179 *
180 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530181 * @param yangPlugin YANG plugin config
182 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530183 * @throws IOException IO operations fails
184 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530185 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
186 YangPluginConfig yangPlugin, boolean isMultiInstance)
187 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530188 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530189 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530190 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530191
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530192 /**
193 * Generate the Java files corresponding to the current node.
194 */
195 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
196
197 /**
198 * Update the current nodes info in its parent nodes generated files.
199 */
200 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
201 }
202
203 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530204 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530205 *
206 * @param javaCodeGeneratorInfo YANG java file info node
207 * @param yangPlugin YANG plugin config
208 * @throws IOException IO operations fails
209 */
210 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
211 YangPluginConfig yangPlugin)
212 throws IOException {
213 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
214 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530215 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530216
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530217 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
218
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530219 /**
220 * For augmentation of nodes.
221 */
222 if (javaCodeGeneratorInfo instanceof YangContainer
223 || javaCodeGeneratorInfo instanceof YangCase
224 || javaCodeGeneratorInfo instanceof YangChoice
225 || javaCodeGeneratorInfo instanceof YangInput
226 || javaCodeGeneratorInfo instanceof YangList
227 || javaCodeGeneratorInfo instanceof YangNotification
228 || javaCodeGeneratorInfo instanceof YangOutput) {
229 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION);
230 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
231 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
232 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530233
234 if (javaCodeGeneratorInfo instanceof YangCase) {
235 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
236 String curNodeName = ((YangCase) javaCodeGeneratorInfo).getName();
237 if (!parent.getName().equals(curNodeName)) {
janani b4a6711a2016-05-17 13:12:22 +0530238 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(getCapitalCase(getCamelCase(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530239 parent.getName(), null)));
240 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
241 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530242 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530243 String parentPackage = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530244 String caseExtendInfo = parentPackage + PERIOD + parent.getName();
245 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(caseExtendInfo);
246 }
247 }
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}