blob: cd6a7cd78a43b019b6369cf156a79f9f796cee51 [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;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053042import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
43
44/**
45 * Represents utility class for YANG java model.
46 */
47public final class YangJavaModelUtils {
48
49 /**
50 * Creates YANG java model utility.
51 */
52 private YangJavaModelUtils() {
53 }
54
55 /**
56 * Updates YANG java file package information.
57 *
Gaurav Agrawal56527662016-04-20 15:49:17 +053058 * @param hasJavaFileInfo YANG java file info node
59 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053060 * @throws IOException IO operations fails
61 */
Gaurav Agrawal56527662016-04-20 15:49:17 +053062 public static void updatePackageInfo(HasJavaFileInfo hasJavaFileInfo, YangPluginConfig yangPlugin)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053063 throws IOException {
Gaurav Agrawal56527662016-04-20 15:49:17 +053064 hasJavaFileInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053065 .setJavaName(getCaptialCase(
Gaurav Agrawal56527662016-04-20 15:49:17 +053066 getCamelCase(((YangNode) hasJavaFileInfo).getName(), yangPlugin.getConflictResolver())));
67 hasJavaFileInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) hasJavaFileInfo));
68 hasJavaFileInfo.getJavaFileInfo().setPackageFilePath(
69 getPackageDirPathFromJavaJPackage(hasJavaFileInfo.getJavaFileInfo().getPackage()));
70 hasJavaFileInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053071 }
72
73 /**
74 * Updates YANG java file package information for specified package.
75 *
76 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +053077 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053078 * @throws IOException IO operations fails
79 */
janani bde4ffab2016-04-15 16:18:30 +053080 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +053081 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053082 throws IOException {
83 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053084 .setJavaName(getCaptialCase(
85 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053086 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
87 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
88 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053089 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053090 }
91
92 /**
93 * Updates temporary java code fragment files.
94 *
95 * @param javaCodeGeneratorInfo YANG java file info node
96 * @throws IOException IO operations fails
97 */
98 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo) throws IOException {
99 String absolutePath = getAbsolutePackagePath(javaCodeGeneratorInfo.getJavaFileInfo().getBaseCodeGenPath(),
100 javaCodeGeneratorInfo.getJavaFileInfo().getPackageFilePath());
101
102 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
103 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo().getGeneratedFileTypes(),
104 absolutePath, javaCodeGeneratorInfo.getJavaFileInfo().getJavaName()));
105 }
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 */
113 private static void updateLeafInfoInTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
114 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530115 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
116 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
117 .addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530118 } else if (javaCodeGeneratorInfo instanceof HasType) {
119 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
120 .addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo);
Bharat saraswald72411a2016-04-19 01:00:16 +0530121 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
122 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
123 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530124 } else {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530125 //TODO throw exception
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530126 }
127 }
128
129 /**
130 * Process generate code entry of YANG node.
131 *
132 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530133 * @param codeGenDir code generation directory
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530134 * @throws IOException IO operations fails
135 */
136 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, String codeGenDir)
137 throws IOException {
138 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
139 // TODO:throw exception
140 }
141 createTempFragmentFile(javaCodeGeneratorInfo);
142 updateLeafInfoInTempFragmentFiles(javaCodeGeneratorInfo);
143
144 }
145
146 /**
147 * Process generate code entry of YANG node.
148 *
149 * @param javaCodeGeneratorInfo YANG java file info node
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530150 * @param yangPlugin YANG plugin config
151 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530152 * @throws IOException IO operations fails
153 */
janani bde4ffab2016-04-15 16:18:30 +0530154 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530155 boolean isMultiInstance) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530156 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
157 // TODO:throw exception
158 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530159 updatePackageInfo((HasJavaFileInfo) javaCodeGeneratorInfo, yangPlugin);
janani bde4ffab2016-04-15 16:18:30 +0530160 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530161
162 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
163 .addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530164
165 /**
166 * For augmentation of nodes.
167 */
168 if (javaCodeGeneratorInfo instanceof YangContainer
169 || javaCodeGeneratorInfo instanceof YangCase
170 || javaCodeGeneratorInfo instanceof YangChoice
171 || javaCodeGeneratorInfo instanceof YangInput
172 || javaCodeGeneratorInfo instanceof YangList
173 || javaCodeGeneratorInfo instanceof YangNotification
174 || javaCodeGeneratorInfo instanceof YangOutput) {
175 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION);
176 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
177 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
178 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530179 }
180
181 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530182 * Process generate code entry of YANG type.
183 *
184 * @param javaCodeGeneratorInfo YANG java file info node
185 * @param yangPlugin YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530186 * @throws IOException IO operations fails
187 */
Gaurav Agrawal56527662016-04-20 15:49:17 +0530188 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin)
189 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530190 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
191 // TODO:throw exception
192 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530193 updatePackageInfo((HasJavaFileInfo) javaCodeGeneratorInfo, yangPlugin);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530194 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}