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; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaRpc; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 27 | import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo; |
| 28 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator; |
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) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 76 | throw new TranslatorException("missing java info in java datamodel node " + |
| 77 | getName() + " in " + |
| 78 | getLineNumber() + " at " + |
| 79 | getCharPosition() |
| 80 | + " in " + getFileName()); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 81 | } |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 82 | return (JavaFileInfoTranslator) javaFileInfo; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Sets the java file info object. |
| 87 | * |
| 88 | * @param javaInfo java file info object |
| 89 | */ |
| 90 | @Override |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 91 | public void setJavaFileInfo(JavaFileInfoTranslator javaInfo) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 92 | javaFileInfo = javaInfo; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
| 97 | return tempJavaCodeFragmentFiles; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 102 | tempJavaCodeFragmentFiles = fileHandle; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Prepares the information for java code generation corresponding to YANG |
| 107 | * RPC info. |
| 108 | * |
| 109 | * @param yangPlugin YANG plugin config |
| 110 | * @throws TranslatorException translator operations fails |
| 111 | */ |
| 112 | @Override |
| 113 | public void generateCodeEntry(YangPluginConfig yangPlugin) |
| 114 | throws TranslatorException { |
| 115 | |
| 116 | // Add package information for rpc and create corresponding folder. |
| 117 | try { |
| 118 | updatePackageInfo(this, yangPlugin); |
| 119 | } catch (IOException e) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 120 | throw new TranslatorException("Failed to prepare generate code entry for RPC node " + |
| 121 | getName() + " in " + |
| 122 | getLineNumber() + " at " + |
| 123 | getCharPosition() |
| 124 | + " in " + getFileName() + " " + e.getLocalizedMessage()); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Creates a java file using the YANG RPC info. |
| 130 | * |
| 131 | * @throws TranslatorException translator operations fails |
| 132 | */ |
| 133 | @Override |
| 134 | public void generateCodeExit() |
| 135 | throws TranslatorException { |
| 136 | // Get the parent module/sub-module. |
| 137 | YangNode parent = getParentNodeInGenCode(this); |
| 138 | |
| 139 | // Parent should be holder of rpc or notification. |
| 140 | if (!(parent instanceof RpcNotificationContainer)) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 141 | throw new TranslatorException("parent node of rpc can only be module or sub-module " + |
| 142 | getName() + " in " + |
| 143 | getLineNumber() + " at " + |
| 144 | getCharPosition() |
| 145 | + " in " + getFileName()); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Create attribute info for input and output of rpc and add it to the |
| 150 | * parent import list. |
| 151 | */ |
| 152 | |
| 153 | JavaAttributeInfo javaAttributeInfoOfInput = null; |
| 154 | JavaAttributeInfo javaAttributeInfoOfOutput = null; |
| 155 | |
| 156 | // Get the child input and output node and obtain create java attribute |
| 157 | // info. |
| 158 | YangNode yangNode = getChild(); |
| 159 | while (yangNode != null) { |
| 160 | if (yangNode instanceof YangInput) { |
| 161 | javaAttributeInfoOfInput = getChildNodeAsAttributeInParentService(yangNode, this); |
| 162 | |
| 163 | } else if (yangNode instanceof YangOutput) { |
| 164 | javaAttributeInfoOfOutput = getChildNodeAsAttributeInParentService(yangNode, this); |
| 165 | } else { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 166 | throw new TranslatorException("RPC should contain only input/output child nodes. " + |
| 167 | yangNode.getName() + " in " + |
| 168 | yangNode.getLineNumber() + " at " + |
| 169 | yangNode.getCharPosition() |
| 170 | + " in " + yangNode.getFileName()); |
| 171 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 172 | } |
| 173 | yangNode = yangNode.getNextSibling(); |
| 174 | } |
| 175 | |
| 176 | if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 177 | throw new TranslatorException("missing parent temp file handle " + |
| 178 | getName() + " in " + |
| 179 | getLineNumber() + " at " + |
| 180 | getCharPosition() |
| 181 | + " in " + getFileName()); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Add the rpc information to the parent's service temp file. |
| 186 | */ |
| 187 | try { |
| 188 | |
| 189 | ((TempJavaCodeFragmentFilesContainer) parent).getTempJavaCodeFragmentFiles().getServiceTempFiles() |
| 190 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, |
| 191 | ((JavaFileInfoContainer) parent).getJavaFileInfo().getPluginConfig(), getName()); |
| 192 | |
| 193 | } catch (IOException e) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 194 | throw new TranslatorException("Failed to generate code for RPC node " + |
| 195 | getName() + " in " + |
| 196 | getLineNumber() + " at " + |
| 197 | getCharPosition() |
| 198 | + " in " + getFileName() + " " + e.getLocalizedMessage()); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 199 | } |
| 200 | // No file will be generated during RPC exit. |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Creates an attribute info object corresponding to a data model node and |
| 205 | * return it. |
| 206 | * |
| 207 | * @param childNode child data model node(input / output) for which the java code generation |
| 208 | * is being handled |
| 209 | * @param currentNode parent node (module / sub-module) in which the child node is an attribute |
| 210 | * @return AttributeInfo attribute details required to add in temporary |
| 211 | * files |
| 212 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 213 | private JavaAttributeInfo getChildNodeAsAttributeInParentService( |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 214 | YangNode childNode, YangNode currentNode) { |
| 215 | |
| 216 | YangNode parentNode = getParentNodeInGenCode(currentNode); |
| 217 | |
| 218 | String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName(); |
| 219 | /* |
| 220 | * Get the import info corresponding to the attribute for import in |
| 221 | * generated java files or qualified access |
| 222 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 223 | JavaQualifiedTypeInfoTranslator qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(childNode, |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 224 | getCapitalCase(childNodeName)); |
| 225 | if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) { |
| 226 | throw new TranslatorException("Parent node does not have file info"); |
| 227 | } |
| 228 | |
| 229 | TempJavaFragmentFiles tempJavaFragmentFiles; |
| 230 | tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode) |
| 231 | .getTempJavaCodeFragmentFiles() |
| 232 | .getServiceTempFiles(); |
| 233 | |
| 234 | if (tempJavaFragmentFiles == null) { |
| 235 | throw new TranslatorException("Parent node does not have service file info"); |
| 236 | } |
| 237 | boolean isQualified = addImportToService(qualifiedTypeInfo); |
| 238 | return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Adds to service class import list. |
| 243 | * |
| 244 | * @param importInfo import info |
| 245 | * @return true or false |
| 246 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 247 | private boolean addImportToService(JavaQualifiedTypeInfoTranslator importInfo) { |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 248 | JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) getParent()).getJavaFileInfo(); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 249 | |
| 250 | if (importInfo.getClassInfo().contentEquals(SERVICE) |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 251 | || importInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 252 | return true; |
| 253 | } |
| 254 | |
| 255 | String className; |
| 256 | className = getCapitalCase(fileInfo.getJavaName()) + "Service"; |
| 257 | |
| 258 | return ((TempJavaCodeFragmentFilesContainer) getParent()).getTempJavaCodeFragmentFiles() |
| 259 | .getServiceTempFiles().getJavaImportData().addImportInfo(importInfo, |
| 260 | className, fileInfo.getPackage()); |
| 261 | } |
| 262 | |
| 263 | } |