Code Improvements-Vnfsdk-refrepo sonar issue fixes
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vnfsdk / marketplace / onboarding / hooks / functiontest / FunctionTestExceutor.java
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
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 package org.onap.vnfsdk.marketplace.onboarding.hooks.functiontest;
17
18 import java.io.BufferedInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import org.apache.http.HttpEntity;
26 import org.apache.http.entity.mime.MultipartEntityBuilder;
27 import org.onap.vnfsdk.marketplace.common.CommonConstant;
28 import org.onap.vnfsdk.marketplace.msb.MsbDetails;
29 import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder;
30 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
31 import org.onap.vnfsdk.marketplace.rest.RestConstant;
32 import org.onap.vnfsdk.marketplace.rest.RestResponse;
33 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
34 import org.onap.vnfsdk.marketplace.common.FileUtil;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /* CALL Flow: onBoardingHandler --> FunctionTestHook--> FunctionTestExecutor */
39 public class FunctionTestExceutor {
40         private static final Logger logger = LoggerFactory.getLogger(FunctionTestExceutor.class);
41
42         private FunctionTestExceutor() {
43                 //Empty constructor
44         }
45
46         /**
47          * Interface to Send Request to Start Function test
48          * 
49          * @param onBoradFuncTestReq
50          * @return null (in case of failure) or function Test Id
51          */
52         public static String execFunctionTest(OnBoradingRequest onBoradFuncTestReq) {
53
54                 String funcTestId = null;               
55
56                 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
57                 logger.info("Package file path Function test:{}" , packagePath);
58
59                 // Validate package path
60                 if (!FileUtil.validatePath(packagePath)) {
61                         logger.error("Failed to validate  path");
62                         return funcTestId;
63                 }
64
65                 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
66                 if (null == oMsbDetails) {
67                         logger.error("Failed to get MSB details during execFunctionTest !!!");
68                         return funcTestId;
69                 }
70
71                 try (FileInputStream ifs = new FileInputStream(packagePath);
72                                 InputStream inStream = new BufferedInputStream(ifs);) {
73
74                         // Validate input stream
75                         if (!FileUtil.validateStream(ifs)) {
76                                 logger.error("Failed to validate file stream");
77                                 return funcTestId;
78                         }
79
80                         // IP and Port needs to be configured !!!
81                         RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),
82                                         Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
83                                         CommonConstant.FunctionTest.FUNCTEST_URL, buildRequest(inStream));
84                         if (!checkValidResponse(rsp)) {
85                                 logger.error("Failed to validate response");
86                                 return funcTestId;
87                         }
88
89                         funcTestId = rsp.getResult();
90                         logger.info("Response for Function Test :{}", funcTestId);
91
92                         return funcTestId.replaceAll("\"", "");
93                 } catch (NumberFormatException e) {
94                         logger.error("Invalid port number :{}", oMsbDetails.getDefaultServer().getPort());
95                 } catch (FileNotFoundException exp) {
96                         logger.error("File not found Exception for file:{}", onBoradFuncTestReq.getPackagePath());
97                         logger.error("File not found Exception for :", exp);
98                 } catch (IOException e) {
99                         logger.error("IOException:", e);
100                 }
101
102                 return funcTestId;
103         }
104
105         /**
106          * Interface to get Function Test Results
107          * 
108          * @param key
109          * @return null or resultkey
110          */
111         public static String getTestResultsByFuncTestKey(String key) {
112
113                 // Input key cannot be null- no need to validate
114
115                 String result = null;
116                 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
117                 if (null == oMsbDetails) {
118                         logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
119                         return result;
120                 }
121
122                 logger.info("GetTestResultsByFuncTestKey for Function Test Results for :{}" , key);
123                 RestResponse rspGet = RestfulClient.get(oMsbDetails.getDefaultServer().getHost(),
124                                 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),
125                                 CommonConstant.FunctionTest.FUNCTEST_RESULT_URL + key);
126                 if (!checkValidResponse(rspGet)) {
127                         logger.error("Failed to convert String Json Response to TestResults list:{}" , rspGet.getResult());
128                         return result;
129                 }
130
131                 result = rspGet.getResult();
132                 logger.info("Function Test Results for Key:{} Response:{}" , key , rspGet.getResult());
133                 return result;
134         }
135
136         /**
137          * Interface to get Function Test Results
138          * 
139          * @param strJsonRequest
140          * @return
141          */
142         public static String executeFunctionTest(String strJsonRequest) {
143
144                 String result = null;
145                 if (null == strJsonRequest) {
146                         logger.error("Invalid input- Input is null");
147                         return result;
148                 }
149
150                 logger.info("ExecuteFunctionTest Test request Received:{}" , strJsonRequest);
151
152                 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
153                 if (null == oMsbDetails) {
154                         logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
155                         return result;
156                 }
157
158                 logger.info("GetTestResultsByFuncTestKey for Function Test Results for :{}" , strJsonRequest);
159                 RestResponse rspGet = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
160                                 oMsbDetails.getDefaultServer().getPort(), CommonConstant.FunctionTest.FUNCTEST_RESULT_URL,
161                                 strJsonRequest);
162                 if (!checkValidResponse(rspGet)) {
163                         logger.error("Failed to convert String Json Response to TestResults list:{}" , rspGet.getResult());
164                         return result;
165                 }
166
167                 result = rspGet.getResult();
168                 logger.info("ExecuteFunctionTest Function Test Result: {}" , rspGet.getResult());
169                 return result;
170         }
171
172         /**
173          * Check Response is Valid
174          * 
175          * @param rsp
176          * @return valid or invalid
177          */
178         private static boolean checkValidResponse(RestResponse rsp) {
179                 return ((null != rsp.getStatusCode()) && (null != rsp.getResult())
180                                 && (RestConstant.RESPONSE_CODE_200 == rsp.getStatusCode()
181                                 || RestConstant.RESPONSE_CODE_201 == rsp.getStatusCode()));
182         }
183
184         private static HttpEntity buildRequest(InputStream inputStream) {
185                 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
186                 builder.addBinaryBody("file", inputStream);
187                 return builder.build();
188         }
189 }