Modify POM parent oparent version as 0.1.1
[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.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfResponse;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
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                         handleCbamTerminateResponse(cbamResponse, jobId);
77                         
78                         cbamMgmr.deleteVnf(vnfInstanceId);
79                         
80                         NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamResponse);
81                         
82                         nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfInstanceId);
83                         
84                 } catch (ClientProtocolException e) {
85                         logger.error("TerminateVnfContinueRunnable run error ClientProtocolException", e);
86                 } catch (IOException e) {
87                         logger.error("TerminateVnfContinueRunnable run error IOException", e);
88                 }
89                 
90         }
91         
92         private void handleCbamTerminateResponse(CBAMTerminateVnfResponse cbamResponse, String jobId) {
93                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.getLong(jobId));
94                 
95                 jobInfo.setVnfmExecutionId(cbamResponse.getId());
96                 if(CommonEnum.OperationStatus.FAILED ==cbamResponse.getStatus()) {
97                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
98                 }
99                 jobDbMgmr.save(jobInfo);
100         }
101         
102         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
103                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
104                 
105                 request.setVnfInstanceId(vnfInstanceId);
106                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
107                 request.setJobId(jobId);
108                 
109                 ResourceDefinition resource = getFreeVnfResource();
110                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
111                 resourceList.add(resource);
112                 request.setRemoveResource(resourceList);
113                 
114                 return request;
115         }
116         
117         private ResourceDefinition getFreeVnfResource() {
118                 // TODO Auto-generated method stub
119                 return null;
120         }
121
122         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMTerminateVnfResponse cbamResponse) {
123                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
124                 if(CommonEnum.OperationStatus.STARTED == cbamResponse.getStatus())
125                 {
126                         request.setStatus(CommonEnum.status.start);
127                 }
128                 else
129                 {
130                         request.setStatus(CommonEnum.status.result);
131                         
132                         //TODO the following are for the result
133 //                      request.setAffectedVnfc(affectedVnfc);
134 //                      request.setAffectedVI(affectedVI);
135 //                      request.setAffectedVirtualStorage(affectedVirtualStorage);
136                 }
137                 
138                 request.setVnfInstanceId(vnfInstanceId);
139                 request.setOperation(CommonConstants.NSLCM_OPERATION_TERMINATE);
140                 request.setJobId(jobId);
141                 return request;
142         }
143
144         private void handleNslcmGrantResponse(NslcmGrantVnfResponse grantResponse) {
145                 // TODO Auto-generated method stub
146                 
147         }
148
149 }