aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessor.java
blob: d1b64e213eaec8add334bde432eb6c117225bd71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation
 *  ================================================================================
 *  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
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding;

import static org.openecomp.sdc.common.errors.Messages.PACKAGE_EMPTY_ERROR;
import static org.openecomp.sdc.common.errors.Messages.PACKAGE_INVALID_ERROR;
import static org.openecomp.sdc.common.errors.Messages.PACKAGE_INVALID_EXTENSION;
import static org.openecomp.sdc.common.errors.Messages.PACKAGE_MISSING_INTERNAL_PACKAGE;
import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_ERROR;
import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_INTERNAL_PACKAGE_ERROR;
import static org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager.ALLOWED_CERTIFICATE_EXTENSIONS;
import static org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager.ALLOWED_SIGNATURE_EXTENSIONS;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.FilenameUtils;
import org.openecomp.core.utilities.file.FileContentHandler;
import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
import org.openecomp.sdc.common.utils.SdcCommon;
import org.openecomp.sdc.common.zip.exception.ZipException;
import org.openecomp.sdc.common.utils.CommonUtil;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.datatypes.error.ErrorMessage;
import org.openecomp.sdc.heat.datatypes.manifest.FileData;
import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardSignedPackage;

public class OnboardingPackageProcessor {
    private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingPackageProcessor.class);
    private static final String CSAR_EXTENSION = "csar";
    private static final String ZIP_EXTENSION = "zip";
    private static  boolean helmBase = false;

    private final String packageFileName;
    private final byte[] packageFileContent;
    private FileContentHandler onboardPackageContentHandler;
    private Set<ErrorMessage> errorMessageSet = new HashSet<>();
    private OnboardPackageInfo onboardPackageInfo;

    public OnboardingPackageProcessor(final String packageFileName, final byte[] packageFileContent) {
        this.packageFileName = packageFileName;
        this.packageFileContent = packageFileContent;
        onboardPackageInfo = processPackage();
    }

    private OnboardPackageInfo processPackage() {
        if (!hasValidExtension()) {
            final String message = PACKAGE_INVALID_EXTENSION.formatMessage(packageFileName, String.join(", ", CSAR_EXTENSION, ZIP_EXTENSION));
            reportError(ErrorLevel.ERROR, message);
            return null;
        }
        try {
            onboardPackageContentHandler = CommonUtil.getZipContent(packageFileContent);
        } catch (final ZipException e) {
            final String message = PACKAGE_PROCESS_ERROR.formatMessage(packageFileName);
            LOGGER.error(message, e);
            reportError(ErrorLevel.ERROR, message);
            return null;
        }
        if (isPackageEmpty()) {
            final String message = PACKAGE_EMPTY_ERROR.formatMessage(packageFileName);
            reportError(ErrorLevel.ERROR, message);
            return null;
        }

        final String packageName = FilenameUtils.getBaseName(packageFileName);
        final String packageExtension = FilenameUtils.getExtension(packageFileName);

        if (hasSignedPackageStructure()) {
            return processSignedPackage(packageName, packageExtension);
        } else {
            if (packageExtension.equalsIgnoreCase(CSAR_EXTENSION)) {
                final OnboardPackage onboardPackage = new OnboardPackage(packageName, packageExtension,
                    ByteBuffer.wrap(packageFileContent), new OnboardingPackageContentHandler(onboardPackageContentHandler));
                return new OnboardPackageInfo(onboardPackage, OnboardingTypesEnum.CSAR);
            } else if (packageExtension.equalsIgnoreCase(ZIP_EXTENSION)) {
                addDummyHeat();
                final OnboardPackage onboardPackage = new OnboardPackage(packageName, packageExtension,
                    ByteBuffer.wrap(packageFileContent), onboardPackageContentHandler);
                return new OnboardPackageInfo(onboardPackage, OnboardingTypesEnum.ZIP);
            }
        }

        reportError(ErrorLevel.ERROR, PACKAGE_INVALID_ERROR.formatMessage(packageFileName));
        return null;
    }

    private void addDummyHeat() {
        // temporary fix for adding dummy base
        List<FileData> newfiledata = new ArrayList<>();
        try (InputStream zipFileManifest = onboardPackageContentHandler.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
            ManifestContent manifestContent =
                    JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
            for (FileData fileData : manifestContent.getData()) {
                if (Objects.nonNull(fileData.getType()) &&
                        fileData.getType().equals(FileData.Type.HELM) && fileData.getBase()) {
                    helmBase = true;
                    fileData.setBase(false);
                    FileData dummyHeat = new FileData();
                    dummyHeat.setBase(true);
                    dummyHeat.setFile("base_template_dummy_ignore.yaml");
                    dummyHeat.setType(FileData.Type.HEAT);
                    FileData dummyEnv = new FileData();
                    dummyEnv.setBase(false);
                    dummyEnv.setFile("base_template_dummy_ignore.env");
                    dummyEnv.setType(FileData.Type.HEAT_ENV);
                    List<FileData> dataEnvList = new ArrayList<>();
                    dataEnvList.add(dummyEnv);
                    dummyHeat.setData(dataEnvList);
                    newfiledata.add(dummyHeat);
                    String filePath = new File("").getAbsolutePath();
                    File envFilePath = new File(filePath + "/base_template.env");
                    File baseFilePath = new File(filePath + "/base_template.yaml");
                    try (
                            InputStream envStream = new FileInputStream(envFilePath);
                            InputStream baseStream = new FileInputStream(baseFilePath);) {
                        onboardPackageContentHandler.addFile("base_template_dummy_ignore.env", envStream);
                        onboardPackageContentHandler.addFile("base_template_dummy_ignore.yaml", baseStream);
                    } catch (Exception e) {
                        LOGGER.error("Failed creating input stream {}", e);
                    }
                }
            }
            if (helmBase) {
                manifestContent.getData().addAll(newfiledata);
                InputStream manifestContentStream = new ByteArrayInputStream((JsonUtil.object2Json(manifestContent)).getBytes(StandardCharsets.UTF_8));
                onboardPackageContentHandler.remove(SdcCommon.MANIFEST_NAME);
                onboardPackageContentHandler.addFile(SdcCommon.MANIFEST_NAME, manifestContentStream);
            }
        } catch (Exception e) {
            final String message = PACKAGE_INVALID_ERROR.formatMessage(packageFileName);
            LOGGER.error(message, e);
        }
    }
    private boolean hasValidExtension() {
        final String packageExtension = FilenameUtils.getExtension(packageFileName);
        return packageExtension.equalsIgnoreCase(CSAR_EXTENSION) || packageExtension.equalsIgnoreCase(ZIP_EXTENSION);
    }

    private OnboardPackageInfo processSignedPackage(final String packageName, final String packageExtension) {
        final String internalPackagePath = findInternalPackagePath().orElse(null);
        if (internalPackagePath == null) {
            reportError(ErrorLevel.ERROR, PACKAGE_MISSING_INTERNAL_PACKAGE.getErrorMessage());
            return null;
        }
        final String signatureFilePath = findSignatureFilePath().orElse(null);
        final String certificateFilePath = findCertificateFilePath().orElse(null);
        final OnboardSignedPackage onboardSignedPackage =
            new OnboardSignedPackage(packageName, packageExtension, ByteBuffer.wrap(packageFileContent),
                onboardPackageContentHandler, signatureFilePath, internalPackagePath, certificateFilePath);

        final String internalPackageName = FilenameUtils.getName(internalPackagePath);
        final String internalPackageBaseName = FilenameUtils.getBaseName(internalPackagePath);
        final String internalPackageExtension = FilenameUtils.getExtension(internalPackagePath);
        final byte[] internalPackageContent = onboardPackageContentHandler.getFileContent(internalPackagePath);
        final OnboardPackage onboardPackage;
        try {
            final OnboardingPackageContentHandler fileContentHandler =
                new OnboardingPackageContentHandler(CommonUtil.getZipContent(internalPackageContent));
            onboardPackage = new OnboardPackage(internalPackageBaseName, internalPackageExtension,
                internalPackageContent, fileContentHandler);
        } catch (final ZipException e) {
            final String message = PACKAGE_PROCESS_INTERNAL_PACKAGE_ERROR.formatMessage(internalPackageName);
            LOGGER.error(message, e);
            reportError(ErrorLevel.ERROR, message);
            return null;
        }

        return new OnboardPackageInfo(onboardSignedPackage, onboardPackage, OnboardingTypesEnum.SIGNED_CSAR);
    }

    private void reportError(final ErrorLevel errorLevel, final String message) {
        errorMessageSet.add(new ErrorMessage(errorLevel, message));
    }

    public boolean hasErrors() {
        return !errorMessageSet.isEmpty();
    }

    public Set<ErrorMessage> getErrorMessageSet() {
        return errorMessageSet;
    }

    private Optional<String> findInternalPackagePath() {
        return onboardPackageContentHandler.getFileList().stream()
            .filter(filePath -> {
                    final String extension = FilenameUtils.getExtension(filePath);
                    return CSAR_EXTENSION.equalsIgnoreCase(extension) || ZIP_EXTENSION.equalsIgnoreCase(extension);
                }
            )
            .findFirst();
    }

    private boolean isPackageEmpty() {
        return MapUtils.isEmpty(onboardPackageContentHandler.getFiles());
    }

    private boolean hasSignedPackageStructure() {
        if (MapUtils.isEmpty(onboardPackageContentHandler.getFiles()) || !CollectionUtils.isEmpty(
            onboardPackageContentHandler.getFolderList())) {
            return false;
        }
        final int numberOfFiles = onboardPackageContentHandler.getFileList().size();
        if (numberOfFiles == 2) {
            return hasOneInternalPackageFile(onboardPackageContentHandler) &&
                hasOneSignatureFile(onboardPackageContentHandler);
        }

        if (numberOfFiles == 3) {
            return hasOneInternalPackageFile(onboardPackageContentHandler) &&
                hasOneSignatureFile(onboardPackageContentHandler) &&
                hasOneCertificateFile(onboardPackageContentHandler);
        }

        return false;
    }

    private boolean hasOneInternalPackageFile(final FileContentHandler fileContentHandler) {
        return fileContentHandler.getFileList().parallelStream()
            .map(FilenameUtils::getExtension)
            .map(String::toLowerCase)
            .filter(file -> file.endsWith(CSAR_EXTENSION)).count() == 1;
    }

    private boolean hasOneSignatureFile(final FileContentHandler fileContentHandler) {
        return fileContentHandler.getFileList().parallelStream()
            .map(FilenameUtils::getExtension)
            .map(String::toLowerCase)
            .filter(ALLOWED_SIGNATURE_EXTENSIONS::contains).count() == 1;
    }

    private boolean hasOneCertificateFile(final FileContentHandler fileContentHandler) {
        return fileContentHandler.getFileList().parallelStream()
            .map(FilenameUtils::getExtension)
            .map(String::toLowerCase)
            .filter(ALLOWED_CERTIFICATE_EXTENSIONS::contains).count() == 1;
    }

    private Optional<String> findSignatureFilePath() {
        final Map<String, byte[]> files = onboardPackageContentHandler.getFiles();
        return files.keySet().stream()
            .filter(fileName -> ALLOWED_SIGNATURE_EXTENSIONS.contains(FilenameUtils.getExtension(fileName).toLowerCase()))
            .findFirst();
    }

    private Optional<String> findCertificateFilePath() {
        final Map<String, byte[]> files = onboardPackageContentHandler.getFiles();
        return files.keySet().stream()
            .filter(fileName -> ALLOWED_CERTIFICATE_EXTENSIONS.contains(FilenameUtils.getExtension(fileName).toLowerCase()))
            .findFirst();
    }

    public Optional<OnboardPackageInfo> getOnboardPackageInfo() {
        return Optional.ofNullable(onboardPackageInfo);
    }
}