Update get operation status part
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / adaptor / InstantiateVnfContinueRunnable.java
index 09cfee8..5ff7f19 100644 (file)
@@ -19,27 +19,36 @@ package org.onap.vfc.nfvo.driver.vnfm.svnfm.adaptor;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.Executors;
 
-import org.apache.http.client.ClientProtocolException;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.bo.entity.VnfPackageInfo;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.catalog.inf.CatalogMgmrInf;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMModifyVnfRequest;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.OperationExecution;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.VnfcResourceInfo;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonEnum.LifecycleOperation;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.bean.VnfmJobExecutionInfo;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.repository.VnfmJobExecutionRepository;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorImpl;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfRequest;
-import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmGrantVnfResponse;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.NslcmNotifyLCMEventsRequest;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AddResource;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.AffectedVnfc;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.ResourceDefinition;
-import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.bo.entity.VnfPackageInfo;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nslcm.inf.NslcmMgmrInf;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.gson.Gson;
+
 
 public class InstantiateVnfContinueRunnable implements Runnable {
        private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
@@ -50,12 +59,15 @@ public class InstantiateVnfContinueRunnable implements Runnable {
        private InstantiateVnfRequest driverRequest;
        private String vnfInstanceId;
        private String jobId;
+       private String vnfmId;
        
        private VnfmJobExecutionRepository jobDbMgmr;
        
        private Driver2CbamRequestConverter requestConverter;
        
-       public InstantiateVnfContinueRunnable(InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
+       private Gson gson = new Gson();
+       
+       public InstantiateVnfContinueRunnable(String vnfmId, InstantiateVnfRequest driverRequest, String vnfInstanceId, String jobId,
                        NslcmMgmrInf nslcmMgmr, CatalogMgmrInf catalogMgmr, CbamMgmrInf cbamMgmr, Driver2CbamRequestConverter requestConverter, VnfmJobExecutionRepository dbManager)
        {
                this.driverRequest = driverRequest;
@@ -66,55 +78,161 @@ public class InstantiateVnfContinueRunnable implements Runnable {
                this.cbamMgmr = cbamMgmr;
                this.requestConverter = requestConverter;
                this.jobDbMgmr = dbManager;
+               this.vnfmId = vnfmId;
        }
        
        public void run() {
+               //step 1 handle vnf package
+               handleVnfPackage();
+               
+               handleGrant();
+               
+               handleModify();
                try {
-                       NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
-                       NslcmGrantVnfResponse grantResponse = nslcmMgmr.grantVnf(grantRequest);
-                       handleNslcmGrantResponse(grantResponse);
-                       
-                       //step 2: query vnfPackage uri
-                       VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
-                       
                        //step 5: instantiate vnf
-                       CBAMInstantiateVnfRequest  instantiateReq = requestConverter.InstantiateReqConvert(driverRequest, grantResponse, null, null);
-                       CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
-                       handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
-                       
-                       NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(cbamInstantiateResponse);
-                       nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfInstanceId);
+                       CBAMInstantiateVnfResponse cbamInstantiateResponse = handleInstantiate();
                        
-               } catch (ClientProtocolException e) {
-                       logger.error("InstantiateVnfContinueRunnable run error ClientProtocolException", e);
-               } catch (IOException e) {
-                       logger.error("InstantiateVnfContinueRunnable run error IOException", e);
+                       handleNotify(cbamInstantiateResponse.getId());
+               } catch (Exception e) {
+                       logger.error("InstantiateVnfContinueRunnable --> handleInstantiate or handleNotify error.", e);
                }
+       }
+
+       private void handleNotify(String execId) {
+               boolean instantiateFinished = false;
+               
+               do {
+                       try {
+                               logger.info(" InstantiateVnfContinueRunnable --> handleNotify execId is " + execId);
+                               CBAMQueryOperExecutionResponse exeResponse = cbamMgmr.queryOperExecution(execId);
+                               if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED || exeResponse.getStatus() == CommonEnum.OperationStatus.FAILED)
+                               {
+                                       instantiateFinished = true;
+                                       handleCbamInstantiateResponse(exeResponse, jobId);
+                                       OperateTaskProgress.stopInstantiateTimerTask();
+                                       if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED)
+                                       {
+                                               
+                                               logger.info("Start to get vnfc resource");
+                                               List<VnfcResourceInfo> vnfcResources = cbamMgmr.queryVnfcResource(execId);
+                                               logger.info("vnfc resource for execId " + execId + " is: " + gson.toJson(vnfcResources));
+                                               logger.info("End to get vnfc resource");
+                                               
+                                               if(vnfcResources != null && !vnfcResources.isEmpty())
+                                               {
+                                                       logger.info("Start to notify LCM the instantiation result");
+                                                       NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
+                                                       
+                                                       OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
+                                                       
+                                                       nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
+                                                       logger.info("End to notify LCM the instantiation result");
+                                               }
+                                       }
+                               }
+                               else {
+                                       Thread.sleep(60000);
+                               }
+                               
+                       } catch (Exception e) {
+                               logger.error("InstantiateVnfContinueRunnable --> handleNotify error.", e);
+                       }
+               } while(!instantiateFinished);
                
        }
-       
-       private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(CBAMInstantiateVnfResponse cbamInstantiateResponse) {
-               NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
-               if(CommonEnum.OperationStatus.STARTED == cbamInstantiateResponse.getStatus())
-               {
-                       request.setStatus(CommonEnum.status.start);
+
+       private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
+               CBAMInstantiateVnfRequest  instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
+               CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
+               handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
+               return cbamInstantiateResponse;
+       }
+
+       private void handleModify() {
+               try {
+                       CBAMModifyVnfRequest  modifyReq = generateModifyVnfRequest();
+                       cbamMgmr.modifyVnf(modifyReq, vnfInstanceId);
+               } catch (Exception e) {
+                       logger.error("InstantiateVnfContinueRunnable --> handleModify error.", e);
                }
-               else
-               {
-                       request.setStatus(CommonEnum.status.result);
-                       
-                       //TODO the following are for the result
-//                     request.setAffectedVnfc(affectedVnfc);
-//                     request.setAffectedVI(affectedVI);
-//                     request.setAffectedVirtualStorage(affectedVirtualStorage);
+       }
+
+       private void handleGrant(){
+               try {
+                       NslcmGrantVnfRequest grantRequest = buildNslcmGrantVnfRequest();
+                       nslcmMgmr.grantVnf(grantRequest);
+               } catch (Exception e) {
+                       logger.error("InstantiateVnfContinueRunnable --> handleGrant error.", e);
                }
+       }
+
+       private CBAMModifyVnfRequest generateModifyVnfRequest() throws IOException{
+               String filePath = "/etc/vnfpkginfo/cbam_extension.json";
+               String fileContent = CommonUtil.getJsonStrFromFile(filePath);
+               CBAMModifyVnfRequest req = gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
                
+               return req;
+       }
+
+       private void handleVnfPackage() {
+               Executors.newSingleThreadExecutor().execute(new Runnable() {
+                       @Override
+                       public void run() {
+                               try {
+                                       //step 1: query vnfPackage uri -- download package -- extract it -- upload CBAM package to CBAM
+                                       VnfPackageInfo vnfPackageInfo = catalogMgmr.queryVnfPackage(driverRequest.getVnfPackageId());
+                                       String packageUrl = vnfPackageInfo.getDownloadUri();
+                                       String saveDir = "/service/vnfPackage";
+                                       String packageFileName = packageUrl.substring(packageUrl.lastIndexOf("/"));
+                                       Process process = Runtime.getRuntime().exec("mkdir -p " + saveDir);
+                                       process.waitFor();
+                                       
+                                       if (HttpClientProcessorImpl.downLoadFromUrl(packageUrl, packageFileName, saveDir)) {
+                                               logger.info("handleVnfPackage download file " + packageUrl + " is successful.");
+//                                             File csarFile = new File(saveDir + "/" + packageFileName);
+//                                             //extract package
+//                                             ZipUtil.explode(csarFile);
+//                                             csarFile.delete();
+                                       }
+                               } catch (Exception e) {
+                                       logger.error("Error to handleVnfPackage from SDC", e);
+                               }
+                               
+                       }
+                       
+               });
+       }
+       
+       private NslcmNotifyLCMEventsRequest buildNslcmNotifyLCMEventsRequest(List<VnfcResourceInfo> vnfcResources) {
+               NslcmNotifyLCMEventsRequest request = new NslcmNotifyLCMEventsRequest();
+           request.setStatus(CommonEnum.status.result);
                request.setVnfInstanceId(vnfInstanceId);
                request.setOperation(CommonConstants.NSLCM_OPERATION_INSTANTIATE);
                request.setJobId(jobId);
+               
+               List<AffectedVnfc> affectedVnfcs = convertVnfcResourceToAffectecVnfc(vnfcResources);
+               request.setAffectedVnfc(affectedVnfcs);
                return request;
        }
 
+       private List<AffectedVnfc> convertVnfcResourceToAffectecVnfc(List<VnfcResourceInfo> vnfcResources) {
+               List<AffectedVnfc> vnfcs = new ArrayList<>();
+               for(VnfcResourceInfo resource : vnfcResources)
+               {
+                       if(resource.getComputeResource() != null && "OS::Nova::Server".equalsIgnoreCase(resource.getComputeResource().getResourceType()))
+                       {
+                               AffectedVnfc vnfc = new AffectedVnfc();
+                               vnfc.setVnfcInstanceId(resource.getId());
+                               vnfc.setVduId(resource.getVduId());
+                               vnfc.setVimid(resource.getComputeResource().getVimId());
+                               vnfc.setVmid(resource.getComputeResource().getResourceId());
+                               
+                               vnfcs.add(vnfc);
+                       }
+               }
+               return vnfcs;
+       }
+
        private NslcmGrantVnfRequest buildNslcmGrantVnfRequest() {
                NslcmGrantVnfRequest request = new NslcmGrantVnfRequest();
                
@@ -131,20 +249,35 @@ public class InstantiateVnfContinueRunnable implements Runnable {
        }
 
        private ResourceDefinition getFreeVnfResource() {
-               // TODO Auto-generated method stub
-               return null;
+               ResourceDefinition def = new ResourceDefinition();
+               def.setVnfInstanceId(vnfInstanceId);
+               def.setVimId("001");
+               List<AddResource> resources = new ArrayList<>();
+               AddResource res = new AddResource();
+               res.setVdu("1");
+               res.setType("vdu");
+               res.setResourceDefinitionId(2);
+               resources.add(res);
+               def.setAddResource(resources);
+               return def;
        }
 
-       private void handleCbamInstantiateResponse(CBAMInstantiateVnfResponse cbamInstantiateResponse, String jobId) {
-               VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.getLong(jobId));
+       private void handleCbamInstantiateResponse(OperationExecution cbamInstantiateResponse, String jobId) {
+               VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
                
                jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
+               if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
+                       jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
+               } else if(CommonEnum.OperationStatus.OTHER == cbamInstantiateResponse.getStatus()){
+                       jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
+               } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
+                       jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
+               }
+               else{
+                       jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
+               }
+                       
                jobDbMgmr.save(jobInfo);
        }
 
-       private void handleNslcmGrantResponse(NslcmGrantVnfResponse grantResponse) {
-               // TODO Auto-generated method stub
-               
-       }
-
 }