2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.babel.csar.extractor;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Enumeration;
26 import java.util.List;
27 import java.util.regex.Pattern;
28 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
29 import org.apache.commons.compress.archivers.zip.ZipFile;
30 import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.lang3.StringUtils;
33 import org.onap.aai.babel.logging.ApplicationMsgs;
34 import org.onap.aai.babel.logging.LogHelper;
35 import org.onap.aai.babel.xml.generator.ModelGenerator;
36 import org.onap.aai.babel.xml.generator.data.Artifact;
37 import org.onap.aai.cl.api.Logger;
40 * The purpose of this class is to process a .csar file in the form of a byte array and extract yaml files from it.
42 * A .csar file is a compressed archive like a zip file and this class will treat the byte array as it if were a zip
45 public class YamlExtractor {
46 private static Logger logger = LogHelper.INSTANCE;
48 private static final Pattern YAMLFILE_EXTENSION_REGEX = Pattern.compile("(?i).*\\.ya?ml$");
50 /** Private constructor */
51 private YamlExtractor() {
52 throw new IllegalAccessError("Utility class");
56 * This method is responsible for filtering the contents of the supplied archive and returning a collection of
57 * {@link Artifact}s that represent the yml files that have been found in the archive.<br>
59 * If the archive contains no yml files it will return an empty list.<br>
61 * @param archive the zip file in the form of a byte array containing one or more yml files
62 * @param name the name of the archive file
63 * @param version the version of the archive file
64 * @return List<Artifact> collection of yml files found in the archive
65 * @throws InvalidArchiveException if an error occurs trying to extract the yml files from the archive, if the
66 * archive is not a zip file or there are no yml files
68 public static List<Artifact> extract(byte[] archive, String name, String version) throws InvalidArchiveException {
69 validateRequest(archive, name, version);
71 logger.info(ApplicationMsgs.DISTRIBUTION_EVENT, "Extracting CSAR archive: " + name);
73 List<Artifact> ymlFiles = new ArrayList<>();
74 try (SeekableInMemoryByteChannel inMemoryByteChannel = new SeekableInMemoryByteChannel(archive);
75 ZipFile zipFile = new ZipFile(inMemoryByteChannel)) {
76 for (Enumeration<ZipArchiveEntry> enumeration = zipFile.getEntries(); enumeration.hasMoreElements();) {
77 ZipArchiveEntry entry = enumeration.nextElement();
78 if (fileShouldBeExtracted(entry)) {
79 ymlFiles.add(ModelGenerator.createArtifact(IOUtils.toByteArray(zipFile.getInputStream(entry)),
80 entry.getName(), version));
83 if (ymlFiles.isEmpty()) {
84 throw new InvalidArchiveException("No valid yml files were found in the csar file.");
86 } catch (IOException e) {
87 throw new InvalidArchiveException(
88 "An error occurred trying to create a ZipFile. Is the content being converted really a csar file?",
92 logger.debug(ApplicationMsgs.DISTRIBUTION_EVENT, ymlFiles.size() + " YAML files extracted.");
97 private static void validateRequest(byte[] archive, String name, String version) throws InvalidArchiveException {
98 if (archive == null || archive.length == 0) {
99 throw new InvalidArchiveException("An archive must be supplied for processing.");
100 } else if (StringUtils.isBlank(name)) {
101 throw new InvalidArchiveException("The name of the archive must be supplied for processing.");
102 } else if (StringUtils.isBlank(version)) {
103 throw new InvalidArchiveException("The version must be supplied for processing.");
111 private static boolean fileShouldBeExtracted(ZipArchiveEntry entry) {
112 boolean extractFile = YAMLFILE_EXTENSION_REGEX.matcher(entry.getName()).matches();
113 logger.debug(ApplicationMsgs.DISTRIBUTION_EVENT,
114 "Checking if " + entry.getName() + " should be extracted... " + extractFile);