69f467512591b5018c77a2c3a6bea125e3e65b1b
[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.validatelifecycle;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.Map;
21
22 import org.apache.http.entity.ContentType;
23 import org.apache.http.entity.mime.MultipartEntityBuilder;
24 import org.onap.vnfsdk.marketplace.common.CommonConstant;
25 import org.onap.vnfsdk.marketplace.common.JsonUtil;
26 import org.onap.vnfsdk.marketplace.msb.MsbDetails;
27 import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder;
28 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
29 import org.onap.vnfsdk.marketplace.rest.RestConstant;
30 import org.onap.vnfsdk.marketplace.rest.RestResponse;
31 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.fasterxml.jackson.core.JsonParseException;
36 import com.fasterxml.jackson.databind.DeserializationFeature;
37 import com.fasterxml.jackson.databind.JsonMappingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 public class LifecycleTestExceutor 
41 {
42     private static final Logger logger = LoggerFactory.getLogger(LifecycleTestExceutor.class);    
43     public static final String CATALOUGE_UPLOAD_URL_IN = "{0}:{1}/openoapi/catalog/v1/csars";
44
45     private LifecycleTestExceutor()
46     {}
47
48     /**
49      * Interface to Send Request to Start Function test 
50      * @param onBoradFuncTestReq
51      * @return
52      */
53     @SuppressWarnings("unchecked")
54     public static String uploadPackageToCatalouge(OnBoradingRequest onBoradFuncTestReq)
55     {            
56         String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
57         logger.info("Package file path uploadPackageToCatalouge:" + packagePath);
58
59         String catalougeCsarId = null;       
60         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
61         if(null == oMsbDetails)
62         {
63             logger.error("Failed to get MSB details during uploadPackageToCatalouge !!!");
64             return catalougeCsarId;
65         }
66         
67         File fileData = new File (packagePath);
68         
69         MultipartEntityBuilder builder = MultipartEntityBuilder.create();            
70         builder.addBinaryBody("file", fileData, ContentType.MULTIPART_FORM_DATA, onBoradFuncTestReq.getPackageName());
71             
72         //IP and Port needs to be configured !!!
73         RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),Integer.parseInt(oMsbDetails.getDefaultServer().getPort()),CommonConstant.CATALOUGE_UPLOAD_URL,builder.build());
74         if(!checkValidResponse(rsp))
75         {
76             logger.error("Failed to upload package to catalouge:" + rsp.getStatusCode());
77             return catalougeCsarId;
78         }
79
80         logger.info("Response for uploadPackageToCatalouge :" +  rsp.getResult());
81         catalougeCsarId = getCsarIdValue(rsp.getResult());
82         
83         logger.info("CSARID for uploadPackageToCatalouge :" + catalougeCsarId);
84         return catalougeCsarId;               
85     }
86
87
88
89
90     public static String execlifecycleTest(OnBoradingRequest onBoradFuncTestReq, LifeCycleTestReq oLifeCycleTestReq)
91     {            
92         String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
93         logger.info("Package file path Function test:" + packagePath);
94
95         MsbDetails oMsbDetails =  MsbDetailsHolder.getMsbDetails();
96         if(null == oMsbDetails) {
97             logger.error("Failed to get MSB details during execlifecycleTest !!!");
98             return null;
99         }
100
101         String rawDataJson = JsonUtil.toJson(oLifeCycleTestReq);
102         if(null == rawDataJson) {
103             logger.error("Failed to convert LifeCycleTestReq object to Json String !!!");
104             return null;
105         }
106
107         RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
108                 oMsbDetails.getDefaultServer().getPort(),
109                 CommonConstant.LifeCycleTest.LIFECYCLE_TEST_URL, rawDataJson);
110
111         if(!checkValidResponse(oResponse)) { 
112             logger.error("execlifecycleTest response is faliure :"+ oResponse.getStatusCode());
113             return null;
114         }
115         logger.info("Response execlifecycleTest :"+ oResponse.getResult());
116         return oResponse.getResult();               
117     }
118
119     /**
120      * Check Response is Valid
121      * @param rsp
122      * @return
123      */
124     private static boolean checkValidResponse(RestResponse rsp) 
125     {
126         if (rsp.getStatusCode() == null || rsp.getResult() == null 
127                 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode() && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode()))
128         {
129             return false;
130         }
131         return true;
132     }    
133     
134     /**
135      * 
136      * @param strJsonData
137      * @return
138      */
139     private static String getCsarIdValue(String strJsonData) 
140     {
141         ObjectMapper mapper = new ObjectMapper();
142         mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
143         Map<String, String> dataMap = null;
144         try 
145         {
146             dataMap = (Map<String, String>)mapper.readValue(strJsonData, Map.class);
147         } catch(JsonParseException e) {
148             logger.error("JsonParseException:Failed to upload package to catalouge:");
149         } catch(JsonMappingException e) {
150             logger.error("JsonMappingException:Failed to upload package to catalouge:");
151         } catch(IOException e) {
152             logger.error("IOException:Failed to upload package to catalouge:");
153         }
154         return dataMap.get("csarId");
155     }
156 }