Improve unit-test coverage
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / adaptor / InstantiateVnfContinueRunnable.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
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
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.adaptor;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.concurrent.Executors;
23
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.bo.entity.VnfPackageInfo;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.inf.CatalogMgmrInf;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMModifyVnfRequest;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.OperationExecution;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.VnfcResourceInfo;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.mapper.VnfcResourceInfoMapper;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.mapper.VnfmJobExecutionMapper;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorImpl;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AffectedVnfc;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
47 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.beans.factory.annotation.Autowired;
51
52 import com.google.gson.Gson;
53
54
55 public class InstantiateVnfContinueRunnable implements Runnable {
56         private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
57         @Autowired
58         private CbamMgmrInf cbamMgmr;
59         @Autowired
60         private CatalogMgmrInf catalogMgmr;
61         @Autowired
62         private NslcmMgmrInf nslcmMgmr;
63         
64         private InstantiateVnfRequest driverRequest;
65         private String vnfInstanceId;
66         private String jobId;
67         private String vnfmId;
68         
69 //      private VnfmJobExecutionRepository jobDbMgmr;
70         @Autowired
71         private VnfmJobExecutionMapper jobDbMgmr;
72         @Autowired
73         private VnfcResourceInfoMapper vnfcDbMgmr;
74         
75         private Driver2CbamRequestConverter requestConverter;
76         
77         private Gson gson = new Gson();
78         
79         public InstantiateVnfContinueRunnable(String vnfmId, InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
80                         NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionMapper dbManager, VnfcResourceInfoMapper vnfcDbMgmr)
81         {
82                 this.driverRequest = driverRequest;
83                 this.vnfInstanceId = vnfInstanceId;
84                 this.jobId = jobId;
85                 this.nslcmMgmr = nslcmMgmr; 
86                 this.catalogMgmr = catalogMgmr;
87                 this.cbamMgmr = cbamMgmr;
88                 this.requestConverter = requestConverter;
89                 this.jobDbMgmr = dbManager;
90                 this.vnfmId = vnfmId;
91                 this.vnfcDbMgmr = vnfcDbMgmr;
92         }
93         
94         public void run() {
95                 //step 1 handle vnf package
96                 handleVnfPackage();
97                 
98                 handleGrant();
99                 
100                 handleModify();
101                 try {
102                         //step 5: instantiate vnf
103                         CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
104                         
105                         handleNotify(cbamInstantiateResponse.getId());
106                 } catch (Exception e) {
107                         logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
108                 }
109         }
110
111         private void handleNotify(String execId) {
112                 boolean instantiateFinished = false;
113                 
114                 do {
115                         try {
116                                 logger.info(" InstantiateVnfContinueRunnable --> handleNotify execId is " + execId);
117                                 CBAMQueryOperExecutionResponse exeResponse = cbamMgmr.queryOperExecution(execId);
118                                 if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED || exeResponse.getStatus() == CommonEnum.OperationStatus.FAILED)
119                                 {
120                                         instantiateFinished = true;
121                                         handleCbamInstantiateResponse(exeResponse, jobId);
122                                         if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED)
123                                         {
124                                                 
125                                                 logger.info("Start to get vnfc resource");
126                                                 List<VnfcResourceInfo> vnfcResources = new ArrayList<>();
127                                                                 
128                                                 try {
129                                                         vnfcResources = cbamMgmr.queryVnfcResource(vnfInstanceId);
130                                                 } catch (Exception e) {
131                                                         logger.error("Error to queryVnfcResource.", e);
132                                                 }
133                                                 
134                                                 logger.info("vnfc resource for vnfInstanceId " + vnfInstanceId + " is: " + gson.toJson(vnfcResources));
135                                                 logger.info("End to get vnfc resource");
136                                                 
137                                                 if(vnfcResources == null)
138                                                 {
139                                                         vnfcResources = new ArrayList<>();
140                                                 }
141                                                 logger.info("Start to notify LCM the instantiation result");
142                                                 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
143                                                 
144 //                                              OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
145                                                 
146                                                 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
147                                                 logger.info("End to notify LCM the instantiation result");
148                                         }
149                                 }
150                                 else {
151                                         Thread.sleep(60000);
152                                 }
153                                 
154                         } catch (Exception e) {
155                                 logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
156                         }
157                 } while(!instantiateFinished);
158                 
159         }
160
161         private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
162                 CBAMInstantiateVnfRequest  instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
163                 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
164                 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
165                 return cbamInstantiateResponse;
166         }
167
168         private void handleModify() {
169                 try {
170                         CBAMModifyVnfRequest  modifyReq = generateModifyVnfRequest();
171                         cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
172                 } catch (Exception e) {
173                         logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
174                 }
175         }
176
177         private void handleGrant(){
178                 try {
179                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
180                         nslcmMgmr.grantVnf(grantRequest);
181                 } catch (Exception e) {
182                         logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
183                 }
184         }
185
186         private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
187                 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
188                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
189                 CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
190                 
191                 return req;
192         }
193
194         private void handleVnfPackage() {
195                 Executors.newSingleThreadExecutor().execute(new Runnable() {
196                         @Override
197                         public void run() {
198                                 try {
199                                         //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
200                                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
201                                         String packageUrl = vnfPackageInfo.getDownloadUri();
202                                         String saveDir = "/service/vnfPackage";
203                                         String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
204                                         Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
205                                         process.waitFor();
206                                         
207                                         if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
208                                                 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
209 //                                              File csarFile = new File(saveDir + "/" + packageFileName);
210 //                                              //extract package
211 //                                              ZipUtil.explode(csarFile);
212 //                                              csarFile.delete();
213                                         }
214                                 } catch (Exception e) {
215                                         logger.error("Error to handleVnfPackage from SDC", e);
216                                 }
217                                 
218                         }
219                         
220                 });
221         }
222         
223         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(List<VnfcResourceInfo> vnfcResources) {
224                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
225             request.setStatus(CommonEnum.status.result);
226                 request.setVnfInstanceId(vnfInstanceId);
227                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
228                 request.setJobId(jobId);
229                 
230                 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
231                 request.setAffectedVnfc(affectedVnfcs);
232                 return request;
233         }
234
235         private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
236                 List<AffectedVnfc> vnfcs = new ArrayList<>();
237                 for(VnfcResourceInfo resource : vnfcResources)
238                 {
239                         if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
240                         {
241                                 AffectedVnfc vnfc = new AffectedVnfc();
242                                 vnfc.setVnfcInstanceId(resource.getId());
243                                 vnfc.setVduId(resource.getVduId());
244                                 vnfc.setVimid(resource.getComputeResource().getVimId());
245                                 vnfc.setVmid(resource.getComputeResource().getResourceId());
246                                 
247                                 vnfcs.add(vnfc);
248                                 
249                                 vnfcDbMgmr.insert(vnfc);
250                         }
251                 }
252                 return vnfcs;
253         }
254
255         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
256                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
257                 
258                 request.setVnfInstanceId(vnfInstanceId);
259                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
260                 request.setJobId(jobId);
261                 
262                 ResourceDefinition resource = getFreeVnfResource();
263                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
264                 resourceList.add(resource);
265                 request.setAddResource(resourceList);
266                 
267                 return request;
268         }
269
270         private ResourceDefinition getFreeVnfResource() {
271                 ResourceDefinition def = new ResourceDefinition();
272                 def.setVnfInstanceId(vnfInstanceId);
273                 def.setVimId("001");
274                 List<AddResource> resources = new ArrayList<>();
275                 AddResource res = new AddResource();
276                 res.setVdu("1");
277                 res.setType("vdu");
278                 res.setResourceDefinitionId(2);
279                 resources.add(res);
280                 def.setAddResource(resources);
281                 return def;
282         }
283
284         private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
285                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
286                 
287                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
288                 long nowTime = System.currentTimeMillis();
289                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
290                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
291 //                      jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
292                 } else if(CommonEnum.OperationStatus.OTHER == cbamInstantiateResponse.getStatus()){
293                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
294                 } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
295                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
296                         jobInfo.setOperateEndTime(nowTime);
297                         
298                 }
299                 else{
300                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
301                 }
302                         
303                 jobDbMgmr.update(jobInfo);
304         }
305
306         public void setDriverRequest(InstantiateVnfRequest driverRequest) {
307                 this.driverRequest = driverRequest;
308         }
309
310         public void setVnfInstanceId(String vnfInstanceId) {
311                 this.vnfInstanceId = vnfInstanceId;
312         }
313
314         public void setJobId(String jobId) {
315                 this.jobId = jobId;
316         }
317
318         public void setVnfmId(String vnfmId) {
319                 this.vnfmId = vnfmId;
320         }
321
322         public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
323                 this.requestConverter = requestConverter;
324         }
325
326 }