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.util.ArrayList;
20 import java.util.List;
22 import org.onap.vnfsdk.marketplace.common.CommonConstant;
23 import org.onap.vnfsdk.marketplace.common.FileUtil;
24 import org.onap.vnfsdk.marketplace.common.ToolUtil;
25 import org.onap.vnfsdk.marketplace.entity.EnumOperationStatus;
26 import org.onap.vnfsdk.marketplace.entity.EnumResult;
27 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingOperResult;
28 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingResult;
29 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
30 import org.onap.vnfsdk.marketplace.onboarding.entity.ResultKey;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 /* It executes the life cycle (create, delete) test for the VNF on the specified VM and collects the result
35 * and return result to the caller and then uploads package to the catalogue
37 * OnBoardingHandler --> LifecycleTestHook---> LifecycleTestExecutor */
39 public class LifecycleTestHook {
40 private static final Logger logger = LoggerFactory.getLogger(LifecycleTestHook.class);
43 * Start Executing Life cycle test
45 * @param onBoradingReq
48 public int exec(OnBoradingRequest onBoradingReq) {
49 logger.info("OnboardingRequest Lifecycle Request received for Package:" + onBoradingReq.getCsarId() + " Path:"
50 + onBoradingReq.getPackagePath());
52 // STEP 1: Validate Input and Build result
53 // ---------------------------------------------------------
54 buildResultPath(onBoradingReq);
56 OnBoardingResult olifecycleTestResult = new OnBoardingResult();
57 buildlifecycleTestResponse(onBoradingReq, olifecycleTestResult);
58 updateResult(olifecycleTestResult);
60 if (null == onBoradingReq.getCsarIdCatalouge() || onBoradingReq.getCsarIdCatalouge().isEmpty()) {
61 olifecycleTestResult.setOperFinished(true);
62 olifecycleTestResult.setOperStatus(EnumResult.FAIL.getIndex());
63 buildFuncTestResponse(olifecycleTestResult, CommonConstant.LifeCycleTest.LIFECYCLE_TEST_EXEC,
64 EnumOperationStatus.FAILED.getIndex());
65 updateResult(olifecycleTestResult);
66 return EnumResult.FAIL.getIndex();
69 LifeCycleTestReq oLifeCycleTestReq = new LifeCycleTestReq();
70 populateLifeCycleReq(onBoradingReq, oLifeCycleTestReq);
72 // STEP 2: Execute Life Cycle Test and Get Result Back !!!!
73 // ---------------------------------------------------------
74 String lifecycleTestResultKey = LifecycleTestExceutor.execlifecycleTest(onBoradingReq, oLifeCycleTestReq);
75 if (null == lifecycleTestResultKey) {
76 olifecycleTestResult.setOperFinished(true);
77 olifecycleTestResult.setOperStatus(EnumResult.FAIL.getIndex());
78 buildFuncTestResponse(olifecycleTestResult, CommonConstant.LifeCycleTest.LIFECYCLE_TEST_EXEC,
79 EnumOperationStatus.FAILED.getIndex());
80 updateResult(olifecycleTestResult);
81 return EnumResult.FAIL.getIndex();
84 olifecycleTestResult.setOperFinished(true);
85 olifecycleTestResult.setOperStatus(EnumResult.SUCCESS.getIndex());
86 buildFuncTestResponse(olifecycleTestResult, CommonConstant.LifeCycleTest.LIFECYCLE_TEST_EXEC,
87 EnumOperationStatus.SUCCESS.getIndex());
88 updateResult(olifecycleTestResult);
90 // STEP 3: Store Lifecycle test key to get Life cycle Test Results
91 // -------------------------------------------------
92 storelifecycleResultKey(onBoradingReq, lifecycleTestResultKey);
94 return (olifecycleTestResult.getOperStatus() == EnumResult.SUCCESS.getIndex()) ? EnumResult.SUCCESS.getIndex()
95 : EnumResult.FAIL.getIndex();
98 private void populateLifeCycleReq(OnBoradingRequest onBoradingReq, LifeCycleTestReq oLifeCycleTestReq) {
100 // Input error handling is done by lifecycle module, not need validate here
101 oLifeCycleTestReq.setCsarId(onBoradingReq.getCsarId());
102 oLifeCycleTestReq.setLabVimId(oLifeCycleTestReq.getLabVimId());
104 // Currently this is not populated, only lavVimId is sufficient
105 List<String> vimIds = new ArrayList<>();
106 oLifeCycleTestReq.setVimIds(vimIds);
110 * Build result path where result is stored as a file
111 * @param onBoradingReq
113 private void buildResultPath(OnBoradingRequest onBoradingReq) {
114 String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId();
115 if (!FileUtil.checkFileExists(filePath)) {
116 FileUtil.createDirectory(filePath);
121 * Store Function test Execution Results
123 * @param oFuncTestResult
125 private void updateResult(OnBoardingResult oFuncTestResult) {
126 // STore Results to DB(Currently we will make JSON and Store JSON to
128 // -------------------------------------------------------------------------------
129 logger.info("Lifecycle test Status for Package Id:" + oFuncTestResult.getCsarId() + ", Result:"
130 + ToolUtil.objectToString(oFuncTestResult));
131 String filePath = getResultStorePath() + File.separator + oFuncTestResult.getCsarId() + File.separator
132 + "lifecycleTest.json";
133 FileUtil.writeJsonDatatoFile(filePath, oFuncTestResult);
137 * Build Function Test Response
139 * @param onBoradingReq
140 * @param oFuncTestResult
142 private void buildlifecycleTestResponse(OnBoradingRequest onBoradingReq, OnBoardingResult oTestResult) {
143 oTestResult.setOperFinished(false);
144 oTestResult.setCsarId(onBoradingReq.getCsarId());
145 oTestResult.setOperTypeId(CommonConstant.LifeCycleTest.LIFECYCLE_TEST_OPERTYPE_ID);
147 OnBoardingOperResult lifecycleTestExec = new OnBoardingOperResult();
148 lifecycleTestExec.setOperId(CommonConstant.LifeCycleTest.LIFECYCLE_TEST_EXEC);
149 lifecycleTestExec.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
151 List<OnBoardingOperResult> operResult = new ArrayList<>();
152 operResult.add(lifecycleTestExec);
153 oTestResult.setOperResult(operResult);
158 * Store Lifecycle Test Result key
160 * @param onBoradingReq
163 private void storelifecycleResultKey(OnBoradingRequest onBoradingReq, String resultKey) {
164 // Currently we will make JSON and Store JSON to Package Path)
165 // -------------------------------------------------------------------------------
166 String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId() + File.separator
167 + "lifecycleTestResultKey.json";
169 logger.debug("Function test Results Key for Package Id:" + onBoradingReq.getCsarId() + ", Key:" + resultKey
170 + " Path" + filePath);
172 ResultKey oResultKey = new ResultKey();
173 oResultKey.setCsarId(onBoradingReq.getCsarId());
174 oResultKey.setOperTypeId(CommonConstant.LifeCycleTest.LIFECYCLE_TEST_OPERTYPE_ID);
175 oResultKey.setKey(resultKey);
177 FileUtil.writeJsonDatatoFile(filePath, oResultKey);
180 private static String getResultStorePath() {
181 // Using full path due to compilation issue
182 return org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath();
185 private void buildFuncTestResponse(OnBoardingResult oFuncTestResult, String opreKey, int operStatusVal) {
186 List<OnBoardingOperResult> operStatusList = oFuncTestResult.getOperResult();
187 for (OnBoardingOperResult operObj : operStatusList) {
188 if (operObj.getOperId().equalsIgnoreCase(opreKey)) {
189 operObj.setStatus(operStatusVal);