Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +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.javamodel; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | |
| 21 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
| 22 | import org.onosproject.yangutils.datamodel.YangInput; |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | import org.onosproject.yangutils.datamodel.YangOutput; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 25 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaRpc; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 28 | import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo; |
| 29 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 30 | import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 32 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 34 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
| 35 | import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 36 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 37 | |
| 38 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode; |
| 39 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 40 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedTypeInfoOfCurNode; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.updatePackageInfo; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Represents rpc information extended to support java code generation. |
| 47 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 48 | public class YangJavaRpcTranslator |
| 49 | extends YangJavaRpc |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 50 | implements JavaCodeGenerator, JavaCodeGeneratorInfo { |
| 51 | |
| 52 | private static final long serialVersionUID = 806201622L; |
| 53 | |
| 54 | /** |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 55 | * Temporary file for code generation. |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 56 | */ |
| 57 | private transient TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles; |
| 58 | |
| 59 | /** |
| 60 | * Creates an instance of YANG java rpc. |
| 61 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 62 | public YangJavaRpcTranslator() { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 63 | super(); |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 64 | setJavaFileInfo(new JavaFileInfoTranslator()); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the generated java file information. |
| 69 | * |
| 70 | * @return generated java file information |
| 71 | */ |
| 72 | @Override |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 73 | public JavaFileInfoTranslator getJavaFileInfo() { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 74 | |
| 75 | if (javaFileInfo == null) { |
| 76 | throw new TranslatorException("missing java info in java datamodel node"); |
| 77 | } |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 78 | return (JavaFileInfoTranslator) javaFileInfo; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sets the java file info object. |
| 83 | * |
| 84 | * @param javaInfo java file info object |
| 85 | */ |
| 86 | @Override |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 87 | public void setJavaFileInfo(JavaFileInfoTranslator javaInfo) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 88 | javaFileInfo = javaInfo; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
| 93 | return tempJavaCodeFragmentFiles; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 98 | tempJavaCodeFragmentFiles = fileHandle; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Prepares the information for java code generation corresponding to YANG |
| 103 | * RPC info. |
| 104 | * |
| 105 | * @param yangPlugin YANG plugin config |
| 106 | * @throws TranslatorException translator operations fails |
| 107 | */ |
| 108 | @Override |
| 109 | public void generateCodeEntry(YangPluginConfig yangPlugin) |
| 110 | throws TranslatorException { |
| 111 | |
| 112 | // Add package information for rpc and create corresponding folder. |
| 113 | try { |
| 114 | updatePackageInfo(this, yangPlugin); |
| 115 | } catch (IOException e) { |
| 116 | throw new TranslatorException("Failed to prepare generate code entry for RPC node " + getName()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Creates a java file using the YANG RPC info. |
| 122 | * |
| 123 | * @throws TranslatorException translator operations fails |
| 124 | */ |
| 125 | @Override |
| 126 | public void generateCodeExit() |
| 127 | throws TranslatorException { |
| 128 | // Get the parent module/sub-module. |
| 129 | YangNode parent = getParentNodeInGenCode(this); |
| 130 | |
| 131 | // Parent should be holder of rpc or notification. |
| 132 | if (!(parent instanceof RpcNotificationContainer)) { |
| 133 | throw new TranslatorException("parent node of rpc can only be module or sub-module"); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Create attribute info for input and output of rpc and add it to the |
| 138 | * parent import list. |
| 139 | */ |
| 140 | |
| 141 | JavaAttributeInfo javaAttributeInfoOfInput = null; |
| 142 | JavaAttributeInfo javaAttributeInfoOfOutput = null; |
| 143 | |
| 144 | // Get the child input and output node and obtain create java attribute |
| 145 | // info. |
| 146 | YangNode yangNode = getChild(); |
| 147 | while (yangNode != null) { |
| 148 | if (yangNode instanceof YangInput) { |
| 149 | javaAttributeInfoOfInput = getChildNodeAsAttributeInParentService(yangNode, this); |
| 150 | |
| 151 | } else if (yangNode instanceof YangOutput) { |
| 152 | javaAttributeInfoOfOutput = getChildNodeAsAttributeInParentService(yangNode, this); |
| 153 | } else { |
| 154 | throw new TranslatorException("RPC should contain only input/output child nodes."); |
| 155 | } |
| 156 | yangNode = yangNode.getNextSibling(); |
| 157 | } |
| 158 | |
| 159 | if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) { |
| 160 | throw new TranslatorException("missing parent temp file handle"); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Add the rpc information to the parent's service temp file. |
| 165 | */ |
| 166 | try { |
| 167 | |
| 168 | ((TempJavaCodeFragmentFilesContainer) parent).getTempJavaCodeFragmentFiles().getServiceTempFiles() |
| 169 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, |
| 170 | ((JavaFileInfoContainer) parent).getJavaFileInfo().getPluginConfig(), getName()); |
| 171 | |
| 172 | } catch (IOException e) { |
| 173 | throw new TranslatorException("Failed to generate code for RPC node " + getName()); |
| 174 | } |
| 175 | // No file will be generated during RPC exit. |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Creates an attribute info object corresponding to a data model node and |
| 180 | * return it. |
| 181 | * |
| 182 | * @param childNode child data model node(input / output) for which the java code generation |
| 183 | * is being handled |
| 184 | * @param currentNode parent node (module / sub-module) in which the child node is an attribute |
| 185 | * @return AttributeInfo attribute details required to add in temporary |
| 186 | * files |
| 187 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 188 | private JavaAttributeInfo getChildNodeAsAttributeInParentService( |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 189 | YangNode childNode, YangNode currentNode) { |
| 190 | |
| 191 | YangNode parentNode = getParentNodeInGenCode(currentNode); |
| 192 | |
| 193 | String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName(); |
| 194 | /* |
| 195 | * Get the import info corresponding to the attribute for import in |
| 196 | * generated java files or qualified access |
| 197 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 198 | JavaQualifiedTypeInfoTranslator qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(childNode, |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 199 | getCapitalCase(childNodeName)); |
| 200 | if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) { |
| 201 | throw new TranslatorException("Parent node does not have file info"); |
| 202 | } |
| 203 | |
| 204 | TempJavaFragmentFiles tempJavaFragmentFiles; |
| 205 | tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode) |
| 206 | .getTempJavaCodeFragmentFiles() |
| 207 | .getServiceTempFiles(); |
| 208 | |
| 209 | if (tempJavaFragmentFiles == null) { |
| 210 | throw new TranslatorException("Parent node does not have service file info"); |
| 211 | } |
| 212 | boolean isQualified = addImportToService(qualifiedTypeInfo); |
| 213 | return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Adds to service class import list. |
| 218 | * |
| 219 | * @param importInfo import info |
| 220 | * @return true or false |
| 221 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 222 | private boolean addImportToService(JavaQualifiedTypeInfoTranslator importInfo) { |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 223 | JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) getParent()).getJavaFileInfo(); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 224 | |
| 225 | if (importInfo.getClassInfo().contentEquals(SERVICE) |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 226 | || importInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 227 | return true; |
| 228 | } |
| 229 | |
| 230 | String className; |
| 231 | className = getCapitalCase(fileInfo.getJavaName()) + "Service"; |
| 232 | |
| 233 | return ((TempJavaCodeFragmentFilesContainer) getParent()).getTempJavaCodeFragmentFiles() |
| 234 | .getServiceTempFiles().getJavaImportData().addImportInfo(importInfo, |
| 235 | className, fileInfo.getPackage()); |
| 236 | } |
| 237 | |
| 238 | } |