Implement builder
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / adaptor / InstantiateVnfContinueRunnable.java
index b3c8bfe..1311fb2 100644 (file)
@@ -1,6 +1,7 @@
-/*
- * Copyright 2016-2017, Nokia Corporation
- *
+/**
+ * Copyright 2016-2017, Nokia Corporation.
+ * Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
+ * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -35,7 +36,8 @@ 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.db.mapper.VnfcResourceInfoMapper;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.db.mapper.VnfmJobExecutionMapper;
 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.NslcmNotifyLCMEventsRequest;
@@ -46,14 +48,18 @@ 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 org.springframework.beans.factory.annotation.Autowired;
 
 import com.google.gson.Gson;
 
 
 public class InstantiateVnfContinueRunnable implements Runnable {
        private static final Logger logger = LoggerFactory.getLogger(InstantiateVnfContinueRunnable.class);
+       @Autowired
        private CbamMgmrInf cbamMgmr;
+       @Autowired
        private CatalogMgmrInf catalogMgmr;
+       @Autowired
        private NslcmMgmrInf nslcmMgmr;
        
        private InstantiateVnfRequest driverRequest;
@@ -61,27 +67,102 @@ public class InstantiateVnfContinueRunnable implements Runnable {
        private String jobId;
        private String vnfmId;
        
-       private VnfmJobExecutionRepository jobDbMgmr;
+//     private VnfmJobExecutionRepository jobDbMgmr;
+       @Autowired
+       private VnfmJobExecutionMapper jobDbMgmr;
+       @Autowired
+       private VnfcResourceInfoMapper vnfcDbMgmr;
        
        private Driver2CbamRequestConverter requestConverter;
        
        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;
-               this.vnfInstanceId = vnfInstanceId;
-               this.jobId = jobId;
-               this.nslcmMgmr = nslcmMgmr; 
-               this.catalogMgmr = catalogMgmr;
-               this.cbamMgmr = cbamMgmr;
-               this.requestConverter = requestConverter;
-               this.jobDbMgmr = dbManager;
-               this.vnfmId = vnfmId;
+       // Builder class
+       
+       public static class InstantiateVnfContinueRunnableBuilder {
+           private String vnfmId;
+           private InstantiateVnfRequest driverRequest;
+           private String vnfInstanceId;
+           private String jobId;
+           private NslcmMgmrInf nslcmMgmr;
+           private CatalogMgmrInf catalogMgmr;
+           private CbamMgmrInf cbamMgmr;
+           private Driver2CbamRequestConverter requestConverter;
+           private VnfmJobExecutionMapper dbManager;
+           private VnfcResourceInfoMapper vnfcDbMgmr;
+
+           public InstantiateVnfContinueRunnableBuilder setVnfmId(String vnfmId) {
+               this.vnfmId = vnfmId;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnableBuilder setDriverRequest(InstantiateVnfRequest driverRequest) {
+               this.driverRequest = driverRequest;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnableBuilder setVnfInstanceId(String vnfInstanceId) {
+               this.vnfInstanceId = vnfInstanceId;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnableBuilder setJobId(String jobId) {
+               this.jobId = jobId;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnableBuilder setNslcmMgmr(NslcmMgmrInf nslcmMgmr) {
+               this.nslcmMgmr = nslcmMgmr;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnableBuilder setCatalogMgmr(CatalogMgmrInf catalogMgmr) {
+               this.catalogMgmr = catalogMgmr;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnableBuilder setCbamMgmr(CbamMgmrInf cbamMgmr) {
+               this.cbamMgmr = cbamMgmr;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnableBuilder setRequestConverter(Driver2CbamRequestConverter requestConverter) {
+               this.requestConverter = requestConverter;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnableBuilder setDbManager(VnfmJobExecutionMapper dbManager) {
+               this.dbManager = dbManager;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnableBuilder setVnfcDbMgmr(VnfcResourceInfoMapper vnfcDbMgmr) {
+               this.vnfcDbMgmr = vnfcDbMgmr;
+               return this;
+           }
+
+           public InstantiateVnfContinueRunnable build() {
+               return new InstantiateVnfContinueRunnable(this);
+           }
        }
        
-       public void run() {
+               
+       private InstantiateVnfContinueRunnable(InstantiateVnfContinueRunnableBuilder builder) {
+           
+           this.driverRequest = builder.driverRequest;
+        this.vnfInstanceId = builder.vnfInstanceId;
+        this.jobId = builder.jobId;
+        this.nslcmMgmr = builder.nslcmMgmr; 
+        this.catalogMgmr = builder.catalogMgmr;
+        this.cbamMgmr = builder.cbamMgmr;
+        this.requestConverter = builder.requestConverter;
+        this.jobDbMgmr = builder.dbManager;
+        this.vnfmId = builder.vnfmId;
+        this.vnfcDbMgmr = builder.vnfcDbMgmr;
+           
+    }
+
+    public void run() {
                //step 1 handle vnf package
                handleVnfPackage();
                
@@ -109,7 +190,6 @@ public class InstantiateVnfContinueRunnable implements Runnable {
                                {
                                        instantiateFinished = true;
                                        handleCbamInstantiateResponse(exeResponse, jobId);
-                                       OperateTaskProgress.stopInstantiateTimerTask();
                                        if (exeResponse.getStatus() == CommonEnum.OperationStatus.FINISHED)
                                        {
                                                
@@ -132,7 +212,7 @@ public class InstantiateVnfContinueRunnable implements Runnable {
                                                logger.info("Start to notify LCM the instantiation result");
                                                NslcmNotifyLCMEventsRequest nslcmNotifyReq = buildNslcmNotifyLCMEventsRequest(vnfcResources);
                                                
-                                               OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
+//                                             OperateTaskProgress.setAffectedVnfc(nslcmNotifyReq.getAffectedVnfc());
                                                
                                                nslcmMgmr.notifyVnf(nslcmNotifyReq, vnfmId, vnfInstanceId);
                                                logger.info("End to notify LCM the instantiation result");
@@ -149,7 +229,7 @@ public class InstantiateVnfContinueRunnable implements Runnable {
                
        }
 
-       private CBAMInstantiateVnfResponse handleInstantiate() throws Exception {
+       private CBAMInstantiateVnfResponse handleInstantiate() throws IOException {
                CBAMInstantiateVnfRequest  instantiateReq = requestConverter.instantiateRequestConvert(driverRequest, null, null, null);
                CBAMInstantiateVnfResponse cbamInstantiateResponse = cbamMgmr.instantiateVnf(instantiateReq, vnfInstanceId);
                handleCbamInstantiateResponse(cbamInstantiateResponse, jobId);
@@ -177,9 +257,7 @@ public class InstantiateVnfContinueRunnable implements Runnable {
        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;
+               return gson.fromJson(fileContent, CBAMModifyVnfRequest.class);
        }
 
        private void handleVnfPackage() {
@@ -236,6 +314,8 @@ public class InstantiateVnfContinueRunnable implements Runnable {
                                vnfc.setVmid(resource.getComputeResource().getResourceId());
                                
                                vnfcs.add(vnfc);
+                               
+                               vnfcDbMgmr.insert(vnfc);
                        }
                }
                return vnfcs;
@@ -249,7 +329,7 @@ public class InstantiateVnfContinueRunnable implements Runnable {
                request.setJobId(jobId);
                
                ResourceDefinition resource = getFreeVnfResource();
-               List<ResourceDefinition> resourceList = new ArrayList<ResourceDefinition>();
+               List<ResourceDefinition> resourceList = new ArrayList<>();
                resourceList.add(resource);
                request.setAddResource(resourceList);
                
@@ -274,6 +354,7 @@ public class InstantiateVnfContinueRunnable implements Runnable {
                VnfmJobExecutionInfo jobInfo = jobDbMgmr.findOne(Long.parseLong(jobId));
                
                jobInfo.setVnfmExecutionId(cbamInstantiateResponse.getId());
+               long nowTime = System.currentTimeMillis();
                if(CommonEnum.OperationStatus.FAILED == cbamInstantiateResponse.getStatus()){
                        jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_ERROR);
 //                     jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
@@ -281,12 +362,34 @@ public class InstantiateVnfContinueRunnable implements Runnable {
                        jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_PROCESSING);
                } else if(CommonEnum.OperationStatus.FINISHED == cbamInstantiateResponse.getStatus()){
                        jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_FINISH);
+                       jobInfo.setOperateEndTime(nowTime);
+                       
                }
                else{
                        jobInfo.setStatus(CommonConstants.CBAM_OPERATION_STATUS_START);
                }
                        
-               jobDbMgmr.save(jobInfo);
+               jobDbMgmr.update(jobInfo);
+       }
+
+       public void setDriverRequest(InstantiateVnfRequest driverRequest) {
+               this.driverRequest = driverRequest;
+       }
+
+       public void setVnfInstanceId(String vnfInstanceId) {
+               this.vnfInstanceId = vnfInstanceId;
+       }
+
+       public void setJobId(String jobId) {
+               this.jobId = jobId;
+       }
+
+       public void setVnfmId(String vnfmId) {
+               this.vnfmId = vnfmId;
+       }
+
+       public void setRequestConverter(Driver2CbamRequestConverter requestConverter) {
+               this.requestConverter = requestConverter;
        }
 
 }