2  * ========================LICENSE_START=================================
 
   4  * ======================================================================
 
   5  * Copyright (C) 2019-2020 Nordix Foundation. 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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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===================================
 
  21 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
 
  23 import com.google.common.base.Strings;
 
  25 import java.util.ArrayList;
 
  26 import java.util.Collection;
 
  27 import java.util.HashMap;
 
  32 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig.HttpProxyConfig;
 
  33 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
 
  34 import org.springframework.beans.factory.annotation.Value;
 
  35 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 
  37 import reactor.core.publisher.Flux;
 
  38 import reactor.netty.transport.ProxyProvider;
 
  40 @EnableConfigurationProperties
 
  41 public class ApplicationConfig {
 
  44     @Value("${app.filepath}")
 
  45     private String localConfigurationFilePath;
 
  48     @Value("${app.config-file-schema-path:}")
 
  49     private String configurationFileSchemaPath;
 
  52     @Value("${app.vardata-directory:null}")
 
  53     private String vardataDirectory;
 
  55     @Value("${server.ssl.key-store-type}")
 
  56     private String sslKeyStoreType = "";
 
  58     @Value("${server.ssl.key-store-password}")
 
  59     private String sslKeyStorePassword = "";
 
  61     @Value("${server.ssl.key-store}")
 
  62     private String sslKeyStore = "";
 
  64     @Value("${server.ssl.key-password}")
 
  65     private String sslKeyPassword = "";
 
  67     @Value("${app.webclient.trust-store-used}")
 
  68     private boolean sslTrustStoreUsed = false;
 
  70     @Value("${app.webclient.trust-store-password}")
 
  71     private String sslTrustStorePassword = "";
 
  73     @Value("${app.webclient.trust-store}")
 
  74     private String sslTrustStore = "";
 
  76     @Value("${app.webclient.http.proxy-host:}")
 
  77     private String httpProxyHost = "";
 
  79     @Value("${app.webclient.http.proxy-port:0}")
 
  80     private int httpProxyPort = 0;
 
  82     @Value("${app.webclient.http.proxy-type:HTTP}")
 
  83     private String httpProxyType = "HTTP";
 
  86     @Value("${app.s3.endpointOverride:}")
 
  87     private String s3EndpointOverride;
 
  90     @Value("${app.s3.accessKeyId:}")
 
  91     private String s3AccessKeyId;
 
  94     @Value("${app.s3.secretAccessKey:}")
 
  95     private String s3SecretAccessKey;
 
  98     @Value("${app.s3.bucket:}")
 
  99     private String s3Bucket;
 
 101     private Map<String, RicConfig> ricConfigs = new HashMap<>();
 
 103     private WebClientConfig webClientConfig = null;
 
 105     public synchronized Collection<RicConfig> getRicConfigs() {
 
 106         return this.ricConfigs.values();
 
 109     public WebClientConfig getWebClientConfig() {
 
 110         if (this.webClientConfig == null) {
 
 111             HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
 
 112                     .httpProxyHost(this.httpProxyHost) //
 
 113                     .httpProxyPort(this.httpProxyPort) //
 
 114                     .httpProxyType(ProxyProvider.Proxy.valueOf(this.httpProxyType)) //
 
 117             this.webClientConfig = WebClientConfig.builder() //
 
 118                     .keyStoreType(this.sslKeyStoreType) //
 
 119                     .keyStorePassword(this.sslKeyStorePassword) //
 
 120                     .keyStore(this.sslKeyStore) //
 
 121                     .keyPassword(this.sslKeyPassword) //
 
 122                     .isTrustStoreUsed(this.sslTrustStoreUsed) //
 
 123                     .trustStore(this.sslTrustStore) //
 
 124                     .trustStorePassword(this.sslTrustStorePassword) //
 
 125                     .httpProxyConfig(httpProxyConfig) //
 
 128         return this.webClientConfig;
 
 131     public synchronized RicConfig getRic(String ricId) throws ServiceException {
 
 132         RicConfig ricConfig = this.ricConfigs.get(ricId);
 
 133         if (ricConfig == null) {
 
 134             throw new ServiceException("Could not find ric configuration: " + ricId);
 
 139     public static class RicConfigUpdate {
 
 141             ADDED, CHANGED, REMOVED
 
 145         private final RicConfig ricConfig;
 
 147         private final Type type;
 
 149         public RicConfigUpdate(RicConfig config, Type event) {
 
 150             this.ricConfig = config;
 
 155     public synchronized Flux<RicConfigUpdate> setConfiguration(
 
 156             ApplicationConfigParser.ConfigParserResult parserResult) {
 
 158         Collection<RicConfigUpdate> modifications = new ArrayList<>();
 
 160         Map<String, RicConfig> newRicConfigs = new HashMap<>();
 
 161         for (RicConfig newConfig : parserResult.getRicConfigs()) {
 
 162             RicConfig oldConfig = this.ricConfigs.get(newConfig.getRicId());
 
 163             this.ricConfigs.remove(newConfig.getRicId());
 
 164             if (oldConfig == null) {
 
 165                 newRicConfigs.put(newConfig.getRicId(), newConfig);
 
 166                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
 
 167             } else if (!newConfig.equals(oldConfig)) {
 
 168                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
 
 169                 newRicConfigs.put(newConfig.getRicId(), newConfig);
 
 171                 newRicConfigs.put(oldConfig.getRicId(), oldConfig);
 
 174         for (RicConfig deletedConfig : this.ricConfigs.values()) {
 
 175             modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
 
 177         this.ricConfigs = newRicConfigs;
 
 179         return Flux.fromIterable(modifications);
 
 182     public boolean isS3Enabled() {
 
 183         return !(Strings.isNullOrEmpty(s3EndpointOverride) || Strings.isNullOrEmpty(s3Bucket));