d40a7c1694d01c8a571a01a91952d6558e945c21
[vnfsdk/refrepo.git] /
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vnfsdk.marketplace.onboarding.hooks.functiontest;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.List;
21
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;
34
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  
37  *  
38  *  OnBoardingHandler --> FunctionTestHook---> FunctionTestExecutor    */
39
40 public class FunctionTestHook {
41         private static final Logger logger = LoggerFactory.getLogger(FunctionTestHook.class);
42
43         /**
44          * Start Executing Function test
45          * 
46          * @param onBoradingReq
47          * @return Failure or success, Onboarding result
48          */
49         public int exec(OnBoradingRequest onBoradingReq) {              
50                 
51                 logger.info("OnboradingRequest received for Package:" + onBoradingReq.getCsarId() + " Path:"
52                                 + onBoradingReq.getPackagePath());
53
54                 buildResultPath(onBoradingReq);
55
56                 OnBoardingResult oFuncTestResult = new OnBoardingResult();
57                 buildFunctResponse(onBoradingReq, oFuncTestResult);
58                 updateResult(oFuncTestResult);
59
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();
71                 }
72
73                 buildFuncTestResponse(oFuncTestResult, CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS,
74                                 EnumOperationStatus.SUCCESS.getIndex());
75                 updateResult(oFuncTestResult);
76
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();
87                 }
88
89                 oFuncTestResult.setOperFinished(true);
90                 oFuncTestResult.setOperStatus(EnumResult.SUCCESS.getIndex());
91                 buildFuncTestResponse(oFuncTestResult, CommonConstant.functionTest.FUNCTEST_EXEC,
92                                 EnumOperationStatus.SUCCESS.getIndex());
93                 updateResult(oFuncTestResult);
94
95                 // STEP 3:Store FuncTest key to get FuncTest Results
96                 // -------------------------------------------------
97                 storeFuncTestResultKey(onBoradingReq, functestResultKey);
98
99                 return (oFuncTestResult.getOperStatus() == EnumResult.SUCCESS.getIndex()) ? EnumResult.SUCCESS.getIndex()
100                                 : EnumResult.FAIL.getIndex();
101         }
102
103         /**
104          * Build result path
105          *
106          * @param onBoradingReq
107          */
108         private void buildResultPath(OnBoradingRequest onBoradingReq) {
109                 String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId();
110                 if (!FileUtil.checkFileExists(filePath)) {
111                         FileUtil.createDirectory(filePath);
112                 }
113         }
114
115         /**Get Function Test result
116          *
117          * @param packageData
118          * @return null on failure, function test result on success
119          */
120         public static String getFuncTestResults(PackageData packageData) {
121                 if (null == packageData) {
122                         logger.error("Package data is invalid - null");
123                         return null;
124                 }
125                 
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());
130                         return null;
131                 }
132                 return FunctionTestExceutor.getTestResultsByFuncTestKey(keydata.getKey());
133         }
134
135         /**
136          * Store Function Test Result key
137          * 
138          * @param onBoradingReq
139          * @param resultKey
140          */
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");
149
150                 logger.debug("Function test Results Key for Package Id:" + onBoradingReq.getCsarId() + ", Key:" + resultKey
151                                 + " Path" + filePath.toString());
152
153                 ResultKey oResultKey = new ResultKey();
154                 oResultKey.setCsarId(onBoradingReq.getCsarId());
155                 oResultKey.setOperTypeId(CommonConstant.functionTest.FUNCTEST_OPERTYPE_ID);
156                 oResultKey.setKey(resultKey);
157
158                 FileUtil.writeJsonDatatoFile(filePath.toString(), oResultKey);
159         }
160
161         /**
162          * Store Function test Execution Results
163          * 
164          * @param oFuncTestResult
165          */
166         private void updateResult(OnBoardingResult oFuncTestResult) {
167                 // STore Results to DB(Currently we will make JSON and Store JSON to
168                 // Package Path)
169                 // -------------------------------------------------------------------------------
170                 logger.debug("Function test Status for Package Id:" + oFuncTestResult.getCsarId() + ", Result:"
171                                 + ToolUtil.objectToString(oFuncTestResult));
172                 
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");
178                 
179                 FileUtil.writeJsonDatatoFile(filePath.toString(), oFuncTestResult);
180         }
181
182         /**
183          * Build Function Test Response
184          * 
185          * @param onBoradingReq
186          * @param oFuncTestResult
187          */
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);
192
193                 OnBoardingOperResult oPackageExists = new OnBoardingOperResult();
194                 oPackageExists.setOperId(CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS);
195                 oPackageExists.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
196
197                 OnBoardingOperResult functTesExec = new OnBoardingOperResult();
198                 functTesExec.setOperId(CommonConstant.functionTest.FUNCTEST_EXEC);
199                 functTesExec.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
200
201                 List<OnBoardingOperResult> operResult = new ArrayList<>();
202                 operResult.add(oPackageExists);
203                 operResult.add(functTesExec);
204
205                 oFuncTestResult.setOperResult(operResult);
206         }
207
208         public static OnBoardingResult getOnBoardingResult(PackageData packageData) {
209
210                 if (null == packageData) {
211                         logger.error("Pacakage data is invalid-null");
212                         return null;
213                 }
214
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");
220
221                 logger.info("On Boarding Status for Package Id:" + packageData.getCsarId() + ", Result Path:" + filePath);
222
223                 return (OnBoardingResult) FileUtil.readJsonDatafFromFile(filePath.toString(), OnBoardingResult.class);
224         }
225
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");
232
233                 logger.info("Func Test Result key for Package Id:" + packageData.getCsarId() + ", Result Path:" + fileName);
234                 return (ResultKey) FileUtil.readJsonDatafFromFile(fileName.toString(), ResultKey.class);
235         }
236
237         private static String getResultStorePath() {
238                 // Using full path due to compilation issue
239                 return org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath();
240         }
241
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);
247                                 break;
248                         }
249                 }
250         }
251 }