blob: c28271b4fc6bb38510a9ffb491168b9f6fb62859 [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 Agrawal1c8f80c2016-04-12 13:30:16 +053031import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
32import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
Bharat saraswald72411a2016-04-19 01:00:16 +053033import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053034
35import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
36import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
37import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
38import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053039import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
40import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053041import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
42
43/**
44 * Represents utility class for YANG java model.
45 */
46public final class YangJavaModelUtils {
47
48 /**
49 * Creates YANG java model utility.
50 */
51 private YangJavaModelUtils() {
52 }
53
54 /**
55 * Updates YANG java file package information.
56 *
57 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +053058 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053059 * @throws IOException IO operations fails
60 */
janani bde4ffab2016-04-15 16:18:30 +053061 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053062 throws IOException {
63 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053064 .setJavaName(getCaptialCase(
65 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053066 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
67 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
68 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053069 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053070 }
71
72 /**
73 * Updates YANG java file package information for specified package.
74 *
75 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +053076 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053077 * @throws IOException IO operations fails
78 */
janani bde4ffab2016-04-15 16:18:30 +053079 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +053080 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053081 throws IOException {
82 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053083 .setJavaName(getCaptialCase(
84 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053085 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
86 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
87 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053088 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053089 }
90
91 /**
92 * Updates temporary java code fragment files.
93 *
94 * @param javaCodeGeneratorInfo YANG java file info node
95 * @throws IOException IO operations fails
96 */
97 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo) throws IOException {
98 String absolutePath = getAbsolutePackagePath(javaCodeGeneratorInfo.getJavaFileInfo().getBaseCodeGenPath(),
99 javaCodeGeneratorInfo.getJavaFileInfo().getPackageFilePath());
100
101 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
102 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo().getGeneratedFileTypes(),
103 absolutePath, javaCodeGeneratorInfo.getJavaFileInfo().getJavaName()));
104 }
105
106 /**
107 * Updates leaf information in temporary java code fragment files.
108 *
109 * @param javaCodeGeneratorInfo YANG java file info node
110 * @throws IOException IO operations fails
111 */
112 private static void updateLeafInfoInTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
113 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530114 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
115 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
116 .addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530117 } else if (javaCodeGeneratorInfo instanceof HasType) {
118 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
119 .addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo);
Bharat saraswald72411a2016-04-19 01:00:16 +0530120 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
121 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
122 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530123 } else {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530124 //TODO throw exception
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530125 }
126 }
127
128 /**
129 * Process generate code entry of YANG node.
130 *
131 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530132 * @param codeGenDir code generation directory
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530133 * @throws IOException IO operations fails
134 */
135 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, String codeGenDir)
136 throws IOException {
137 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
138 // TODO:throw exception
139 }
140 createTempFragmentFile(javaCodeGeneratorInfo);
141 updateLeafInfoInTempFragmentFiles(javaCodeGeneratorInfo);
142
143 }
144
145 /**
146 * Process generate code entry of YANG node.
147 *
148 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530149 * @param yangPlugin YANG plugin config
150 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530151 * @throws IOException IO operations fails
152 */
janani bde4ffab2016-04-15 16:18:30 +0530153 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530154 boolean isMultiInstance) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530155 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
156 // TODO:throw exception
157 }
janani bde4ffab2016-04-15 16:18:30 +0530158 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin);
159 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530160
161 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
162 .addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530163
164 /**
165 * For augmentation of nodes.
166 */
167 if (javaCodeGeneratorInfo instanceof YangContainer
168 || javaCodeGeneratorInfo instanceof YangCase
169 || javaCodeGeneratorInfo instanceof YangChoice
170 || javaCodeGeneratorInfo instanceof YangInput
171 || javaCodeGeneratorInfo instanceof YangList
172 || javaCodeGeneratorInfo instanceof YangNotification
173 || javaCodeGeneratorInfo instanceof YangOutput) {
174 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION);
175 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
176 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
177 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530178 }
179
180 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530181 * Process generate code entry of YANG type.
182 *
183 * @param javaCodeGeneratorInfo YANG java file info node
184 * @param yangPlugin YANG plugin config
185 * @param isMultiInstance flag to indicate whether it's a list
186 * @throws IOException IO operations fails
187 */
188 public static void generateCodeOfType(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
189 boolean isMultiInstance) throws IOException {
190 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
191 // TODO:throw exception
192 }
193 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin);
194 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
195 }
196
197 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530198 * Process generate code entry of root node.
199 *
200 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530201 * @param yangPlugin YANG plugin config
202 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530203 * @throws IOException IO operations fails
204 */
janani bde4ffab2016-04-15 16:18:30 +0530205 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530206 String rootPkg) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530207 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
208 // TODO:throw exception
209 }
janani bde4ffab2016-04-15 16:18:30 +0530210 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin, rootPkg);
211 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530212 }
213}