e527b1050a565fa83e2b1f4149b008fb93c61a36
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / adaptor / InstantiateVnfContinueRunnable.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.catalog.bo.entity.VnfPackageInfo;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.inf.CatalogMgmrInf;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
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.InstantiateVnfRequest;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44 public class InstantiateVnfContinueRunnable implements Runnable {
45         private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
46         private CbamMgmrInf cbamMgmr;
47         private CatalogMgmrInf catalogMgmr;
48         private NslcmMgmrInf nslcmMgmr;
49         
50         private InstantiateVnfRequest driverRequest;
51         private String vnfInstanceId;
52         private String jobId;
53         
54         private VnfmJobExecutionRepository jobDbMgmr;
55         
56         private Driver2CbamRequestConverter requestConverter;
57         
58         public InstantiateVnfContinueRunnable(InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
59                         NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
60         {
61                 this.driverRequest = driverRequest;
62                 this.vnfInstanceId = vnfInstanceId;
63                 this.jobId = jobId;
64                 this.nslcmMgmr = nslcmMgmr; 
65                 this.catalogMgmr = catalogMgmr;
66                 this.cbamMgmr = cbamMgmr;
67                 this.requestConverter = requestConverter;
68                 this.jobDbMgmr = dbManager;
69         }
70         
71         public void run() {
72                 try {
73                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
74                         NslcmGrantVnfResponse grantResponse = nslcmMgmr.grantVnf(grantRequest);
75                         handleNslcmGrantResponse(grantResponse);
76                         
77                         //step 2: query vnfPackage uri
78                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
79                         
80                         //step 5: instantiate vnf
81                         CBAMInstantiateVnfRequest  instantiateReq = requestConverter.InstantiateReqConvert(driverRequest, grantResponse, null, null);
82                         CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
83                         handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
84                         
85                         NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamInstantiateResponse);
86                         nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfInstanceId);
87                         
88                 } catch (ClientProtocolException e) {
89                         logger.error("InstantiateVnfContinueRunnable run error ClientProtocolException", e);
90                 } catch (IOException e) {
91                         logger.error("InstantiateVnfContinueRunnable run error IOException", e);
92                 }
93                 
94         }
95         
96         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
97                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
98                 if(CommonEnum.OperationStatus.STARTED == cbamInstantiateResponse.getStatus())
99                 {
100                         request.setStatus(CommonEnum.status.start);
101                 }
102                 else
103                 {
104                         request.setStatus(CommonEnum.status.result);
105                         
106                         //TODO the following are for the result
107 //                      request.setAffectedVnfc(affectedVnfc);
108 //                      request.setAffectedVI(affectedVI);
109 //                      request.setAffectedVirtualStorage(affectedVirtualStorage);
110                 }
111                 
112                 request.setVnfInstanceId(vnfInstanceId);
113                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
114                 request.setJobId(jobId);
115                 return request;
116         }
117
118         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
119                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
120                 
121                 request.setVnfInstanceId(vnfInstanceId);
122                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
123                 request.setJobId(jobId);
124                 
125                 ResourceDefinition resource = getFreeVnfResource();
126                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
127                 resourceList.add(resource);
128                 request.setAddResource(resourceList);
129                 
130                 return request;
131         }
132
133         private ResourceDefinition getFreeVnfResource() {
134                 // TODO Auto-generated method stub
135                 return null;
136         }
137
138         private void handleCbamInstantiateResponse(CBAMInstantiateVnfResponse cbamInstantiateResponse, String jobId) {
139                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.getLong(jobId));
140                 
141                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
142                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
143                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
144                 }
145                 jobDbMgmr.save(jobInfo);
146         }
147
148         private void handleNslcmGrantResponse(NslcmGrantVnfResponse grantResponse) {
149                 // TODO Auto-generated method stub
150                 
151         }
152
153 }