blob: e5c775efebe5f98c1d96a8c44dee8d37a443830e [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;
Bharat saraswale50edca2016-08-05 01:58:25 +053025import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator;
Shankara-Huaweib7564772016-08-02 18:13:13 +053026import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaRpc;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053027import org.onosproject.yangutils.translator.exception.TranslatorException;
28import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
29import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Bharat saraswale50edca2016-08-05 01:58:25 +053030import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053031import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
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) {
76 throw new TranslatorException("missing java info in java datamodel node");
77 }
Bharat saraswale50edca2016-08-05 01:58:25 +053078 return (JavaFileInfoTranslator) javaFileInfo;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053079 }
80
81 /**
82 * Sets the java file info object.
83 *
84 * @param javaInfo java file info object
85 */
86 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053087 public void setJavaFileInfo(JavaFileInfoTranslator javaInfo) {
Bharat saraswalc2d3be12016-06-16 00:29:12 +053088 javaFileInfo = javaInfo;
89 }
90
91 @Override
92 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
93 return tempJavaCodeFragmentFiles;
94 }
95
96 @Override
97 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
98 tempJavaCodeFragmentFiles = fileHandle;
99 }
100
101 /**
102 * Prepares the information for java code generation corresponding to YANG
103 * RPC info.
104 *
105 * @param yangPlugin YANG plugin config
106 * @throws TranslatorException translator operations fails
107 */
108 @Override
109 public void generateCodeEntry(YangPluginConfig yangPlugin)
110 throws TranslatorException {
111
112 // Add package information for rpc and create corresponding folder.
113 try {
114 updatePackageInfo(this, yangPlugin);
115 } catch (IOException e) {
116 throw new TranslatorException("Failed to prepare generate code entry for RPC node " + getName());
117 }
118 }
119
120 /**
121 * Creates a java file using the YANG RPC info.
122 *
123 * @throws TranslatorException translator operations fails
124 */
125 @Override
126 public void generateCodeExit()
127 throws TranslatorException {
128 // Get the parent module/sub-module.
129 YangNode parent = getParentNodeInGenCode(this);
130
131 // Parent should be holder of rpc or notification.
132 if (!(parent instanceof RpcNotificationContainer)) {
133 throw new TranslatorException("parent node of rpc can only be module or sub-module");
134 }
135
136 /*
137 * Create attribute info for input and output of rpc and add it to the
138 * parent import list.
139 */
140
141 JavaAttributeInfo javaAttributeInfoOfInput = null;
142 JavaAttributeInfo javaAttributeInfoOfOutput = null;
143
144 // Get the child input and output node and obtain create java attribute
145 // info.
146 YangNode yangNode = getChild();
147 while (yangNode != null) {
148 if (yangNode instanceof YangInput) {
149 javaAttributeInfoOfInput = getChildNodeAsAttributeInParentService(yangNode, this);
150
151 } else if (yangNode instanceof YangOutput) {
152 javaAttributeInfoOfOutput = getChildNodeAsAttributeInParentService(yangNode, this);
153 } else {
154 throw new TranslatorException("RPC should contain only input/output child nodes.");
155 }
156 yangNode = yangNode.getNextSibling();
157 }
158
159 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
160 throw new TranslatorException("missing parent temp file handle");
161 }
162
163 /*
164 * Add the rpc information to the parent's service temp file.
165 */
166 try {
167
168 ((TempJavaCodeFragmentFilesContainer) parent).getTempJavaCodeFragmentFiles().getServiceTempFiles()
169 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput,
170 ((JavaFileInfoContainer) parent).getJavaFileInfo().getPluginConfig(), getName());
171
172 } catch (IOException e) {
173 throw new TranslatorException("Failed to generate code for RPC node " + getName());
174 }
175 // No file will be generated during RPC exit.
176 }
177
178 /**
179 * Creates an attribute info object corresponding to a data model node and
180 * return it.
181 *
182 * @param childNode child data model node(input / output) for which the java code generation
183 * is being handled
184 * @param currentNode parent node (module / sub-module) in which the child node is an attribute
185 * @return AttributeInfo attribute details required to add in temporary
186 * files
187 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530188 private JavaAttributeInfo getChildNodeAsAttributeInParentService(
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530189 YangNode childNode, YangNode currentNode) {
190
191 YangNode parentNode = getParentNodeInGenCode(currentNode);
192
193 String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName();
194 /*
195 * Get the import info corresponding to the attribute for import in
196 * generated java files or qualified access
197 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530198 JavaQualifiedTypeInfoTranslator qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(childNode,
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530199 getCapitalCase(childNodeName));
200 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
201 throw new TranslatorException("Parent node does not have file info");
202 }
203
204 TempJavaFragmentFiles tempJavaFragmentFiles;
205 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode)
206 .getTempJavaCodeFragmentFiles()
207 .getServiceTempFiles();
208
209 if (tempJavaFragmentFiles == null) {
210 throw new TranslatorException("Parent node does not have service file info");
211 }
212 boolean isQualified = addImportToService(qualifiedTypeInfo);
213 return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false);
214 }
215
216 /**
217 * Adds to service class import list.
218 *
219 * @param importInfo import info
220 * @return true or false
221 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530222 private boolean addImportToService(JavaQualifiedTypeInfoTranslator importInfo) {
Bharat saraswale50edca2016-08-05 01:58:25 +0530223 JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) getParent()).getJavaFileInfo();
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530224
225 if (importInfo.getClassInfo().contentEquals(SERVICE)
Bharat saraswal8beac342016-08-04 02:00:03 +0530226 || importInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))) {
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530227 return true;
228 }
229
230 String className;
231 className = getCapitalCase(fileInfo.getJavaName()) + "Service";
232
233 return ((TempJavaCodeFragmentFilesContainer) getParent()).getTempJavaCodeFragmentFiles()
234 .getServiceTempFiles().getJavaImportData().addImportInfo(importInfo,
235 className, fileInfo.getPackage());
236 }
237
238}