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; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 34 | |
| 35 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 36 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 37 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage; |
| 38 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO; |
| 40 | import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
| 42 | |
| 43 | /** |
| 44 | * Represents utility class for YANG java model. |
| 45 | */ |
| 46 | public final class YangJavaModelUtils { |
| 47 | |
| 48 | /** |
| 49 | * Creates YANG java model utility. |
| 50 | */ |
| 51 | private YangJavaModelUtils() { |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Updates YANG java file package information. |
| 56 | * |
| 57 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 58 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 59 | * @throws IOException IO operations fails |
| 60 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 61 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 62 | throws IOException { |
| 63 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 64 | .setJavaName(getCaptialCase( |
| 65 | getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver()))); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 66 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo)); |
| 67 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 68 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 69 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Updates YANG java file package information for specified package. |
| 74 | * |
| 75 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 76 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 77 | * @throws IOException IO operations fails |
| 78 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 79 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 80 | String pkg) |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 81 | throws IOException { |
| 82 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 83 | .setJavaName(getCaptialCase( |
| 84 | getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver()))); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 85 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg); |
| 86 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 87 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 88 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Updates temporary java code fragment files. |
| 93 | * |
| 94 | * @param javaCodeGeneratorInfo YANG java file info node |
| 95 | * @throws IOException IO operations fails |
| 96 | */ |
| 97 | private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo) throws IOException { |
| 98 | String absolutePath = getAbsolutePackagePath(javaCodeGeneratorInfo.getJavaFileInfo().getBaseCodeGenPath(), |
| 99 | javaCodeGeneratorInfo.getJavaFileInfo().getPackageFilePath()); |
| 100 | |
| 101 | javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles( |
| 102 | new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo().getGeneratedFileTypes(), |
| 103 | absolutePath, javaCodeGeneratorInfo.getJavaFileInfo().getJavaName())); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Updates leaf information in temporary java code fragment files. |
| 108 | * |
| 109 | * @param javaCodeGeneratorInfo YANG java file info node |
| 110 | * @throws IOException IO operations fails |
| 111 | */ |
| 112 | private static void updateLeafInfoInTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo) |
| 113 | throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 114 | if (javaCodeGeneratorInfo instanceof YangLeavesHolder) { |
| 115 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 116 | .addCurNodeLeavesInfoToTempFiles((YangNode) javaCodeGeneratorInfo); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 117 | } else if (javaCodeGeneratorInfo instanceof HasType) { |
| 118 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 119 | .addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 120 | } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) { |
| 121 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 122 | .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 123 | } else { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 124 | //TODO throw exception |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Process generate code entry of YANG node. |
| 130 | * |
| 131 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 132 | * @param codeGenDir code generation directory |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 133 | * @throws IOException IO operations fails |
| 134 | */ |
| 135 | private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo, String codeGenDir) |
| 136 | throws IOException { |
| 137 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 138 | // TODO:throw exception |
| 139 | } |
| 140 | createTempFragmentFile(javaCodeGeneratorInfo); |
| 141 | updateLeafInfoInTempFragmentFiles(javaCodeGeneratorInfo); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Process generate code entry of YANG node. |
| 147 | * |
| 148 | * @param javaCodeGeneratorInfo YANG java file info node |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 149 | * @param yangPlugin YANG plugin config |
| 150 | * @param isMultiInstance flag to indicate whether it's a list |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 151 | * @throws IOException IO operations fails |
| 152 | */ |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 153 | public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 154 | boolean isMultiInstance) throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 155 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 156 | // TODO:throw exception |
| 157 | } |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 158 | updatePackageInfo(javaCodeGeneratorInfo, yangPlugin); |
| 159 | generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 160 | |
| 161 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 162 | .addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance); |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * For augmentation of nodes. |
| 166 | */ |
| 167 | if (javaCodeGeneratorInfo instanceof YangContainer |
| 168 | || javaCodeGeneratorInfo instanceof YangCase |
| 169 | || javaCodeGeneratorInfo instanceof YangChoice |
| 170 | || javaCodeGeneratorInfo instanceof YangInput |
| 171 | || javaCodeGeneratorInfo instanceof YangList |
| 172 | || javaCodeGeneratorInfo instanceof YangNotification |
| 173 | || javaCodeGeneratorInfo instanceof YangOutput) { |
| 174 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(HAS_AUGMENTATION); |
| 175 | } else if (javaCodeGeneratorInfo instanceof YangAugment) { |
| 176 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO); |
| 177 | } |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 181 | * Process generate code entry of YANG type. |
| 182 | * |
| 183 | * @param javaCodeGeneratorInfo YANG java file info node |
| 184 | * @param yangPlugin YANG plugin config |
| 185 | * @param isMultiInstance flag to indicate whether it's a list |
| 186 | * @throws IOException IO operations fails |
| 187 | */ |
| 188 | public static void generateCodeOfType(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
| 189 | boolean isMultiInstance) throws IOException { |
| 190 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 191 | // TODO:throw exception |
| 192 | } |
| 193 | updatePackageInfo(javaCodeGeneratorInfo, yangPlugin); |
| 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 | } |