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 Exception {
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 CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
194 private void handleVnfPackage() {
195 Executors.newSingleThreadExecutor().execute(new Runnable() {
199 //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
200 VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
201 String packageUrl = vnfPackageInfo.getDownloadUri();
202 String saveDir = "/service/vnfPackage";
203 String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
204 Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
207 if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
208 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
209 // File csarFile = new File(saveDir + "/" + packageFileName);
211 // ZipUtil.explode(csarFile);
212 // csarFile.delete();
214 } catch (Exception e) {
215 logger.error("Error to handleVnfPackage from SDC", e);
223 private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(List<VnfcResourceInfo> vnfcResources) {
224 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
225 request.setStatus(CommonEnum.status.result);
226 request.setVnfInstanceId(vnfInstanceId);
227 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
228 request.setJobId(jobId);
230 List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
231 request.setAffectedVnfc(affectedVnfcs);
235 private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
236 List<AffectedVnfc> vnfcs = new ArrayList<>();
237 for(VnfcResourceInfo resource : vnfcResources)
239 if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
241 AffectedVnfc vnfc = new AffectedVnfc();
242 vnfc.setVnfcInstanceId(resource.getId());
243 vnfc.setVduId(resource.getVduId());
244 vnfc.setVimid(resource.getComputeResource().getVimId());
245 vnfc.setVmid(resource.getComputeResource().getResourceId());
249 vnfcDbMgmr.insert(vnfc);
255 private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
256 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
258 request.setVnfInstanceId(vnfInstanceId);
259 request.setLifecycleOperation(LifecycleOperation.Instantiate);
260 request.setJobId(jobId);
262 ResourceDefinition resource = getFreeVnfResource();
263 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
264 resourceList.add(resource);
265 request.setAddResource(resourceList);
270 private ResourceDefinition getFreeVnfResource() {
271 ResourceDefinition def = new ResourceDefinition();
272 def.setVnfInstanceId(vnfInstanceId);
274 List<AddResource> resources = new ArrayList<>();
275 AddResource res = new AddResource();
278 res.setResourceDefinitionId(2);
280 def.setAddResource(resources);
284 private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
285 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
287 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
288 long nowTime = System.currentTimeMillis();
289 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
290 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
291 // jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
292 } else if(CommonEnum.OperationStatus.OTHER == cbamInstantiateResponse.getStatus()){
293 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
294 } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
295 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
296 jobInfo.setOperateEndTime(nowTime);
300 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
303 jobDbMgmr.update(jobInfo);
306 public void setDriverRequest(InstantiateVnfRequest driverRequest) {
307 this.driverRequest = driverRequest;
310 public void setVnfInstanceId(String vnfInstanceId) {
311 this.vnfInstanceId = vnfInstanceId;
314 public void setJobId(String jobId) {
318 public void setVnfmId(String vnfmId) {
319 this.vnfmId = vnfmId;
322 public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
323 this.requestConverter = requestConverter;