blob: 8d087541795592bb3682a2faab5c90e5aec90f45 [file] [log] [blame]
Vinod Kumar S79a374b2016-04-30 21:09:15 +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;
18
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053019import org.onosproject.yangutils.datamodel.YangNode;
Shankara-Huaweib7564772016-08-02 18:13:13 +053020import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
21import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
Bharat saraswalaf413b82016-07-14 15:18:20 +053022import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053023import org.onosproject.yangutils.utils.io.YangPluginConfig;
24
25import java.io.File;
26import java.io.IOException;
27import java.util.List;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053028
29import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK;
30import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK;
Bharat saraswalaf413b82016-07-14 15:18:20 +053031import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.addListenersImport;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053032import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile;
Bharat saraswal8beac342016-08-04 02:00:03 +053033import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.addResolvedAugmentedDataNodeImports;
Bharat saraswalaf413b82016-07-14 15:18:20 +053034import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053035import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod;
36import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod;
Bharat saraswal250a7472016-05-12 13:16:57 +053037import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053038import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053039import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053040import static org.onosproject.yangutils.utils.UtilConstants.Operation.ADD;
Bharat saraswal250a7472016-05-12 13:16:57 +053041import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
42import static org.onosproject.yangutils.utils.UtilConstants.VOID;
Bharat saraswalaf413b82016-07-14 15:18:20 +053043import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Bharat saraswalaf413b82016-07-14 15:18:20 +053044import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
Bharat saraswalaf413b82016-07-14 15:18:20 +053045import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswalaf413b82016-07-14 15:18:20 +053046import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053047
48/**
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053049 * Represents implementation of java service code fragments temporary
50 * implementations. Maintains the temp files required specific for service
51 * and manager java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053052 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053053public class TempJavaServiceFragmentFiles extends TempJavaFragmentFiles {
Vinod Kumar S79a374b2016-04-30 21:09:15 +053054
55 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053056 * File name for rpc method.
57 */
58 private static final String RPC_INTERFACE_FILE_NAME = "Rpc";
59
60 /**
61 * File name for rpc implementation method.
62 */
63 private static final String RPC_IMPL_FILE_NAME = "RpcImpl";
64
65 /**
Bharat saraswal039f59c2016-07-14 21:57:13 +053066 * File name for generated class file for service suffix.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053067 */
68 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
69
70 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053071 * Temporary file handle for rpc interface.
72 */
73 private File rpcInterfaceTempFileHandle;
74
75 /**
76 * Temporary file handle for rpc manager impl.
77 */
78 private File rpcImplTempFileHandle;
79
80 /**
81 * Java file handle for rpc interface file.
82 */
83 private File serviceInterfaceJavaFileHandle;
84
85 /**
Bharat saraswalaf413b82016-07-14 15:18:20 +053086 * Creates an instance of temporary java code fragment.
87 *
88 * @param javaFileInfo generated file information
89 * @throws IOException when fails to create new file handle
90 */
Bharat saraswale50edca2016-08-05 01:58:25 +053091 TempJavaServiceFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Bharat saraswalaf413b82016-07-14 15:18:20 +053092 throws IOException {
93 setJavaExtendsListHolder(new JavaExtendsListHolder());
94 setJavaImportData(new JavaImportData());
95 setJavaFileInfo(javaFileInfo);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053096 setAbsoluteDirPath(getAbsolutePackagePath(
97 getJavaFileInfo().getBaseCodeGenPath(),
Bharat saraswalaf413b82016-07-14 15:18:20 +053098 getJavaFileInfo().getPackageFilePath()));
99 addGeneratedTempFile(RPC_INTERFACE_MASK);
100 addGeneratedTempFile(RPC_IMPL_MASK);
101
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530102 rpcInterfaceTempFileHandle =
103 getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME);
104 rpcImplTempFileHandle = getTemporaryFileHandle(RPC_IMPL_FILE_NAME);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530105 }
106
107 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530108 * Returns rpc method's temporary file handle.
109 *
110 * @return temporary file handle
111 */
112 public File getRpcInterfaceTempFileHandle() {
113 return rpcInterfaceTempFileHandle;
114 }
115
116 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530117 * Retrieves the manager impl temp file.
118 *
119 * @return the manager impl temp file
120 */
121 public File getRpcImplTempFileHandle() {
122 return rpcImplTempFileHandle;
123 }
124
125 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530126 * Constructs java code exit.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530127 *
128 * @param fileType generated file type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530129 * @param curNode current YANG node
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530130 * @throws IOException when fails to generate java files
131 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530132 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530133 public void generateJavaFile(int fileType, YangNode curNode)
134 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530135
136 addResolvedAugmentedDataNodeImports(curNode);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530137 List<String> imports = ((JavaCodeGeneratorInfo) curNode)
138 .getTempJavaCodeFragmentFiles().getServiceTempFiles()
Bharat saraswalaf413b82016-07-14 15:18:20 +0530139 .getJavaImportData().getImports();
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530140 createPackage(curNode);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530141 boolean notification = false;
Shankara-Huaweib7564772016-08-02 18:13:13 +0530142 if (curNode instanceof YangJavaModuleTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530143 if (!((YangJavaModuleTranslator) curNode).getNotificationNodes()
144 .isEmpty()) {
145 notification = true;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530146 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530147 } else if (curNode instanceof YangJavaSubModuleTranslator) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530148 if (!((YangJavaSubModuleTranslator) curNode).getNotificationNodes()
149 .isEmpty()) {
150 notification = true;
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530151 }
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530152 }
153
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530154 if (notification) {
155 addListenersImport(curNode, imports, ADD, LISTENER_SERVICE);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530156 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530157
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530158 serviceInterfaceJavaFileHandle =
159 getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX));
160 generateServiceInterfaceFile(serviceInterfaceJavaFileHandle, curNode,
161 imports);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530162
163 // Close all the file handles.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530164 freeTemporaryResources(false);
165 }
166
167 /**
168 * Adds rpc string information to applicable temp file.
169 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530170 * @param inputAttr RPCs input node attribute info
171 * @param outputAttr RPCs output node attribute info
172 * @param rpcName name of the rpc function
173 * @param config plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530174 * @throws IOException IO operation fail
175 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530176 private void addRpcString(JavaAttributeInfo inputAttr,
177 JavaAttributeInfo outputAttr,
178 YangPluginConfig config, String rpcName)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530179 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530180 String rpcInput = EMPTY_STRING;
181 String rpcOutput = VOID;
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530182 String rpcInputJavaDoc = EMPTY_STRING;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530183 if (inputAttr != null) {
184 rpcInput = getCapitalCase(inputAttr.getAttributeName());
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530185 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530186 if (outputAttr != null) {
187 rpcOutput = getCapitalCase(outputAttr.getAttributeName());
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530188 }
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530189 if (!rpcInput.equals(EMPTY_STRING)) {
190 rpcInputJavaDoc = RPC_INPUT_VAR_NAME;
191 }
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530192 appendToFile(rpcInterfaceTempFileHandle,
193 generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput,
194 config) +
195 getRpcServiceMethod(rpcName, rpcInput, rpcOutput,
196 config) + NEW_LINE);
197 appendToFile(rpcImplTempFileHandle,
198 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, config) +
199 NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530200 }
201
202 /**
203 * Adds the JAVA rpc snippet information.
204 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530205 * @param inputAttr RPCs input node attribute info
206 * @param outputAttr RPCs output node attribute info
207 * @param config plugin configurations
208 * @param rpcName name of the rpc function
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530209 * @throws IOException IO operation fail
210 */
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530211 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo inputAttr,
212 JavaAttributeInfo outputAttr,
213 YangPluginConfig config,
214 String rpcName)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530215 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530216 addRpcString(inputAttr, outputAttr, config, rpcName);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530217 }
218
219 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530220 * Removes all temporary file handles.
221 *
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530222 * @param errorOccurred flag indicating error
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530223 * @throws IOException when failed to delete the temporary files
224 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530225 @Override
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530226 public void freeTemporaryResources(boolean errorOccurred)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530227 throws IOException {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530228 closeFile(serviceInterfaceJavaFileHandle, errorOccurred);
229 closeFile(rpcInterfaceTempFileHandle);
230 closeFile(rpcImplTempFileHandle);
231 closeFile(getGetterInterfaceTempFileHandle());
232 closeFile(getSetterInterfaceTempFileHandle());
233 closeFile(getSetterImplTempFileHandle());
234 super.freeTemporaryResources(errorOccurred);
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530235 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530236}