bbe2cf34ac3a3431a88c680ae2c03657ea4bc722
[vnfsdk/refrepo.git] /
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.InputStream;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.http.HttpEntity;
27 import org.apache.http.entity.ContentType;
28 import org.apache.http.entity.mime.MultipartEntityBuilder;
29 import org.onap.vnfsdk.marketplace.common.CommonConstant;
30 import org.onap.vnfsdk.marketplace.common.FileUtil;
31 import org.onap.vnfsdk.marketplace.msb.MsbDetails;
32 import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder;
33 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
34 import org.onap.vnfsdk.marketplace.rest.RestConstant;
35 import org.onap.vnfsdk.marketplace.rest.RestResponse;
36 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
37 import org.onap.vnfsdk.marketplace.rest.RestfulUtil;
38 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class FunctionTestExceutor 
43 {
44     private static final Logger logger = LoggerFactory.getLogger(FunctionTestExceutor.class);    
45     
46     private FunctionTestExceutor()
47     {}
48     
49     /**
50      * Interface to Send Request to Start Function test 
51      * @param onBoradFuncTestReq
52      * @return
53      */
54     public static String execFunctionTest(OnBoradingRequest onBoradFuncTestReq)
55     {            
56         String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
57         logger.info("Package file path Function test:" + packagePath);
58         
59         String funcTestId = null;       
60         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
61         if(null == oMsbDetails)
62         {
63             logger.error("Failed to get MSB details during execFunctionTest !!!");
64             return funcTestId;
65         }
66              
67         FileInputStream ifs = null;
68         InputStream inStream = null;
69         
70         try
71         {
72             ifs = new FileInputStream(packagePath);
73             inStream  = new BufferedInputStream(ifs);
74             
75             //IP and Port needs to be configured !!!
76             RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(), 
77                                                     Integer.parseInt(oMsbDetails.getDefaultServer().getPort()), 
78                                                     CommonConstant.functionTest.FUNCTEST_URL,buildRequest(inStream));
79             if(!checkValidResponse(rsp))
80             {
81                 return funcTestId;
82             }
83                        
84             logger.error("Response for Function Test :" , rsp.getResult());
85             funcTestId = rsp.getResult();              
86             return funcTestId.replaceAll("\"", ""); 
87         }
88         catch (FileNotFoundException exp)
89         {
90             logger.error("Fine not fond Exception for file:" , onBoradFuncTestReq.getPackagePath());
91             logger.error("Fine not fond Exception for :" , exp);
92         }      
93         finally
94         {
95             FileUtil.closeInputStream(inStream);
96             FileUtil.closeFileStream(ifs);
97         }      
98         return funcTestId;               
99     }
100        
101     /**
102      * Interface to get Function Test Results
103      * @param key
104      * @return
105      */
106     public static String getTestResultsByFuncTestKey(String key) 
107     {    
108         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
109         if(null == oMsbDetails)
110         {
111             logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
112             return null;
113         }
114         
115         logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + key);         
116         RestResponse rspGet  = RestfulClient.get(oMsbDetails.getDefaultServer().getHost(), 
117                                 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()), 
118                                 CommonConstant.functionTest.FUNCTEST_RESULT_URL + key);
119         if(!checkValidResponse(rspGet))
120         {
121             logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
122             return null;
123         }
124         logger.info("Function Test Results for Key:" + key + "Response:" + rspGet.getResult());
125         return  rspGet.getResult();
126     }   
127     
128     /**
129      * Interface to get Function Test Results
130      * @param key
131      * @return
132      */
133     public static String executeFunctionTest(String strJsonRequest) 
134     {    
135         logger.info("executeFunctionTest Test request Received:" + strJsonRequest);
136         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
137         if(null == oMsbDetails)
138         {
139             logger.error("Failed to get MSB details during getTestResultsByFuncTestKey !!!");
140             return null;
141         }
142         
143         logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + strJsonRequest);         
144         RestResponse rspGet  = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(), 
145                                                             oMsbDetails.getDefaultServer().getPort(), 
146                                                             CommonConstant.functionTest.FUNCTEST_RESULT_URL,
147                                                             strJsonRequest);
148         if(!checkValidResponse(rspGet))
149         {
150             logger.error("Failed to convert String Json Response to TestResults list:" + rspGet.getResult());
151             return null;
152         }
153         logger.info("executeFunctionTest Function Test Result: " + rspGet.getResult());
154         return  rspGet.getResult();
155     } 
156     
157     /**
158      * Interface to get Function Test Results
159      * @param key
160      * @return
161      */
162     public static String getTestResultsByFuncTestKeyMsb(String key) 
163     {    
164         logger.info("getTestResultsByFuncTestKey for Function Test Results for :" + key);         
165        
166         Map<String, String> paramsMap = new HashMap<String, String>();       
167         paramsMap.put(CommonConstant.HttpContext.URL, CommonConstant.functionTest.FUNCTEST_RESULT_URL + key);  
168         paramsMap.put(CommonConstant.HttpContext.METHOD_TYPE, CommonConstant.MethodType.GET);          
169         
170         RestfulResponse response = RestfulUtil.sendRestRequest(paramsMap, null, null);
171         if(!checkValidRestResponse(response))
172         {
173             logger.error("Respone for getTestResultsByFuncTestKeyMsb is not valid !!!");
174             return null;
175         }
176         
177         if(null != response.getResponseContent())
178         {
179             logger.info("Function Test Results via MSB for Key:" + key + "Response:" + response.getResponseContent());            
180         }
181         else
182         {
183             logger.info("NULL Function Test Results via MSB for Key:" + key);  
184         }   
185         return response.getResponseContent();
186     }  
187         
188     /**
189      * Check Response is Valid
190      * @param rsp
191      * @return
192      */
193     private static boolean checkValidResponse(RestResponse rsp) 
194     {
195         if (rsp.getStatusCode() == null || rsp.getResult() == null 
196                 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode() && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode()))
197         {
198             return false;
199         }
200         return true;
201     }
202
203     private static boolean checkValidRestResponse(RestfulResponse rsp) 
204     {
205         if ((rsp == null) || (RestConstant.RESPONSE_CODE_200 != rsp.getStatus() && RestConstant.RESPONSE_CODE_201 != rsp.getStatus()))
206         {
207             return false;
208         }
209         return true;
210     }
211     
212     @SuppressWarnings("deprecation")
213     private static HttpEntity buildRequest(InputStream inputStream)
214             throws FileNotFoundException {
215           MultipartEntityBuilder builder = MultipartEntityBuilder.create();
216           builder.seContentType(ContentType.MULTIPART_FORM_DATA);
217           builder.addBinaryBody("file", inputStream);
218           return builder.build();
219         }
220 }