Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
| 19 | import java.io.IOException; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.HasType; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangAugment; |
| 22 | import org.onosproject.yangutils.datamodel.YangCase; |
| 23 | import org.onosproject.yangutils.datamodel.YangChoice; |
| 24 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 25 | import org.onosproject.yangutils.datamodel.YangInput; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangList; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangNotification; |
| 30 | import org.onosproject.yangutils.datamodel.YangOutput; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 32 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo; |
| 33 | |
| 34 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 35 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 36 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage; |
| 37 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
| 41 | |
| 42 | /** |
| 43 | * Represents utility class for YANG java model. |
| 44 | */ |
| 45 | public 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 Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 57 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 58 | * @throws IOException IO operations fails |
| 59 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 60 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 61 | throws IOException { |
| 62 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 63 | .setJavaName(getCaptialCase( |
| 64 | getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver()))); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 65 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo)); |
| 66 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 67 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 68 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Updates YANG java file package information for specified package. |
| 73 | * |
| 74 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 75 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 76 | * @throws IOException IO operations fails |
| 77 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 78 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 79 | String pkg) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 80 | throws IOException { |
| 81 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 82 | .setJavaName(getCaptialCase( |
| 83 | getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver()))); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 84 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg); |
| 85 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 86 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 87 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 88 | } |
| 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 Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 113 | if (javaCodeGeneratorInfo instanceof YangLeavesHolder) { |
| 114 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 115 | .addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 116 | } else if (javaCodeGeneratorInfo instanceof HasType) { |
| 117 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 118 | .addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 119 | } else { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 120 | //TODO throw exception |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Process generate code entry of YANG node. |
| 126 | * |
| 127 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 128 | * @param codeGenDir code generation directory |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 129 | * @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 Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 145 | * @param yangPlugin YANG plugin config |
| 146 | * @param isMultiInstance flag to indicate whether it's a list |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 147 | * @throws IOException IO operations fails |
| 148 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 149 | public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 150 | boolean isMultiInstance) throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 151 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 152 | // TODO:throw exception |
| 153 | } |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 154 | updatePackageInfo(javaCodeGeneratorInfo, yangPlugin); |
| 155 | generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 156 | |
| 157 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 158 | .addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 159 | |
| 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 Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 177 | * 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 Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 194 | * Process generate code entry of root node. |
| 195 | * |
| 196 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 197 | * @param yangPlugin YANG plugin config |
| 198 | * @param rootPkg package of the root node |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 199 | * @throws IOException IO operations fails |
| 200 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 201 | public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 202 | String rootPkg) throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 203 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 204 | // TODO:throw exception |
| 205 | } |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 206 | updatePackageInfo(javaCodeGeneratorInfo, yangPlugin, rootPkg); |
| 207 | generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 208 | } |
| 209 | } |