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:{} Path:{}" , onBoradingReq.getCsarId()
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 String jsonoFuncTestResult = ToolUtil.objectToString(oFuncTestResult);
130 logger.info("Lifecycle test Status for Package Id:{} Result:{}" , oFuncTestResult.getCsarId()
131 , jsonoFuncTestResult);
132 String filePath = getResultStorePath() + File.separator + oFuncTestResult.getCsarId() + File.separator
133 + "lifecycleTest.json";
134 FileUtil.writeJsonDatatoFile(filePath, oFuncTestResult);
138 * Build Function Test Response
140 * @param onBoradingReq
141 * @param oFuncTestResult
143 private void buildlifecycleTestResponse(OnBoradingRequest onBoradingReq, OnBoardingResult oTestResult) {
144 oTestResult.setOperFinished(false);
145 oTestResult.setCsarId(onBoradingReq.getCsarId());
146 oTestResult.setOperTypeId(CommonConstant.LifeCycleTest.LIFECYCLE_TEST_OPERTYPE_ID);
148 OnBoardingOperResult lifecycleTestExec = new OnBoardingOperResult();
149 lifecycleTestExec.setOperId(CommonConstant.LifeCycleTest.LIFECYCLE_TEST_EXEC);
150 lifecycleTestExec.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
152 List<OnBoardingOperResult> operResult = new ArrayList<>();
153 operResult.add(lifecycleTestExec);
154 oTestResult.setOperResult(operResult);
159 * Store Lifecycle Test Result key
161 * @param onBoradingReq
164 private void storelifecycleResultKey(OnBoradingRequest onBoradingReq, String resultKey) {
165 // Currently we will make JSON and Store JSON to Package Path)
166 // -------------------------------------------------------------------------------
167 String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId() + File.separator
168 + "lifecycleTestResultKey.json";
170 logger.debug("Function test Results Key for Package Id:{} Key:{} Path{}" , onBoradingReq.getCsarId(), resultKey
173 ResultKey oResultKey = new ResultKey();
174 oResultKey.setCsarId(onBoradingReq.getCsarId());
175 oResultKey.setOperTypeId(CommonConstant.LifeCycleTest.LIFECYCLE_TEST_OPERTYPE_ID);
176 oResultKey.setKey(resultKey);
178 FileUtil.writeJsonDatatoFile(filePath, oResultKey);
181 private static String getResultStorePath() {
182 // Using full path due to compilation issue
183 return org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath();
186 private void buildFuncTestResponse(OnBoardingResult oFuncTestResult, String opreKey, int operStatusVal) {
187 List<OnBoardingOperResult> operStatusList = oFuncTestResult.getOperResult();
188 for (OnBoardingOperResult operObj : operStatusList) {
189 if (operObj.getOperId().equalsIgnoreCase(opreKey)) {
190 operObj.setStatus(operStatusVal);