Gaurav Agrawal | 1c8f80c | 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; |
| 20 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 21 | import org.onosproject.yangutils.datamodel.YangNode; |
| 22 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 23 | import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo; |
| 24 | |
| 25 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 26 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 27 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage; |
| 28 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
| 29 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
| 30 | |
| 31 | /** |
| 32 | * Represents utility class for YANG java model. |
| 33 | */ |
| 34 | public 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 b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 46 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 47 | * @throws IOException IO operations fails |
| 48 | */ |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 49 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin) |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 50 | throws IOException { |
| 51 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 52 | .setJavaName(getCaptialCase( |
| 53 | getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver()))); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 54 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo)); |
| 55 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 56 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 57 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Updates YANG java file package information for specified package. |
| 62 | * |
| 63 | * @param javaCodeGeneratorInfo YANG java file info node |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 64 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 65 | * @throws IOException IO operations fails |
| 66 | */ |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 67 | private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
| 68 | String pkg) |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 69 | throws IOException { |
| 70 | javaCodeGeneratorInfo.getJavaFileInfo() |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 71 | .setJavaName(getCaptialCase( |
| 72 | getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(), yangPlugin.getConflictResolver()))); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 73 | javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg); |
| 74 | javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath( |
| 75 | getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage())); |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 76 | javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 77 | } |
| 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 Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 102 | 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 b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 132 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 133 | * @param isMultiInstance flag to indicate whether it's a list |
| 134 | * @throws IOException IO operations fails |
| 135 | */ |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 136 | public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 137 | boolean isMultiInstance) throws IOException { |
| 138 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 139 | // TODO:throw exception |
| 140 | } |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 141 | updatePackageInfo(javaCodeGeneratorInfo, yangPlugin); |
| 142 | generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 143 | |
| 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 b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 152 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 153 | * @param rootPkg package of the root node |
| 154 | * @throws IOException IO operations fails |
| 155 | */ |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 156 | public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin, |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 157 | String rootPkg) throws IOException { |
| 158 | if (!(javaCodeGeneratorInfo instanceof YangNode)) { |
| 159 | // TODO:throw exception |
| 160 | } |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame^] | 161 | updatePackageInfo(javaCodeGeneratorInfo, yangPlugin, rootPkg); |
| 162 | generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir()); |
Gaurav Agrawal | 1c8f80c | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 163 | } |
| 164 | } |