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 public class FunctionTestHook
37 private static final Logger logger = LoggerFactory.getLogger(FunctionTestHook.class);
40 * Start Executing Function test
41 * @param onBoradingReq
44 public int exec(OnBoradingRequest onBoradingReq)
46 logger.info("OnboradingRequest received for Package:" + onBoradingReq.getCsarId() + " Path:"+ onBoradingReq.getPackagePath());
48 buildResultPath(onBoradingReq);
50 OnBoardingResult oFuncTestResult = new OnBoardingResult();
51 buildFunctResponse(onBoradingReq,oFuncTestResult);
52 updateResult(oFuncTestResult);
54 //STEP 1:Check Package Exists
55 //---------------------------
56 if(!FileUtil.checkFileExists(onBoradingReq.getPackagePath()))
58 logger.info("Package Not Found at Path:" + onBoradingReq.getPackagePath() + ", Package Id:" + onBoradingReq.getCsarId());
59 oFuncTestResult.setOperFinished(true);
60 oFuncTestResult.setOperStatus(EnumResult.FAIL.getIndex());
61 buildFuncTestResponse(oFuncTestResult,CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS,EnumOperationStatus.FAILED.getIndex());
62 updateResult(oFuncTestResult);
63 return EnumResult.FAIL.getIndex();
66 buildFuncTestResponse(oFuncTestResult,CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS,EnumOperationStatus.SUCCESS.getIndex());
67 updateResult(oFuncTestResult);
69 //STEP 2:Handle function test for Package
70 //---------------------------------------
71 String functestResultKey = FunctionTestExceutor.execFunctionTest(onBoradingReq);
72 if(null == functestResultKey)
74 oFuncTestResult.setOperFinished(true);
75 oFuncTestResult.setOperStatus(EnumResult.FAIL.getIndex());
76 buildFuncTestResponse(oFuncTestResult,CommonConstant.functionTest.FUNCTEST_EXEC,EnumOperationStatus.FAILED.getIndex());
77 updateResult(oFuncTestResult);
78 return EnumResult.FAIL.getIndex();
81 oFuncTestResult.setOperFinished(true);
82 oFuncTestResult.setOperStatus(EnumResult.SUCCESS.getIndex());
83 buildFuncTestResponse(oFuncTestResult,CommonConstant.functionTest.FUNCTEST_EXEC,EnumOperationStatus.SUCCESS.getIndex());
84 updateResult(oFuncTestResult);
86 //STEP 3:Store FuncTest key to get FuncTest Results
87 //-------------------------------------------------
88 storeFuncTestResultKey(onBoradingReq,functestResultKey);
90 return (oFuncTestResult.getOperStatus() == EnumResult.SUCCESS.getIndex())
91 ? EnumResult.SUCCESS.getIndex() : EnumResult.FAIL.getIndex();
96 * @param onBoradingReq
98 private void buildResultPath(OnBoradingRequest onBoradingReq)
100 String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId();
101 if(!FileUtil.checkFileExists(filePath))
103 FileUtil.createDirectory(filePath);
112 public static String getFuncTestResults(PackageData packageData)
114 logger.info("Function Test results request for Package:" + packageData.getCsarId());
115 ResultKey keydata = getFuncTestResultKey(packageData);
116 if(null == keydata || keydata.getKey().isEmpty())
118 logger.info("Function Test key Not Found for Package Id:",packageData.getCsarId());
121 return FunctionTestExceutor.getTestResultsByFuncTestKey(keydata.getKey());
125 * Store Function Test Result key
126 * @param onBoradingReq
129 private void storeFuncTestResultKey(OnBoradingRequest onBoradingReq,String resultKey)
131 //Currently we will make JSON and Store JSON to Package Path)
132 //-------------------------------------------------------------------------------
133 String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId() + File.separator + "functestResultKey.json";
135 logger.info("Function test Results Key for Package Id:" + onBoradingReq.getCsarId() + ", Key:" + resultKey + " Path" + filePath);
137 ResultKey oResultKey = new ResultKey();
138 oResultKey.setCsarId(onBoradingReq.getCsarId());
139 oResultKey.setOperTypeId(CommonConstant.functionTest.FUNCTEST_OPERTYPE_ID);
140 oResultKey.setKey(resultKey);
142 FileUtil.writeJsonDatatoFile(filePath,oResultKey);
146 * Store Function test Execution Results
147 * @param oFuncTestResult
149 private void updateResult(OnBoardingResult oFuncTestResult)
151 //STore Results to DB(Currently we will make JSON and Store JSON to Package Path)
152 //-------------------------------------------------------------------------------
153 logger.info("Function test Status for Package Id:" + oFuncTestResult.getCsarId() + ", Result:" + ToolUtil.objectToString(oFuncTestResult));
154 String filePath = getResultStorePath() + File.separator + oFuncTestResult.getCsarId() + File.separator + "functionTest.json";
155 FileUtil.writeJsonDatatoFile(filePath,oFuncTestResult);
159 * Build Function Test Response
160 * @param onBoradingReq
161 * @param oFuncTestResult
163 private void buildFunctResponse(OnBoradingRequest onBoradingReq, OnBoardingResult oFuncTestResult)
165 oFuncTestResult.setOperFinished(false);
166 oFuncTestResult.setCsarId(onBoradingReq.getCsarId());
167 oFuncTestResult.setOperTypeId(CommonConstant.functionTest.FUNCTEST_OPERTYPE_ID);
169 OnBoardingOperResult oPackageExists = new OnBoardingOperResult();
170 oPackageExists.setOperId(CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS);
171 oPackageExists.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
173 OnBoardingOperResult functTesExec = new OnBoardingOperResult();
174 functTesExec.setOperId(CommonConstant.functionTest.FUNCTEST_EXEC);
175 functTesExec.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
177 List<OnBoardingOperResult> operResult = new ArrayList<>();
178 operResult.add(oPackageExists);
179 operResult.add(functTesExec);
181 oFuncTestResult.setOperResult(operResult);
184 public static OnBoardingResult getOnBoardingResult(PackageData packageData)
186 String filePath = getResultStorePath() + File.separator + packageData.getCsarId() +File.separator + "functionTest.json";
187 logger.info("On Boarding Status for Package Id:" + packageData.getCsarId() + ", Result Path:" + filePath);
189 return (OnBoardingResult)FileUtil.readJsonDatafFromFile(filePath,OnBoardingResult.class);
192 private static ResultKey getFuncTestResultKey(PackageData packageData)
194 String fileName = getResultStorePath() + File.separator + packageData.getCsarId() + File.separator + "functestResultKey.json";
196 logger.info("Func Test Result key for Package Id:" + packageData.getCsarId() + ", Result Path:" + fileName);
197 return (ResultKey) FileUtil.readJsonDatafFromFile(fileName,ResultKey.class);
200 private static String getResultStorePath()
202 return org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath();
205 private void buildFuncTestResponse(OnBoardingResult oFuncTestResult, String opreKey, int operStatusVal)
207 List<OnBoardingOperResult> operStatusList = oFuncTestResult.getOperResult();
208 for(OnBoardingOperResult operObj: operStatusList)
210 if(operObj.getOperId().equalsIgnoreCase(opreKey))
212 operObj.setStatus(operStatusVal);