Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 6a72b79 | 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 | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 20 | |
| 21 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Gaurav Agrawal | 5652766 | 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 | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangRpc; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 27 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo; |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 31 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 33 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 34 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_RPC_INTERFACE; |
| 35 | import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.getCurNodeAsAttributeInParent; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode; |
| 37 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo; |
| 38 | |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 39 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 40 | * Represents rpc information extended to support java code generation. |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 41 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 42 | public class YangJavaRpc |
| 43 | extends YangRpc |
| 44 | implements JavaCodeGenerator, JavaCodeGeneratorInfo { |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 47 | * Contains the information of the java file being generated. |
| 48 | */ |
| 49 | private JavaFileInfo javaFileInfo; |
| 50 | |
| 51 | /** |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 52 | * Temproary file for code generation. |
| 53 | */ |
| 54 | private TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles; |
| 55 | |
| 56 | /** |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 57 | * Creates an instance of YANG java rpc. |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 58 | */ |
| 59 | public YangJavaRpc() { |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 60 | super(); |
| 61 | setJavaFileInfo(new JavaFileInfo()); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 62 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_RPC_INTERFACE); |
| 63 | try { |
| 64 | setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(getJavaFileInfo())); |
| 65 | } catch (IOException e) { |
| 66 | throw new RuntimeException("Failed to create temporary RPC file handle"); |
| 67 | } |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 71 | * Prepares the information for java code generation corresponding to YANG |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 72 | * RPC info. |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 73 | * |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 74 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 75 | * @throws IOException IO operations fails |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 76 | */ |
| 77 | @Override |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 78 | public void generateCodeEntry(YangPluginConfig yangPlugin) |
| 79 | throws IOException { |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 80 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 81 | if (!(this instanceof JavaCodeGeneratorInfo)) { |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 82 | // TODO:throw exception |
| 83 | } |
| 84 | |
| 85 | // Add package information for rpc and create corresponding folder. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 86 | updatePackageInfo((JavaCodeGeneratorInfo) this, yangPlugin); |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 87 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates a java file using the YANG RPC info. |
| 92 | * |
| 93 | * @throws IOException IO operations fails |
| 94 | */ |
| 95 | @Override |
| 96 | public void generateCodeExit() |
| 97 | throws IOException { |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 98 | // Get the parent module/sub-module. |
| 99 | YangNode parent = getParentNodeInGenCode((YangNode) this); |
| 100 | |
| 101 | // Parent should be holder of rpc or notification. |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 102 | if (!(parent instanceof RpcNotificationContainer)) { |
Gaurav Agrawal | 5652766 | 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 | |
| 114 | // Get the child input and output node and obtain create java attribute info. |
| 115 | YangNode yangNode = this.getChild(); |
| 116 | while (yangNode != null) { |
| 117 | if (yangNode instanceof YangInput) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 118 | javaAttributeInfoOfInput = getCurNodeAsAttributeInParent(yangNode, this, false); |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 119 | } else if (yangNode instanceof YangOutput) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 120 | javaAttributeInfoOfOutput = getCurNodeAsAttributeInParent(yangNode, this, false); |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 121 | } else { |
| 122 | // TODO throw exception |
| 123 | } |
| 124 | yangNode = yangNode.getNextSibling(); |
| 125 | } |
| 126 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 127 | if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) { |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 128 | throw new TranslatorException("missing parent temp file handle"); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Add the rpc information to the parent's service temp file. |
| 133 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 134 | ((TempJavaCodeFragmentFilesContainer) parent) |
| 135 | .getTempJavaCodeFragmentFiles().getServiceTempFiles() |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 136 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, |
| 137 | ((YangNode) this).getName()); |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 138 | // No file will be generated during RPC exit. |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the generated java file information. |
| 143 | * |
| 144 | * @return generated java file information |
| 145 | */ |
| 146 | @Override |
| 147 | public JavaFileInfo getJavaFileInfo() { |
| 148 | |
| 149 | if (javaFileInfo == null) { |
| 150 | throw new TranslatorException("missing java info in java datamodel node"); |
| 151 | } |
| 152 | return javaFileInfo; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Sets the java file info object. |
| 157 | * |
| 158 | * @param javaInfo java file info object |
| 159 | */ |
| 160 | @Override |
| 161 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 162 | javaFileInfo = javaInfo; |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 163 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame^] | 164 | |
| 165 | @Override |
| 166 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
| 167 | return tempJavaCodeFragmentFiles; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 172 | tempJavaCodeFragmentFiles = fileHandle; |
| 173 | } |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 174 | } |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 175 | |