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;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.concurrent.Executors;
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.mapper.VnfcResourceInfoMapper;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.mapper.VnfmJobExecutionMapper;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorImpl;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AffectedVnfc;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
47 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.beans.factory.annotation.Autowired;
52 import com.google.gson.Gson;
55 public class InstantiateVnfContinueRunnable implements Runnable {
56 private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
58 private CbamMgmrInf cbamMgmr;
60 private CatalogMgmrInf catalogMgmr;
62 private NslcmMgmrInf nslcmMgmr;
64 private InstantiateVnfRequest driverRequest;
65 private String vnfInstanceId;
67 private String vnfmId;
69 // private VnfmJobExecutionRepository jobDbMgmr;
71 private VnfmJobExecutionMapper jobDbMgmr;
73 private VnfcResourceInfoMapper vnfcDbMgmr;
75 private Driver2CbamRequestConverter requestConverter;
77 private Gson gson = new Gson();
79 public InstantiateVnfContinueRunnable(String vnfmId, InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
80 NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionMapper dbManager, VnfcResourceInfoMapper vnfcDbMgmr)
82 this.driverRequest = driverRequest;
83 this.vnfInstanceId = vnfInstanceId;
85 this.nslcmMgmr = nslcmMgmr;
86 this.catalogMgmr = catalogMgmr;
87 this.cbamMgmr = cbamMgmr;
88 this.requestConverter = requestConverter;
89 this.jobDbMgmr = dbManager;
91 this.vnfcDbMgmr = vnfcDbMgmr;
95 //step 1 handle vnf package
102 //step 5: instantiate vnf
103 CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
105 handleNotify(cbamInstantiateResponse.getId());
106 } catch (Exception e) {
107 logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
111 private void handleNotify(String execId) {
112 boolean instantiateFinished = false;
116 logger.info(" InstantiateVnfContinueRunnable --> handleNotify execId is " + execId);
117 CBAMQueryOperExecutionResponse exeResponse = cbamMgmr.queryOperExecution(execId);
118 if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED || exeResponse.getStatus() == CommonEnum.OperationStatus.FAILED)
120 instantiateFinished = true;
121 handleCbamInstantiateResponse(exeResponse, jobId);
122 if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED)
125 logger.info("Start to get vnfc resource");
126 List<VnfcResourceInfo> vnfcResources = new ArrayList<>();
129 vnfcResources = cbamMgmr.queryVnfcResource(vnfInstanceId);
130 } catch (Exception e) {
131 logger.error("Error to queryVnfcResource.", e);
134 logger.info("vnfc resource for vnfInstanceId " + vnfInstanceId + " is: " + gson.toJson(vnfcResources));
135 logger.info("End to get vnfc resource");
137 if(vnfcResources == null)
139 vnfcResources = new ArrayList<>();
141 logger.info("Start to notify LCM the instantiation result");
142 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
144 // OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
146 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
147 logger.info("End to notify LCM the instantiation result");
154 } catch (Exception e) {
155 logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
157 } while(!instantiateFinished);
161 private CBAMInstantiateVnfResponse handleInstantiate() throws IOException {
162 CBAMInstantiateVnfRequest instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
163 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
164 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
165 return cbamInstantiateResponse;
168 private void handleModify() {
170 CBAMModifyVnfRequest modifyReq = generateModifyVnfRequest();
171 cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
172 } catch (Exception e) {
173 logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
177 private void handleGrant(){
179 NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
180 nslcmMgmr.grantVnf(grantRequest);
181 } catch (Exception e) {
182 logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
186 private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
187 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
188 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
189 return gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
192 private void handleVnfPackage() {
193 Executors.newSingleThreadExecutor().execute(new Runnable() {
197 //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
198 VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
199 String packageUrl = vnfPackageInfo.getDownloadUri();
200 String saveDir = "/service/vnfPackage";
201 String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
202 Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
205 if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
206 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
207 // File csarFile = new File(saveDir + "/" + packageFileName);
209 // ZipUtil.explode(csarFile);
210 // csarFile.delete();
212 } catch (Exception e) {
213 logger.error("Error to handleVnfPackage from SDC", e);
221 private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(List<VnfcResourceInfo> vnfcResources) {
222 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
223 request.setStatus(CommonEnum.status.result);
224 request.setVnfInstanceId(vnfInstanceId);
225 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
226 request.setJobId(jobId);
228 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
229 request.setAffectedVnfc(affectedVnfcs);
233 private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
234 List<AffectedVnfc> vnfcs = new ArrayList<>();
235 for(VnfcResourceInfo resource : vnfcResources)
237 if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
239 AffectedVnfc vnfc = new AffectedVnfc();
240 vnfc.setVnfcInstanceId(resource.getId());
241 vnfc.setVduId(resource.getVduId());
242 vnfc.setVimid(resource.getComputeResource().getVimId());
243 vnfc.setVmid(resource.getComputeResource().getResourceId());
247 vnfcDbMgmr.insert(vnfc);
253 private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
254 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
256 request.setVnfInstanceId(vnfInstanceId);
257 request.setLifecycleOperation(LifecycleOperation.Instantiate);
258 request.setJobId(jobId);
260 ResourceDefinition resource = getFreeVnfResource();
261 List<ResourceDefinition> resourceList = new ArrayList<>();
262 resourceList.add(resource);
263 request.setAddResource(resourceList);
268 private ResourceDefinition getFreeVnfResource() {
269 ResourceDefinition def = new ResourceDefinition();
270 def.setVnfInstanceId(vnfInstanceId);
272 List<AddResource> resources = new ArrayList<>();
273 AddResource res = new AddResource();
276 res.setResourceDefinitionId(2);
278 def.setAddResource(resources);
282 private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
283 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
285 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
286 long nowTime = System.currentTimeMillis();
287 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
288 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
289 // jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
290 } else if(CommonEnum.OperationStatus.OTHER == cbamInstantiateResponse.getStatus()){
291 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
292 } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
293 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
294 jobInfo.setOperateEndTime(nowTime);
298 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
301 jobDbMgmr.update(jobInfo);
304 public void setDriverRequest(InstantiateVnfRequest driverRequest) {
305 this.driverRequest = driverRequest;
308 public void setVnfInstanceId(String vnfInstanceId) {
309 this.vnfInstanceId = vnfInstanceId;
312 public void setJobId(String jobId) {
316 public void setVnfmId(String vnfmId) {
317 this.vnfmId = vnfmId;
320 public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
321 this.requestConverter = requestConverter;