1710 Rebase - Second Attempt
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / client / ASDCController.java
index a95d25f..736cd1a 100644 (file)
 package org.openecomp.mso.asdc.client;\r
 \r
 \r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
 import java.io.IOException;\r
 import java.io.UnsupportedEncodingException;\r
+import java.util.List;\r
+\r
 import org.openecomp.sdc.api.IDistributionClient;\r
 import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;\r
 import org.openecomp.sdc.api.consumer.INotificationCallback;\r
@@ -39,7 +43,9 @@ import org.openecomp.mso.asdc.client.exceptions.ASDCDownloadException;
 import org.openecomp.mso.asdc.client.exceptions.ASDCParametersException;\r
 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;\r
 import org.openecomp.mso.asdc.installer.IVfResourceInstaller;\r
+import org.openecomp.mso.asdc.installer.ToscaResourceStructure;\r
 import org.openecomp.mso.asdc.installer.VfResourceStructure;\r
+import org.openecomp.mso.asdc.installer.heat.ToscaResourceInstaller;\r
 import org.openecomp.mso.asdc.installer.heat.VfResourceInstaller;\r
 import org.openecomp.mso.asdc.util.ASDCNotificationLogging;\r
 import org.openecomp.mso.logger.MessageEnum;\r
@@ -57,6 +63,9 @@ public class ASDCController {
 \r
     protected String controllerName;\r
 \r
+    protected ToscaResourceInstaller toscaInstaller;\r
+    \r
+\r
     /**\r
      * Inner class for Notification callback\r
      *\r
@@ -135,6 +144,7 @@ public class ASDCController {
         isAsdcClientAutoManaged = true;\r
         this.controllerName = controllerConfigName;\r
         this.resourceInstaller = new VfResourceInstaller();\r
+        toscaInstaller = new ToscaResourceInstaller();\r
     }\r
 \r
     public ASDCController (String controllerConfigName, IDistributionClient asdcClient, IVfResourceInstaller resourceinstaller) {\r
@@ -148,6 +158,7 @@ public class ASDCController {
         distributionClient = asdcClient;\r
         this.controllerName = controllerConfigName;\r
         this.resourceInstaller = new VfResourceInstaller();\r
+        toscaInstaller = new ToscaResourceInstaller();\r
     }\r
 \r
     /**\r
@@ -272,7 +283,7 @@ public class ASDCController {
 \r
     private boolean checkResourceAlreadyDeployed (VfResourceStructure resource) throws ArtifactInstallerException {\r
 \r
-        if (this.resourceInstaller.isResourceAlreadyDeployed (resource)) {\r
+       if (toscaInstaller.isResourceAlreadyDeployed (resource)) {\r
             LOGGER.info (MessageEnum.ASDC_ARTIFACT_ALREADY_EXIST,\r
                     resource.getResourceInstance().getResourceInstanceName(),\r
                     resource.getResourceInstance().getResourceUUID(),\r
@@ -366,6 +377,36 @@ public class ASDCController {
 \r
     }\r
 \r
+    private void writeArtifactToFile (IArtifactInfo artifact,\r
+               IDistributionClientDownloadResult resultArtifact) throws ASDCDownloadException {\r
+\r
+       LOGGER.debug ("Trying to download the artifact : " + artifact.getArtifactURL ()\r
+                       + UUID_PARAM\r
+                       + artifact.getArtifactUUID ()\r
+                       + ")");\r
+       \r
+       File spoolFile = new File(System.getProperty("mso.config.path") + "/ASDC" + "/" + artifact.getArtifactName()); \r
+               \r
+       \r
+       byte[] payloadBytes = resultArtifact.getArtifactPayload();\r
+       \r
+       try {\r
+               LOGGER.info(MessageEnum.ASDC_RECEIVE_SERVICE_NOTIF, "***WRITE FILE ARTIFACT NAME", "ASDC", artifact.getArtifactName());\r
+\r
+               FileOutputStream outFile = new FileOutputStream(System.getProperty("mso.config.path") + "/ASDC" + "/" + artifact.getArtifactName());\r
+               outFile.write(payloadBytes, 0, payloadBytes.length);\r
+               outFile.close();\r
+               } catch (Exception e) { \r
+               e.printStackTrace();\r
+                LOGGER.error(MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL,\r
+                                       artifact.getArtifactName (),\r
+                                       artifact.getArtifactURL (),\r
+                                       artifact.getArtifactUUID (),\r
+                                       resultArtifact.getDistributionMessageResult (), "", "", MsoLogger.ErrorCode.DataError, "ASDC write to file failed"); \r
+            } \r
+       \r
+    }\r
+\r
 \r
     private void sendDeployNotificationsForResource(VfResourceStructure vfResourceStructure,DistributionStatusEnum distribStatus, String errorReason) {\r
 \r
@@ -394,7 +435,7 @@ public class ASDCController {
        }\r
     }\r
 \r
-    private void deployResourceStructure (VfResourceStructure resourceStructure) throws ArtifactInstallerException {\r
+    private void deployResourceStructure (VfResourceStructure resourceStructure, ToscaResourceStructure toscaResourceStructure) throws ArtifactInstallerException {\r
 \r
        LOGGER.info (MessageEnum.ASDC_START_DEPLOY_ARTIFACT, resourceStructure.getResourceInstance().getResourceInstanceName(), resourceStructure.getResourceInstance().getResourceUUID(), "ASDC", "deployResourceStructure");\r
         try {\r
@@ -403,10 +444,27 @@ public class ASDCController {
                if(resourceType.equals("VF") && !category.equalsIgnoreCase("Allotted Resource")){\r
                        resourceStructure.createVfModuleStructures();\r
                }\r
-               resourceInstaller.installTheResource (resourceStructure);\r
+               //resourceInstaller.installTheResource (resourceStructure);\r
+                               \r
+                       //ToscaResourceInstaller tri = new ToscaResourceInstaller();\r
+               toscaInstaller.installTheResource(toscaResourceStructure, resourceStructure);\r
+                       \r
+               /*      if(toscaResourceStructure.isVnfAlreadyInstalled()){\r
+                   LOGGER.info (MessageEnum.ASDC_ARTIFACT_ALREADY_EXIST,\r
+                               toscaResourceStructure.getCatalogVnfResource().getModelName(),\r
+                               toscaResourceStructure.getCatalogVnfResource().getModelUuid(),\r
+                               toscaResourceStructure.getCatalogVnfResource().getModelUuid(),"","");\r
+\r
+            \r
+                   this.sendDeployNotificationsForResource(resourceStructure,DistributionStatusEnum.ALREADY_DOWNLOADED,null);\r
+                   this.sendDeployNotificationsForResource(resourceStructure,DistributionStatusEnum.ALREADY_DEPLOYED,null);\r
+                       } */\r
 \r
         } catch (ArtifactInstallerException e) {\r
-\r
+               LOGGER.info (MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL,\r
+                               resourceStructure.getResourceInstance().getResourceName(),\r
+                               resourceStructure.getResourceInstance().getResourceUUID(),\r
+                       String.valueOf (resourceStructure.getVfModuleStructure().size()), "ASDC", "deployResourceStructure");\r
                sendDeployNotificationsForResource(resourceStructure,DistributionStatusEnum.DEPLOY_ERROR,e.getMessage());\r
                throw e;\r
         }\r
