Merge "Removed MsoLogger from 'so-bpmn-tasks'"
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / installer / bpmn / BpmnInstaller.java
index e4a4c7c..7e3f5df 100644 (file)
@@ -4,6 +4,8 @@
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * 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
@@ -27,9 +29,10 @@ import java.io.IOException;
 import java.net.URI;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.Enumeration;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
 import java.util.zip.ZipInputStream;
-
 import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
@@ -44,13 +47,15 @@ import org.apache.http.entity.mime.content.StringBody;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.onap.so.logger.MessageEnum;
 import org.onap.so.logger.MsoLogger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.env.Environment;
 import org.springframework.stereotype.Component;
 
 @Component
 public class BpmnInstaller {
-       protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC,BpmnInstaller.class);
+       protected static final Logger logger = LoggerFactory.getLogger(BpmnInstaller.class);
        private static final String BPMN_SUFFIX = ".bpmn";
        private static final String CAMUNDA_URL = "mso.camundaURL";
        private static final String CREATE_DEPLOYMENT_PATH = "/sobpmnengine/deployment/create";
@@ -59,7 +64,7 @@ public class BpmnInstaller {
        private Environment env;        
        
        public void installBpmn(String csarFilePath) {
-               LOGGER.info("Deploying BPMN files from " + csarFilePath);               
+               logger.info("Deploying BPMN files from {}", csarFilePath);
                try {                   
                        ZipInputStream csarFile = new ZipInputStream(new FileInputStream(Paths.get(csarFilePath).normalize().toString()));
                        ZipEntry entry = csarFile.getNextEntry();               
@@ -67,49 +72,62 @@ public class BpmnInstaller {
                        while (entry != null) {                         
                                String name = entry.getName();
                                if (name.endsWith(BPMN_SUFFIX)) {
-                                       LOGGER.debug("Attempting to deploy BPMN file: " + name);
+                                       logger.debug("Attempting to deploy BPMN file: {}", name);
                                        try {
                                                Path p = Paths.get(name);
                                                String fileName = p.getFileName().toString();
                                                extractBpmnFileFromCsar(csarFile, fileName);
                                                HttpResponse response = sendDeploymentRequest(fileName);
-                                               LOGGER.debug("Response status line: " + response.getStatusLine());
-                                               LOGGER.debug("Response entity: " + response.getEntity().toString());
+                                               logger.debug("Response status line: {}", response.getStatusLine());
+                                               logger.debug("Response entity: {}", response.getEntity().toString());
                                                if (response.getStatusLine().getStatusCode() != 200) {
-                                                       LOGGER.debug("Failed deploying BPMN " + name);
-                                       LOGGER.error(MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL,
-                                                               name,
-                                                               fileName,
-                                                               "",
-                                                               Integer.toString(response.getStatusLine().getStatusCode()), "", "", MsoLogger.ErrorCode.DataError, "ASDC BPMN deploy failed");  
+                                                       logger.debug("Failed deploying BPMN {}", name);
+                                                       logger
+                                                               .error("{} {} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL.toString(), name, fileName,
+                                                                       Integer.toString(response.getStatusLine().getStatusCode()), MsoLogger.ErrorCode.DataError.getValue(),
+                                                                       "ASDC BPMN deploy failed");
                                                }                                               
                                                else {
-                                                       LOGGER.debug("Successfully deployed to Camunda: " + name);
+                                                       logger.debug("Successfully deployed to Camunda: {}", name);
                                                }
                                        }
                                        catch (Exception e) {
-                                               LOGGER.debug("Exception :",e);
-                               LOGGER.error(MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL,
-                                                       name,
-                                                       "",
-                                                       "",
-                                                       e.getMessage(), "", "", MsoLogger.ErrorCode.DataError, "ASDC BPMN deploy failed");                                      
+                                               logger.debug("Exception :", e);
+                                               logger
+                                                       .error("{} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL.toString(), name, e.getMessage(),
+                                                               MsoLogger.ErrorCode.DataError.getValue(), "ASDC BPMN deploy failed");
                                        }                                                       
                                }
                                entry = csarFile.getNextEntry();
                }
                        csarFile.close();
                } catch (IOException ex) {
-                       LOGGER.debug("Exception :",ex);
-            LOGGER.error(MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL,
-                               csarFilePath,
-                               "",
-                               "",
-                               ex.getMessage(), "", "", MsoLogger.ErrorCode.DataError, "ASDC reading CSAR with workflows failed");
+                       logger.debug("Exception :", ex);
+                       logger.error("{} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL.toString(), csarFilePath,
+                               ex.getMessage(), MsoLogger.ErrorCode.DataError.getValue(), "ASDC reading CSAR with workflows failed");
                }
                return;
-       }       
-       
+       }
+
+    public boolean containsWorkflows(String csarFilePath) {
+        boolean workflowsInCsar = false;
+        try (ZipFile zipFile = new ZipFile(csarFilePath)) {
+            Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
+            while (zipEntries.hasMoreElements()) {
+                String fileName = zipEntries.nextElement().getName();
+                if (fileName.endsWith(BPMN_SUFFIX)) {
+                    workflowsInCsar = true;
+                    break;
+                }
+            }
+        } catch (Exception e) {
+                                       logger.debug("Exception :", e);
+                                       logger.error("{} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_CHECK_EXC.toString(), csarFilePath, e.getMessage(),
+                                               MsoLogger.ErrorCode.DataError.getValue(), "ASDC Unable to check CSAR entries");
+                               }
+        return workflowsInCsar;
+    }
+
        protected HttpResponse sendDeploymentRequest(String bpmnFileName) throws Exception {
                HttpClient client = HttpClientBuilder.create().build(); 
                URI deploymentUri = new URI(this.env.getProperty(CAMUNDA_URL) + CREATE_DEPLOYMENT_PATH);
@@ -158,14 +176,19 @@ public class BpmnInstaller {
                 return requestEntity;
        }
        
-       protected void extractBpmnFileFromCsar(ZipInputStream zipIn, String fileName) throws IOException {
+       /* protected void extractBpmnFileFromCsar(ZipInputStream zipIn, String fileName) throws IOException */
+        protected void extractBpmnFileFromCsar(ZipInputStream zipIn, String fileName)  {
                String filePath = Paths.get(System.getProperty("mso.config.path"), "ASDC", fileName).normalize().toString();
-               BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath));
+               /* BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath)); */
+               try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath))){
                byte[] bytesIn = new byte[4096];
                int read = 0;
                while ((read = zipIn.read(bytesIn)) != -1) {
                        outputStream.write(bytesIn, 0, read);
                }
-               outputStream.close();
+               /* outputStream.close(); */
+               } catch (IOException e) {
+              logger.error("Unable to open file.", e);
+        }
        }
 }