Merge 'origin/casablanca' into master
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / client / ASDCController.java
index fae3a49..ca1d033 100644 (file)
@@ -22,10 +22,11 @@ d * ============LICENSE_START===================================================
 package org.onap.so.asdc.client;
 
 
+import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
+import java.nio.file.Paths;
 import java.util.List;
 
 import org.onap.sdc.api.IDistributionClient;
@@ -45,8 +46,8 @@ import org.onap.so.asdc.client.exceptions.ASDCParametersException;
 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
 import org.onap.so.asdc.installer.IVfResourceInstaller;
 import org.onap.so.asdc.installer.ToscaResourceStructure;
-import org.onap.so.asdc.installer.VfModuleStructure;
 import org.onap.so.asdc.installer.VfResourceStructure;
+import org.onap.so.asdc.installer.bpmn.BpmnInstaller;
 import org.onap.so.asdc.installer.heat.ToscaResourceInstaller;
 import org.onap.so.asdc.tenantIsolation.DistributionStatus;
 import org.onap.so.asdc.tenantIsolation.WatchdogDistribution;
@@ -54,7 +55,7 @@ import org.onap.so.asdc.util.ASDCNotificationLogging;
 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
 import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
 import org.onap.so.logger.MessageEnum;
-import org.onap.so.logger.MsoAlarmLogger;
+
 import org.onap.so.logger.MsoLogger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -64,7 +65,7 @@ public class ASDCController {
 
     protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC,ASDCController.class);
 
-    protected static MsoAlarmLogger alarmLogger = new MsoAlarmLogger ();
+
 
     protected boolean isAsdcClientAutoManaged = false;
 