@@ -497,6 +555,9 @@ public class ASDCController {
                LOGGER.debug(ASDCNotificationLogging.dumpASDCNotification(iNotif));\r
                        LOGGER.info(MessageEnum.ASDC_RECEIVE_SERVICE_NOTIF, iNotif.getServiceUUID(), "ASDC", "treatNotification");\r
                        this.changeControllerStatus(ASDCControllerStatus.BUSY);\r
+                       \r
+                       \r
+                                               \r
                        // Process only the Resource artifacts in MSO\r
                        for (IResourceInstance resource : iNotif.getResources()) {\r
 \r
@@ -505,6 +566,7 @@ public class ASDCController {
                                if ("VF".equals(resource.getResourceType()) || "VL".equals(resource.getResourceType())) {\r
                                        this.processResourceNotification(iNotif,resource);\r
                                }\r
+\r
                        }\r
 \r
 \r
@@ -522,6 +584,7 @@ public class ASDCController {
     private void processResourceNotification (INotificationData iNotif,IResourceInstance resource) {\r
                // For each artifact, create a structure describing the VFModule in a ordered flat level\r
        VfResourceStructure resourceStructure = new VfResourceStructure(iNotif,resource);\r
+       ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure();\r
 \r
                try {\r
 \r
@@ -542,7 +605,9 @@ public class ASDCController {
 \r
                                }\r
 \r
-                               this.deployResourceStructure(resourceStructure);\r
+                               this.processCsarServiceArtifacts(iNotif, toscaResourceStructure);\r
+                               \r
+                               this.deployResourceStructure(resourceStructure, toscaResourceStructure);\r
 \r
                        }\r
                } catch (ArtifactInstallerException | ASDCDownloadException | UnsupportedEncodingException e) {\r
@@ -551,6 +616,39 @@ public class ASDCController {
                }\r
     }\r
 \r
+    private void processCsarServiceArtifacts (INotificationData iNotif, ToscaResourceStructure toscaResourceStructure) {\r
+       \r
+       List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();\r
+       \r
+               for(IArtifactInfo artifact : serviceArtifacts){\r
+               \r
+                       if(artifact.getArtifactType().equals(ASDCConfiguration.TOSCA_CSAR)){\r
+                               \r
+                               try{\r
+                                       \r
+                                       toscaResourceStructure.setToscaArtifact(artifact);\r
+                                       \r
+                                       IDistributionClientDownloadResult resultArtifact = this.downloadTheArtifact(artifact,iNotif.getDistributionID());\r
+                                       \r
+                                       writeArtifactToFile(artifact, resultArtifact);\r
+                                       \r
+                                       toscaResourceStructure.updateResourceStructure(artifact);\r
+                                       \r
+                                       toscaResourceStructure.setServiceVersion(iNotif.getServiceVersion());\r
+                                       \r
+                                       LOGGER.debug(ASDCNotificationLogging.dumpCSARNotification(iNotif, toscaResourceStructure));\r
+                                       \r
+\r
+                               } catch(Exception e){\r
+                                       System.out.println("Whats the error " + e.getMessage());\r
+                                       LOGGER.error(MessageEnum.ASDC_GENERAL_EXCEPTION_ARG,\r
+                                                       "Exception caught during processCsarServiceArtifacts", "ASDC", "processCsarServiceArtifacts", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in processCsarServiceArtifacts", e);\r
+                               }\r
+                       }\r
+                               \r
+               }\r
+    }\r
+    \r
     private static final String UNKNOWN="Unknown";\r
 \r
     /**\r