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:{} Path:{}", onBoradingReq.getCsarId()
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:{} Package Id:{}" , onBoradingReq.getPackagePath()
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 StringBuilder filePath = new StringBuilder(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:{} Key:{} Path{}" , onBoradingReq.getCsarId(), resultKey
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 String jsonoFuncTestResult = ToolUtil.objectToString(oFuncTestResult);
171 logger.debug("Function test Status for Package Id:{} Result:{}" ,oFuncTestResult.getCsarId()
172 , jsonoFuncTestResult);
174 StringBuilder filePath = new StringBuilder(getResultStorePath());
175 filePath.append(File.separator);
176 filePath.append(oFuncTestResult.getCsarId());
177 filePath.append(File.separator);
178 filePath.append("functionTest.json");
180 FileUtil.writeJsonDatatoFile(filePath.toString(), oFuncTestResult);
184 * Build Function Test Response
186 * @param onBoradingReq
187 * @param oFuncTestResult
189 private void buildFunctResponse(OnBoradingRequest onBoradingReq, OnBoardingResult oFuncTestResult) {
190 oFuncTestResult.setOperFinished(false);
191 oFuncTestResult.setCsarId(onBoradingReq.getCsarId());
192 oFuncTestResult.setOperTypeId(CommonConstant.FunctionTest.FUNCTEST_OPERTYPE_ID);
194 OnBoardingOperResult oPackageExists = new OnBoardingOperResult();
195 oPackageExists.setOperId(CommonConstant.FunctionTest.FUNCTEST_PACKAGE_EXISTS);
196 oPackageExists.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
198 OnBoardingOperResult functTesExec = new OnBoardingOperResult();
199 functTesExec.setOperId(CommonConstant.FunctionTest.FUNCTEST_EXEC);
200 functTesExec.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
202 List<OnBoardingOperResult> operResult = new ArrayList<>();
203 operResult.add(oPackageExists);
204 operResult.add(functTesExec);
206 oFuncTestResult.setOperResult(operResult);
209 public static OnBoardingResult getOnBoardingResult(PackageData packageData) {
211 if (null == packageData) {
212 logger.error("Pacakage data is invalid-null");
216 StringBuilder filePath = new StringBuilder(getResultStorePath());
217 filePath.append(File.separator);
218 filePath.append(packageData.getCsarId());
219 filePath.append(File.separator);
220 filePath.append("functionTest.json");
222 logger.info("On Boarding Status for Package Id:{} Result Path:{}" ,packageData.getCsarId(), filePath);
224 return (OnBoardingResult) FileUtil.readJsonDatafFromFile(filePath.toString(), OnBoardingResult.class);
227 private static ResultKey getFuncTestResultKey(PackageData packageData) {
228 StringBuilder fileName = new StringBuilder(getResultStorePath());
229 fileName.append(File.separator);
230 fileName.append(packageData.getCsarId());
231 fileName.append(File.separator);
232 fileName.append("functestResultKey.json");
234 logger.info("Func Test Result key for Package Id:{} Result Path:{}" , packageData.getCsarId(),fileName);
235 return (ResultKey) FileUtil.readJsonDatafFromFile(fileName.toString(), ResultKey.class);
238 private static String getResultStorePath() {
239 // Using full path due to compilation issue
240 return org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath();
243 private void buildFuncTestResponse(OnBoardingResult oFuncTestResult, String opreKey, int operStatusVal) {
244 List<OnBoardingOperResult> operStatusList = oFuncTestResult.getOperResult();
245 for (OnBoardingOperResult operObj : operStatusList) {
246 if (operObj.getOperId().equalsIgnoreCase(opreKey)) {
247 operObj.setStatus(operStatusVal);