2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding;
22 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_EMPTY_ERROR;
23 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_INVALID_ERROR;
24 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_INVALID_EXTENSION;
25 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_MISSING_INTERNAL_PACKAGE;
26 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_ERROR;
27 import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_INTERNAL_PACKAGE_ERROR;
29 import com.google.common.collect.ImmutableSet;
30 import java.nio.ByteBuffer;
31 import java.util.HashSet;
33 import java.util.Optional;
35 import org.apache.commons.collections4.CollectionUtils;
36 import org.apache.commons.collections4.MapUtils;
37 import org.apache.commons.io.FilenameUtils;
38 import org.openecomp.core.utilities.file.FileContentHandler;
39 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
40 import org.openecomp.sdc.common.zip.exception.ZipException;
41 import org.openecomp.sdc.common.utils.CommonUtil;
42 import org.openecomp.sdc.datatypes.error.ErrorLevel;
43 import org.openecomp.sdc.datatypes.error.ErrorMessage;
44 import org.openecomp.sdc.logging.api.Logger;
45 import org.openecomp.sdc.logging.api.LoggerFactory;
46 import org.openecomp.sdc.vendorsoftwareproduct.exception.OnboardPackageException;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardSignedPackage;
51 public class OnboardingPackageProcessor {
52 private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingPackageProcessor.class);
53 private static final Set<String> ALLOWED_SIGNATURE_EXTENSIONS = ImmutableSet.of("cms");
54 private static final Set<String> ALLOWED_CERTIFICATE_EXTENSIONS = ImmutableSet.of("cert", "crt");
55 private static final String CSAR_EXTENSION = "csar";
56 private static final String ZIP_EXTENSION = "zip";
58 private final String packageFileName;
59 private final byte[] packageFileContent;
60 private FileContentHandler onboardPackageContentHandler;
61 private Set<ErrorMessage> errorMessageSet = new HashSet<>();
62 private OnboardPackageInfo onboardPackageInfo;
64 public OnboardingPackageProcessor(final String packageFileName, final byte[] packageFileContent) {
65 this.packageFileName = packageFileName;
66 this.packageFileContent = packageFileContent;
67 onboardPackageInfo = processPackage();
70 private OnboardPackageInfo processPackage() {
71 if (!hasValidExtension()) {
72 final String message = PACKAGE_INVALID_EXTENSION.formatMessage(packageFileName, String.join(", ", CSAR_EXTENSION, ZIP_EXTENSION));
73 reportError(ErrorLevel.ERROR, message);
77 onboardPackageContentHandler = CommonUtil.getZipContent(packageFileContent);
78 } catch (final ZipException e) {
79 final String message = PACKAGE_PROCESS_ERROR.formatMessage(packageFileName);
80 LOGGER.error(message, e);
81 reportError(ErrorLevel.ERROR, message);
84 if (isPackageEmpty()) {
85 final String message = PACKAGE_EMPTY_ERROR.formatMessage(packageFileName);
86 reportError(ErrorLevel.ERROR, message);
90 final String packageName = FilenameUtils.getBaseName(packageFileName);
91 final String packageExtension = FilenameUtils.getExtension(packageFileName);
93 if (hasSignedPackageStructure()) {
94 return processSignedPackage(packageName, packageExtension);
96 final OnboardPackage onboardPackage = new OnboardPackage(packageName, packageExtension,
97 ByteBuffer.wrap(packageFileContent), onboardPackageContentHandler);
98 if (packageExtension.equalsIgnoreCase(CSAR_EXTENSION)) {
99 return new OnboardPackageInfo(onboardPackage, OnboardingTypesEnum.CSAR);
100 } else if (packageExtension.equalsIgnoreCase(ZIP_EXTENSION)) {
101 return new OnboardPackageInfo(onboardPackage, OnboardingTypesEnum.ZIP);
105 reportError(ErrorLevel.ERROR, PACKAGE_INVALID_ERROR.formatMessage(packageFileName));
109 private boolean hasValidExtension() {
110 final String packageExtension = FilenameUtils.getExtension(packageFileName);
111 return packageExtension.equalsIgnoreCase(CSAR_EXTENSION) || packageExtension.equalsIgnoreCase(ZIP_EXTENSION);
114 private OnboardPackageInfo processSignedPackage(final String packageName, final String packageExtension) {
115 final String internalPackagePath = findInternalPackagePath().orElse(null);
116 if (internalPackagePath == null) {
117 reportError(ErrorLevel.ERROR, PACKAGE_MISSING_INTERNAL_PACKAGE.getErrorMessage());
120 final String signatureFilePath = findSignatureFilePath().orElse(null);
121 final String certificateFilePath = findCertificateFilePath().orElse(null);
122 final OnboardSignedPackage onboardSignedPackage =
123 new OnboardSignedPackage(packageName, packageExtension, ByteBuffer.wrap(packageFileContent),
124 onboardPackageContentHandler, signatureFilePath, internalPackagePath, certificateFilePath);
126 final String internalPackageName = FilenameUtils.getName(internalPackagePath);
127 final String internalPackageBaseName = FilenameUtils.getBaseName(internalPackagePath);
128 final String internalPackageExtension = FilenameUtils.getExtension(internalPackagePath);
129 final byte[] internalPackageContent = onboardPackageContentHandler.getFileContent(internalPackagePath);
131 final OnboardPackage onboardPackage;
133 onboardPackage = new OnboardPackage(internalPackageBaseName, internalPackageExtension,
134 internalPackageContent);
135 } catch (final OnboardPackageException e) {
136 final String message = PACKAGE_PROCESS_INTERNAL_PACKAGE_ERROR.formatMessage(internalPackageName);
137 LOGGER.error(message, e);
138 reportError(ErrorLevel.ERROR, message);
142 return new OnboardPackageInfo(onboardSignedPackage, onboardPackage, OnboardingTypesEnum.SIGNED_CSAR);
145 private void reportError(final ErrorLevel errorLevel, final String message) {
146 errorMessageSet.add(new ErrorMessage(errorLevel, message));
149 public boolean hasErrors() {
150 return !errorMessageSet.isEmpty();
153 public Set<ErrorMessage> getErrorMessageSet() {
154 return errorMessageSet;
157 private Optional<String> findInternalPackagePath() {
158 return onboardPackageContentHandler.getFileList().stream()
159 .filter(filePath -> {
160 final String extension = FilenameUtils.getExtension(filePath);
161 return CSAR_EXTENSION.equalsIgnoreCase(extension) || ZIP_EXTENSION.equalsIgnoreCase(extension);
167 private boolean isPackageEmpty() {
168 return MapUtils.isEmpty(onboardPackageContentHandler.getFiles());
171 private boolean hasSignedPackageStructure() {
172 if (MapUtils.isEmpty(onboardPackageContentHandler.getFiles()) || !CollectionUtils.isEmpty(
173 onboardPackageContentHandler.getFolderList())) {
176 final int numberOfFiles = onboardPackageContentHandler.getFileList().size();
177 if (numberOfFiles == 2) {
178 return hasOneInternalPackageFile(onboardPackageContentHandler) &&
179 hasOneSignatureFile(onboardPackageContentHandler);
182 if (numberOfFiles == 3) {
183 return hasOneInternalPackageFile(onboardPackageContentHandler) &&
184 hasOneSignatureFile(onboardPackageContentHandler) &&
185 hasOneCertificateFile(onboardPackageContentHandler);
191 private boolean hasOneInternalPackageFile(final FileContentHandler fileContentHandler) {
192 return fileContentHandler.getFileList().parallelStream()
193 .map(FilenameUtils::getExtension)
194 .map(String::toLowerCase)
195 .filter(file -> file.endsWith(CSAR_EXTENSION)).count() == 1;
198 private boolean hasOneSignatureFile(final FileContentHandler fileContentHandler) {
199 return fileContentHandler.getFileList().parallelStream()
200 .map(FilenameUtils::getExtension)
201 .map(String::toLowerCase)
202 .filter(ALLOWED_SIGNATURE_EXTENSIONS::contains).count() == 1;
205 private boolean hasOneCertificateFile(final FileContentHandler fileContentHandler) {
206 return fileContentHandler.getFileList().parallelStream()
207 .map(FilenameUtils::getExtension)
208 .map(String::toLowerCase)
209 .filter(ALLOWED_CERTIFICATE_EXTENSIONS::contains).count() == 1;
212 private Optional<String> findSignatureFilePath() {
213 final Map<String, byte[]> files = onboardPackageContentHandler.getFiles();
214 return files.keySet().stream()
215 .filter(fileName -> ALLOWED_SIGNATURE_EXTENSIONS.contains(FilenameUtils.getExtension(fileName).toLowerCase()))
219 private Optional<String> findCertificateFilePath() {
220 final Map<String, byte[]> files = onboardPackageContentHandler.getFiles();
221 return files.keySet().stream()
222 .filter(fileName -> ALLOWED_CERTIFICATE_EXTENSIONS.contains(FilenameUtils.getExtension(fileName).toLowerCase()))
226 public Optional<OnboardPackageInfo> getOnboardPackageInfo() {
227 return Optional.ofNullable(onboardPackageInfo);