Implement improved MinIo client
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / csar / storage / StorageFactory.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.csar.storage;
22
23 import static org.openecomp.sdc.be.csar.storage.StorageFactory.StorageType.NONE;
24 import static org.openecomp.sdc.be.csar.storage.StorageFactory.StorageType.findByName;
25
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.Set;
31 import java.util.stream.Collectors;
32 import lombok.NoArgsConstructor;
33 import org.openecomp.sdc.common.CommonConfigurationManager;
34 import org.openecomp.sdc.logging.api.Logger;
35 import org.openecomp.sdc.logging.api.LoggerFactory;
36
37 @NoArgsConstructor
38 public class StorageFactory {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(StorageFactory.class);
41     private static final String EXTERNAL_CSAR_STORE = "externalCsarStore";
42
43     public ArtifactStorageManager createArtifactStorageManager() {
44         switch (getConfiguredArtifactStorageType()) {
45             case MINIO: //  MinIoStorage enabled
46                 return new MinIoStorageArtifactStorageManager();
47             default://  all configured, nothing enabled
48                 return new NoneStorageManager();
49         }
50     }
51
52     public Optional<PackageSizeReducer> createPackageSizeReducer() {
53         switch (getConfiguredArtifactStorageType()) {
54             case MINIO: //  MinIoStorage enabled
55                 return Optional.of(new MinIoStorageCsarSizeReducer(readPackageReducerConfiguration()));
56             default://  all configured, nothing enabled
57                 return Optional.empty();
58         }
59     }
60
61     private StorageType getConfiguredArtifactStorageType() {
62         final var commonConfigurationManager = CommonConfigurationManager.getInstance();
63         commonConfigurationManager.reload();
64         final String storageType = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, "storageType", NONE.name());
65         LOGGER.info("ArtifactConfig.storageType: '{}'", storageType);
66         return findByName(storageType);
67     }
68
69     private CsarPackageReducerConfiguration readPackageReducerConfiguration() {
70         final var commonConfigurationManager = CommonConfigurationManager.getInstance();
71         final List<String> foldersToStrip = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, "foldersToStrip", new ArrayList<>());
72         final int sizeLimit = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, "sizeLimit", 1000000);
73         final int thresholdEntries = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, "thresholdEntries", 10000);
74         LOGGER.info("Folders to strip: '{}'", String.join(", ", foldersToStrip));
75         final Set<Path> foldersToStripPathSet = foldersToStrip.stream().map(Path::of).collect(Collectors.toSet());
76         return new CsarPackageReducerConfiguration(foldersToStripPathSet, sizeLimit, thresholdEntries);
77     }
78
79     public enum StorageType {
80         NONE,
81         MINIO;
82
83         public static StorageType findByName(String name) {
84             for (StorageType curr : StorageType.values()) {
85                 if (curr.name().equals(name)) {
86                     return curr;
87                 }
88             }
89             return NONE;
90         }
91     }
92 }