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.google.gson.Gson;
36 import com.google.gson.reflect.TypeToken;
39 /** CALL Flow: onBoardingHandler --> LifecycleTestHook--> LifecycleTestExecutor */
40 public class LifecycleTestExceutor {
41 private static final Logger logger = LoggerFactory.getLogger(LifecycleTestExceutor.class);
42 private static Gson gson = new Gson();
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 (!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 (!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 (!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 (!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 = "";
132 //TBD - Use Gson - jackson has security issue//JsonUtil.toJson(oLifeCycleTestReq);
134 RestResponse oResponse = RestfulClient.sendPostRequest(oMsbDetails.getDefaultServer().getHost(),
135 oMsbDetails.getDefaultServer().getPort(), CommonConstant.LifeCycleTest.LIFECYCLE_TEST_URL, rawDataJson);
137 if (!checkValidResponse(oResponse)) {
138 logger.error("execlifecycleTest response is faliure :" + oResponse.getStatusCode());
142 result = oResponse.getResult();
143 logger.info("Response execlifecycleTest :" + oResponse.getResult());
148 * Check Response is Valid
151 * @return valid(true) or invalid(false)
153 private static boolean checkValidResponse(RestResponse rsp) {
154 return ((null != rsp.getStatusCode()) && (null != rsp.getResult())
155 && (RestConstant.RESPONSE_CODE_200 == rsp.getStatusCode()
156 || RestConstant.RESPONSE_CODE_201 == rsp.getStatusCode()));
163 * @return empty(failure), or csarId(success)
165 private static String getCsarIdValue(String strJsonData) {
167 Gson will ignore the unknown fields and simply match the fields that it's able to.
168 ref: https://www.baeldung.com/gson-deserialization-guide
169 By default, Gson just ignores extra JSON elements that do not have matching Java fields.
170 ref: https://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html
172 Map<String, String> dataMap = null;
175 dataMap = gson.fromJson(strJsonData, new TypeToken<Map<String,String>>(){}.getType());
176 } catch (Exception e) { //NOSONAR
177 logger.error("Exception:Failed to upload package to catalouge:", e);
180 if (null != dataMap) {
181 return dataMap.get("csarId");
183 } catch (NullPointerException e) {
184 logger.error("NullPointerException:Failed to get csarId", e);