blob: 6716251cdb7fecb93b4d1e5c78efaf3eb4f9bd03 [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;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053020
21import 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
janani bde4ffab2016-04-15 16:18:30 +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
janani bde4ffab2016-04-15 16:18:30 +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,
79 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);
116 } else {
117 // TODO: either write a util for ENUM and UNION or, call the
118 // corresponding implementation in ENUM and UNION
119 }
120 }
121
122 /**
123 * Process generate code entry of YANG node.
124 *
125 * @param javaCodeGeneratorInfo YANG java file info node
126 * @param codeGenDir code generation directory
127 * @throws IOException IO operations fails
128 */
129 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, String codeGenDir)
130 throws IOException {
131 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
132 // TODO:throw exception
133 }
134 createTempFragmentFile(javaCodeGeneratorInfo);
135 updateLeafInfoInTempFragmentFiles(javaCodeGeneratorInfo);
136
137 }
138
139 /**
140 * Process generate code entry of YANG node.
141 *
142 * @param javaCodeGeneratorInfo YANG java file info node
janani bde4ffab2016-04-15 16:18:30 +0530143 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530144 * @param isMultiInstance flag to indicate whether it's a list
145 * @throws IOException IO operations fails
146 */
janani bde4ffab2016-04-15 16:18:30 +0530147 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530148 boolean isMultiInstance) throws IOException {
149 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
150 // TODO:throw exception
151 }
janani bde4ffab2016-04-15 16:18:30 +0530152 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin);
153 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530154
155 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
156 .addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530157
158 /**
159 * For augmentation of nodes.
160 */
161 if (javaCodeGeneratorInfo instanceof YangContainer
162 || javaCodeGeneratorInfo instanceof YangCase
163 || javaCodeGeneratorInfo instanceof YangChoice
164 || javaCodeGeneratorInfo instanceof YangInput
165 || javaCodeGeneratorInfo instanceof YangList
166 || javaCodeGeneratorInfo instanceof YangNotification
167 || javaCodeGeneratorInfo instanceof YangOutput) {
168 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION);
169 } else if (javaCodeGeneratorInfo instanceof YangAugment) {
170 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
171 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530172 }
173
174 /**
175 * Process generate code entry of root node.
176 *
177 * @param javaCodeGeneratorInfo YANG java file info node
janani bde4ffab2016-04-15 16:18:30 +0530178 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530179 * @param rootPkg package of the root node
180 * @throws IOException IO operations fails
181 */
janani bde4ffab2016-04-15 16:18:30 +0530182 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530183 String rootPkg) throws IOException {
184 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
185 // TODO:throw exception
186 }
janani bde4ffab2016-04-15 16:18:30 +0530187 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin, rootPkg);
188 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530189 }
190}