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.inf.CbamMgmrInf;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
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.NslcmNotifyLCMEventsRequest;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
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;
46 import com.google.gson.Gson;
49 public class InstantiateVnfContinueRunnable implements Runnable {
50 private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
51 private CbamMgmrInf cbamMgmr;
52 private CatalogMgmrInf catalogMgmr;
53 private NslcmMgmrInf nslcmMgmr;
55 private InstantiateVnfRequest driverRequest;
56 private String vnfInstanceId;
58 private String vnfmId;
60 private VnfmJobExecutionRepository jobDbMgmr;
62 private Driver2CbamRequestConverter requestConverter;
64 private Gson gson = new Gson();
66 public InstantiateVnfContinueRunnable(String vnfmId, InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
67 NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
69 this.driverRequest = driverRequest;
70 this.vnfInstanceId = vnfInstanceId;
72 this.nslcmMgmr = nslcmMgmr;
73 this.catalogMgmr = catalogMgmr;
74 this.cbamMgmr = cbamMgmr;
75 this.requestConverter = requestConverter;
76 this.jobDbMgmr = dbManager;
81 //step 1 handle vnf package
88 //step 5: instantiate vnf
89 CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
91 handleNotify(cbamInstantiateResponse);
92 } catch (Exception e) {
93 logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
97 private void handleNotify(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
99 NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamInstantiateResponse);
100 nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
101 } catch (Exception e) {
102 logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
106 private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
107 CBAMInstantiateVnfRequest instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
108 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
109 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
110 return cbamInstantiateResponse;
113 private void handleModify() {
115 CBAMModifyVnfRequest modifyReq = generateModifyVnfRequest();
116 cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
117 } catch (Exception e) {
118 logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
122 private void handleGrant(){
124 NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
125 nslcmMgmr.grantVnf(grantRequest);
126 } catch (Exception e) {
127 logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
131 private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
132 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
133 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
134 CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
139 private void handleVnfPackage() {
140 Executors.newSingleThreadExecutor().execute(new Runnable() {
144 //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
145 VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
146 String packageUrl = vnfPackageInfo.getDownloadUri();
147 String saveDir = "/service/vnfPackage";
148 String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
149 Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
152 if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
153 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
154 // File csarFile = new File(saveDir + "/" + packageFileName);
156 // ZipUtil.explode(csarFile);
157 // csarFile.delete();
159 } catch (Exception e) {
160 logger.error("Error to handleVnfPackage from SDC", e);
168 private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
169 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
170 if(CommonEnum.OperationStatus.STARTED == cbamInstantiateResponse.getStatus())
172 request.setStatus(CommonEnum.status.start);
176 request.setStatus(CommonEnum.status.result);
178 //TODO the following are for the result
179 // request.setAffectedVnfc(affectedVnfc);
180 // request.setAffectedVI(affectedVI);
181 // request.setAffectedVirtualStorage(affectedVirtualStorage);
184 request.setVnfInstanceId(vnfInstanceId);
185 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
186 request.setJobId(jobId);
190 private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
191 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
193 request.setVnfInstanceId(vnfInstanceId);
194 request.setLifecycleOperation(LifecycleOperation.Instantiate);
195 request.setJobId(jobId);
197 ResourceDefinition resource = getFreeVnfResource();
198 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
199 resourceList.add(resource);
200 request.setAddResource(resourceList);
205 private ResourceDefinition getFreeVnfResource() {
206 ResourceDefinition def = new ResourceDefinition();
207 def.setVnfInstanceId(vnfInstanceId);
209 List<AddResource> resources = new ArrayList<>();
210 AddResource res = new AddResource();
213 res.setResourceDefinitionId(2);
215 def.setAddResource(resources);
219 private void handleCbamInstantiateResponse(CBAMInstantiateVnfResponse cbamInstantiateResponse, String jobId) {
220 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
222 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
223 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
224 jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
226 jobDbMgmr.save(jobInfo);