2 * Copyright 2016-2017, Nokia Corporation.
3 * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.vfc.nfvo.driver.vnfm.svnfm.adaptor;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.Executors;
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;
53 import com.google.gson.Gson;
56 public class InstantiateVnfContinueRunnable implements Runnable {
57 private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
59 private CbamMgmrInf cbamMgmr;
61 private CatalogMgmrInf catalogMgmr;
63 private NslcmMgmrInf nslcmMgmr;
65 private InstantiateVnfRequest driverRequest;
66 private String vnfInstanceId;
68 private String vnfmId;
70 // private VnfmJobExecutionRepository jobDbMgmr;
72 private VnfmJobExecutionMapper jobDbMgmr;
74 private VnfcResourceInfoMapper vnfcDbMgmr;
76 private Driver2CbamRequestConverter requestConverter;
78 private Gson gson = new Gson();
82 public static class InstantiateVnfContinueRunnableBuilder {
83 private String vnfmId;
84 private InstantiateVnfRequest driverRequest;
85 private String vnfInstanceId;
87 private NslcmMgmrInf nslcmMgmr;
88 private CatalogMgmrInf catalogMgmr;
89 private CbamMgmrInf cbamMgmr;
90 private Driver2CbamRequestConverter requestConverter;
91 private VnfmJobExecutionMapper dbManager;
92 private VnfcResourceInfoMapper vnfcDbMgmr;
94 public InstantiateVnfContinueRunnableBuilder setVnfmId(String vnfmId) {
99 public InstantiateVnfContinueRunnableBuilder setDriverRequest(InstantiateVnfRequest driverRequest) {
100 this.driverRequest = driverRequest;
104 public InstantiateVnfContinueRunnableBuilder setVnfInstanceId(String vnfInstanceId) {
105 this.vnfInstanceId = vnfInstanceId;
109 public InstantiateVnfContinueRunnableBuilder setJobId(String jobId) {
114 public InstantiateVnfContinueRunnableBuilder setNslcmMgmr(NslcmMgmrInf nslcmMgmr) {
115 this.nslcmMgmr = nslcmMgmr;
119 public InstantiateVnfContinueRunnableBuilder setCatalogMgmr(CatalogMgmrInf catalogMgmr) {
120 this.catalogMgmr = catalogMgmr;
124 public InstantiateVnfContinueRunnableBuilder setCbamMgmr(CbamMgmrInf cbamMgmr) {
125 this.cbamMgmr = cbamMgmr;
129 public InstantiateVnfContinueRunnableBuilder setRequestConverter(Driver2CbamRequestConverter requestConverter) {
130 this.requestConverter = requestConverter;
134 public InstantiateVnfContinueRunnableBuilder setDbManager(VnfmJobExecutionMapper dbManager) {
135 this.dbManager = dbManager;
139 public InstantiateVnfContinueRunnableBuilder setVnfcDbMgmr(VnfcResourceInfoMapper vnfcDbMgmr) {
140 this.vnfcDbMgmr = vnfcDbMgmr;
144 public InstantiateVnfContinueRunnable build() {
145 return new InstantiateVnfContinueRunnable(this);
150 private InstantiateVnfContinueRunnable(InstantiateVnfContinueRunnableBuilder builder) {
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;
166 //step 1 handle vnf package
173 //step 5: instantiate vnf
174 CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
176 handleNotify(cbamInstantiateResponse.getId());
177 } catch (Exception e) {
178 logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
182 private void handleNotify(String execId) {
183 boolean instantiateFinished = false;
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)
191 instantiateFinished = true;
192 handleCbamInstantiateResponse(exeResponse, jobId);
193 if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED)
196 logger.info("Start to get vnfc resource");
197 List<VnfcResourceInfo> vnfcResources = new ArrayList<>();
200 vnfcResources = cbamMgmr.queryVnfcResource(vnfInstanceId);
201 } catch (Exception e) {
202 logger.error("Error to queryVnfcResource.", e);
205 logger.info("vnfc resource for vnfInstanceId " + vnfInstanceId + " is: " + gson.toJson(vnfcResources));
206 logger.info("End to get vnfc resource");
208 if(vnfcResources == null)
210 vnfcResources = new ArrayList<>();
212 logger.info("Start to notify LCM the instantiation result");
213 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
215 // OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
217 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
218 logger.info("End to notify LCM the instantiation result");
225 } catch (Exception e) {
226 logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
228 } while(!instantiateFinished);
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;
239 private void handleModify() {
241 CBAMModifyVnfRequest modifyReq = generateModifyVnfRequest();
242 cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
243 } catch (Exception e) {
244 logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
248 private void handleGrant(){
250 NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
251 nslcmMgmr.grantVnf(grantRequest);
252 } catch (Exception e) {
253 logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
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);
263 private void handleVnfPackage() {
264 Executors.newSingleThreadExecutor().execute(new Runnable() {
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);
276 if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
277 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
278 // File csarFile = new File(saveDir + "/" + packageFileName);
280 // ZipUtil.explode(csarFile);
281 // csarFile.delete();
283 } catch (Exception e) {
284 logger.error("Error to handleVnfPackage from SDC", e);
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);
299 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
300 request.setAffectedVnfc(affectedVnfcs);
304 private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
305 List<AffectedVnfc> vnfcs = new ArrayList<>();
306 for(VnfcResourceInfo resource : vnfcResources)
308 if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
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());
318 vnfcDbMgmr.insert(vnfc);
324 private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
325 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
327 request.setVnfInstanceId(vnfInstanceId);
328 request.setLifecycleOperation(LifecycleOperation.Instantiate);
329 request.setJobId(jobId);
331 ResourceDefinition resource = getFreeVnfResource();
332 List<ResourceDefinition> resourceList = new ArrayList<>();
333 resourceList.add(resource);
334 request.setAddResource(resourceList);
339 private ResourceDefinition getFreeVnfResource() {
340 ResourceDefinition def = new ResourceDefinition();
341 def.setVnfInstanceId(vnfInstanceId);
343 List<AddResource> resources = new ArrayList<>();
344 AddResource res = new AddResource();
347 res.setResourceDefinitionId(2);
349 def.setAddResource(resources);
353 private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
354 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
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);
369 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
372 jobDbMgmr.update(jobInfo);
375 public void setDriverRequest(InstantiateVnfRequest driverRequest) {
376 this.driverRequest = driverRequest;
379 public void setVnfInstanceId(String vnfInstanceId) {
380 this.vnfInstanceId = vnfInstanceId;
383 public void setJobId(String jobId) {
387 public void setVnfmId(String vnfmId) {
388 this.vnfmId = vnfmId;
391 public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
392 this.requestConverter = requestConverter;