X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fbabel.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2Fcsar%2Fextractor%2FYamlExtractor.java;h=7700a54564ce04203b1c3daee361391c9c35e5b6;hp=fb51933840419c34c0b6e7fd5fb88814582fe98d;hb=66b3afa06776e9944ad515206d281d67747c9770;hpb=161f5a7d9b900ae34a4886d7f7fb01ea496f71eb diff --git a/src/main/java/org/onap/aai/babel/csar/extractor/YamlExtractor.java b/src/main/java/org/onap/aai/babel/csar/extractor/YamlExtractor.java index fb51933..7700a54 100644 --- a/src/main/java/org/onap/aai/babel/csar/extractor/YamlExtractor.java +++ b/src/main/java/org/onap/aai/babel/csar/extractor/YamlExtractor.java @@ -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. @@ -17,20 +17,13 @@ * 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 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 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 yaml = - (LinkedHashMap) yamlParser.load(zipFile.getInputStream(entry)); - HashMap metadata = (LinkedHashMap) 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; } } -