Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 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.javamodel; |
| 18 | |
| 19 | import java.io.IOException; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 20 | |
| 21 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangInput; |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | import org.onosproject.yangutils.datamodel.YangOutput; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangRpc; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame^] | 30 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
| 31 | import org.onosproject.yangutils.translator.tojava.JavaImportData; |
| 32 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame^] | 34 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
| 35 | import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles; |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 37 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame^] | 38 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData; |
| 39 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode; |
| 41 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo; |
| 42 | |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 43 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 44 | * Represents rpc information extended to support java code generation. |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 45 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 46 | public class YangJavaRpc |
| 47 | extends YangRpc |
| 48 | implements JavaCodeGenerator, JavaCodeGeneratorInfo { |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 49 | |
| 50 | /** |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 51 | * Contains the information of the java file being generated. |
| 52 | */ |
| 53 | private JavaFileInfo javaFileInfo; |
| 54 | |
| 55 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 56 | * Temproary file for code generation. |
| 57 | */ |
| 58 | private TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles; |
| 59 | |
| 60 | /** |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 61 | * Creates an instance of YANG java rpc. |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 62 | */ |
| 63 | public YangJavaRpc() { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 64 | super(); |
| 65 | setJavaFileInfo(new JavaFileInfo()); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 69 | * Prepares the information for java code generation corresponding to YANG |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 70 | * RPC info. |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 71 | * |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 72 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 73 | * @throws IOException IO operations fails |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 74 | */ |
| 75 | @Override |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 76 | public void generateCodeEntry(YangPluginConfig yangPlugin) |
| 77 | throws IOException { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 78 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 79 | if (!(this instanceof JavaCodeGeneratorInfo)) { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 80 | // TODO:throw exception |
| 81 | } |
| 82 | |
| 83 | // Add package information for rpc and create corresponding folder. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 84 | updatePackageInfo((JavaCodeGeneratorInfo) this, yangPlugin); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 85 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Creates a java file using the YANG RPC info. |
| 90 | * |
| 91 | * @throws IOException IO operations fails |
| 92 | */ |
| 93 | @Override |
| 94 | public void generateCodeExit() |
| 95 | throws IOException { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 96 | // Get the parent module/sub-module. |
| 97 | YangNode parent = getParentNodeInGenCode((YangNode) this); |
| 98 | |
| 99 | // Parent should be holder of rpc or notification. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 100 | if (!(parent instanceof RpcNotificationContainer)) { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 101 | throw new TranslatorException("parent node of rpc can only be module or sub-module"); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Create attribute info for input and output of rpc and add it to the |
| 106 | * parent import list. |
| 107 | */ |
| 108 | |
| 109 | JavaAttributeInfo javaAttributeInfoOfInput = null; |
| 110 | JavaAttributeInfo javaAttributeInfoOfOutput = null; |
| 111 | |
| 112 | // Get the child input and output node and obtain create java attribute info. |
| 113 | YangNode yangNode = this.getChild(); |
| 114 | while (yangNode != null) { |
| 115 | if (yangNode instanceof YangInput) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame^] | 116 | javaAttributeInfoOfInput = getChildNodeAsAttributeInParentService(yangNode, this); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 117 | } else if (yangNode instanceof YangOutput) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame^] | 118 | javaAttributeInfoOfOutput = getChildNodeAsAttributeInParentService(yangNode, this); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 119 | } else { |
| 120 | // TODO throw exception |
| 121 | } |
| 122 | yangNode = yangNode.getNextSibling(); |
| 123 | } |
| 124 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 125 | if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 126 | throw new TranslatorException("missing parent temp file handle"); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Add the rpc information to the parent's service temp file. |
| 131 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 132 | ((TempJavaCodeFragmentFilesContainer) parent) |
| 133 | .getTempJavaCodeFragmentFiles().getServiceTempFiles() |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 134 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, |
| 135 | ((YangNode) this).getName()); |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 136 | // No file will be generated during RPC exit. |
| 137 | } |
| 138 | |
| 139 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame^] | 140 | * Creates an attribute info object corresponding to a data model node and |
| 141 | * return it. |
| 142 | * |
| 143 | * @param childNode child data model node(input / output) for which the java code generation |
| 144 | * is being handled |
| 145 | * @param currentNode parent node (module / sub-module) in which the child node is an attribute |
| 146 | * @return AttributeInfo attribute details required to add in temporary |
| 147 | * files |
| 148 | */ |
| 149 | public static JavaAttributeInfo getChildNodeAsAttributeInParentService( |
| 150 | YangNode childNode, YangNode currentNode) { |
| 151 | |
| 152 | YangNode parentNode = getParentNodeInGenCode(currentNode); |
| 153 | |
| 154 | String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName(); |
| 155 | /* |
| 156 | * Get the import info corresponding to the attribute for import in |
| 157 | * generated java files or qualified access |
| 158 | */ |
| 159 | JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(currentNode, |
| 160 | childNodeName); |
| 161 | if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) { |
| 162 | throw new TranslatorException("Parent node does not have file info"); |
| 163 | } |
| 164 | |
| 165 | TempJavaFragmentFiles tempJavaFragmentFiles; |
| 166 | tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode) |
| 167 | .getTempJavaCodeFragmentFiles() |
| 168 | .getServiceTempFiles(); |
| 169 | |
| 170 | if (tempJavaFragmentFiles == null) { |
| 171 | throw new TranslatorException("Parent node does not have service file info"); |
| 172 | } |
| 173 | |
| 174 | JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData(); |
| 175 | boolean isQualified = parentImportData.addImportInfo(qualifiedTypeInfo); |
| 176 | return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false); |
| 177 | } |
| 178 | |
| 179 | /** |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 180 | * Returns the generated java file information. |
| 181 | * |
| 182 | * @return generated java file information |
| 183 | */ |
| 184 | @Override |
| 185 | public JavaFileInfo getJavaFileInfo() { |
| 186 | |
| 187 | if (javaFileInfo == null) { |
| 188 | throw new TranslatorException("missing java info in java datamodel node"); |
| 189 | } |
| 190 | return javaFileInfo; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Sets the java file info object. |
| 195 | * |
| 196 | * @param javaInfo java file info object |
| 197 | */ |
| 198 | @Override |
| 199 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 200 | javaFileInfo = javaInfo; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 201 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 202 | |
| 203 | @Override |
| 204 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
| 205 | return tempJavaCodeFragmentFiles; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 210 | tempJavaCodeFragmentFiles = fileHandle; |
| 211 | } |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 212 | } |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 213 | |