2 * Copyright 2017-2018 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.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;
35 import com.fasterxml.jackson.core.JsonParseException;
36 /** note jackson has security vulnerabilities. use with care */
37 import com.fasterxml.jackson.databind.DeserializationFeature;
38 import com.fasterxml.jackson.databind.JsonMappingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
42 /** CALL Flow: onBoardingHandler --> LifecycleTestHook--> LifecycleTestExecutor */
43 public class LifecycleTestExceutor {
44 private static final Logger logger = LoggerFactory.getLogger(LifecycleTestExceutor.class);
45 public static final String CATALOUGE_UPLOAD_URL_IN = "{0}:{1}/onapapi/catalog/v1/csars";
47 private LifecycleTestExceutor() {
52 * Interface to upload package to catalogue
54 * @param onBoradFuncTestReq
55 * @return- csarId or null (in case of failure)
57 @SuppressWarnings("unchecked")
58 public static String uploadPackageToCatalouge(OnBoradingRequest onBoradFuncTestReq) {
59 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
60 logger.info("Package file path uploadPackageToCatalouge:" + packagePath);
62 String catalougeCsarId = null;
64 // Validate package path
65 if (false == FileUtil.validatePath(packagePath)) {
66 logger.error("Failed to validate package path");
67 return catalougeCsarId;
70 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
71 if (null == oMsbDetails) {
72 logger.error("Failed to get MSB details during uploadPackageToCatalouge !!!");
73 return catalougeCsarId;
76 File fileData = new File(packagePath);
79 if (false == FileUtil.validateFile(fileData)) {
80 logger.error("Failed to validate file information");
81 return catalougeCsarId;
84 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
85 builder.addBinaryBody("file", fileData, ContentType.MULTIPART_FORM_DATA, onBoradFuncTestReq.getPackageName());
87 // IP and Port needs to be configured !!!
88 RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),
89 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()), CommonConstant.CATALOUGE_UPLOAD_URL,
91 if (false == checkValidResponse(rsp)) {
92 logger.error("Failed to upload package to catalouge:" + rsp.getStatusCode());
93 return catalougeCsarId;
96 logger.info("Response for uploadPackageToCatalouge :" + rsp.getResult());
97 catalougeCsarId = getCsarIdValue(rsp.getResult());
99 logger.info("CSARID for uploadPackageToCatalouge :" + catalougeCsarId);
100 return catalougeCsarId;
104 * Interface to execute lifecycle test
106 * @param onBoradFuncTestReq,
108 * @return result of the test or null (in case of failure)
110 public static String execlifecycleTest(OnBoradingRequest onBoradFuncTestReq, LifeCycleTestReq oLifeCycleTestReq) {
112 String result = null;
113 if ((null == onBoradFuncTestReq.getPackagePath()) || (null == onBoradFuncTestReq.getPackageName())) {
114 logger.error("Package path or name is invalid");
118 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
119 logger.info("Package file path Function test:" + packagePath);
121 // Validate package path
122 if (false == FileUtil.validatePath(packagePath)) {
123 logger.error("Failed to validate path");
127 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
128 if (null == oMsbDetails) {
129 logger.error("Failed to get MSB details during execlifecycleTest !!!");
133 String rawDataJson = ""; //TBD - Use Gson - jackson has security issue//JsonUtil.toJson(oLifeCycleTestReq);
134 // if (null == rawDataJson) {
135 // logger.error("Failed to convert LifeCycleTestReq object to Json String !!!");
139 RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
140 oMsbDetails.getDefaultServer().getPort(), CommonConstant.LifeCycleTest.LIFECYCLE_TEST_URL, rawDataJson);
142 if (false == checkValidResponse(oResponse)) {
143 logger.error("execlifecycleTest response is faliure :" + oResponse.getStatusCode());
147 result = oResponse.getResult();
148 logger.info("Response execlifecycleTest :" + oResponse.getResult());
153 * Check Response is Valid
156 * @return valid(true) or invalid(false)
158 private static boolean checkValidResponse(RestResponse rsp) {
159 if ((null == rsp.getStatusCode()) || (null == rsp.getResult())
160 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode()
161 && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode())) {
171 * @return empty(failure), or csarId(success)
173 private static String getCsarIdValue(String strJsonData) {
174 ObjectMapper mapper = new ObjectMapper();
175 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
176 Map<String, String> dataMap = null;
179 dataMap = mapper.readValue(strJsonData, Map.class);
180 } catch (JsonParseException e) {
181 logger.error("JsonParseException:Failed to upload package to catalouge:", e);
182 } catch (JsonMappingException e) {
183 logger.error("JsonMappingException:Failed to upload package to catalouge:", e);
184 } catch (IOException e) {
185 logger.error("IOException:Failed to upload package to catalouge:", e);
188 if (null != dataMap) {
189 return dataMap.get("csarId");
191 } catch (NullPointerException e) {
192 logger.error("NullPointerException:Failed to get csarId", e);