e5ec7e6e6a34a103de8af020629ccde211ef6da6
[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 public class FunctionTestHook 
36 {
37     private static final Logger logger = LoggerFactory.getLogger(FunctionTestHook.class);
38
39     /**
40      * Start Executing Function test
41      * @param onBoradingReq
42      * @return
43      */
44     public int exec(OnBoradingRequest onBoradingReq)
45     {       
46         logger.info("OnboradingRequest received for Package:" + onBoradingReq.getCsarId() + " Path:"+ onBoradingReq.getPackagePath());
47
48         buildResultPath(onBoradingReq);
49         
50         OnBoardingResult oFuncTestResult = new OnBoardingResult();
51         buildFunctResponse(onBoradingReq,oFuncTestResult);
52         updateResult(oFuncTestResult);
53
54         //STEP 1:Check Package Exists
55         //---------------------------      
56         if(!FileUtil.checkFileExists(onBoradingReq.getPackagePath()))
57         {
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();
64         }       
65
66         buildFuncTestResponse(oFuncTestResult,CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS,EnumOperationStatus.SUCCESS.getIndex());
67         updateResult(oFuncTestResult);
68
69         //STEP 2:Handle function test for Package
70         //---------------------------------------
71         String functestResultKey = FunctionTestExceutor.execFunctionTest(onBoradingReq);
72         if(null == functestResultKey)
73         {          
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();
79         }       
80
81         oFuncTestResult.setOperFinished(true);
82         oFuncTestResult.setOperStatus(EnumResult.SUCCESS.getIndex());
83         buildFuncTestResponse(oFuncTestResult,CommonConstant.functionTest.FUNCTEST_EXEC,EnumOperationStatus.SUCCESS.getIndex());
84         updateResult(oFuncTestResult);
85
86         //STEP 3:Store FuncTest key to get FuncTest Results
87         //-------------------------------------------------
88         storeFuncTestResultKey(onBoradingReq,functestResultKey);
89
90         return (oFuncTestResult.getOperStatus() == EnumResult.SUCCESS.getIndex()) 
91                 ? EnumResult.SUCCESS.getIndex() : EnumResult.FAIL.getIndex();
92     }
93
94     /**
95      * 
96      * @param onBoradingReq
97      */
98     private void buildResultPath(OnBoradingRequest onBoradingReq) 
99     {
100         String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId();
101         if(!FileUtil.checkFileExists(filePath))
102         {
103             FileUtil.createDirectory(filePath);
104         }
105     }
106
107     /**
108      * 
109      * @param packageData
110      * @return
111      */
112     public static String getFuncTestResults(PackageData packageData) 
113     {
114         logger.info("Function Test results request for Package:" + packageData.getCsarId());
115         ResultKey keydata = getFuncTestResultKey(packageData);
116         if(null == keydata || keydata.getKey().isEmpty())
117         {
118             logger.info("Function Test key Not Found for Package Id:",packageData.getCsarId());
119             return null;
120         }        
121         return  FunctionTestExceutor.getTestResultsByFuncTestKey(keydata.getKey());
122     }
123
124     /**
125      * Store Function Test Result key
126      * @param onBoradingReq
127      * @param resultKey
128      */
129     private void storeFuncTestResultKey(OnBoradingRequest onBoradingReq,String resultKey) 
130     {
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";        
134
135         logger.info("Function test Results Key for Package Id:" + onBoradingReq.getCsarId() + ", Key:" + resultKey + " Path" + filePath);
136         
137         ResultKey oResultKey = new ResultKey();
138         oResultKey.setCsarId(onBoradingReq.getCsarId());
139         oResultKey.setOperTypeId(CommonConstant.functionTest.FUNCTEST_OPERTYPE_ID);
140         oResultKey.setKey(resultKey);
141
142         FileUtil.writeJsonDatatoFile(filePath,oResultKey);
143     }
144
145     /**
146      * Store Function test Execution Results
147      * @param oFuncTestResult
148      */
149     private void updateResult(OnBoardingResult oFuncTestResult) 
150     {   
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);
156     }
157
158     /**
159      * Build Function Test Response
160      * @param onBoradingReq
161      * @param oFuncTestResult
162      */
163     private void buildFunctResponse(OnBoradingRequest onBoradingReq, OnBoardingResult oFuncTestResult) 
164     {
165         oFuncTestResult.setOperFinished(false);
166         oFuncTestResult.setCsarId(onBoradingReq.getCsarId());
167         oFuncTestResult.setOperTypeId(CommonConstant.functionTest.FUNCTEST_OPERTYPE_ID);
168
169         OnBoardingOperResult oPackageExists = new OnBoardingOperResult();
170         oPackageExists.setOperId(CommonConstant.functionTest.FUNCTEST_PACKAGE_EXISTS);
171         oPackageExists.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
172
173         OnBoardingOperResult functTesExec = new OnBoardingOperResult();
174         functTesExec.setOperId(CommonConstant.functionTest.FUNCTEST_EXEC);
175         functTesExec.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
176
177         List<OnBoardingOperResult> operResult = new ArrayList<OnBoardingOperResult>();
178         operResult.add(oPackageExists);
179         operResult.add(functTesExec); 
180
181         oFuncTestResult.setOperResult(operResult);
182     }
183
184     public static OnBoardingResult getOnBoardingResult(PackageData packageData) 
185     {
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);
188
189         return (OnBoardingResult)FileUtil.readJsonDatafFromFile(filePath,OnBoardingResult.class);
190     }
191
192     private static ResultKey getFuncTestResultKey(PackageData packageData) 
193     {
194         String fileName = getResultStorePath() + File.separator + packageData.getCsarId() + File.separator + "functestResultKey.json";
195         
196         logger.info("Func Test Result key for Package Id:" + packageData.getCsarId() + ", Result Path:" + fileName);
197         return (ResultKey) FileUtil.readJsonDatafFromFile(fileName,ResultKey.class);       
198     }
199     
200     private static String getResultStorePath() 
201     {
202         return org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath();
203     }
204
205     private void buildFuncTestResponse(OnBoardingResult oFuncTestResult, String opreKey, int operStatusVal) 
206     {        
207         List<OnBoardingOperResult>  operStatusList = oFuncTestResult.getOperResult();
208         for(OnBoardingOperResult operObj: operStatusList)
209         {
210             if(operObj.getOperId().equalsIgnoreCase(opreKey))
211             {
212                 operObj.setStatus(operStatusVal);
213                 break;
214             }
215         }
216     }
217 }