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