blob: 6e6482b6898ac084e514208b6c4f2c86beb01e70 [file] [log] [blame]
Bharat saraswalc2d3be12016-06-16 00:29:12 +05301/*
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
17package org.onosproject.yangutils.translator.tojava.javamodel;
18
19import java.io.IOException;
20
21import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
22import org.onosproject.yangutils.datamodel.YangInput;
23import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.datamodel.YangOutput;
Shankara-Huaweib7564772016-08-02 18:13:13 +053025import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaRpc;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053026import org.onosproject.yangutils.translator.exception.TranslatorException;
27import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
28import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Bharat saraswale50edca2016-08-05 01:58:25 +053029import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053030import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Bharat saraswale3175d32016-08-31 17:50:11 +053031import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator;
Bharat saraswale50edca2016-08-05 01:58:25 +053032import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053033import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
34import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
35import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles;
Bharat saraswale50edca2016-08-05 01:58:25 +053036import org.onosproject.yangutils.utils.io.YangPluginConfig;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053037
38import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
39import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswale50edca2016-08-05 01:58:25 +053040import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedTypeInfoOfCurNode;
Bharat saraswalaf413b82016-07-14 15:18:20 +053041import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.updatePackageInfo;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053042import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Shankara-Huaweib7564772016-08-02 18:13:13 +053043import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053044
45/**
46 * Represents rpc information extended to support java code generation.
47 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053048public class YangJavaRpcTranslator
49 extends YangJavaRpc
Bharat saraswalc2d3be12016-06-16 00:29:12 +053050 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
51
52 private static final long serialVersionUID = 806201622L;
53
54 /**
Bharat saraswal8beac342016-08-04 02:00:03 +053055 * Temporary file for code generation.
Bharat saraswalc2d3be12016-06-16 00:29:12 +053056 */
57 private transient TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles;
58
59 /**
60 * Creates an instance of YANG java rpc.
61 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053062 public YangJavaRpcTranslator() {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053063 super();
Bharat saraswale50edca2016-08-05 01:58:25 +053064 setJavaFileInfo(new JavaFileInfoTranslator());
Bharat saraswalc2d3be12016-06-16 00:29:12 +053065 }
66
67 /**
68 * Returns the generated java file information.
69 *
70 * @return generated java file information
71 */
72 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053073 public JavaFileInfoTranslator getJavaFileInfo() {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053074
75 if (javaFileInfo == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +053076 throw new TranslatorException("missing java info in java datamodel node " +
77 getName() + " in " +
78 getLineNumber() + " at " +
79 getCharPosition()
80 + " in " + getFileName());
Bharat saraswalc2d3be12016-06-16 00:29:12 +053081 }
Bharat saraswale50edca2016-08-05 01:58:25 +053082 return (JavaFileInfoTranslator) javaFileInfo;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053083 }
84
85 /**
86 * Sets the java file info object.
87 *
88 * @param javaInfo java file info object
89 */
90 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053091 public void setJavaFileInfo(JavaFileInfoTranslator javaInfo) {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053092 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 saraswale3175d32016-08-31 17:50:11 +0530120 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 saraswalc2d3be12016-06-16 00:29:12 +0530125 }
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 saraswale3175d32016-08-31 17:50:11 +0530141 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 saraswalc2d3be12016-06-16 00:29:12 +0530146 }
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 saraswale3175d32016-08-31 17:50:11 +0530166 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 saraswalc2d3be12016-06-16 00:29:12 +0530172 }
173 yangNode = yangNode.getNextSibling();
174 }
175
176 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530177 throw new TranslatorException("missing parent temp file handle " +
178 getName() + " in " +
179 getLineNumber() + " at " +
180 getCharPosition()
181 + " in " + getFileName());
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530182 }
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 saraswale3175d32016-08-31 17:50:11 +0530194 throw new TranslatorException("Failed to generate code for RPC node " +
195 getName() + " in " +
196 getLineNumber() + " at " +
197 getCharPosition()
198 + " in " + getFileName() + " " + e.getLocalizedMessage());
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530199 }
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 saraswal8beac342016-08-04 02:00:03 +0530213 private JavaAttributeInfo getChildNodeAsAttributeInParentService(
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530214 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-Huaweib7564772016-08-02 18:13:13 +0530223 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(childNode,
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530224 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-Huaweib7564772016-08-02 18:13:13 +0530247 private boolean addImportToService(JavaQualifiedTypeInfoTranslator importInfo) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530248 JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) getParent()).getJavaFileInfo();
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530249
250 if (importInfo.getClassInfo().contentEquals(SERVICE)
Bharat saraswal8beac342016-08-04 02:00:03 +0530251 || importInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))) {
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530252 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}