Incorporate the ECOMP SDC Artefact Generator code
[aai/babel.git] / src / main / java / org / onap / aai / babel / csar / extractor / YamlExtractor.java
index fb51933..7700a54 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017 European Software Marketing Ltd.
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 European Software Marketing Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
 package org.onap.aai.babel.csar.extractor;
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
 import java.util.List;
-import java.util.Set;
 import java.util.regex.Pattern;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipFile;
@@ -38,11 +31,10 @@ import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.onap.aai.babel.logging.ApplicationMsgs;
+import org.onap.aai.babel.logging.LogHelper;
 import org.onap.aai.babel.xml.generator.ModelGenerator;
+import org.onap.aai.babel.xml.generator.data.Artifact;
 import org.onap.aai.cl.api.Logger;
-import org.onap.aai.cl.eelf.LoggerFactory;
-import org.openecomp.sdc.generator.data.Artifact;
-import org.yaml.snakeyaml.Yaml;
 
 /**
  * The purpose of this class is to process a .csar file in the form of a byte array and extract yaml files from it.
@@ -51,19 +43,11 @@ import org.yaml.snakeyaml.Yaml;
  * file.
  */
 public class YamlExtractor {
-    private static Logger logger = LoggerFactory.getInstance().getLogger(YamlExtractor.class);
+    private static Logger logger = LogHelper.INSTANCE;
 
-    private static final String TYPE = "type";
     private static final Pattern YAMLFILE_EXTENSION_REGEX = Pattern.compile("(?i).*\\.ya?ml$");
-    private static final Set<String> INVALID_TYPES = new HashSet<>();
-
-    static {
-        Collections.addAll(INVALID_TYPES, "CP", "VL", "VFC", "VFCMT", "ABSTRACT");
-    }
 
-    /**
-     * Private constructor
-     */
+    /** Private constructor */
     private YamlExtractor() {
         throw new IllegalAccessError("Utility class");
     }
@@ -91,7 +75,7 @@ public class YamlExtractor {
                 ZipFile zipFile = new ZipFile(inMemoryByteChannel)) {
             for (Enumeration<ZipArchiveEntry> enumeration = zipFile.getEntries(); enumeration.hasMoreElements();) {
                 ZipArchiveEntry entry = enumeration.nextElement();
-                if (fileShouldBeExtracted(zipFile, entry)) {
+                if (fileShouldBeExtracted(entry)) {
                     ymlFiles.add(ModelGenerator.createArtifact(IOUtils.toByteArray(zipFile.getInputStream(entry)),
                             entry.getName(), version));
                 }
@@ -120,30 +104,14 @@ public class YamlExtractor {
         }
     }
 
-    @SuppressWarnings("unchecked")
-    private static boolean fileShouldBeExtracted(ZipFile zipFile, ZipArchiveEntry entry) throws IOException {
-        logger.debug(ApplicationMsgs.DISTRIBUTION_EVENT, "Checking if " + entry.getName() + " should be extracted...");
-
-        boolean extractFile = false;
-        if (YAMLFILE_EXTENSION_REGEX.matcher(entry.getName()).matches()) {
-            try {
-                Yaml yamlParser = new Yaml();
-                HashMap<String, Object> yaml =
-                        (LinkedHashMap<String, Object>) yamlParser.load(zipFile.getInputStream(entry));
-                HashMap<String, Object> metadata = (LinkedHashMap<String, Object>) yaml.get("metadata");
-
-                extractFile = metadata != null && metadata.containsKey(TYPE)
-                        && !INVALID_TYPES.contains(metadata.get(TYPE).toString().toUpperCase())
-                        && !metadata.get(TYPE).toString().isEmpty();
-            } catch (Exception e) {
-                logger.error(ApplicationMsgs.DISTRIBUTION_EVENT,
-                        "Unable to verify " + entry.getName() + " contains a valid resource type: " + e.getMessage());
-            }
-        }
-
-        logger.debug(ApplicationMsgs.DISTRIBUTION_EVENT, "Keeping file: " + entry.getName() + "? : " + extractFile);
-
+    /**
+     * @param entry
+     * @return
+     */
+    private static boolean fileShouldBeExtracted(ZipArchiveEntry entry) {
+        boolean extractFile = YAMLFILE_EXTENSION_REGEX.matcher(entry.getName()).matches();
+        logger.debug(ApplicationMsgs.DISTRIBUTION_EVENT,
+                "Checking if " + entry.getName() + " should be extracted... " + extractFile);
         return extractFile;
     }
 }
-