Reformat openecomp-be
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / validation / utils / FileExtractor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.utils;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.stream.Stream;
26 import org.openecomp.sdc.be.config.NonManoArtifactType;
27 import org.openecomp.sdc.tosca.csar.Manifest;
28 import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding;
29 import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.OnboardingPackageContentHandler;
30
31 public class FileExtractor {
32
33     private final InternalFilesFilter internalFilesFilter;
34     private final String etsiEntryManifestFilePath;
35     private final OnboardingPackageContentHandler contentHandler;
36
37     public FileExtractor(String etsiEntryManifestPath, OnboardingPackageContentHandler contentHandler) {
38         this(etsiEntryManifestPath, contentHandler, new InternalFilesFilter());
39     }
40
41     FileExtractor(String etsiEntryManifestPath, OnboardingPackageContentHandler contentHandler, InternalFilesFilter internalFilesFilter) {
42         this.etsiEntryManifestFilePath = etsiEntryManifestPath;
43         this.contentHandler = contentHandler;
44         this.internalFilesFilter = internalFilesFilter;
45     }
46
47     public Stream<byte[]> findFiles(NonManoArtifactType fileType) {
48         Map<String, List<String>> nonManoSources = extractNonManoSources();
49         List<String> pathsToSources = nonManoSources.getOrDefault(fileType.getType(), new ArrayList<>());
50         List<String> pathsToLocalFiles = internalFilesFilter.filter(pathsToSources);
51         return pathsToLocalFiles.stream().map(contentHandler::getFileContent);
52     }
53
54     private Map<String, List<String>> extractNonManoSources() {
55         Manifest onboardingManifest = new SOL004ManifestOnboarding();
56         onboardingManifest.parse(contentHandler.getFileContentAsStream(etsiEntryManifestFilePath));
57         return onboardingManifest.getNonManoSources();
58     }
59 }