2 * Copyright 2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vnfsdk.marketplace.onboarding.hooks.validatelifecycle;
19 import java.io.IOException;
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;
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;
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}/onapapi/catalog/v1/csars";
46 private LifecycleTestExceutor() {
51 * Interface to upload package to catalogue
53 * @param onBoradFuncTestReq
54 * @return- csarId or null (in case of failure)
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);
61 String catalougeCsarId = null;
63 // Validate package path
64 if (false == FileUtil.validatePath(packagePath)) {
65 logger.error("Failed to validate package path");
66 return catalougeCsarId;
69 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
70 if (null == oMsbDetails) {
71 logger.error("Failed to get MSB details during uploadPackageToCatalouge !!!");
72 return catalougeCsarId;
75 File fileData = new File(packagePath);
78 if (false == FileUtil.validateFile(fileData)) {
79 logger.error("Failed to validate file information");
80 return catalougeCsarId;
83 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
84 builder.addBinaryBody("file", fileData, ContentType.MULTIPART_FORM_DATA, onBoradFuncTestReq.getPackageName());
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,
90 if (false == checkValidResponse(rsp)) {
91 logger.error("Failed to upload package to catalouge:" + rsp.getStatusCode());
92 return catalougeCsarId;
95 logger.info("Response for uploadPackageToCatalouge :" + rsp.getResult());
96 catalougeCsarId = getCsarIdValue(rsp.getResult());
98 logger.info("CSARID for uploadPackageToCatalouge :" + catalougeCsarId);
99 return catalougeCsarId;
103 * Interface to execute lifecycle test
105 * @param onBoradFuncTestReq,
107 * @return result of the test or null (in case of failure)
109 public static String execlifecycleTest(OnBoradingRequest onBoradFuncTestReq, LifeCycleTestReq oLifeCycleTestReq) {
111 String result = null;
112 if ((null == onBoradFuncTestReq.getPackagePath()) || (null == onBoradFuncTestReq.getPackageName())) {
113 logger.error("Package path or name is invalid");
117 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
118 logger.info("Package file path Function test:" + packagePath);
120 // Validate package path
121 if (false == FileUtil.validatePath(packagePath)) {
122 logger.error("Failed to validate path");
126 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
127 if (null == oMsbDetails) {
128 logger.error("Failed to get MSB details during execlifecycleTest !!!");
132 String rawDataJson = JsonUtil.toJson(oLifeCycleTestReq);
133 if (null == rawDataJson) {
134 logger.error("Failed to convert LifeCycleTestReq object to Json String !!!");
138 RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
139 oMsbDetails.getDefaultServer().getPort(), CommonConstant.LifeCycleTest.LIFECYCLE_TEST_URL, rawDataJson);
141 if (false == checkValidResponse(oResponse)) {
142 logger.error("execlifecycleTest response is faliure :" + oResponse.getStatusCode());
146 result = oResponse.getResult();
147 logger.info("Response execlifecycleTest :" + oResponse.getResult());
152 * Check Response is Valid
155 * @return valid(true) or invalid(false)
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())) {
170 * @return empty(failure), or csarId(success)
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;
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);
187 if (null != dataMap) {
188 return dataMap.get("csarId");
190 } catch (NullPointerException e) {
191 logger.error("NullPointerException:Failed to get csarId", e);