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