2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.adaptor;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfResponse;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class TerminateVnfContinueRunnable implements Runnable {
41 private static final Logger logger = LoggerFactory.getLogger(TerminateVnfContinueRunnable.class);
43 private CbamMgmrInf cbamMgmr;
44 private NslcmMgmrInf nslcmMgmr;
46 private TerminateVnfRequest driverRequest;
47 private String vnfInstanceId;
49 private String vnfmId;
50 private VnfmJobExecutionRepository jobDbMgmr;
52 private Driver2CbamRequestConverter requestConverter;
54 public TerminateVnfContinueRunnable(String vnfmId, TerminateVnfRequest driverRequest, String vnfInstanceId, String jobId,
55 NslcmMgmrInf nslcmMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
57 this.driverRequest = driverRequest;
58 this.vnfInstanceId = vnfInstanceId;
59 this.nslcmMgmr = nslcmMgmr;
60 this.cbamMgmr = cbamMgmr;
61 this.requestConverter = requestConverter;
63 this.jobDbMgmr = dbManager;
67 private void handleGrant(){
69 NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
70 nslcmMgmr.grantVnf(grantRequest);
71 } catch (Exception e) {
72 logger.error("TerminateVnfContinueRunnable --> handleGrant error.", e);
82 private void handleDelete() {
84 cbamMgmr.deleteVnf(vnfInstanceId);
85 } catch (Exception e) {
86 logger.error("TerminateVnfContinueRunnable --> handleDelete error.", e);
90 private CBAMTerminateVnfResponse handleTerminate() {
91 CBAMTerminateVnfResponse cbamResponse = null;
93 CBAMTerminateVnfRequest modifyReq = requestConverter.terminateReqConvert(driverRequest);
94 cbamResponse = cbamMgmr.terminateVnf(modifyReq, vnfInstanceId);
95 handleCbamTerminateResponse(cbamResponse, jobId);
96 } catch (Exception e) {
97 logger.error("TerminateVnfContinueRunnable --> handleTerminate error.", e);
101 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamResponse);
102 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
103 } catch (Exception e) {
104 logger.error("TerminateVnfContinueRunnable --> handleNotify error.", e);
111 private void handleCbamTerminateResponse(CBAMTerminateVnfResponse cbamResponse, String jobId) {
112 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
114 jobInfo.setVnfmExecutionId(cbamResponse.getId());
115 if(CommonEnum.OperationStatus.FAILED == cbamResponse.getStatus()) {
116 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
119 jobInfo.setStatus(cbamResponse.getStatus().toString());
121 jobDbMgmr.save(jobInfo);
124 private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
125 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
127 request.setVnfInstanceId(vnfInstanceId);
128 request.setLifecycleOperation(LifecycleOperation.Terminal);
129 request.setJobId(jobId);
131 ResourceDefinition resource = getFreeVnfResource();
132 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
133 resourceList.add(resource);
134 request.setRemoveResource(resourceList);
139 private ResourceDefinition getFreeVnfResource() {
140 ResourceDefinition def = new ResourceDefinition();
141 def.setVnfInstanceId(vnfInstanceId);
143 List<AddResource> resources = new ArrayList<>();
144 AddResource res = new AddResource();
147 res.setResourceDefinitionId(2);
149 def.setAddResource(resources);
153 private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMTerminateVnfResponse cbamResponse) {
154 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
155 if(CommonEnum.OperationStatus.STARTED == cbamResponse.getStatus())
157 request.setStatus(CommonEnum.status.start);
161 request.setStatus(CommonEnum.status.result);
163 //TODO the following are for the result
164 // request.setAffectedVnfc(affectedVnfc);
165 // request.setAffectedVI(affectedVI);
166 // request.setAffectedVirtualStorage(affectedVirtualStorage);
169 request.setVnfInstanceId(vnfInstanceId);
170 request.setOperation(CommonConstants.NSLCM_OPERATION_TERMINATE);
171 request.setJobId(jobId);
175 private void handleNslcmGrantResponse(NslcmGrantVnfResponse grantResponse) {
176 // TODO Auto-generated method stub