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.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;
50 import com.google.gson.Gson;
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;
59 private InstantiateVnfRequest driverRequest;
60 private String vnfInstanceId;
62 private String vnfmId;
64 private VnfmJobExecutionRepository jobDbMgmr;
66 private Driver2CbamRequestConverter requestConverter;
68 private Gson gson = new Gson();
70 public InstantiateVnfContinueRunnable(String vnfmId, InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
71 NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
73 this.driverRequest = driverRequest;
74 this.vnfInstanceId = vnfInstanceId;
76 this.nslcmMgmr = nslcmMgmr;
77 this.catalogMgmr = catalogMgmr;
78 this.cbamMgmr = cbamMgmr;
79 this.requestConverter = requestConverter;
80 this.jobDbMgmr = dbManager;
85 //step 1 handle vnf package
92 //step 5: instantiate vnf
93 CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
95 handleNotify(cbamInstantiateResponse.getId());
96 } catch (Exception e) {
97 logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
101 private void handleNotify(String execId) {
102 boolean instantiateFinished = false;
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)
110 instantiateFinished = true;
111 handleCbamInstantiateResponse(exeResponse, jobId);
112 OperateTaskProgress.stopInstantiateTimerTask();
113 if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED)
116 logger.info("Start to get vnfc resource");
117 List<VnfcResourceInfo> vnfcResources = new ArrayList<>();
120 vnfcResources = cbamMgmr.queryVnfcResource(vnfInstanceId);
121 } catch (Exception e) {
122 logger.error("Error to queryVnfcResource.", e);
125 logger.info("vnfc resource for vnfInstanceId " + vnfInstanceId + " is: " + gson.toJson(vnfcResources));
126 logger.info("End to get vnfc resource");
128 if(vnfcResources == null)
130 vnfcResources = new ArrayList<>();
132 logger.info("Start to notify LCM the instantiation result");
133 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
135 OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
137 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
138 logger.info("End to notify LCM the instantiation result");
145 } catch (Exception e) {
146 logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
148 } while(!instantiateFinished);
152 private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
153 CBAMInstantiateVnfRequest instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
154 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
155 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
156 return cbamInstantiateResponse;
159 private void handleModify() {
161 CBAMModifyVnfRequest modifyReq = generateModifyVnfRequest();
162 cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
163 } catch (Exception e) {
164 logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
168 private void handleGrant(){
170 NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
171 nslcmMgmr.grantVnf(grantRequest);
172 } catch (Exception e) {
173 logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
177 private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
178 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
179 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
180 CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
185 private void handleVnfPackage() {
186 Executors.newSingleThreadExecutor().execute(new Runnable() {
190 //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
191 VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
192 String packageUrl = vnfPackageInfo.getDownloadUri();
193 String saveDir = "/service/vnfPackage";
194 String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
195 Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
198 if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
199 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
200 // File csarFile = new File(saveDir + "/" + packageFileName);
202 // ZipUtil.explode(csarFile);
203 // csarFile.delete();
205 } catch (Exception e) {
206 logger.error("Error to handleVnfPackage from SDC", e);
214 private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(List<VnfcResourceInfo> vnfcResources) {
215 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
216 request.setStatus(CommonEnum.status.result);
217 request.setVnfInstanceId(vnfInstanceId);
218 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
219 request.setJobId(jobId);
221 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
222 request.setAffectedVnfc(affectedVnfcs);
226 private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
227 List<AffectedVnfc> vnfcs = new ArrayList<>();
228 for(VnfcResourceInfo resource : vnfcResources)
230 if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
232 AffectedVnfc vnfc = new AffectedVnfc();
233 vnfc.setVnfcInstanceId(resource.getId());
234 vnfc.setVduId(resource.getVduId());
235 vnfc.setVimid(resource.getComputeResource().getVimId());
236 vnfc.setVmid(resource.getComputeResource().getResourceId());
244 private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
245 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
247 request.setVnfInstanceId(vnfInstanceId);
248 request.setLifecycleOperation(LifecycleOperation.Instantiate);
249 request.setJobId(jobId);
251 ResourceDefinition resource = getFreeVnfResource();
252 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
253 resourceList.add(resource);
254 request.setAddResource(resourceList);
259 private ResourceDefinition getFreeVnfResource() {
260 ResourceDefinition def = new ResourceDefinition();
261 def.setVnfInstanceId(vnfInstanceId);
263 List<AddResource> resources = new ArrayList<>();
264 AddResource res = new AddResource();
267 res.setResourceDefinitionId(2);
269 def.setAddResource(resources);
273 private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
274 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
276 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
277 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
278 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
279 // jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
280 } else if(CommonEnum.OperationStatus.OTHER == cbamInstantiateResponse.getStatus()){
281 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
282 } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
283 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
286 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
289 jobDbMgmr.save(jobInfo);