blob: d63b8871d386bd82716030882e76e88da1368983 [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,
138 ((YangNode) this).getName());
139 } catch (IOException e) {
140 throw new TranslatorException("Failed to generate code for RPC node " + this.getName());
141 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530142 // No file will be generated during RPC exit.
143 }
144
145 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530146 * Creates an attribute info object corresponding to a data model node and
147 * return it.
148 *
149 * @param childNode child data model node(input / output) for which the java code generation
150 * is being handled
151 * @param currentNode parent node (module / sub-module) in which the child node is an attribute
152 * @return AttributeInfo attribute details required to add in temporary
153 * files
154 */
155 public static JavaAttributeInfo getChildNodeAsAttributeInParentService(
156 YangNode childNode, YangNode currentNode) {
157
158 YangNode parentNode = getParentNodeInGenCode(currentNode);
159
160 String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName();
161 /*
162 * Get the import info corresponding to the attribute for import in
163 * generated java files or qualified access
164 */
165 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(currentNode,
janani b4a6711a2016-05-17 13:12:22 +0530166 getCapitalCase(childNodeName));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530167 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
168 throw new TranslatorException("Parent node does not have file info");
169 }
170
171 TempJavaFragmentFiles tempJavaFragmentFiles;
172 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode)
173 .getTempJavaCodeFragmentFiles()
174 .getServiceTempFiles();
175
176 if (tempJavaFragmentFiles == null) {
177 throw new TranslatorException("Parent node does not have service file info");
178 }
179
180 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
181 boolean isQualified = parentImportData.addImportInfo(qualifiedTypeInfo);
182 return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false);
183 }
184
185 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530186 * Returns the generated java file information.
187 *
188 * @return generated java file information
189 */
190 @Override
191 public JavaFileInfo getJavaFileInfo() {
192
193 if (javaFileInfo == null) {
194 throw new TranslatorException("missing java info in java datamodel node");
195 }
196 return javaFileInfo;
197 }
198
199 /**
200 * Sets the java file info object.
201 *
202 * @param javaInfo java file info object
203 */
204 @Override
205 public void setJavaFileInfo(JavaFileInfo javaInfo) {
206 javaFileInfo = javaInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530207 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530208
209 @Override
210 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
211 return tempJavaCodeFragmentFiles;
212 }
213
214 @Override
215 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
216 tempJavaCodeFragmentFiles = fileHandle;
217 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530218
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530219}