3cf5310bf09ca7906267139554acbf4a557bc94f
[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.FileUtil;
26 import org.onap.vnfsdk.marketplace.common.JsonUtil;
27 import org.onap.vnfsdk.marketplace.msb.MsbDetails;
28 import org.onap.vnfsdk.marketplace.msb.MsbDetailsHolder;
29 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
30 import org.onap.vnfsdk.marketplace.rest.RestConstant;
31 import org.onap.vnfsdk.marketplace.rest.RestResponse;
32 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.fasterxml.jackson.core.JsonParseException;
37 import com.fasterxml.jackson.databind.DeserializationFeature;
38 import com.fasterxml.jackson.databind.JsonMappingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41 /* CALL Flow: onBoardingHandler --> LifecycleTestHook--> LifecycleTestExecutor */
42 public class LifecycleTestExceutor {
43         private static final Logger logger = LoggerFactory.getLogger(LifecycleTestExceutor.class);
44         public static final String CATALOUGE_UPLOAD_URL_IN = "{0}:{1}/openoapi/catalog/v1/csars";
45
46         private LifecycleTestExceutor() {
47                 // Empty constructor
48         }
49
50         /**
51          * Interface to upload package to catalogue
52          * 
53          * @param onBoradFuncTestReq
54          * @return- csarId or null (in case of failure)
55          */
56         @SuppressWarnings("unchecked")
57         public static String uploadPackageToCatalouge(OnBoradingRequest onBoradFuncTestReq) {
58                 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
59                 logger.info("Package file path uploadPackageToCatalouge:" + packagePath);
60
61                 String catalougeCsarId = null;
62
63                 // Validate package path
64                 if (false == FileUtil.validatePath(packagePath)) {
65                         logger.error("Failed to validate  package path");
66                         return catalougeCsarId;
67                 }
68
69                 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
70                 if (null == oMsbDetails) {
71                         logger.error("Failed to get MSB details during uploadPackageToCatalouge !!!");
72                         return catalougeCsarId;
73                 }
74
75                 File fileData = new File(packagePath);
76
77                 // Validate file
78                 if (false == FileUtil.validateFile(fileData)) {
79                         logger.error("Failed to validate file information");
80                         return catalougeCsarId;
81                 }
82
83                 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
84                 builder.addBinaryBody("file", fileData, ContentType.MULTIPART_FORM_DATA, onBoradFuncTestReq.getPackageName());
85
86                 // IP and Port needs to be configured !!!
87                 RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),
88                                 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()), CommonConstant.CATALOUGE_UPLOAD_URL,
89                                 builder.build());
90                 if (false == checkValidResponse(rsp)) {
91                         logger.error("Failed to upload package to catalouge:" + rsp.getStatusCode());
92                         return catalougeCsarId;
93                 }
94
95                 logger.info("Response for uploadPackageToCatalouge :" + rsp.getResult());
96                 catalougeCsarId = getCsarIdValue(rsp.getResult());
97
98                 logger.info("CSARID for uploadPackageToCatalouge :" + catalougeCsarId);
99                 return catalougeCsarId;
100         }
101
102         /**
103          * Interface to execute lifecycle test
104          * 
105          * @param onBoradFuncTestReq,
106          *            oLifeCycleTestReq
107          * @return result of the test or null (in case of failure)
108          */
109         public static String execlifecycleTest(OnBoradingRequest onBoradFuncTestReq, LifeCycleTestReq oLifeCycleTestReq) {
110
111                 String result = null;
112                 if ((null == onBoradFuncTestReq.getPackagePath()) || (null == onBoradFuncTestReq.getPackageName())) {
113                         logger.error("Package path or name is invalid");
114                         return result;
115                 }
116
117                 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
118                 logger.info("Package file path Function test:" + packagePath);
119
120                 // Validate package path
121                 if (false == FileUtil.validatePath(packagePath)) {
122                         logger.error("Failed to validate  path");
123                         return result;
124                 }
125
126                 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
127                 if (null == oMsbDetails) {
128                         logger.error("Failed to get MSB details during execlifecycleTest !!!");
129                         return result;
130                 }
131
132                 String rawDataJson = JsonUtil.toJson(oLifeCycleTestReq);
133                 if (null == rawDataJson) {
134                         logger.error("Failed to convert LifeCycleTestReq object to Json String !!!");
135                         return result;
136                 }
137
138                 RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
139                                 oMsbDetails.getDefaultServer().getPort(), CommonConstant.LifeCycleTest.LIFECYCLE_TEST_URL, rawDataJson);
140
141                 if (false == checkValidResponse(oResponse)) {
142                         logger.error("execlifecycleTest response is faliure :" + oResponse.getStatusCode());
143                         return result;
144                 }
145
146                 result = oResponse.getResult();
147                 logger.info("Response execlifecycleTest :" + oResponse.getResult());
148                 return result;
149         }
150
151         /**
152          * Check Response is Valid
153          * 
154          * @param rsp
155          * @return valid(true) or invalid(false)
156          */
157         private static boolean checkValidResponse(RestResponse rsp) {
158                 if ((null == rsp.getStatusCode()) || (null == rsp.getResult())
159                                 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode()
160                                                 && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode())) {
161                         return false;
162                 }
163                 return true;
164         }
165
166         /**
167          * Get csar Id value
168          *
169          * @param strJsonData
170          * @return empty(failure), or csarId(success)
171          */
172         private static String getCsarIdValue(String strJsonData) {
173                 ObjectMapper mapper = new ObjectMapper();
174                 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
175                 Map<String, String> dataMap = null;
176
177                 try {
178                         dataMap = mapper.readValue(strJsonData, Map.class);
179                 } catch (JsonParseException e) {
180                         logger.error("JsonParseException:Failed to upload package to catalouge:", e);
181                 } catch (JsonMappingException e) {
182                         logger.error("JsonMappingException:Failed to upload package to catalouge:", e);
183                 } catch (IOException e) {
184                         logger.error("IOException:Failed to upload package to catalouge:", e);
185                 }
186                 try {
187                         if (null != dataMap) {
188                                 return dataMap.get("csarId");
189                         }
190                 } catch (NullPointerException e) {
191                         logger.error("NullPointerException:Failed to get csarId", e);
192                 }
193                 return "";
194         }
195 }