5ff7f19a62d539ac80d8530e8c180a314dea0e6b
[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 import java.util.concurrent.Executors;
23
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.bo.CBAMModifyVnfRequest;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.OperationExecution;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.VnfcResourceInfo;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorImpl;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AffectedVnfc;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import com.google.gson.Gson;
51
52
53 public class InstantiateVnfContinueRunnable implements Runnable {
54         private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
55         private CbamMgmrInf cbamMgmr;
56         private CatalogMgmrInf catalogMgmr;
57         private NslcmMgmrInf nslcmMgmr;
58         
59         private InstantiateVnfRequest driverRequest;
60         private String vnfInstanceId;
61         private String jobId;
62         private String vnfmId;
63         
64         private VnfmJobExecutionRepository jobDbMgmr;
65         
66         private Driver2CbamRequestConverter requestConverter;
67         
68         private Gson gson = new Gson();
69         
70         public InstantiateVnfContinueRunnable(String vnfmId, InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
71                         NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
72         {
73                 this.driverRequest = driverRequest;
74                 this.vnfInstanceId = vnfInstanceId;
75                 this.jobId = jobId;
76                 this.nslcmMgmr = nslcmMgmr; 
77                 this.catalogMgmr = catalogMgmr;
78                 this.cbamMgmr = cbamMgmr;
79                 this.requestConverter = requestConverter;
80                 this.jobDbMgmr = dbManager;
81                 this.vnfmId = vnfmId;
82         }
83         
84         public void run() {
85                 //step 1 handle vnf package
86                 handleVnfPackage();
87                 
88                 handleGrant();
89                 
90                 handleModify();
91                 try {
92                         //step 5: instantiate vnf
93                         CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
94                         
95                         handleNotify(cbamInstantiateResponse.getId());
96                 } catch (Exception e) {
97                         logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
98                 }
99         }
100
101         private void handleNotify(String execId) {
102                 boolean instantiateFinished = false;
103                 
104                 do {
105                         try {
106                                 logger.info(" InstantiateVnfContinueRunnable --> handleNotify execId is " + execId);
107                                 CBAMQueryOperExecutionResponse exeResponse = cbamMgmr.queryOperExecution(execId);
108                                 if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED || exeResponse.getStatus() == CommonEnum.OperationStatus.FAILED)
109                                 {
110                                         instantiateFinished = true;
111                                         handleCbamInstantiateResponse(exeResponse, jobId);
112                                         OperateTaskProgress.stopInstantiateTimerTask();
113                                         if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED)
114                                         {
115                                                 
116                                                 logger.info("Start to get vnfc resource");
117                                                 List<VnfcResourceInfo> vnfcResources = cbamMgmr.queryVnfcResource(execId);
118                                                 logger.info("vnfc resource for execId " + execId + " is: " + gson.toJson(vnfcResources));
119                                                 logger.info("End to get vnfc resource");
120                                                 
121                                                 if(vnfcResources != null && !vnfcResources.isEmpty())
122                                                 {
123                                                         logger.info("Start to notify LCM the instantiation result");
124                                                         NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
125                                                         
126                                                         OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
127                                                         
128                                                         nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
129                                                         logger.info("End to notify LCM the instantiation result");
130                                                 }
131                                         }
132                                 }
133                                 else {
134                                         Thread.sleep(60000);
135                                 }
136                                 
137                         } catch (Exception e) {
138                                 logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
139                         }
140                 } while(!instantiateFinished);
141                 
142         }
143
144         private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
145                 CBAMInstantiateVnfRequest  instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
146                 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
147                 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
148                 return cbamInstantiateResponse;
149         }
150
151         private void handleModify() {
152                 try {
153                         CBAMModifyVnfRequest  modifyReq = generateModifyVnfRequest();
154                         cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
155                 } catch (Exception e) {
156                         logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
157                 }
158         }
159
160         private void handleGrant(){
161                 try {
162                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
163                         nslcmMgmr.grantVnf(grantRequest);
164                 } catch (Exception e) {
165                         logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
166                 }
167         }
168
169         private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
170                 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
171                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
172                 CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
173                 
174                 return req;
175         }
176
177         private void handleVnfPackage() {
178                 Executors.newSingleThreadExecutor().execute(new Runnable() {
179                         @Override
180                         public void run() {
181                                 try {
182                                         //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
183                                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
184                                         String packageUrl = vnfPackageInfo.getDownloadUri();
185                                         String saveDir = "/service/vnfPackage";
186                                         String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
187                                         Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
188                                         process.waitFor();
189                                         
190                                         if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
191                                                 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
192 //                                              File csarFile = new File(saveDir + "/" + packageFileName);
193 //                                              //extract package
194 //                                              ZipUtil.explode(csarFile);
195 //                                              csarFile.delete();
196                                         }
197                                 } catch (Exception e) {
198                                         logger.error("Error to handleVnfPackage from SDC", e);
199                                 }
200                                 
201                         }
202                         
203                 });
204         }
205         
206         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(List<VnfcResourceInfo> vnfcResources) {
207                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
208             request.setStatus(CommonEnum.status.result);
209                 request.setVnfInstanceId(vnfInstanceId);
210                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
211                 request.setJobId(jobId);
212                 
213                 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
214                 request.setAffectedVnfc(affectedVnfcs);
215                 return request;
216         }
217
218         private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
219                 List<AffectedVnfc> vnfcs = new ArrayList<>();
220                 for(VnfcResourceInfo resource : vnfcResources)
221                 {
222                         if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
223                         {
224                                 AffectedVnfc vnfc = new AffectedVnfc();
225                                 vnfc.setVnfcInstanceId(resource.getId());
226                                 vnfc.setVduId(resource.getVduId());
227                                 vnfc.setVimid(resource.getComputeResource().getVimId());
228                                 vnfc.setVmid(resource.getComputeResource().getResourceId());
229                                 
230                                 vnfcs.add(vnfc);
231                         }
232                 }
233                 return vnfcs;
234         }
235
236         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
237                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
238                 
239                 request.setVnfInstanceId(vnfInstanceId);
240                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
241                 request.setJobId(jobId);
242                 
243                 ResourceDefinition resource = getFreeVnfResource();
244                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
245                 resourceList.add(resource);
246                 request.setAddResource(resourceList);
247                 
248                 return request;
249         }
250
251         private ResourceDefinition getFreeVnfResource() {
252                 ResourceDefinition def = new ResourceDefinition();
253                 def.setVnfInstanceId(vnfInstanceId);
254                 def.setVimId("001");
255                 List<AddResource> resources = new ArrayList<>();
256                 AddResource res = new AddResource();
257                 res.setVdu("1");
258                 res.setType("vdu");
259                 res.setResourceDefinitionId(2);
260                 resources.add(res);
261                 def.setAddResource(resources);
262                 return def;
263         }
264
265         private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
266                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
267                 
268                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
269                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
270                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
271                 } else if(CommonEnum.OperationStatus.OTHER == cbamInstantiateResponse.getStatus()){
272                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
273                 } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
274                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
275                 }
276                 else{
277                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
278                 }
279                         
280                 jobDbMgmr.save(jobInfo);
281         }
282
283 }