blob: 4f3cb45fb049bf3b2490f3df237b8189919cfe2c [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;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053020import org.onosproject.yangutils.datamodel.HasType;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053021import org.onosproject.yangutils.datamodel.YangAugment;
22import org.onosproject.yangutils.datamodel.YangCase;
23import org.onosproject.yangutils.datamodel.YangChoice;
24import org.onosproject.yangutils.datamodel.YangContainer;
25import org.onosproject.yangutils.datamodel.YangInput;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053026import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053027import org.onosproject.yangutils.datamodel.YangList;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053028import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053029import org.onosproject.yangutils.datamodel.YangNotification;
30import org.onosproject.yangutils.datamodel.YangOutput;
Gaurav Agrawal56527662016-04-20 15:49:17 +053031import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
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
36import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
37import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
38import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
39import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053040import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
41import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053042import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053043import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
44
45/**
46 * Represents utility class for YANG java model.
47 */
48public final class YangJavaModelUtils {
49
50 /**
51 * Creates YANG java model utility.
52 */
53 private YangJavaModelUtils() {
54 }
55
56 /**
57 * Updates YANG java file package information.
58 *
Gaurav Agrawal56527662016-04-20 15:49:17 +053059 * @param hasJavaFileInfo YANG java file info node
60 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053061 * @throws IOException IO operations fails
62 */
Gaurav Agrawal56527662016-04-20 15:49:17 +053063 public static void updatePackageInfo(HasJavaFileInfo hasJavaFileInfo, YangPluginConfig yangPlugin)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053064 throws IOException {
Gaurav Agrawal56527662016-04-20 15:49:17 +053065 hasJavaFileInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053066 .setJavaName(getCaptialCase(
Gaurav Agrawal56527662016-04-20 15:49:17 +053067 getCamelCase(((YangNode) hasJavaFileInfo).getName(), yangPlugin.getConflictResolver())));
68 hasJavaFileInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) hasJavaFileInfo));
69 hasJavaFileInfo.getJavaFileInfo().setPackageFilePath(
70 getPackageDirPathFromJavaJPackage(hasJavaFileInfo.getJavaFileInfo().getPackage()));
71 hasJavaFileInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053072 }
73
74 /**
75 * Updates YANG java file package information for specified package.
76 *
77 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +053078 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053079 * @throws IOException IO operations fails
80 */
janani bde4ffab2016-04-15 16:18:30 +053081 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +053082 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053083 throws IOException {
84 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053085 .setJavaName(getCaptialCase(
86 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053087 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
88 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
89 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053090 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053091 }
92
93 /**
94 * Updates temporary java code fragment files.
95 *
96 * @param javaCodeGeneratorInfo YANG java file info node
97 * @throws IOException IO operations fails
98 */
99 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo) throws IOException {
100 String absolutePath = getAbsolutePackagePath(javaCodeGeneratorInfo.getJavaFileInfo().getBaseCodeGenPath(),
101 javaCodeGeneratorInfo.getJavaFileInfo().getPackageFilePath());
102
103 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
104 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo().getGeneratedFileTypes(),
105 absolutePath, javaCodeGeneratorInfo.getJavaFileInfo().getJavaName()));
106 }
107
108 /**
109 * Updates leaf information in temporary java code fragment files.
110 *
111 * @param javaCodeGeneratorInfo YANG java file info node
112 * @throws IOException IO operations fails
113 */
114 private static void updateLeafInfoInTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
115 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530116 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
117 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
118 .addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530119 } else if (javaCodeGeneratorInfo instanceof HasType) {
120 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
121 .addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo);
Bharat saraswald72411a2016-04-19 01:00:16 +0530122 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
123 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
124 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530125 } else {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530126 //TODO throw exception
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530127 }
128 }
129
130 /**
131 * Process generate code entry of YANG node.
132 *
133 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530134 * @param codeGenDir code generation directory
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)) {
140 // TODO:throw exception
141 }
142 createTempFragmentFile(javaCodeGeneratorInfo);
143 updateLeafInfoInTempFragmentFiles(javaCodeGeneratorInfo);
144
145 }
146
147 /**
148 * Process generate code entry of YANG node.
149 *
150 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +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 */
janani bde4ffab2016-04-15 16:18:30 +0530155 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530156 boolean isMultiInstance) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530157 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
158 // TODO:throw exception
159 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530160 updatePackageInfo((HasJavaFileInfo) javaCodeGeneratorInfo, yangPlugin);
janani bde4ffab2016-04-15 16:18:30 +0530161 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530162
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530163 if (!(javaCodeGeneratorInfo instanceof YangCase)) {
164 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
165 .addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
166 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530167
168 /**
169 * For augmentation of nodes.
170 */
171 if (javaCodeGeneratorInfo instanceof YangContainer
172 || javaCodeGeneratorInfo instanceof YangCase
173 || javaCodeGeneratorInfo instanceof YangChoice
174 || javaCodeGeneratorInfo instanceof YangInput
175 || javaCodeGeneratorInfo instanceof YangList
176 || javaCodeGeneratorInfo instanceof YangNotification
177 || javaCodeGeneratorInfo instanceof YangOutput) {
178 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION);
179 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
180 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
181 }
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530182
183 if (javaCodeGeneratorInfo instanceof YangCase) {
184 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
185 String curNodeName = ((YangCase) javaCodeGeneratorInfo).getName();
186 if (!parent.getName().equals(curNodeName)) {
187 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(getCaptialCase(getCamelCase(
188 parent.getName(), null)));
189 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addParentInfoInCurNodeTempFile((YangNode)
190 javaCodeGeneratorInfo);
191 } else {
192 String parentPackage = ((HasJavaFileInfo) parent).getJavaFileInfo().getPackage();
193 String caseExtendInfo = parentPackage + PERIOD + parent.getName();
194 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(caseExtendInfo);
195 }
196 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530197 }
198
199 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530200 * Process generate code entry of YANG type.
201 *
202 * @param javaCodeGeneratorInfo YANG java file info node
203 * @param yangPlugin YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530204 * @throws IOException IO operations fails
205 */
Gaurav Agrawal56527662016-04-20 15:49:17 +0530206 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin)
207 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530208 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
209 // TODO:throw exception
210 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530211 updatePackageInfo((HasJavaFileInfo) javaCodeGeneratorInfo, yangPlugin);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530212 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
213 }
214
215 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530216 * Process generate code entry of root node.
217 *
218 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530219 * @param yangPlugin YANG plugin config
220 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530221 * @throws IOException IO operations fails
222 */
janani bde4ffab2016-04-15 16:18:30 +0530223 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530224 String rootPkg) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530225 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
226 // TODO:throw exception
227 }
janani bde4ffab2016-04-15 16:18:30 +0530228 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin, rootPkg);
229 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530230 }
231}