1 /*============LICENSE_START=======================================================
2 * oom-truststore-merger
3 * ================================================================================
4 * Copyright (C) 2020 Nokia. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.oom.certservice.postprocessor.configuration;
23 import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.KEYSTORE_DESTINATION_PATHS;
24 import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.KEYSTORE_SOURCE_PATHS;
25 import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS;
26 import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.TRUSTSTORES_PATHS;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Optional;
31 import org.onap.oom.certservice.postprocessor.configuration.path.env.EnvReader;
32 import org.onap.oom.certservice.postprocessor.configuration.exception.ConfigurationException;
33 import org.onap.oom.certservice.postprocessor.configuration.model.AppConfiguration;
34 import org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable;
35 import org.onap.oom.certservice.postprocessor.configuration.path.DelimitedPathsSplitter;
37 public class AppConfigurationProvider {
39 private final EnvReader envReader;
40 private final DelimitedPathsSplitter pathsSplitter;
42 public AppConfigurationProvider(DelimitedPathsSplitter pathsSplitter, EnvReader envReader) {
43 this.envReader = envReader;
44 this.pathsSplitter = pathsSplitter;
47 public AppConfiguration createConfiguration() {
48 List<String> truststoresPaths = getPaths(TRUSTSTORES_PATHS);
49 List<String> truststoresPasswordsPaths = getPaths(TRUSTSTORES_PASSWORDS_PATHS);
50 List<String> sourceKeystorePaths = getPaths(KEYSTORE_SOURCE_PATHS);
51 List<String> destinationKeystorePaths = getPaths(KEYSTORE_DESTINATION_PATHS);
53 ensureSameSize(truststoresPaths, truststoresPasswordsPaths, TRUSTSTORES_PATHS.name(),
54 TRUSTSTORES_PASSWORDS_PATHS.name());
55 ensureSameSize(sourceKeystorePaths, destinationKeystorePaths, KEYSTORE_SOURCE_PATHS.name(),
56 KEYSTORE_DESTINATION_PATHS.name());
58 return new AppConfiguration(truststoresPaths, truststoresPasswordsPaths, sourceKeystorePaths,
59 destinationKeystorePaths);
62 private List<String> getPaths(EnvVariable envVariable) {
63 Optional<String> envValue = envReader.getEnv(envVariable.name());
64 isMandatoryEnvPresent(envVariable, envValue);
65 return envValue.isPresent() ? pathsSplitter.getValidatedPaths(envVariable, envValue) : Collections.emptyList();
68 private void isMandatoryEnvPresent(EnvVariable envVariable, Optional<String> envValue) {
69 if (envVariable.isMandatory() && envValue.isEmpty()) {
70 throw new ConfigurationException(envVariable + " mandatory environment variable is not defined");
74 private void ensureSameSize(List<String> firstList, List<String> secondList, String firstListEnvName,
75 String secondListEnvName) {
76 if (firstList.size() != secondList.size()) {
77 throw new ConfigurationException(
78 "Size of " + firstListEnvName
79 + " does not match size of " + secondListEnvName + " environment variables");