2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.adaptor;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.Executors;
25 import org.apache.http.client.ClientProtocolException;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.bo.entity.VnfPackageInfo;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.inf.CatalogMgmrInf;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorImpl;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfResponse;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.zeroturnaround.zip.ZipUtil;
48 public class InstantiateVnfContinueRunnable implements Runnable {
49 private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
50 private CbamMgmrInf cbamMgmr;
51 private CatalogMgmrInf catalogMgmr;
52 private NslcmMgmrInf nslcmMgmr;
54 private InstantiateVnfRequest driverRequest;
55 private String vnfInstanceId;
58 private VnfmJobExecutionRepository jobDbMgmr;
60 private Driver2CbamRequestConverter requestConverter;
62 public InstantiateVnfContinueRunnable(InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
63 NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
65 this.driverRequest = driverRequest;
66 this.vnfInstanceId = vnfInstanceId;
68 this.nslcmMgmr = nslcmMgmr;
69 this.catalogMgmr = catalogMgmr;
70 this.cbamMgmr = cbamMgmr;
71 this.requestConverter = requestConverter;
72 this.jobDbMgmr = dbManager;
77 //step 1 handle vnf package
80 NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
81 NslcmGrantVnfResponse grantResponse = nslcmMgmr.grantVnf(grantRequest);
82 handleNslcmGrantResponse(grantResponse);
85 //step 5: instantiate vnf
86 CBAMInstantiateVnfRequest instantiateReq = requestConverter.InstantiateReqConvert(driverRequest, grantResponse, null, null);
87 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
88 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
90 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamInstantiateResponse);
91 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfInstanceId);
93 } catch (ClientProtocolException e) {
94 logger.error("InstantiateVnfContinueRunnable run error ClientProtocolException", e);
95 } catch (IOException e) {
96 logger.error("InstantiateVnfContinueRunnable run error IOException", e);
101 private void handleVnfPackage() {
102 Executors.newSingleThreadExecutor().execute(new Runnable() {
106 //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
107 VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
108 String packageUrl = vnfPackageInfo.getDownloadUri();
109 String saveDir = "/service/vnfPackage";
110 String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
111 Process process = Runtime.getRuntime().exec("mkdir " + saveDir);
114 if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
116 ZipUtil.unpack(new File(saveDir + "/" + packageFileName), new File(saveDir));
118 String cbamPackageDirName = saveDir + "/"
119 + packageFileName.substring(0, packageFileName.length() - 4) + "/Artifacts";
120 String cbamPackageName = new File(cbamPackageDirName).list()[0];
121 cbamMgmr.uploadVnfPackage(cbamPackageName);
123 } catch (Exception e) {
124 logger.error("Error to handleVnfPackage", e);
131 private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
132 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
133 if(CommonEnum.OperationStatus.STARTED == cbamInstantiateResponse.getStatus())
135 request.setStatus(CommonEnum.status.start);
139 request.setStatus(CommonEnum.status.result);
141 //TODO the following are for the result
142 // request.setAffectedVnfc(affectedVnfc);
143 // request.setAffectedVI(affectedVI);
144 // request.setAffectedVirtualStorage(affectedVirtualStorage);
147 request.setVnfInstanceId(vnfInstanceId);
148 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
149 request.setJobId(jobId);
153 private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
154 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
156 request.setVnfInstanceId(vnfInstanceId);
157 request.setLifecycleOperation(LifecycleOperation.Instantiate);
158 request.setJobId(jobId);
160 ResourceDefinition resource = getFreeVnfResource();
161 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
162 resourceList.add(resource);
163 request.setAddResource(resourceList);
168 private ResourceDefinition getFreeVnfResource() {
169 // TODO Auto-generated method stub
173 private void handleCbamInstantiateResponse(CBAMInstantiateVnfResponse cbamInstantiateResponse, String jobId) {
174 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.getLong(jobId));
176 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
177 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
178 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
180 jobDbMgmr.save(jobInfo);
183 private void handleNslcmGrantResponse(NslcmGrantVnfResponse grantResponse) {
184 // TODO Auto-generated method stub