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 | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 31 | import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 33 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 35 | |
| 36 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 37 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 38 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage; |
| 39 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO; |
| 41 | import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
| 43 | |
| 44 | /** |
| 45 | * Represents utility class for YANG java model. |
| 46 | */ |
| 47 | public 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 Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 58 | * @param hasJavaFileInfo YANG java file info node |
| 59 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 60 | * @throws IOException IO operations fails |
| 61 | */ |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 62 | public static void updatePackageInfo(HasJavaFileInfo hasJavaFileInfo, YangPluginConfig yangPlugin) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 63 | throws IOException { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 64 | hasJavaFileInfo.getJavaFileInfo() |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 65 | .setJavaName(getCaptialCase( |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 66 | 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 Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Updates YANG java file package information for specified package. |
| 75 | * |
| 76 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 77 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 78 | * @throws IOException IO operations fails |
| 79 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 80 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 81 | String pkg) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 82 | throws IOException { |
| 83 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 84 | .setJavaName(getCaptialCase( |
| 85 | getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver()))); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 86 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg); |
| 87 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 88 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 89 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 90 | } |
| 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 Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 115 | if (javaCodeGeneratorInfo instanceof YangLeavesHolder) { |
| 116 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 117 | .addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 118 | } else if (javaCodeGeneratorInfo instanceof HasType) { |
| 119 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 120 | .addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 121 | } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) { |
| 122 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 123 | .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 124 | } else { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 125 | //TODO throw exception |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Process generate code entry of YANG node. |
| 131 | * |
| 132 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 133 | * @param codeGenDir code generation directory |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 134 | * @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 Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 150 | * @param yangPlugin YANG plugin config |
| 151 | * @param isMultiInstance flag to indicate whether it's a list |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 152 | * @throws IOException IO operations fails |
| 153 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 154 | public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 155 | boolean isMultiInstance) throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 156 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 157 | // TODO:throw exception |
| 158 | } |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 159 | updatePackageInfo((HasJavaFileInfo) javaCodeGeneratorInfo, yangPlugin); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 160 | generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 161 | |
| 162 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 163 | .addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 164 | |
| 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 Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 182 | * Process generate code entry of YANG type. |
| 183 | * |
| 184 | * @param javaCodeGeneratorInfo YANG java file info node |
| 185 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 186 | * @throws IOException IO operations fails |
| 187 | */ |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 188 | public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin) |
| 189 | throws IOException { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 190 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 191 | // TODO:throw exception |
| 192 | } |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame^] | 193 | updatePackageInfo((HasJavaFileInfo) javaCodeGeneratorInfo, yangPlugin); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 194 | generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir()); |
| 195 | } |
| 196 | |
| 197 | /** |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 198 | * Process generate code entry of root node. |
| 199 | * |
| 200 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 201 | * @param yangPlugin YANG plugin config |
| 202 | * @param rootPkg package of the root node |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 203 | * @throws IOException IO operations fails |
| 204 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 205 | public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 206 | String rootPkg) throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 207 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 208 | // TODO:throw exception |
| 209 | } |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 210 | updatePackageInfo(javaCodeGeneratorInfo, yangPlugin, rootPkg); |
| 211 | generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 212 | } |
| 213 | } |