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; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.HasRpcNotification; |
| 21 | import org.onosproject.yangutils.datamodel.YangInput; |
| 22 | import org.onosproject.yangutils.datamodel.YangNode; |
| 23 | import org.onosproject.yangutils.datamodel.YangOutput; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangRpc; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 26 | import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo; |
| 27 | import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles; |
| 28 | import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 32 | |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent; |
| 34 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode; |
| 35 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo; |
| 36 | |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 37 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 38 | * Represents rpc information extended to support java code generation. |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 39 | */ |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 40 | public class YangJavaRpc extends YangRpc implements JavaCodeGenerator, HasJavaFileInfo { |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 41 | |
| 42 | /** |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 43 | * Contains the information of the java file being generated. |
| 44 | */ |
| 45 | private JavaFileInfo javaFileInfo; |
| 46 | |
| 47 | /** |
| 48 | * Creates an instance of YANG java rpc. |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 49 | */ |
| 50 | public YangJavaRpc() { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 51 | super(); |
| 52 | setJavaFileInfo(new JavaFileInfo()); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 56 | * Prepares the information for java code generation corresponding to YANG |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 57 | * rpc info. |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 58 | * |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 59 | * @param yangPlugin YANG plugin config |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 60 | * @throws IOException IO operations fails |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 61 | */ |
| 62 | @Override |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 63 | public void generateCodeEntry(YangPluginConfig yangPlugin) throws IOException { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 64 | |
| 65 | if (!(this instanceof YangNode)) { |
| 66 | // TODO:throw exception |
| 67 | } |
| 68 | |
| 69 | // Add package information for rpc and create corresponding folder. |
| 70 | updatePackageInfo(this, yangPlugin); |
| 71 | |
| 72 | // Get the parent module/sub-module. |
| 73 | YangNode parent = getParentNodeInGenCode((YangNode) this); |
| 74 | |
| 75 | // Parent should be holder of rpc or notification. |
| 76 | if (!(parent instanceof HasRpcNotification)) { |
| 77 | throw new TranslatorException("parent node of rpc can only be module or sub-module"); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Create attribute info for input and output of rpc and add it to the |
| 82 | * parent import list. |
| 83 | */ |
| 84 | |
| 85 | JavaAttributeInfo javaAttributeInfoOfInput = null; |
| 86 | JavaAttributeInfo javaAttributeInfoOfOutput = null; |
| 87 | |
| 88 | // Get the child input and output node and obtain create java attribute info. |
| 89 | YangNode yangNode = this.getChild(); |
| 90 | while (yangNode != null) { |
| 91 | if (yangNode instanceof YangInput) { |
| 92 | javaAttributeInfoOfInput = getCurNodeAsAttributeInParent(parent, false, yangNode.getName()); |
| 93 | } else if (yangNode instanceof YangOutput) { |
| 94 | javaAttributeInfoOfOutput = getCurNodeAsAttributeInParent(parent, false, yangNode.getName()); |
| 95 | } else { |
| 96 | // TODO throw exception |
| 97 | } |
| 98 | yangNode = yangNode.getNextSibling(); |
| 99 | } |
| 100 | |
| 101 | if (!(parent instanceof HasTempJavaCodeFragmentFiles)) { |
| 102 | throw new TranslatorException("missing parent temp file handle"); |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Add the rpc information to the parent's service temp file. |
| 107 | */ |
| 108 | ((HasTempJavaCodeFragmentFiles) parent) |
| 109 | .getTempJavaCodeFragmentFiles() |
| 110 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, |
| 111 | ((YangNode) this).getName()); |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 115 | * Creates a java file using the YANG rpc info. |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 116 | * |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 117 | * @throws IOException IO operations fails |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 118 | */ |
| 119 | @Override |
| 120 | public void generateCodeExit() throws IOException { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 121 | // No file will be generated during RPC exit. |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the generated java file information. |
| 126 | * |
| 127 | * @return generated java file information |
| 128 | */ |
| 129 | @Override |
| 130 | public JavaFileInfo getJavaFileInfo() { |
| 131 | |
| 132 | if (javaFileInfo == null) { |
| 133 | throw new TranslatorException("missing java info in java datamodel node"); |
| 134 | } |
| 135 | return javaFileInfo; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Sets the java file info object. |
| 140 | * |
| 141 | * @param javaInfo java file info object |
| 142 | */ |
| 143 | @Override |
| 144 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 145 | javaFileInfo = javaInfo; |
Vidyashree Rama | 09e1ca5 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 146 | } |
| 147 | } |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 148 | |