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