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.functiontest;
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.db.entity.PackageData;
26 import org.onap.vnfsdk.marketplace.entity.EnumOperationStatus;
27 import org.onap.vnfsdk.marketplace.entity.EnumResult;
28 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingOperResult;
29 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingResult;
30 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
31 import org.onap.vnfsdk.marketplace.onboarding.entity.ResultKey;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 /* It executes the function test (test cases in robot framework) test for the VNF on the specified VM and
36 * collects the result and return result to the caller
38 * OnBoardingHandler --> FunctionTestHook---> FunctionTestExecutor */
40 public class FunctionTestHook {
41 private static final Logger logger = LoggerFactory.getLogger(FunctionTestHook.class);
44 * Start Executing Function test
46 * @param onBoradingReq
47 * @return Failure or success, Onboarding result
49 public int exec(OnBoradingRequest onBoradingReq) {
51 logger.info("OnboradingRequest received for Package:" + onBoradingReq.getCsarId() + " Path:"
52 + onBoradingReq.getPackagePath());
54 buildResultPath(onBoradingReq);
56 OnBoardingResult oFuncTestResult = new OnBoardingResult();
57 buildFunctResponse(onBoradingReq, oFuncTestResult);
58 updateResult(oFuncTestResult);
60 // STEP 1:Check Package Exists
61 // ---------------------------
62 if (!FileUtil.checkFileExists(onBoradingReq.getPackagePath())) {
63 logger.error("Package Not Found at Path:" + onBoradingReq.getPackagePath() + ", Package Id:"
64 + onBoradingReq.getCsarId());
65 oFuncTestResult.setOperFinished(true);
66 oFuncTestResult.setOperStatus(EnumResult.FAIL.getIndex());
67 buildFuncTestResponse(oFuncTestResult, CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS,
68 EnumOperationStatus.FAILED.getIndex());
69 updateResult(oFuncTestResult);
70 return EnumResult.FAIL.getIndex();
73 buildFuncTestResponse(oFuncTestResult, CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS,
74 EnumOperationStatus.SUCCESS.getIndex());
75 updateResult(oFuncTestResult);
77 // STEP 2:Handle function test for Package
78 // ---------------------------------------
79 String functestResultKey = FunctionTestExceutor.execFunctionTest(onBoradingReq);
80 if (null == functestResultKey) {
81 oFuncTestResult.setOperFinished(true);
82 oFuncTestResult.setOperStatus(EnumResult.FAIL.getIndex());
83 buildFuncTestResponse(oFuncTestResult, CommonConstant.functionTest.FUNCTEST_EXEC,
84 EnumOperationStatus.FAILED.getIndex());
85 updateResult(oFuncTestResult);
86 return EnumResult.FAIL.getIndex();
89 oFuncTestResult.setOperFinished(true);
90 oFuncTestResult.setOperStatus(EnumResult.SUCCESS.getIndex());
91 buildFuncTestResponse(oFuncTestResult, CommonConstant.functionTest.FUNCTEST_EXEC,
92 EnumOperationStatus.SUCCESS.getIndex());
93 updateResult(oFuncTestResult);
95 // STEP 3:Store FuncTest key to get FuncTest Results
96 // -------------------------------------------------
97 storeFuncTestResultKey(onBoradingReq, functestResultKey);
99 return (oFuncTestResult.getOperStatus() == EnumResult.SUCCESS.getIndex()) ? EnumResult.SUCCESS.getIndex()
100 : EnumResult.FAIL.getIndex();
106 * @param onBoradingReq
108 private void buildResultPath(OnBoradingRequest onBoradingReq) {
109 String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId();
110 if (!FileUtil.checkFileExists(filePath)) {
111 FileUtil.createDirectory(filePath);
115 /**Get Function Test result
118 * @return null on failure, function test result on success
120 public static String getFuncTestResults(PackageData packageData) {
121 if (null == packageData) {
122 logger.error("Package data is invalid - null");
126 logger.info("Function Test results request for Package:" + packageData.getCsarId());
127 ResultKey keydata = getFuncTestResultKey(packageData);
128 if ((null == keydata) || (keydata.getKey().isEmpty())) {
129 logger.error("Function Test key Not Found for Package Id:", packageData.getCsarId());
132 return FunctionTestExceutor.getTestResultsByFuncTestKey(keydata.getKey());
136 * Store Function Test Result key
138 * @param onBoradingReq
141 private void storeFuncTestResultKey(OnBoradingRequest onBoradingReq, String resultKey) {
142 // Currently we will make JSON and Store JSON to Package Path)
143 // -------------------------------------------------------------------------------
144 StringBuffer filePath = new StringBuffer(getResultStorePath());
145 filePath.append(File.separator);
146 filePath.append(onBoradingReq.getCsarId());
147 filePath.append(File.separator);
148 filePath.append("functestResultKey.json");
150 logger.debug("Function test Results Key for Package Id:" + onBoradingReq.getCsarId() + ", Key:" + resultKey
151 + " Path" + filePath.toString());
153 ResultKey oResultKey = new ResultKey();
154 oResultKey.setCsarId(onBoradingReq.getCsarId());
155 oResultKey.setOperTypeId(CommonConstant.functionTest.FUNCTEST_OPERTYPE_ID);
156 oResultKey.setKey(resultKey);
158 FileUtil.writeJsonDatatoFile(filePath.toString(), oResultKey);
162 * Store Function test Execution Results
164 * @param oFuncTestResult
166 private void updateResult(OnBoardingResult oFuncTestResult) {
167 // STore Results to DB(Currently we will make JSON and Store JSON to
169 // -------------------------------------------------------------------------------
170 logger.debug("Function test Status for Package Id:" + oFuncTestResult.getCsarId() + ", Result:"
171 + ToolUtil.objectToString(oFuncTestResult));
173 StringBuffer filePath = new StringBuffer(getResultStorePath());
174 filePath.append(File.separator);
175 filePath.append(oFuncTestResult.getCsarId());
176 filePath.append(File.separator);
177 filePath.append("functionTest.json");
179 FileUtil.writeJsonDatatoFile(filePath.toString(), oFuncTestResult);
183 * Build Function Test Response
185 * @param onBoradingReq
186 * @param oFuncTestResult
188 private void buildFunctResponse(OnBoradingRequest onBoradingReq, OnBoardingResult oFuncTestResult) {
189 oFuncTestResult.setOperFinished(false);
190 oFuncTestResult.setCsarId(onBoradingReq.getCsarId());
191 oFuncTestResult.setOperTypeId(CommonConstant.functionTest.FUNCTEST_OPERTYPE_ID);
193 OnBoardingOperResult oPackageExists = new OnBoardingOperResult();
194 oPackageExists.setOperId(CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS);
195 oPackageExists.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
197 OnBoardingOperResult functTesExec = new OnBoardingOperResult();
198 functTesExec.setOperId(CommonConstant.functionTest.FUNCTEST_EXEC);
199 functTesExec.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
201 List<OnBoardingOperResult> operResult = new ArrayList<>();
202 operResult.add(oPackageExists);
203 operResult.add(functTesExec);
205 oFuncTestResult.setOperResult(operResult);
208 public static OnBoardingResult getOnBoardingResult(PackageData packageData) {
210 if (null == packageData) {
211 logger.error("Pacakage data is invalid-null");
215 StringBuffer filePath = new StringBuffer(getResultStorePath());
216 filePath.append(File.separator);
217 filePath.append(packageData.getCsarId());
218 filePath.append(File.separator);
219 filePath.append("functionTest.json");
221 logger.info("On Boarding Status for Package Id:" + packageData.getCsarId() + ", Result Path:" + filePath);
223 return (OnBoardingResult) FileUtil.readJsonDatafFromFile(filePath.toString(), OnBoardingResult.class);
226 private static ResultKey getFuncTestResultKey(PackageData packageData) {
227 StringBuffer fileName = new StringBuffer(getResultStorePath());
228 fileName.append(File.separator);
229 fileName.append(packageData.getCsarId());
230 fileName.append(File.separator);
231 fileName.append("functestResultKey.json");
233 logger.info("Func Test Result key for Package Id:" + packageData.getCsarId() + ", Result Path:" + fileName);
234 return (ResultKey) FileUtil.readJsonDatafFromFile(fileName.toString(), ResultKey.class);
237 private static String getResultStorePath() {
238 // Using full path due to compilation issue
239 return org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath();
242 private void buildFuncTestResponse(OnBoardingResult oFuncTestResult, String opreKey, int operStatusVal) {
243 List<OnBoardingOperResult> operStatusList = oFuncTestResult.getOperResult();
244 for (OnBoardingOperResult operObj : operStatusList) {
245 if (operObj.getOperId().equalsIgnoreCase(opreKey)) {
246 operObj.setStatus(operStatusVal);