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.truststoremerger.configuration;
 
  23 import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.KEYSTORE_DESTINATION_PATHS_ENV;
 
  24 import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.KEYSTORE_SOURCE_PATHS_ENV;
 
  25 import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS_ENV;
 
  26 import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PATHS_ENV;
 
  28 import java.util.Collections;
 
  29 import java.util.List;
 
  30 import java.util.Optional;
 
  31 import org.onap.oom.truststoremerger.configuration.exception.CertificatesPathsValidationException;
 
  32 import org.onap.oom.truststoremerger.configuration.exception.ConfigurationException;
 
  33 import org.onap.oom.truststoremerger.configuration.model.AppConfiguration;
 
  34 import org.onap.oom.truststoremerger.configuration.model.EnvVariable;
 
  35 import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsSplitter;
 
  36 import org.onap.oom.truststoremerger.configuration.path.env.EnvReader;
 
  38 public class AppConfigurationProvider {
 
  40     private final EnvReader envReader;
 
  41     private final DelimitedPathsSplitter pathsSplitter;
 
  43     public AppConfigurationProvider(DelimitedPathsSplitter pathsSplitter, EnvReader envReader) {
 
  44         this.envReader = envReader;
 
  45         this.pathsSplitter = pathsSplitter;
 
  48     public AppConfiguration createConfiguration()
 
  49         throws ConfigurationException, CertificatesPathsValidationException {
 
  51         List<String> truststoresPaths = getPaths(TRUSTSTORES_PATHS_ENV);
 
  52         List<String> truststoresPasswordsPaths = getPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV);
 
  53         List<String> sourceKeystorePaths = getPaths(KEYSTORE_SOURCE_PATHS_ENV);
 
  54         List<String> destinationKeystorePaths = getPaths(KEYSTORE_DESTINATION_PATHS_ENV);
 
  56         ensureSameSize(truststoresPaths, truststoresPasswordsPaths, TRUSTSTORES_PATHS_ENV.name(),
 
  57             TRUSTSTORES_PASSWORDS_PATHS_ENV.name());
 
  58         ensureSameSize(sourceKeystorePaths, destinationKeystorePaths, KEYSTORE_SOURCE_PATHS_ENV.name(),
 
  59             KEYSTORE_DESTINATION_PATHS_ENV.name());
 
  61         return new AppConfiguration(truststoresPaths, truststoresPasswordsPaths, sourceKeystorePaths,
 
  62             destinationKeystorePaths);
 
  65     private List<String> getPaths(EnvVariable envVariable) throws ConfigurationException {
 
  66         Optional<String> envValue = envReader.getEnv(envVariable.name());
 
  67         isMandatoryEnvPresent(envVariable, envValue);
 
  68         return envValue.isPresent() ? pathsSplitter.getValidatedPaths(envVariable, envValue) : Collections.emptyList();
 
  71     private void isMandatoryEnvPresent(EnvVariable envVariable, Optional<String> envValue) {
 
  72         if (envVariable.isMandatory() && envValue.isEmpty()) {
 
  73             throw new ConfigurationException(envVariable + " mandatory environment variable is not defined");
 
  77     private void ensureSameSize(List<String> firstList, List<String> secondList, String firstListEnvName,
 
  78         String secondListEnvName) throws ConfigurationException {
 
  79         if (firstList.size() != secondList.size()) {
 
  80             throw new ConfigurationException(
 
  81                 "Size of " + firstListEnvName
 
  82                     + " does not match size of " + secondListEnvName + " environment variables");