CsarHandler rework 63/36263/1
authorDeterme, Sebastien (sd378r) <sd378r@intl.att.com>
Fri, 16 Mar 2018 18:44:41 +0000 (19:44 +0100)
committerDeterme, Sebastien (sd378r) <sd378r@intl.att.com>
Fri, 16 Mar 2018 18:44:41 +0000 (19:44 +0100)
Reworking of the csarHandler to support cache of dcae blueprint

Issue-ID: CLAMP-81
Change-Id: Ic9c71d8f23ac75a6273781991f5ac7a60c4f328d
Signed-off-by: Determe, Sebastien (sd378r) <sd378r@intl.att.com>
src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandler.java
src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java

index 270286b..940b7cf 100644 (file)
 
 package org.onap.clamp.clds.sdc.controller.installer;
 
+import com.att.aft.dme2.internal.apache.commons.io.IOUtils;
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 
 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
@@ -43,28 +49,28 @@ import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
 import org.openecomp.sdc.tosca.parser.impl.SdcToscaParserFactory;
 
 /**
- * CsarDescriptor that will be used to deploy in CLAMP.
+ * CsarDescriptor that will be used to deploy file in CLAMP file system. Some
+ * methods can also be used to get some data from it.
  */
 public class CsarHandler {
 
     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarHandler.class);
     private IArtifactInfo artifactElement;
-    private String filePath;
+    private String csarFilePath;
     private String controllerName;
     private SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
     private ISdcCsarHelper sdcCsarHelper;
+    private String dcaeBlueprint;
     public static final String CSAR_TYPE = "TOSCA_CSAR";
-    private String csarPath;
 
-    public CsarHandler(INotificationData iNotif, String controller, String sdcCsarPath) throws CsarHandlerException {
-        this.csarPath = sdcCsarPath;
+    public CsarHandler(INotificationData iNotif, String controller, String clampCsarPath) throws CsarHandlerException {
         this.controllerName = controller;
         this.artifactElement = searchForUniqueCsar(iNotif);
-        this.filePath = buildFilePathForCsar(artifactElement);
+        this.csarFilePath = buildFilePathForCsar(artifactElement, clampCsarPath);
     }
 
-    private String buildFilePathForCsar(IArtifactInfo artifactElement) {
-        return csarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
+    private String buildFilePathForCsar(IArtifactInfo artifactElement, String clampCsarPath) {
+        return clampCsarPath + "/" + controllerName + "/" + artifactElement.getArtifactName();
     }
 
     private IArtifactInfo searchForUniqueCsar(INotificationData iNotif) throws CsarHandlerException {
@@ -77,21 +83,41 @@ public class CsarHandler {
         throw new CsarHandlerException("Unable to find a CSAR in the Sdc Notification");
     }
 
-    public void save(IDistributionClientDownloadResult resultArtifact)
+    public synchronized void save(IDistributionClientDownloadResult resultArtifact)
             throws SdcArtifactInstallerException, SdcToscaParserException {
         try {
             logger.info("Writing CSAR file : " + artifactElement.getArtifactURL() + " UUID "
                     + artifactElement.getArtifactUUID() + ")");
-            Path path = Paths.get(filePath);
+            Path path = Paths.get(csarFilePath);
             Files.createDirectories(path.getParent());
             Files.createFile(path);
-            try (FileOutputStream outFile = new FileOutputStream(filePath)) {
+            try (FileOutputStream outFile = new FileOutputStream(csarFilePath)) {
                 outFile.write(resultArtifact.getArtifactPayload(), 0, resultArtifact.getArtifactPayload().length);
             }
-            sdcCsarHelper = factory.getSdcCsarHelper(filePath);
+            sdcCsarHelper = factory.getSdcCsarHelper(csarFilePath);
+            this.loadDcaeBlueprint();
         } catch (IOException e) {
             throw new SdcArtifactInstallerException(
-                    "Exception caught when trying to write the CSAR on the file system to " + filePath, e);
+                    "Exception caught when trying to write the CSAR on the file system to " + csarFilePath, e);
+        }
+    }
+
+    private void loadDcaeBlueprint() throws IOException, SdcArtifactInstallerException {
+        List<ZipEntry> listEntries = new ArrayList<>();
+        try (ZipFile zipFile = new ZipFile(csarFilePath)) {
+            Enumeration<? extends ZipEntry> entries = zipFile.entries();
+            while (entries.hasMoreElements()) {
+                ZipEntry entry = entries.nextElement();
+                if (entry.getName().contains("DCAE_INVENTORY_BLUEPRINT")) {
+                    listEntries.add(entry);
+                }
+            }
+            if (listEntries.size() > 1) {
+                throw new SdcArtifactInstallerException("There are multiple entries in the DCAE inventory");
+            }
+            try (InputStream stream = zipFile.getInputStream(listEntries.get(0))) {
+                this.dcaeBlueprint = IOUtils.toString(stream);
+            }
         }
     }
 
@@ -100,10 +126,14 @@ public class CsarHandler {
     }
 
     public String getFilePath() {
-        return filePath;
+        return csarFilePath;
     }
 
-    public ISdcCsarHelper getSdcCsarHelper() {
+    public synchronized ISdcCsarHelper getSdcCsarHelper() {
         return sdcCsarHelper;
     }
+
+    public synchronized String getDcaeBlueprint() {
+        return dcaeBlueprint;
+    }
 }
index d1b177d..34805d8 100644 (file)
@@ -38,7 +38,6 @@ import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.junit.AfterClass;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
@@ -54,13 +53,6 @@ public class CsarHandlerTest {
     private static final String sdcFolder = "/tmp/csar-handler-tests";
     private static final String csarArtifactName = "testArtifact.csar";
 
-    @AfterClass
-    public static void removeAllFiles() throws IOException {
-        // Do some cleanup
-        Path path = Paths.get(sdcFolder + "/test-controller/" + csarArtifactName);
-        Files.deleteIfExists(path);
-    }
-
     @Test
     public void testConstructor() throws CsarHandlerException {
         IArtifactInfo serviceArtifact = Mockito.mock(IArtifactInfo.class);
@@ -96,9 +88,18 @@ public class CsarHandlerTest {
         IDistributionClientDownloadResult resultArtifact = Mockito.mock(IDistributionClientDownloadResult.class);
         Mockito.when(resultArtifact.getArtifactPayload()).thenReturn(
                 IOUtils.toByteArray(ResourceFileUtil.getResourceAsStream("example/sdc/service-Simsfoimap0112.csar")));
+        // Test the save
         csar.save(resultArtifact);
         assertTrue((new File(sdcFolder + "/test-controller/" + csarArtifactName)).exists());
         assertEquals(csarArtifactName, csar.getArtifactElement().getArtifactName());
         assertNotNull(csar.getSdcCsarHelper());
+        // Test dcaeBlueprint
+        String blueprint = csar.getDcaeBlueprint();
+        assertNotNull(blueprint);
+        assertTrue(!blueprint.isEmpty());
+        assertTrue(blueprint.contains("DCAE-VES-PM-EVENT-v1"));
+        // Do some cleanup
+        Path path = Paths.get(sdcFolder + "/test-controller/" + csarArtifactName);
+        Files.deleteIfExists(path);
     }
 }