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