Modify database part in the deployment
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / adaptor / TerminateVnfContinueRunnable.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
23 import org.apache.http.client.ClientProtocolException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfResponse;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
40
41
42
43 public class TerminateVnfContinueRunnable implements Runnable {
44         private static final Logger logger = LoggerFactory.getLogger(TerminateVnfContinueRunnable.class);
45
46         private CbamMgmrInf cbamMgmr;
47         private NslcmMgmrInf nslcmMgmr;
48         
49         private TerminateVnfRequest driverRequest;
50         private String vnfInstanceId;
51         private String jobId;
52         private VnfmJobExecutionRepository jobDbMgmr;
53         
54         private Driver2CbamRequestConverter requestConverter;
55         
56         public TerminateVnfContinueRunnable(TerminateVnfRequest driverRequest, String vnfInstanceId, String jobId,
57                         NslcmMgmrInf nslcmMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
58         {
59                 this.driverRequest = driverRequest;
60                 this.vnfInstanceId = vnfInstanceId;
61                 this.nslcmMgmr = nslcmMgmr; 
62                 this.cbamMgmr = cbamMgmr;
63                 this.requestConverter = requestConverter;
64                 this.jobId = jobId;
65                 this.jobDbMgmr = dbManager;
66         }
67         
68         public void run() {
69                 try {
70                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
71                         NslcmGrantVnfResponse grantResponse = nslcmMgmr.grantVnf(grantRequest);
72                         handleNslcmGrantResponse(grantResponse);
73                         
74                         CBAMTerminateVnfRequest cbamRequest = requestConverter.terminateReqConvert(driverRequest);
75                         CBAMTerminateVnfResponse cbamResponse = cbamMgmr.terminateVnf(cbamRequest, vnfInstanceId);
76                         handleCbamInstantiateResponse(cbamResponse, jobId);
77                         
78                         cbamMgmr.deleteVnf(vnfInstanceId);
79                         
80                         NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamResponse);
81                         nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfInstanceId);
82                         
83                 } catch (ClientProtocolException e) {
84                         logger.error("TerminateVnfContinueRunnable run error ClientProtocolException", e);
85                 } catch (IOException e) {
86                         logger.error("TerminateVnfContinueRunnable run error IOException", e);
87                 }
88                 
89         }
90         
91         private void handleCbamInstantiateResponse(CBAMTerminateVnfResponse cbamResponse, String jobId) {
92                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.getLong(jobId));
93                 
94                 jobInfo.setVnfmExecutionId(cbamResponse.getId());
95                 jobDbMgmr.save(jobInfo);
96         }
97         
98         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
99                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
100                 
101                 request.setVnfInstanceId(vnfInstanceId);
102                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
103                 request.setJobId(jobId);
104                 
105                 ResourceDefinition resource = getFreeVnfResource();
106                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
107                 resourceList.add(resource);
108                 request.setRemoveResource(resourceList);
109                 
110                 return request;
111         }
112         
113         private ResourceDefinition getFreeVnfResource() {
114                 // TODO Auto-generated method stub
115                 return null;
116         }
117
118         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMTerminateVnfResponse cbamResponse) {
119                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
120                 if(CommonEnum.OperationStatus.STARTED == cbamResponse.getStatus())
121                 {
122                         request.setStatus(CommonEnum.status.start);
123                 }
124                 else
125                 {
126                         request.setStatus(CommonEnum.status.result);
127                         
128                         //TODO the following are for the result
129 //                      request.setAffectedVnfc(affectedVnfc);
130 //                      request.setAffectedVI(affectedVI);
131 //                      request.setAffectedVirtualStorage(affectedVirtualStorage);
132                 }
133                 
134                 request.setVnfInstanceId(vnfInstanceId);
135                 request.setOperation(CommonConstants.NSLCM_OPERATION_TERMINATE);
136                 request.setJobId(jobId);
137                 return request;
138         }
139
140         private void handleNslcmGrantResponse(NslcmGrantVnfResponse grantResponse) {
141                 // TODO Auto-generated method stub
142                 
143         }
144
145 }