blob: 315f7e343e5944bb87b44f153b022cd88c7246e8 [file] [log] [blame]
Bharat saraswal96dfef02016-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;
25import org.onosproject.yangutils.datamodel.YangRpc;
26import org.onosproject.yangutils.translator.exception.TranslatorException;
27import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
28import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053029import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswal96dfef02016-06-16 00:29:12 +053030import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
31import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
32import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
33import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
34import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
35import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053036import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Bharat saraswal96dfef02016-06-16 00:29:12 +053037
38import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
39import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
40import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053041import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Bharat saraswalb551aae2016-07-14 15:18:20 +053042import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.updatePackageInfo;
Bharat saraswal96dfef02016-06-16 00:29:12 +053043import static org.onosproject.yangutils.utils.UtilConstants.ACTIVATE;
44import static org.onosproject.yangutils.utils.UtilConstants.COMPONENT;
45import static org.onosproject.yangutils.utils.UtilConstants.DEACTIVATE;
46import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
47import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE;
48import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE_CARDINALITY;
49import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
50
51/**
52 * Represents rpc information extended to support java code generation.
53 */
54public class YangJavaRpc
55 extends YangRpc
56 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
57
58 private static final long serialVersionUID = 806201622L;
59
60 /**
61 * Contains the information of the java file being generated.
62 */
63 private JavaFileInfo javaFileInfo;
64
65 /**
66 * Temproary file for code generation.
67 */
68 private transient TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles;
69
70 /**
71 * Creates an instance of YANG java rpc.
72 */
73 public YangJavaRpc() {
74 super();
75 setJavaFileInfo(new JavaFileInfo());
76 }
77
78 /**
79 * Returns the generated java file information.
80 *
81 * @return generated java file information
82 */
83 @Override
84 public JavaFileInfo getJavaFileInfo() {
85
86 if (javaFileInfo == null) {
87 throw new TranslatorException("missing java info in java datamodel node");
88 }
89 return javaFileInfo;
90 }
91
92 /**
93 * Sets the java file info object.
94 *
95 * @param javaInfo java file info object
96 */
97 @Override
98 public void setJavaFileInfo(JavaFileInfo javaInfo) {
99 javaFileInfo = javaInfo;
100 }
101
102 @Override
103 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
104 return tempJavaCodeFragmentFiles;
105 }
106
107 @Override
108 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
109 tempJavaCodeFragmentFiles = fileHandle;
110 }
111
112 /**
113 * Prepares the information for java code generation corresponding to YANG
114 * RPC info.
115 *
116 * @param yangPlugin YANG plugin config
117 * @throws TranslatorException translator operations fails
118 */
119 @Override
120 public void generateCodeEntry(YangPluginConfig yangPlugin)
121 throws TranslatorException {
122
123 // Add package information for rpc and create corresponding folder.
124 try {
125 updatePackageInfo(this, yangPlugin);
126 } catch (IOException e) {
127 throw new TranslatorException("Failed to prepare generate code entry for RPC node " + getName());
128 }
129 }
130
131 /**
132 * Creates a java file using the YANG RPC info.
133 *
134 * @throws TranslatorException translator operations fails
135 */
136 @Override
137 public void generateCodeExit()
138 throws TranslatorException {
139 // Get the parent module/sub-module.
140 YangNode parent = getParentNodeInGenCode(this);
141
142 // Parent should be holder of rpc or notification.
143 if (!(parent instanceof RpcNotificationContainer)) {
144 throw new TranslatorException("parent node of rpc can only be module or sub-module");
145 }
146
147 /*
148 * Create attribute info for input and output of rpc and add it to the
149 * parent import list.
150 */
151
152 JavaAttributeInfo javaAttributeInfoOfInput = null;
153 JavaAttributeInfo javaAttributeInfoOfOutput = null;
154
155 // Get the child input and output node and obtain create java attribute
156 // info.
157 YangNode yangNode = getChild();
158 while (yangNode != null) {
159 if (yangNode instanceof YangInput) {
160 javaAttributeInfoOfInput = getChildNodeAsAttributeInParentService(yangNode, this);
161
162 } else if (yangNode instanceof YangOutput) {
163 javaAttributeInfoOfOutput = getChildNodeAsAttributeInParentService(yangNode, this);
164 } else {
165 throw new TranslatorException("RPC should contain only input/output child nodes.");
166 }
167 yangNode = yangNode.getNextSibling();
168 }
169
170 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
171 throw new TranslatorException("missing parent temp file handle");
172 }
173
174 /*
175 * Add the rpc information to the parent's service temp file.
176 */
177 try {
178
179 ((TempJavaCodeFragmentFilesContainer) parent).getTempJavaCodeFragmentFiles().getServiceTempFiles()
180 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput,
181 ((JavaFileInfoContainer) parent).getJavaFileInfo().getPluginConfig(), getName());
182
183 } catch (IOException e) {
184 throw new TranslatorException("Failed to generate code for RPC node " + getName());
185 }
186 // No file will be generated during RPC exit.
187 }
188
189 /**
190 * Creates an attribute info object corresponding to a data model node and
191 * return it.
192 *
193 * @param childNode child data model node(input / output) for which the java code generation
194 * is being handled
195 * @param currentNode parent node (module / sub-module) in which the child node is an attribute
196 * @return AttributeInfo attribute details required to add in temporary
197 * files
198 */
199 public JavaAttributeInfo getChildNodeAsAttributeInParentService(
200 YangNode childNode, YangNode currentNode) {
201
202 YangNode parentNode = getParentNodeInGenCode(currentNode);
203
204 String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName();
205 /*
206 * Get the import info corresponding to the attribute for import in
207 * generated java files or qualified access
208 */
209 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(childNode,
210 getCapitalCase(childNodeName));
211 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
212 throw new TranslatorException("Parent node does not have file info");
213 }
214
215 TempJavaFragmentFiles tempJavaFragmentFiles;
216 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode)
217 .getTempJavaCodeFragmentFiles()
218 .getServiceTempFiles();
219
220 if (tempJavaFragmentFiles == null) {
221 throw new TranslatorException("Parent node does not have service file info");
222 }
223 boolean isQualified = addImportToService(qualifiedTypeInfo);
224 return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false);
225 }
226
227 /**
228 * Adds to service class import list.
229 *
230 * @param importInfo import info
231 * @return true or false
232 */
233 private boolean addImportToService(JavaQualifiedTypeInfo importInfo) {
234 JavaFileInfo fileInfo = ((JavaFileInfoContainer) getParent()).getJavaFileInfo();
235
236 if (importInfo.getClassInfo().contentEquals(SERVICE)
237 || importInfo.getClassInfo().contentEquals(COMPONENT)
238 || importInfo.getClassInfo().contentEquals(getCapitalCase(ACTIVATE))
239 || importInfo.getClassInfo().contentEquals(getCapitalCase(DEACTIVATE))
240 || importInfo.getClassInfo().contentEquals(REFERENCE_CARDINALITY)
241 || importInfo.getClassInfo().contentEquals(REFERENCE)
242 || importInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))
243 || importInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + MANAGER))) {
244 return true;
245 }
246
247 String className;
248 className = getCapitalCase(fileInfo.getJavaName()) + "Service";
249
250 return ((TempJavaCodeFragmentFilesContainer) getParent()).getTempJavaCodeFragmentFiles()
251 .getServiceTempFiles().getJavaImportData().addImportInfo(importInfo,
252 className, fileInfo.getPackage());
253 }
254
255}