blob: f0fb9bc06f15222db7320de8bdc3d31f46be50ae [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;
20import org.onosproject.yangutils.datamodel.YangLeavesHolder;
21import org.onosproject.yangutils.datamodel.YangNode;
22import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
23import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
24
25import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
26import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
27import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
28import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
29import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
30
31/**
32 * Represents utility class for YANG java model.
33 */
34public final class YangJavaModelUtils {
35
36 /**
37 * Creates YANG java model utility.
38 */
39 private YangJavaModelUtils() {
40 }
41
42 /**
43 * Updates YANG java file package information.
44 *
45 * @param javaCodeGeneratorInfo YANG java file info node
janani bde4ffab2016-04-15 16:18:30 +053046 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053047 * @throws IOException IO operations fails
48 */
janani bde4ffab2016-04-15 16:18:30 +053049 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053050 throws IOException {
51 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053052 .setJavaName(getCaptialCase(
53 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053054 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
55 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
56 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053057 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053058 }
59
60 /**
61 * Updates YANG java file package information for specified package.
62 *
63 * @param javaCodeGeneratorInfo YANG java file info node
janani bde4ffab2016-04-15 16:18:30 +053064 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053065 * @throws IOException IO operations fails
66 */
janani bde4ffab2016-04-15 16:18:30 +053067 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
68 String pkg)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053069 throws IOException {
70 javaCodeGeneratorInfo.getJavaFileInfo()
janani bde4ffab2016-04-15 16:18:30 +053071 .setJavaName(getCaptialCase(
72 getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver())));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053073 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
74 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
75 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
janani bde4ffab2016-04-15 16:18:30 +053076 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053077 }
78
79 /**
80 * Updates temporary java code fragment files.
81 *
82 * @param javaCodeGeneratorInfo YANG java file info node
83 * @throws IOException IO operations fails
84 */
85 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo) throws IOException {
86 String absolutePath = getAbsolutePackagePath(javaCodeGeneratorInfo.getJavaFileInfo().getBaseCodeGenPath(),
87 javaCodeGeneratorInfo.getJavaFileInfo().getPackageFilePath());
88
89 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
90 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo().getGeneratedFileTypes(),
91 absolutePath, javaCodeGeneratorInfo.getJavaFileInfo().getJavaName()));
92 }
93
94 /**
95 * Updates leaf information in temporary java code fragment files.
96 *
97 * @param javaCodeGeneratorInfo YANG java file info node
98 * @throws IOException IO operations fails
99 */
100 private static void updateLeafInfoInTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
101 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530102 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
103 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
104 .addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo);
105 } else {
106 // TODO: either write a util for ENUM and UNION or, call the
107 // corresponding implementation in ENUM and UNION
108 }
109 }
110
111 /**
112 * Process generate code entry of YANG node.
113 *
114 * @param javaCodeGeneratorInfo YANG java file info node
115 * @param codeGenDir code generation directory
116 * @throws IOException IO operations fails
117 */
118 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, String codeGenDir)
119 throws IOException {
120 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
121 // TODO:throw exception
122 }
123 createTempFragmentFile(javaCodeGeneratorInfo);
124 updateLeafInfoInTempFragmentFiles(javaCodeGeneratorInfo);
125
126 }
127
128 /**
129 * Process generate code entry of YANG node.
130 *
131 * @param javaCodeGeneratorInfo YANG java file info node
janani bde4ffab2016-04-15 16:18:30 +0530132 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530133 * @param isMultiInstance flag to indicate whether it's a list
134 * @throws IOException IO operations fails
135 */
janani bde4ffab2016-04-15 16:18:30 +0530136 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530137 boolean isMultiInstance) throws IOException {
138 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
139 // TODO:throw exception
140 }
janani bde4ffab2016-04-15 16:18:30 +0530141 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin);
142 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530143
144 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
145 .addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
146 }
147
148 /**
149 * Process generate code entry of root node.
150 *
151 * @param javaCodeGeneratorInfo YANG java file info node
janani bde4ffab2016-04-15 16:18:30 +0530152 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530153 * @param rootPkg package of the root node
154 * @throws IOException IO operations fails
155 */
janani bde4ffab2016-04-15 16:18:30 +0530156 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530157 String rootPkg) throws IOException {
158 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
159 // TODO:throw exception
160 }
janani bde4ffab2016-04-15 16:18:30 +0530161 updatePackageInfo(javaCodeGeneratorInfo, yangPlugin, rootPkg);
162 generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530163 }
164}