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 = cbamMgmr.queryVnfcResource(execId);
118 logger.info("vnfc resource for execId " + execId + " is: " + gson.toJson(vnfcResources));
119 logger.info("End to get vnfc resource");
121 if(vnfcResources != null && !vnfcResources.isEmpty())
123 logger.info("Start to notify LCM the instantiation result");
124 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
126 OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
128 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
129 logger.info("End to notify LCM the instantiation result");
137 } catch (Exception e) {
138 logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
140 } while(!instantiateFinished);
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;
151 private void handleModify() {
153 CBAMModifyVnfRequest modifyReq = generateModifyVnfRequest();
154 cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
155 } catch (Exception e) {
156 logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
160 private void handleGrant(){
162 NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
163 nslcmMgmr.grantVnf(grantRequest);
164 } catch (Exception e) {
165 logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
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);
177 private void handleVnfPackage() {
178 Executors.newSingleThreadExecutor().execute(new Runnable() {
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);
190 if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
191 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
192 // File csarFile = new File(saveDir + "/" + packageFileName);
194 // ZipUtil.explode(csarFile);
195 // csarFile.delete();
197 } catch (Exception e) {
198 logger.error("Error to handleVnfPackage from SDC", e);
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);
213 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
214 request.setAffectedVnfc(affectedVnfcs);
218 private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
219 List<AffectedVnfc> vnfcs = new ArrayList<>();
220 for(VnfcResourceInfo resource : vnfcResources)
222 if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
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());
236 private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
237 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
239 request.setVnfInstanceId(vnfInstanceId);
240 request.setLifecycleOperation(LifecycleOperation.Instantiate);
241 request.setJobId(jobId);
243 ResourceDefinition resource = getFreeVnfResource();
244 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
245 resourceList.add(resource);
246 request.setAddResource(resourceList);
251 private ResourceDefinition getFreeVnfResource() {
252 ResourceDefinition def = new ResourceDefinition();
253 def.setVnfInstanceId(vnfInstanceId);
255 List<AddResource> resources = new ArrayList<>();
256 AddResource res = new AddResource();
259 res.setResourceDefinitionId(2);
261 def.setAddResource(resources);
265 private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
266 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
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);
277 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
280 jobDbMgmr.save(jobInfo);