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.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 import com.fasterxml.jackson.databind.DeserializationFeature;
37 import com.fasterxml.jackson.databind.JsonMappingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
40 /* CALL Flow: onBoardingHandler --> LifecycleTestHook--> LifecycleTestExecutor */
41 public class LifecycleTestExceutor {
42 private static final Logger logger = LoggerFactory.getLogger(LifecycleTestExceutor.class);
43 public static final String CATALOUGE_UPLOAD_URL_IN = "{0}:{1}/onapapi/catalog/v1/csars";
45 private LifecycleTestExceutor() {
50 * Interface to upload package to catalogue
52 * @param onBoradFuncTestReq
53 * @return- csarId or null (in case of failure)
55 @SuppressWarnings("unchecked")
56 public static String uploadPackageToCatalouge(OnBoradingRequest onBoradFuncTestReq) {
57 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
58 logger.info("Package file path uploadPackageToCatalouge:" + packagePath);
60 String catalougeCsarId = null;
62 // Validate package path
63 if (false == FileUtil.validatePath(packagePath)) {
64 logger.error("Failed to validate package path");
65 return catalougeCsarId;
68 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
69 if (null == oMsbDetails) {
70 logger.error("Failed to get MSB details during uploadPackageToCatalouge !!!");
71 return catalougeCsarId;
74 File fileData = new File(packagePath);
77 if (false == FileUtil.validateFile(fileData)) {
78 logger.error("Failed to validate file information");
79 return catalougeCsarId;
82 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
83 builder.addBinaryBody("file", fileData, ContentType.MULTIPART_FORM_DATA, onBoradFuncTestReq.getPackageName());
85 // IP and Port needs to be configured !!!
86 RestResponse rsp = RestfulClient.post(oMsbDetails.getDefaultServer().getHost(),
87 Integer.parseInt(oMsbDetails.getDefaultServer().getPort()), CommonConstant.CATALOUGE_UPLOAD_URL,
89 if (false == checkValidResponse(rsp)) {
90 logger.error("Failed to upload package to catalouge:" + rsp.getStatusCode());
91 return catalougeCsarId;
94 logger.info("Response for uploadPackageToCatalouge :" + rsp.getResult());
95 catalougeCsarId = getCsarIdValue(rsp.getResult());
97 logger.info("CSARID for uploadPackageToCatalouge :" + catalougeCsarId);
98 return catalougeCsarId;
102 * Interface to execute lifecycle test
104 * @param onBoradFuncTestReq,
106 * @return result of the test or null (in case of failure)
108 public static String execlifecycleTest(OnBoradingRequest onBoradFuncTestReq, LifeCycleTestReq oLifeCycleTestReq) {
110 String result = null;
111 if ((null == onBoradFuncTestReq.getPackagePath()) || (null == onBoradFuncTestReq.getPackageName())) {
112 logger.error("Package path or name is invalid");
116 String packagePath = onBoradFuncTestReq.getPackagePath() + File.separator + onBoradFuncTestReq.getPackageName();
117 logger.info("Package file path Function test:" + packagePath);
119 // Validate package path
120 if (false == FileUtil.validatePath(packagePath)) {
121 logger.error("Failed to validate path");
125 MsbDetails oMsbDetails = MsbDetailsHolder.getMsbDetails();
126 if (null == oMsbDetails) {
127 logger.error("Failed to get MSB details during execlifecycleTest !!!");
131 String rawDataJson = ""; //TBD - Use Gson - jackson has security issue//JsonUtil.toJson(oLifeCycleTestReq);
132 // if (null == rawDataJson) {
133 // logger.error("Failed to convert LifeCycleTestReq object to Json String !!!");
137 RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
138 oMsbDetails.getDefaultServer().getPort(), CommonConstant.LifeCycleTest.LIFECYCLE_TEST_URL, rawDataJson);
140 if (false == checkValidResponse(oResponse)) {
141 logger.error("execlifecycleTest response is faliure :" + oResponse.getStatusCode());
145 result = oResponse.getResult();
146 logger.info("Response execlifecycleTest :" + oResponse.getResult());
151 * Check Response is Valid
154 * @return valid(true) or invalid(false)
156 private static boolean checkValidResponse(RestResponse rsp) {
157 if ((null == rsp.getStatusCode()) || (null == rsp.getResult())
158 || (RestConstant.RESPONSE_CODE_200 != rsp.getStatusCode()
159 && RestConstant.RESPONSE_CODE_201 != rsp.getStatusCode())) {
169 * @return empty(failure), or csarId(success)
171 private static String getCsarIdValue(String strJsonData) {
172 ObjectMapper mapper = new ObjectMapper();
173 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
174 Map<String, String> dataMap = null;
177 dataMap = mapper.readValue(strJsonData, Map.class);
178 } catch (JsonParseException e) {
179 logger.error("JsonParseException:Failed to upload package to catalouge:", e);
180 } catch (JsonMappingException e) {
181 logger.error("JsonMappingException:Failed to upload package to catalouge:", e);
182 } catch (IOException e) {
183 logger.error("IOException:Failed to upload package to catalouge:", e);
186 if (null != dataMap) {
187 return dataMap.get("csarId");
189 } catch (NullPointerException e) {
190 logger.error("NullPointerException:Failed to get csarId", e);