29524ccd441cf1a1e009279ecf009256cd519f44
[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 IOException {
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                 return gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
190         }
191
192         private void handleVnfPackage() {
193                 Executors.newSingleThreadExecutor().execute(new Runnable() {
194                         @Override
195                         public void run() {
196                                 try {
197                                         //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
198                                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
199                                         String packageUrl = vnfPackageInfo.getDownloadUri();
200                                         String saveDir = "/service/vnfPackage";
201                                         String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
202                                         Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
203                                         process.waitFor();
204                                         
205                                         if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
206                                                 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
207 //                                              File csarFile = new File(saveDir + "/" + packageFileName);
208 //                                              //extract package
209 //                                              ZipUtil.explode(csarFile);
210 //                                              csarFile.delete();
211                                         }
212                                 } catch (Exception e) {
213                                         logger.error("Error to handleVnfPackage from SDC", e);
214                                 }
215                                 
216                         }
217                         
218                 });
219         }
220         
221         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(List<VnfcResourceInfo> vnfcResources) {
222                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
223             request.setStatus(CommonEnum.status.result);
224                 request.setVnfInstanceId(vnfInstanceId);
225                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
226                 request.setJobId(jobId);
227                 
228                 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
229                 request.setAffectedVnfc(affectedVnfcs);
230                 return request;
231         }
232
233         private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
234                 List<AffectedVnfc> vnfcs = new ArrayList<>();
235                 for(VnfcResourceInfo resource : vnfcResources)
236                 {
237                         if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
238                         {
239                                 AffectedVnfc vnfc = new AffectedVnfc();
240                                 vnfc.setVnfcInstanceId(resource.getId());
241                                 vnfc.setVduId(resource.getVduId());
242                                 vnfc.setVimid(resource.getComputeResource().getVimId());
243                                 vnfc.setVmid(resource.getComputeResource().getResourceId());
244                                 
245                                 vnfcs.add(vnfc);
246                                 
247                                 vnfcDbMgmr.insert(vnfc);
248                         }
249                 }
250                 return vnfcs;
251         }
252
253         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
254                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
255                 
256                 request.setVnfInstanceId(vnfInstanceId);
257                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
258                 request.setJobId(jobId);
259                 
260                 ResourceDefinition resource = getFreeVnfResource();
261                 List<ResourceDefinition> resourceList = new ArrayList<>();
262                 resourceList.add(resource);
263                 request.setAddResource(resourceList);
264                 
265                 return request;
266         }
267
268         private ResourceDefinition getFreeVnfResource() {
269                 ResourceDefinition def = new ResourceDefinition();
270                 def.setVnfInstanceId(vnfInstanceId);
271                 def.setVimId("001");
272                 List<AddResource> resources = new ArrayList<>();
273                 AddResource res = new AddResource();
274                 res.setVdu("1");
275                 res.setType("vdu");
276                 res.setResourceDefinitionId(2);
277                 resources.add(res);
278                 def.setAddResource(resources);
279                 return def;
280         }
281
282         private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
283                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
284                 
285                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
286                 long nowTime = System.currentTimeMillis();
287                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
288                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
289 //                      jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
290                 } else if(CommonEnum.OperationStatus.OTHER == cbamInstantiateResponse.getStatus()){
291                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
292                 } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
293                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
294                         jobInfo.setOperateEndTime(nowTime);
295                         
296                 }
297                 else{
298                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
299                 }
300                         
301                 jobDbMgmr.update(jobInfo);
302         }
303
304         public void setDriverRequest(InstantiateVnfRequest driverRequest) {
305                 this.driverRequest = driverRequest;
306         }
307
308         public void setVnfInstanceId(String vnfInstanceId) {
309                 this.vnfInstanceId = vnfInstanceId;
310         }
311
312         public void setJobId(String jobId) {
313                 this.jobId = jobId;
314         }
315
316         public void setVnfmId(String vnfmId) {
317                 this.vnfmId = vnfmId;
318         }
319
320         public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
321                 this.requestConverter = requestConverter;
322         }
323
324 }