blob: eb5d5372a0ed46636412740ae34649ddf2489d93 [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
21import org.onosproject.yangutils.datamodel.YangTypeContainer;
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;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053032import org.onosproject.yangutils.translator.exception.TranslatorException;
33import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053034import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
35import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
Bharat saraswald72411a2016-04-19 01:00:16 +053036import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053037
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;
40import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
41import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
42import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053043import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
44import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053045import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053046
47/**
48 * Represents utility class for YANG java model.
49 */
50public final class YangJavaModelUtils {
51
52 /**
53 * Creates YANG java model utility.
54 */
55 private YangJavaModelUtils() {
56 }
57
58 /**
59 * Updates YANG java file package information.
60 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053061 * @param javaCodeGeneratorInfo YANG java file info node
62 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053063 * @throws IOException IO operations fails
64 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053065 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053066 throws IOException {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053067 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053068 .setJavaName(getCaptialCase(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053069 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
70 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
71 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
72 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
73 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.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 bde4ffab2016-04-15 16:18:30 +053087 .setJavaName(getCaptialCase(
88 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), 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 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530113 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530114 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530115 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
116 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
117 .addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118 } else if (javaCodeGeneratorInfo instanceof YangTypeContainer) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530119 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530120 .addTypeInfoToTempFiles((YangTypeContainer) javaCodeGeneratorInfo);
Bharat saraswald72411a2016-04-19 01:00:16 +0530121 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
122 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
123 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530124 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
125 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530126 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530127 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530128 }
129 }
130
131 /**
132 * Process generate code entry of YANG node.
133 *
134 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530135 * @throws IOException IO operations fails
136 */
137 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, String codeGenDir)
138 throws IOException {
139 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530140 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530141 }
142 createTempFragmentFile(javaCodeGeneratorInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530143 updateTempFragmentFiles(javaCodeGeneratorInfo);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530144
145 }
146
147 /**
148 * Process generate code entry of YANG node.
149 *
150 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530151 * @param yangPlugin YANG plugin config
152 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530153 * @throws IOException IO operations fails
154 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530155 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
156 YangPluginConfig yangPlugin, boolean isMultiInstance)
157 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530158 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530159 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530160 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530161
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530162 /**
163 * Generate the Java files corresponding to the current node.
164 */
165 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
166
167 /**
168 * Update the current nodes info in its parent nodes generated files.
169 */
170 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
171 }
172
173 /**
174 * Process generate code entry of YANG type.
175 *
176 * @param javaCodeGeneratorInfo YANG java file info node
177 * @param yangPlugin YANG plugin config
178 * @throws IOException IO operations fails
179 */
180 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
181 YangPluginConfig yangPlugin)
182 throws IOException {
183 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
184 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530185 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530186
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530187 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
188
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530189 /**
190 * For augmentation of nodes.
191 */
192 if (javaCodeGeneratorInfo instanceof YangContainer
193 || javaCodeGeneratorInfo instanceof YangCase
194 || javaCodeGeneratorInfo instanceof YangChoice
195 || javaCodeGeneratorInfo instanceof YangInput
196 || javaCodeGeneratorInfo instanceof YangList
197 || javaCodeGeneratorInfo instanceof YangNotification
198 || javaCodeGeneratorInfo instanceof YangOutput) {
199 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION);
200 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
201 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
202 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530203
204 if (javaCodeGeneratorInfo instanceof YangCase) {
205 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
206 String curNodeName = ((YangCase) javaCodeGeneratorInfo).getName();
207 if (!parent.getName().equals(curNodeName)) {
208 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(getCaptialCase(getCamelCase(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530209 parent.getName(), null)));
210 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
211 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530212 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530213 String parentPackage = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530214 String caseExtendInfo = parentPackage + PERIOD + parent.getName();
215 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(caseExtendInfo);
216 }
217 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530218 }
219
220 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530221 * Process generate code entry of YANG type.
222 *
223 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530224 * @param yangPlugin YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530225 * @throws IOException IO operations fails
226 */
Gaurav Agrawal56527662016-04-20 15:49:17 +0530227 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin)
228 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530229 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
230 // TODO:throw exception
231 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530232 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530233 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
234 }
235
236 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530237 * Process generate code entry of root node.
238 *
239 * @param javaCodeGeneratorInfo YANG java file info node
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530240 * @param yangPlugin YANG plugin config
241 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530242 * @throws IOException IO operations fails
243 */
janani bde4ffab2016-04-15 16:18:30 +0530244 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530245 String rootPkg)
246 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530247 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
248 // TODO:throw exception
249 }
janani bde4ffab2016-04-15 16:18:30 +0530250 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin, rootPkg);
251 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530252 }
253}