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