Implement builder
[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  * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
4  * 
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.vfc.nfvo.driver.vnfm.svnfm.adaptor;
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.Executors;
24
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.bo.entity.VnfPackageInfo;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.inf.CatalogMgmrInf;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMModifyVnfRequest;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.OperationExecution;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.VnfcResourceInfo;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.mapper.VnfcResourceInfoMapper;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.mapper.VnfmJobExecutionMapper;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorImpl;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AffectedVnfc;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
47 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
48 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.beans.factory.annotation.Autowired;
52
53 import com.google.gson.Gson;
54
55
56 public class InstantiateVnfContinueRunnable implements Runnable {
57         private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
58         @Autowired
59         private CbamMgmrInf cbamMgmr;
60         @Autowired
61         private CatalogMgmrInf catalogMgmr;
62         @Autowired
63         private NslcmMgmrInf nslcmMgmr;
64         
65         private InstantiateVnfRequest driverRequest;
66         private String vnfInstanceId;
67         private String jobId;
68         private String vnfmId;
69         
70 //      private VnfmJobExecutionRepository jobDbMgmr;
71         @Autowired
72         private VnfmJobExecutionMapper jobDbMgmr;
73         @Autowired
74         private VnfcResourceInfoMapper vnfcDbMgmr;
75         
76         private Driver2CbamRequestConverter requestConverter;
77         
78         private Gson gson = new Gson();
79         
80         // Builder class
81         
82         public static class InstantiateVnfContinueRunnableBuilder {
83             private String vnfmId;
84             private InstantiateVnfRequest driverRequest;
85             private String vnfInstanceId;
86             private String jobId;
87             private NslcmMgmrInf nslcmMgmr;
88             private CatalogMgmrInf catalogMgmr;
89             private CbamMgmrInf cbamMgmr;
90             private Driver2CbamRequestConverter requestConverter;
91             private VnfmJobExecutionMapper dbManager;
92             private VnfcResourceInfoMapper vnfcDbMgmr;
93
94             public InstantiateVnfContinueRunnableBuilder setVnfmId(String vnfmId) {
95                 this.vnfmId = vnfmId;
96                 return this;
97             }
98
99             public InstantiateVnfContinueRunnableBuilder setDriverRequest(InstantiateVnfRequest driverRequest) {
100                 this.driverRequest = driverRequest;
101                 return this;
102             }
103
104             public InstantiateVnfContinueRunnableBuilder setVnfInstanceId(String vnfInstanceId) {
105                 this.vnfInstanceId = vnfInstanceId;
106                 return this;
107             }
108
109             public InstantiateVnfContinueRunnableBuilder setJobId(String jobId) {
110                 this.jobId = jobId;
111                 return this;
112             }
113
114             public InstantiateVnfContinueRunnableBuilder setNslcmMgmr(NslcmMgmrInf nslcmMgmr) {
115                 this.nslcmMgmr = nslcmMgmr;
116                 return this;
117             }
118
119             public InstantiateVnfContinueRunnableBuilder setCatalogMgmr(CatalogMgmrInf catalogMgmr) {
120                 this.catalogMgmr = catalogMgmr;
121                 return this;
122             }
123
124             public InstantiateVnfContinueRunnableBuilder setCbamMgmr(CbamMgmrInf cbamMgmr) {
125                 this.cbamMgmr = cbamMgmr;
126                 return this;
127             }
128
129             public InstantiateVnfContinueRunnableBuilder setRequestConverter(Driver2CbamRequestConverter requestConverter) {
130                 this.requestConverter = requestConverter;
131                 return this;
132             }
133
134             public InstantiateVnfContinueRunnableBuilder setDbManager(VnfmJobExecutionMapper dbManager) {
135                 this.dbManager = dbManager;
136                 return this;
137             }
138
139             public InstantiateVnfContinueRunnableBuilder setVnfcDbMgmr(VnfcResourceInfoMapper vnfcDbMgmr) {
140                 this.vnfcDbMgmr = vnfcDbMgmr;
141                 return this;
142             }
143
144             public InstantiateVnfContinueRunnable build() {
145                 return new InstantiateVnfContinueRunnable(this);
146             }
147         }
148         
149                 
150         private InstantiateVnfContinueRunnable(InstantiateVnfContinueRunnableBuilder builder) {
151             
152             this.driverRequest = builder.driverRequest;
153         this.vnfInstanceId = builder.vnfInstanceId;
154         this.jobId = builder.jobId;
155         this.nslcmMgmr = builder.nslcmMgmr; 
156         this.catalogMgmr = builder.catalogMgmr;
157         this.cbamMgmr = builder.cbamMgmr;
158         this.requestConverter = builder.requestConverter;
159         this.jobDbMgmr = builder.dbManager;
160         this.vnfmId = builder.vnfmId;
161         this.vnfcDbMgmr = builder.vnfcDbMgmr;
162             
163     }
164
165     public void run() {
166                 //step 1 handle vnf package
167                 handleVnfPackage();
168                 
169                 handleGrant();
170                 
171                 handleModify();
172                 try {
173                         //step 5: instantiate vnf
174                         CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
175                         
176                         handleNotify(cbamInstantiateResponse.getId());
177                 } catch (Exception e) {
178                         logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
179                 }
180         }
181
182         private void handleNotify(String execId) {
183                 boolean instantiateFinished = false;
184                 
185                 do {
186                         try {
187                                 logger.info(" InstantiateVnfContinueRunnable --> handleNotify execId is " + execId);
188                                 CBAMQueryOperExecutionResponse exeResponse = cbamMgmr.queryOperExecution(execId);
189                                 if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED || exeResponse.getStatus() == CommonEnum.OperationStatus.FAILED)
190                                 {
191                                         instantiateFinished = true;
192                                         handleCbamInstantiateResponse(exeResponse, jobId);
193                                         if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED)
194                                         {
195                                                 
196                                                 logger.info("Start to get vnfc resource");
197                                                 List<VnfcResourceInfo> vnfcResources = new ArrayList<>();
198                                                                 
199                                                 try {
200                                                         vnfcResources = cbamMgmr.queryVnfcResource(vnfInstanceId);
201                                                 } catch (Exception e) {
202                                                         logger.error("Error to queryVnfcResource.", e);
203                                                 }
204                                                 
205                                                 logger.info("vnfc resource for vnfInstanceId " + vnfInstanceId + " is: " + gson.toJson(vnfcResources));
206                                                 logger.info("End to get vnfc resource");
207                                                 
208                                                 if(vnfcResources == null)
209                                                 {
210                                                         vnfcResources = new ArrayList<>();
211                                                 }
212                                                 logger.info("Start to notify LCM the instantiation result");
213                                                 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
214                                                 
215 //                                              OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
216                                                 
217                                                 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
218                                                 logger.info("End to notify LCM the instantiation result");
219                                         }
220                                 }
221                                 else {
222                                         Thread.sleep(60000);
223                                 }
224                                 
225                         } catch (Exception e) {
226                                 logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
227                         }
228                 } while(!instantiateFinished);
229                 
230         }
231
232         private CBAMInstantiateVnfResponse handleInstantiate() throws IOException {
233                 CBAMInstantiateVnfRequest  instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
234                 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
235                 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
236                 return cbamInstantiateResponse;
237         }
238
239         private void handleModify() {
240                 try {
241                         CBAMModifyVnfRequest  modifyReq = generateModifyVnfRequest();
242                         cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
243                 } catch (Exception e) {
244                         logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
245                 }
246         }
247
248         private void handleGrant(){
249                 try {
250                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
251                         nslcmMgmr.grantVnf(grantRequest);
252                 } catch (Exception e) {
253                         logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
254                 }
255         }
256
257         private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
258                 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
259                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
260                 return gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
261         }
262
263         private void handleVnfPackage() {
264                 Executors.newSingleThreadExecutor().execute(new Runnable() {
265                         @Override
266                         public void run() {
267                                 try {
268                                         //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
269                                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
270                                         String packageUrl = vnfPackageInfo.getDownloadUri();
271                                         String saveDir = "/service/vnfPackage";
272                                         String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
273                                         Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
274                                         process.waitFor();
275                                         
276                                         if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
277                                                 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
278 //                                              File csarFile = new File(saveDir + "/" + packageFileName);
279 //                                              //extract package
280 //                                              ZipUtil.explode(csarFile);
281 //                                              csarFile.delete();
282                                         }
283                                 } catch (Exception e) {
284                                         logger.error("Error to handleVnfPackage from SDC", e);
285                                 }
286                                 
287                         }
288                         
289                 });
290         }
291         
292         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(List<VnfcResourceInfo> vnfcResources) {
293                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
294             request.setStatus(CommonEnum.status.result);
295                 request.setVnfInstanceId(vnfInstanceId);
296                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
297                 request.setJobId(jobId);
298                 
299                 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
300                 request.setAffectedVnfc(affectedVnfcs);
301                 return request;
302         }
303
304         private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
305                 List<AffectedVnfc> vnfcs = new ArrayList<>();
306                 for(VnfcResourceInfo resource : vnfcResources)
307                 {
308                         if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
309                         {
310                                 AffectedVnfc vnfc = new AffectedVnfc();
311                                 vnfc.setVnfcInstanceId(resource.getId());
312                                 vnfc.setVduId(resource.getVduId());
313                                 vnfc.setVimid(resource.getComputeResource().getVimId());
314                                 vnfc.setVmid(resource.getComputeResource().getResourceId());
315                                 
316                                 vnfcs.add(vnfc);
317                                 
318                                 vnfcDbMgmr.insert(vnfc);
319                         }
320                 }
321                 return vnfcs;
322         }
323
324         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
325                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
326                 
327                 request.setVnfInstanceId(vnfInstanceId);
328                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
329                 request.setJobId(jobId);
330                 
331                 ResourceDefinition resource = getFreeVnfResource();
332                 List<ResourceDefinition> resourceList = new ArrayList<>();
333                 resourceList.add(resource);
334                 request.setAddResource(resourceList);
335                 
336                 return request;
337         }
338
339         private ResourceDefinition getFreeVnfResource() {
340                 ResourceDefinition def = new ResourceDefinition();
341                 def.setVnfInstanceId(vnfInstanceId);
342                 def.setVimId("001");
343                 List<AddResource> resources = new ArrayList<>();
344                 AddResource res = new AddResource();
345                 res.setVdu("1");
346                 res.setType("vdu");
347                 res.setResourceDefinitionId(2);
348                 resources.add(res);
349                 def.setAddResource(resources);
350                 return def;
351         }
352
353         private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
354                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
355                 
356                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
357                 long nowTime = System.currentTimeMillis();
358                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
359                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
360 //                      jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
361                 } else if(CommonEnum.OperationStatus.OTHER == cbamInstantiateResponse.getStatus()){
362                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
363                 } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
364                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
365                         jobInfo.setOperateEndTime(nowTime);
366                         
367                 }
368                 else{
369                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
370                 }
371                         
372                 jobDbMgmr.update(jobInfo);
373         }
374
375         public void setDriverRequest(InstantiateVnfRequest driverRequest) {
376                 this.driverRequest = driverRequest;
377         }
378
379         public void setVnfInstanceId(String vnfInstanceId) {
380                 this.vnfInstanceId = vnfInstanceId;
381         }
382
383         public void setJobId(String jobId) {
384                 this.jobId = jobId;
385         }
386
387         public void setVnfmId(String vnfmId) {
388                 this.vnfmId = vnfmId;
389         }
390
391         public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
392                 this.requestConverter = requestConverter;
393         }
394
395 }