@@ -77,6 +78,9 @@ public class ASDCController {
     @Autowired
     private ToscaResourceInstaller toscaInstaller;
     
+    @Autowired
+    private BpmnInstaller bpmnInstaller;
+    
     @Autowired
     private WatchdogDistributionStatusRepository wdsRepo;
     
@@ -352,9 +356,17 @@ public class ASDCController {
                        + artifact.getArtifactUUID ()
                        + ")");
        
+        String filePath = Paths.get(System.getProperty("mso.config.path"), "ASDC",  artifact.getArtifactVersion(), artifact.getArtifactName()).normalize().toString();
+       // make parent directory
+       File file = new File(filePath);         
+       File fileParent = file.getParentFile();
+       if (!fileParent.exists()) {
+           fileParent.mkdirs();
+       }
+
        byte[] payloadBytes = resultArtifact.getArtifactPayload();
        
-       try (FileOutputStream outFile = new FileOutputStream(System.getProperty("mso.config.path") + "/ASDC" + "/" + artifact.getArtifactName())) {
+       try (FileOutputStream outFile = new FileOutputStream(filePath)) {
                LOGGER.info(MessageEnum.ASDC_RECEIVE_SERVICE_NOTIF, "***WRITE FILE ARTIFACT NAME", "ASDC", artifact.getArtifactName());
                outFile.write(payloadBytes, 0, payloadBytes.length);
                outFile.close();
@@ -583,7 +595,7 @@ public class ASDCController {
                                distributionStatus = wd.getOverallDistributionStatus(iNotif.getDistributionID());
                                Thread.sleep(watchDogTimeout / 10);             
                        }catch(Exception e){
-                               LOGGER.debug ("Exception in Watchdog Loop " + e.getMessage());
+                               LOGGER.debug ("Exception in Watchdog Loop " + e);
                                Thread.sleep(watchDogTimeout / 10);
                        }
                        
@@ -615,7 +627,7 @@ public class ASDCController {
                        LOGGER.debug ("A&AI Updated succefully with Distribution Status!");
                }
                catch(Exception e) {
-                       LOGGER.debug ("Exception in Watchdog executePatchAAI(): " + e.getMessage());
+                       LOGGER.debug ("Exception in Watchdog executePatchAAI(): " + e);
                        watchdogError = "Error calling A&AI " + e.getMessage();
                        if(e.getCause() != null) {
                                LOGGER.debug ("Exception caused by: " + e.getCause().getMessage());
@@ -647,7 +659,7 @@ public class ASDCController {
                        LOGGER.debug ("A&AI Updated succefully with Distribution Status of " + DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name());
                }
                catch(Exception aaiException) {
-                       LOGGER.debug ("Exception in executePatchAAI(): " + aaiException.getMessage());
+                       LOGGER.debug ("Exception in executePatchAAI(): " + aaiException);
                        if(aaiException.getCause() != null) {
                                LOGGER.debug ("Exception caused by: " + aaiException.getCause().getMessage());
                        }
@@ -669,28 +681,20 @@ public class ASDCController {
        VfResourceStructure resourceStructure = null;
        ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure();
        boolean deploySuccessful = true;
+       boolean hasVFResource = false;
        String errorMessage = null;
 
        try {
                
                        this.processCsarServiceArtifacts(iNotif, toscaResourceStructure);
-                       
-                       // Install a service with no resources, only the service itself
-                       if (iNotif.getResources() == null || iNotif.getResources().size() < 1) {
-                               
-                               LOGGER.debug("No resources found for Service: " + iNotif.getServiceUUID());
-                               
-                               try{
-                                       resourceStructure = new VfResourceStructure(iNotif,new ResourceInstance()); 
-                                       
-                                       this.deployResourceStructure(resourceStructure, toscaResourceStructure);
-
-                               } catch(ArtifactInstallerException e){
-                                       deploySuccessful = false;
-                                       errorMessage = e.getMessage();
-                               }  
-                       } else { // Services with resources
-                       
+                       IArtifactInfo iArtifact = toscaResourceStructure.getToscaArtifact();
+                       String filePath = System.getProperty("mso.config.path") + "/ASDC/" + iArtifact.getArtifactVersion() + "/" + iArtifact.getArtifactName();
+                       File csarFile = new File(filePath);
+                       String csarFilePath = csarFile.getAbsolutePath();
+                       if (bpmnInstaller.containsWorkflows(csarFilePath)) {
+                               bpmnInstaller.installBpmn(csarFilePath);
+                       }                       
+                                               
                for (IResourceInstance resource : iNotif.getResources()){
                        
                        resourceStructure = new VfResourceStructure(iNotif,resource);
@@ -701,11 +705,14 @@ public class ASDCController {
                 LOGGER.debug("Processing Resource Type: " + resourceType + " and Model UUID: " + resourceStructure.getResourceInstance().getResourceUUID());
                        
                                if("VF".equals(resourceType) && !"Allotted Resource".equalsIgnoreCase(category)){
+                                       
+                                       hasVFResource = true;
                        
                                for (IArtifactInfo artifact : resource.getArtifacts()) {
                                        IDistributionClientDownloadResult resultArtifact = this.downloadTheArtifact(artifact,
                                                        iNotif.getDistributionID());
                                        if (resultArtifact != null) {
+                                                                                       
                                                if (ASDCConfiguration.VF_MODULES_METADATA.equals(artifact.getArtifactType())) {
                                                        LOGGER.debug("VF_MODULE_ARTIFACT: "+new String(resultArtifact.getArtifactPayload(),"UTF-8"));
                                                        LOGGER.debug(ASDCNotificationLogging.dumpVfModuleMetaDataList(resourceStructure.decodeVfModuleArtifact(resultArtifact.getArtifactPayload())));
@@ -713,20 +720,37 @@ public class ASDCController {
                                                resourceStructure.addArtifactToStructure(distributionClient,artifact, resultArtifact);
                                        }
                                }
+                               
+                                       //Deploy VF resource and artifacts
+                                       LOGGER.debug("Preparing to deploy Service: " + iNotif.getServiceUUID());
+                                       try{
+                                               
+                                               this.deployResourceStructure(resourceStructure, toscaResourceStructure);
+
+                                       } catch(ArtifactInstallerException e){
+                                               deploySuccessful = false;
+                                               errorMessage = e.getMessage();
+                                               LOGGER.error(e);
+                                       }  
                                }
+                                               
+               }       
+               
+                       // There are cases where the Service has no VF resources, those are handled here
+                       if (!hasVFResource) {
+                               
+                               LOGGER.debug("No resources found for Service: " + iNotif.getServiceUUID());
                                
-                               //Deploy All resources and artifacts
-                               LOGGER.debug("Preparing to deploy Service: " + iNotif.getServiceUUID());
-                               try{
-                                       
+                               try{
+                                       resourceStructure = new VfResourceStructure(iNotif,new ResourceInstance()); 
+                                       
                                        this.deployResourceStructure(resourceStructure, toscaResourceStructure);
 
                                } catch(ArtifactInstallerException e){
                                        deploySuccessful = false;
                                        errorMessage = e.getMessage();
-                               }  
-                               
-               }       
+                                       LOGGER.error(e);
+                  }
                }
                         this.sendCsarDeployNotification(iNotif, resourceStructure, toscaResourceStructure, deploySuccessful, errorMessage);
                
@@ -763,6 +787,26 @@ public class ASDCController {
                                                        "Exception caught during processCsarServiceArtifacts", "ASDC", "processCsarServiceArtifacts", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in processCsarServiceArtifacts", e);
                                }
                        }
+                       else if(artifact.getArtifactType().equals(ASDCConfiguration.WORKFLOWS)){
+                               
+                               try{                                    
+                                       
+                                       IDistributionClientDownloadResult resultArtifact = this.downloadTheArtifact(artifact,iNotif.getDistributionID());
+                                       
+                                       writeArtifactToFile(artifact, resultArtifact);
+                                       
+                                       toscaResourceStructure.setToscaArtifact(artifact);
+                                       
+                                       LOGGER.debug(ASDCNotificationLogging.dumpASDCNotification(iNotif));
+                                       
+
+                               } catch(Exception e){
+                                       System.out.println("Whats the error " + e.getMessage());
+                                       LOGGER.error(MessageEnum.ASDC_GENERAL_EXCEPTION_ARG,
+                                                       "Exception caught during processCsarServiceArtifacts", "ASDC", "processCsarServiceArtifacts", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in processCsarServiceArtifacts", e);
+                               }
+                       }
+
                                
                }
     }