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