Code Improvements-Vnfsdk-refrepo sonar issue fixes
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vnfsdk / marketplace / onboarding / hooks / functiontest / FunctionTestHook.java
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:{} Path:{}", onBoradingReq.getCsarId()
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:{} 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();
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                 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");
149
150                 logger.debug("Function test Results Key for Package Id:{} Key:{} Path{}" , onBoradingReq.getCsarId(), resultKey
151                                 , filePath);
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                 String jsonoFuncTestResult =  ToolUtil.objectToString(oFuncTestResult);
171                 logger.debug("Function test Status for Package Id:{} Result:{}" ,oFuncTestResult.getCsarId()
172                                 , jsonoFuncTestResult);
173                 
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");
179                 
180                 FileUtil.writeJsonDatatoFile(filePath.toString(), oFuncTestResult);
181         }
182
183         /**
184          * Build Function Test Response
185          * 
186          * @param onBoradingReq
187          * @param oFuncTestResult
188          */
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);
193
194                 OnBoardingOperResult oPackageExists = new OnBoardingOperResult();
195                 oPackageExists.setOperId(CommonConstant.FunctionTest.FUNCTEST_PACKAGE_EXISTS);
196                 oPackageExists.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
197
198                 OnBoardingOperResult functTesExec = new OnBoardingOperResult();
199                 functTesExec.setOperId(CommonConstant.FunctionTest.FUNCTEST_EXEC);
200                 functTesExec.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
201
202                 List<OnBoardingOperResult> operResult = new ArrayList<>();
203                 operResult.add(oPackageExists);
204                 operResult.add(functTesExec);
205
206                 oFuncTestResult.setOperResult(operResult);
207         }
208
209         public static OnBoardingResult getOnBoardingResult(PackageData packageData) {
210
211                 if (null == packageData) {
212                         logger.error("Pacakage data is invalid-null");
213                         return null;
214                 }
215
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");
221
222                 logger.info("On Boarding Status for Package Id:{} Result Path:{}" ,packageData.getCsarId(), filePath);
223
224                 return (OnBoardingResult) FileUtil.readJsonDatafFromFile(filePath.toString(), OnBoardingResult.class);
225         }
226
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");
233
234                 logger.info("Func Test Result key for Package Id:{} Result Path:{}" , packageData.getCsarId(),fileName);
235                 return (ResultKey) FileUtil.readJsonDatafFromFile(fileName.toString(), ResultKey.class);
236         }
237
238         private static String getResultStorePath() {
239                 // Using full path due to compilation issue
240                 return org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath();
241         }
242
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);
248                                 break;
249                         }
250                 }
251         }
252 }