blob: e721fa78e5858301715a76fc762ab57651e3fb22 [file] [log] [blame]
Vidyashree Rama6a72b792016-03-29 12:00:42 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama6a72b792016-03-29 12:00:42 +05303 *
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
17package org.onosproject.yangutils.translator.tojava.javamodel;
18
19import java.io.IOException;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020
21import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Gaurav Agrawal56527662016-04-20 15:49:17 +053022import org.onosproject.yangutils.datamodel.YangInput;
23import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053025import org.onosproject.yangutils.datamodel.YangRpc;
Gaurav Agrawal56527662016-04-20 15:49:17 +053026import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal56527662016-04-20 15:49:17 +053027import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053028import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal56527662016-04-20 15:49:17 +053029import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
31import org.onosproject.yangutils.translator.tojava.JavaImportData;
32import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053033import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
35import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053036import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053037
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053038import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
39import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
janani b4a6711a2016-05-17 13:12:22 +053040import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Gaurav Agrawal56527662016-04-20 15:49:17 +053041import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
42import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo;
43
Vidyashree Rama6a72b792016-03-29 12:00:42 +053044/**
Bharat saraswald9822e92016-04-05 15:13:44 +053045 * Represents rpc information extended to support java code generation.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053046 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047public class YangJavaRpc
48 extends YangRpc
49 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama6a72b792016-03-29 12:00:42 +053050
51 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053052 * Contains the information of the java file being generated.
53 */
54 private JavaFileInfo javaFileInfo;
55
56 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053057 * Temproary file for code generation.
58 */
59 private TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles;
60
61 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053062 * Creates an instance of YANG java rpc.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053063 */
64 public YangJavaRpc() {
Gaurav Agrawal56527662016-04-20 15:49:17 +053065 super();
66 setJavaFileInfo(new JavaFileInfo());
Vidyashree Rama6a72b792016-03-29 12:00:42 +053067 }
68
69 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Prepares the information for java code generation corresponding to YANG
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 * RPC info.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053072 *
janani bde4ffab2016-04-15 16:18:30 +053073 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053074 * @throws TranslatorException translator operations fails
Vidyashree Rama6a72b792016-03-29 12:00:42 +053075 */
76 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053077 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Gaurav Agrawal56527662016-04-20 15:49:17 +053078
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053079 if (!(this instanceof JavaCodeGeneratorInfo)) {
Gaurav Agrawal56527662016-04-20 15:49:17 +053080 // TODO:throw exception
81 }
82
83 // Add package information for rpc and create corresponding folder.
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053084 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 Se4b9b0c2016-04-30 21:09:15 +053089 }
90
91 /**
92 * Creates a java file using the YANG RPC info.
93 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053094 * @throws TranslatorException translator operations fails
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053095 */
96 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053097 public void generateCodeExit() throws TranslatorException {
Gaurav Agrawal56527662016-04-20 15:49:17 +053098 // Get the parent module/sub-module.
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053099 YangNode parent = getParentNodeInGenCode(this);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530100
101 // Parent should be holder of rpc or notification.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530102 if (!(parent instanceof RpcNotificationContainer)) {
Gaurav Agrawal56527662016-04-20 15:49:17 +0530103 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 saraswalab4c6ba2016-05-17 14:19:38 +0530114 // Get the child input and output node and obtain create java attribute
115 // info.
Gaurav Agrawal56527662016-04-20 15:49:17 +0530116 YangNode yangNode = this.getChild();
117 while (yangNode != null) {
118 if (yangNode instanceof YangInput) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530119 javaAttributeInfoOfInput = getChildNodeAsAttributeInParentService(yangNode, this);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530120 } else if (yangNode instanceof YangOutput) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530121 javaAttributeInfoOfOutput = getChildNodeAsAttributeInParentService(yangNode, this);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530122 } else {
123 // TODO throw exception
124 }
125 yangNode = yangNode.getNextSibling();
126 }
127
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530128 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
Gaurav Agrawal56527662016-04-20 15:49:17 +0530129 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 saraswalab4c6ba2016-05-17 14:19:38 +0530135 try {
136 ((TempJavaCodeFragmentFilesContainer) parent).getTempJavaCodeFragmentFiles().getServiceTempFiles()
137 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530138 ((JavaFileInfoContainer) parent).getJavaFileInfo().getPluginConfig(),
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530139 ((YangNode) this).getName());
140 } catch (IOException e) {
141 throw new TranslatorException("Failed to generate code for RPC node " + this.getName());
142 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530143 // No file will be generated during RPC exit.
144 }
145
146 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530147 * Creates an attribute info object corresponding to a data model node and
148 * return it.
149 *
150 * @param childNode child data model node(input / output) for which the java code generation
151 * is being handled
152 * @param currentNode parent node (module / sub-module) in which the child node is an attribute
153 * @return AttributeInfo attribute details required to add in temporary
154 * files
155 */
156 public static JavaAttributeInfo getChildNodeAsAttributeInParentService(
157 YangNode childNode, YangNode currentNode) {
158
159 YangNode parentNode = getParentNodeInGenCode(currentNode);
160
161 String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName();
162 /*
163 * Get the import info corresponding to the attribute for import in
164 * generated java files or qualified access
165 */
166 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(currentNode,
janani b4a6711a2016-05-17 13:12:22 +0530167 getCapitalCase(childNodeName));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530168 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
169 throw new TranslatorException("Parent node does not have file info");
170 }
171
172 TempJavaFragmentFiles tempJavaFragmentFiles;
173 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode)
174 .getTempJavaCodeFragmentFiles()
175 .getServiceTempFiles();
176
177 if (tempJavaFragmentFiles == null) {
178 throw new TranslatorException("Parent node does not have service file info");
179 }
180
181 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
182 boolean isQualified = parentImportData.addImportInfo(qualifiedTypeInfo);
183 return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false);
184 }
185
186 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530187 * Returns the generated java file information.
188 *
189 * @return generated java file information
190 */
191 @Override
192 public JavaFileInfo getJavaFileInfo() {
193
194 if (javaFileInfo == null) {
195 throw new TranslatorException("missing java info in java datamodel node");
196 }
197 return javaFileInfo;
198 }
199
200 /**
201 * Sets the java file info object.
202 *
203 * @param javaInfo java file info object
204 */
205 @Override
206 public void setJavaFileInfo(JavaFileInfo javaInfo) {
207 javaFileInfo = javaInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530208 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530209
210 @Override
211 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
212 return tempJavaCodeFragmentFiles;
213 }
214
215 @Override
216 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
217 tempJavaCodeFragmentFiles = fileHandle;
218 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530219
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530220}