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                         logger.info("Start to notify LCM the instantiation result");
 
 100                         NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamInstantiateResponse);
 
 101                         nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
 
 102                         logger.info("End to notify LCM the instantiation result");
 
 103                 } catch (Exception e) {
 
 104                         logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
 
 108         private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
 
 109                 CBAMInstantiateVnfRequest  instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
 
 110                 CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
 
 111                 handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
 
 112                 return cbamInstantiateResponse;
 
 115         private void handleModify() {
 
 117                         CBAMModifyVnfRequest  modifyReq = generateModifyVnfRequest();
 
 118                         cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
 
 119                 } catch (Exception e) {
 
 120                         logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
 
 124         private void handleGrant(){
 
 126                         NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
 
 127                         nslcmMgmr.grantVnf(grantRequest);
 
 128                 } catch (Exception e) {
 
 129                         logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
 
 133         private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
 
 134                 String filePath = "/etc/vnfpkginfo/cbam_extension.json";
 
 135                 String fileContent = CommonUtil.getJsonStrFromFile(filePath);
 
 136                 CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
 
 141         private void handleVnfPackage() {
 
 142                 Executors.newSingleThreadExecutor().execute(new Runnable() {
 
 146                                         //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
 
 147                                         VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
 
 148                                         String packageUrl = vnfPackageInfo.getDownloadUri();
 
 149                                         String saveDir = "/service/vnfPackage";
 
 150                                         String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
 
 151                                         Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
 
 154                                         if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
 
 155                                                 logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
 
 156 //                                              File csarFile = new File(saveDir + "/" + packageFileName);
 
 158 //                                              ZipUtil.explode(csarFile);
 
 159 //                                              csarFile.delete();
 
 161                                 } catch (Exception e) {
 
 162                                         logger.error("Error to handleVnfPackage from SDC", e);
 
 170         private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
 
 171                 NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
 
 172                 if(CommonEnum.OperationStatus.STARTED == cbamInstantiateResponse.getStatus())
 
 174                         request.setStatus(CommonEnum.status.start);
 
 178                         request.setStatus(CommonEnum.status.result);
 
 180                         //TODO the following are for the result
 
 181 //                      request.setAffectedVnfc(affectedVnfc);
 
 182 //                      request.setAffectedVI(affectedVI);
 
 183 //                      request.setAffectedVirtualStorage(affectedVirtualStorage);
 
 186                 request.setVnfInstanceId(vnfInstanceId);
 
 187                 request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
 
 188                 request.setJobId(jobId);
 
 192         private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
 
 193                 NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
 
 195                 request.setVnfInstanceId(vnfInstanceId);
 
 196                 request.setLifecycleOperation(LifecycleOperation.Instantiate);
 
 197                 request.setJobId(jobId);
 
 199                 ResourceDefinition resource = getFreeVnfResource();
 
 200                 List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
 
 201                 resourceList.add(resource);
 
 202                 request.setAddResource(resourceList);
 
 207         private ResourceDefinition getFreeVnfResource() {
 
 208                 ResourceDefinition def = new ResourceDefinition();
 
 209                 def.setVnfInstanceId(vnfInstanceId);
 
 211                 List<AddResource> resources = new ArrayList<>();
 
 212                 AddResource res = new AddResource();
 
 215                 res.setResourceDefinitionId(2);
 
 217                 def.setAddResource(resources);
 
 221         private void handleCbamInstantiateResponse(CBAMInstantiateVnfResponse cbamInstantiateResponse, String jobId) {
 
 222                 VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
 
 224                 jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
 
 225                 if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
 
 226                         jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
 
 228                 jobDbMgmr.save(jobInfo);