blob: c706b7d2e8195a434a88f008e47b30992848d9e4 [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;
33
34import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
35import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
36import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
37import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053038import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
39import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053040import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
41
42/**
43 * Represents utility class for YANG java model.
44 */
45public final class YangJavaModelUtils {
46
47 /**
48 * Creates YANG java model utility.
49 */
50 private YangJavaModelUtils() {
51 }
52
53 /**
54 * Updates YANG java file package information.
55 *
56 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +053057 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053058 * @throws IOException IO operations fails
59 */
janani bde4ffab2016-04-15 16:18:30 +053060 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053061 throws IOException {
62 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053063 .setJavaName(getCaptialCase(
64 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053065 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
66 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
67 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053068 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053069 }
70
71 /**
72 * Updates YANG java file package information for specified package.
73 *
74 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +053075 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053076 * @throws IOException IO operations fails
77 */
janani bde4ffab2016-04-15 16:18:30 +053078 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +053079 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053080 throws IOException {
81 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053082 .setJavaName(getCaptialCase(
83 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053084 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
85 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
86 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053087 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053088 }
89
90 /**
91 * Updates temporary java code fragment files.
92 *
93 * @param javaCodeGeneratorInfo YANG java file info node
94 * @throws IOException IO operations fails
95 */
96 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo) throws IOException {
97 String absolutePath = getAbsolutePackagePath(javaCodeGeneratorInfo.getJavaFileInfo().getBaseCodeGenPath(),
98 javaCodeGeneratorInfo.getJavaFileInfo().getPackageFilePath());
99
100 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
101 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo().getGeneratedFileTypes(),
102 absolutePath, javaCodeGeneratorInfo.getJavaFileInfo().getJavaName()));
103 }
104
105 /**
106 * Updates leaf information in temporary java code fragment files.
107 *
108 * @param javaCodeGeneratorInfo YANG java file info node
109 * @throws IOException IO operations fails
110 */
111 private static void updateLeafInfoInTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
112 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530113 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
114 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
115 .addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530116 } else if (javaCodeGeneratorInfo instanceof HasType) {
117 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
118 .addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530119 } else {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530120 //TODO throw exception
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530121 }
122 }
123
124 /**
125 * Process generate code entry of YANG node.
126 *
127 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530128 * @param codeGenDir code generation directory
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530129 * @throws IOException IO operations fails
130 */
131 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, String codeGenDir)
132 throws IOException {
133 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
134 // TODO:throw exception
135 }
136 createTempFragmentFile(javaCodeGeneratorInfo);
137 updateLeafInfoInTempFragmentFiles(javaCodeGeneratorInfo);
138
139 }
140
141 /**
142 * Process generate code entry of YANG node.
143 *
144 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530145 * @param yangPlugin YANG plugin config
146 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530147 * @throws IOException IO operations fails
148 */
janani bde4ffab2016-04-15 16:18:30 +0530149 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530150 boolean isMultiInstance) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530151 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
152 // TODO:throw exception
153 }
janani bde4ffab2016-04-15 16:18:30 +0530154 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin);
155 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530156
157 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
158 .addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530159
160 /**
161 * For augmentation of nodes.
162 */
163 if (javaCodeGeneratorInfo instanceof YangContainer
164 || javaCodeGeneratorInfo instanceof YangCase
165 || javaCodeGeneratorInfo instanceof YangChoice
166 || javaCodeGeneratorInfo instanceof YangInput
167 || javaCodeGeneratorInfo instanceof YangList
168 || javaCodeGeneratorInfo instanceof YangNotification
169 || javaCodeGeneratorInfo instanceof YangOutput) {
170 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION);
171 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
172 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
173 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530174 }
175
176 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530177 * Process generate code entry of YANG type.
178 *
179 * @param javaCodeGeneratorInfo YANG java file info node
180 * @param yangPlugin YANG plugin config
181 * @param isMultiInstance flag to indicate whether it's a list
182 * @throws IOException IO operations fails
183 */
184 public static void generateCodeOfType(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
185 boolean isMultiInstance) throws IOException {
186 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
187 // TODO:throw exception
188 }
189 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin);
190 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
191 }
192
193 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530194 * Process generate code entry of root node.
195 *
196 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530197 * @param yangPlugin YANG plugin config
198 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530199 * @throws IOException IO operations fails
200 */
janani bde4ffab2016-04-15 16:18:30 +0530201 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530202 String rootPkg) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530203 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
204 // TODO:throw exception
205 }
janani bde4ffab2016-04-15 16:18:30 +0530206 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin, rootPkg);
207 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530208 }
209}