Code Improvements-Vnfsdk-refrepo sonar issue fixes
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / main / java / org / onap / vnfsdk / marketplace / onboarding / hooks / validatelifecycle / LifecycleTestHook.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.validatelifecycle;
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.entity.EnumOperationStatus;
26 import org.onap.vnfsdk.marketplace.entity.EnumResult;
27 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingOperResult;
28 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingResult;
29 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoradingRequest;
30 import org.onap.vnfsdk.marketplace.onboarding.entity.ResultKey;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /* It executes the life cycle (create, delete) test for the VNF on the specified VM and collects the result 
35  * and return result to the caller and then uploads package to the catalogue 
36  *  
37  * OnBoardingHandler --> LifecycleTestHook---> LifecycleTestExecutor */
38
39 public class LifecycleTestHook {
40         private static final Logger logger = LoggerFactory.getLogger(LifecycleTestHook.class);
41
42         /**
43          * Start Executing Life cycle test
44          * 
45          * @param onBoradingReq
46          * @return
47          */
48         public int exec(OnBoradingRequest onBoradingReq) {
49                 logger.info("OnboardingRequest Lifecycle Request received for Package:{} Path:{}" , onBoradingReq.getCsarId()
50                                 , onBoradingReq.getPackagePath());
51
52                 // STEP 1: Validate Input and Build result
53             // ---------------------------------------------------------
54                 buildResultPath(onBoradingReq);
55
56                 OnBoardingResult olifecycleTestResult = new OnBoardingResult();
57                 buildlifecycleTestResponse(onBoradingReq, olifecycleTestResult);
58                 updateResult(olifecycleTestResult);
59
60                 if (null == onBoradingReq.getCsarIdCatalouge() || onBoradingReq.getCsarIdCatalouge().isEmpty()) {
61                         olifecycleTestResult.setOperFinished(true);
62                         olifecycleTestResult.setOperStatus(EnumResult.FAIL.getIndex());
63                         buildFuncTestResponse(olifecycleTestResult, CommonConstant.LifeCycleTest.LIFECYCLE_TEST_EXEC,
64                                         EnumOperationStatus.FAILED.getIndex());
65                         updateResult(olifecycleTestResult);
66                         return EnumResult.FAIL.getIndex();
67                 }
68
69                 LifeCycleTestReq oLifeCycleTestReq = new LifeCycleTestReq();
70                 populateLifeCycleReq(onBoradingReq, oLifeCycleTestReq);
71
72                 // STEP 2: Execute Life Cycle Test and Get Result Back !!!!
73                 // ---------------------------------------------------------
74                 String lifecycleTestResultKey = LifecycleTestExceutor.execlifecycleTest(onBoradingReq, oLifeCycleTestReq);
75                 if (null == lifecycleTestResultKey) {
76                         olifecycleTestResult.setOperFinished(true);
77                         olifecycleTestResult.setOperStatus(EnumResult.FAIL.getIndex());
78                         buildFuncTestResponse(olifecycleTestResult, CommonConstant.LifeCycleTest.LIFECYCLE_TEST_EXEC,
79                                         EnumOperationStatus.FAILED.getIndex());
80                         updateResult(olifecycleTestResult);
81                         return EnumResult.FAIL.getIndex();
82                 }
83
84                 olifecycleTestResult.setOperFinished(true);
85                 olifecycleTestResult.setOperStatus(EnumResult.SUCCESS.getIndex());
86                 buildFuncTestResponse(olifecycleTestResult, CommonConstant.LifeCycleTest.LIFECYCLE_TEST_EXEC,
87                                 EnumOperationStatus.SUCCESS.getIndex());
88                 updateResult(olifecycleTestResult);
89
90                 // STEP 3: Store Lifecycle test key to get Life cycle Test Results
91                 // -------------------------------------------------
92                 storelifecycleResultKey(onBoradingReq, lifecycleTestResultKey);
93
94                 return (olifecycleTestResult.getOperStatus() == EnumResult.SUCCESS.getIndex()) ? EnumResult.SUCCESS.getIndex()
95                                 : EnumResult.FAIL.getIndex();
96         }
97
98         private void populateLifeCycleReq(OnBoradingRequest onBoradingReq, LifeCycleTestReq oLifeCycleTestReq) {
99                 
100                 // Input error handling is done by lifecycle module, not need validate here
101                 oLifeCycleTestReq.setCsarId(onBoradingReq.getCsarId());
102                 oLifeCycleTestReq.setLabVimId(oLifeCycleTestReq.getLabVimId());
103
104                 // Currently this is not populated, only lavVimId is sufficient
105                 List<String> vimIds = new ArrayList<>();
106                 oLifeCycleTestReq.setVimIds(vimIds);
107         }
108
109         /**
110          * Build result path where result is stored as a file
111          * @param onBoradingReq
112          */
113         private void buildResultPath(OnBoradingRequest onBoradingReq) {
114                 String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId();
115                 if (!FileUtil.checkFileExists(filePath)) {
116                         FileUtil.createDirectory(filePath);
117                 }
118         }
119
120         /**
121          * Store Function test Execution Results
122          * 
123          * @param oFuncTestResult
124          */
125         private void updateResult(OnBoardingResult oFuncTestResult) {
126                 // STore Results to DB(Currently we will make JSON and Store JSON to
127                 // Package Path)
128                 // -------------------------------------------------------------------------------
129                 String jsonoFuncTestResult = ToolUtil.objectToString(oFuncTestResult);
130                 logger.info("Lifecycle test Status for Package Id:{} Result:{}" , oFuncTestResult.getCsarId()
131                                 , jsonoFuncTestResult);
132                 String filePath = getResultStorePath() + File.separator + oFuncTestResult.getCsarId() + File.separator
133                                 + "lifecycleTest.json";
134                 FileUtil.writeJsonDatatoFile(filePath, oFuncTestResult);
135         }
136
137         /**
138          * Build Function Test Response
139          * 
140          * @param onBoradingReq
141          * @param oFuncTestResult
142          */
143         private void buildlifecycleTestResponse(OnBoradingRequest onBoradingReq, OnBoardingResult oTestResult) {
144                 oTestResult.setOperFinished(false);
145                 oTestResult.setCsarId(onBoradingReq.getCsarId());
146                 oTestResult.setOperTypeId(CommonConstant.LifeCycleTest.LIFECYCLE_TEST_OPERTYPE_ID);
147
148                 OnBoardingOperResult lifecycleTestExec = new OnBoardingOperResult();
149                 lifecycleTestExec.setOperId(CommonConstant.LifeCycleTest.LIFECYCLE_TEST_EXEC);
150                 lifecycleTestExec.setStatus(EnumOperationStatus.NOTSTARTED.getIndex());
151
152                 List<OnBoardingOperResult> operResult = new ArrayList<>();
153                 operResult.add(lifecycleTestExec);
154                 oTestResult.setOperResult(operResult);
155         }
156
157
158         /**
159          * Store Lifecycle Test Result key
160          * 
161          * @param onBoradingReq
162          * @param resultKey
163          */
164         private void storelifecycleResultKey(OnBoradingRequest onBoradingReq, String resultKey) {
165                 // Currently we will make JSON and Store JSON to Package Path)
166                 // -------------------------------------------------------------------------------
167                 String filePath = getResultStorePath() + File.separator + onBoradingReq.getCsarId() + File.separator
168                                 + "lifecycleTestResultKey.json";
169
170                 logger.debug("Function test Results Key for Package Id:{} Key:{} Path{}" , onBoradingReq.getCsarId(), resultKey
171                                 , filePath);
172
173                 ResultKey oResultKey = new ResultKey();
174                 oResultKey.setCsarId(onBoradingReq.getCsarId());
175                 oResultKey.setOperTypeId(CommonConstant.LifeCycleTest.LIFECYCLE_TEST_OPERTYPE_ID);
176                 oResultKey.setKey(resultKey);
177
178                 FileUtil.writeJsonDatatoFile(filePath, oResultKey);
179         }
180
181         private static String getResultStorePath() {
182                 // Using full path due to compilation issue
183                 return org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil.getHttpServerAbsolutePath();
184         }
185
186         private void buildFuncTestResponse(OnBoardingResult oFuncTestResult, String opreKey, int operStatusVal) {
187                 List<OnBoardingOperResult> operStatusList = oFuncTestResult.getOperResult();
188                 for (OnBoardingOperResult operObj : operStatusList) {
189                         if (operObj.getOperId().equalsIgnoreCase(opreKey)) {
190                                 operObj.setStatus(operStatusVal);
191                                 break;
192                         }
193                 }
194         }
195 }