796d6ad94f5a58d548b4b85de803ce529291e847
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
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
21 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
22
23 import com.google.common.base.Strings;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import lombok.Getter;
31
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;
36
37 import reactor.core.publisher.Flux;
38 import reactor.netty.transport.ProxyProvider;
39
40 @EnableConfigurationProperties
41 public class ApplicationConfig {
42
43     @Getter
44     @Value("${app.filepath}")
45     private String localConfigurationFilePath;
46
47     @Getter
48     @Value("${app.config-file-schema-path:}")
49     private String configurationFileSchemaPath;
50
51     @Getter
52     @Value("${app.vardata-directory:null}")
53     private String vardataDirectory;
54
55     @Value("${server.ssl.key-store-type}")
56     private String sslKeyStoreType = "";
57
58     @Value("${server.ssl.key-store-password}")
59     private String sslKeyStorePassword = "";
60
61     @Value("${server.ssl.key-store}")
62     private String sslKeyStore = "";
63
64     @Value("${server.ssl.key-password}")
65     private String sslKeyPassword = "";
66
67     @Value("${app.webclient.trust-store-used}")
68     private boolean sslTrustStoreUsed = false;
69
70     @Value("${app.webclient.trust-store-password}")
71     private String sslTrustStorePassword = "";
72
73     @Value("${app.webclient.trust-store}")
74     private String sslTrustStore = "";
75
76     @Value("${app.webclient.http.proxy-host:}")
77     private String httpProxyHost = "";
78
79     @Value("${app.webclient.http.proxy-port:0}")
80     private int httpProxyPort = 0;
81
82     @Value("${app.webclient.http.proxy-type:HTTP}")
83     private String httpProxyType = "HTTP";
84
85     @Getter
86     @Value("${app.s3.endpointOverride:}")
87     private String s3EndpointOverride;
88
89     @Getter
90     @Value("${app.s3.accessKeyId:}")
91     private String s3AccessKeyId;
92
93     @Getter
94     @Value("${app.s3.secretAccessKey:}")
95     private String s3SecretAccessKey;
96
97     @Getter
98     @Value("${app.s3.bucket:}")
99     private String s3Bucket;
100
101     private Map<String, RicConfig> ricConfigs = new HashMap<>();
102
103     @Getter
104     private String dmaapConsumerTopicUrl;
105
106     @Getter
107     private String dmaapProducerTopicUrl;
108
109     private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
110
111     private WebClientConfig webClientConfig = null;
112
113     public synchronized Collection<RicConfig> getRicConfigs() {
114         return this.ricConfigs.values();
115     }
116
117     public WebClientConfig getWebClientConfig() {
118         if (this.webClientConfig == null) {
119             HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
120                     .httpProxyHost(this.httpProxyHost) //
121                     .httpProxyPort(this.httpProxyPort) //
122                     .httpProxyType(ProxyProvider.Proxy.valueOf(this.httpProxyType)) //
123                     .build();
124
125             this.webClientConfig = WebClientConfig.builder() //
126                     .keyStoreType(this.sslKeyStoreType) //
127                     .keyStorePassword(this.sslKeyStorePassword) //
128                     .keyStore(this.sslKeyStore) //
129                     .keyPassword(this.sslKeyPassword) //
130                     .isTrustStoreUsed(this.sslTrustStoreUsed) //
131                     .trustStore(this.sslTrustStore) //
132                     .trustStorePassword(this.sslTrustStorePassword) //
133                     .httpProxyConfig(httpProxyConfig) //
134                     .build();
135         }
136         return this.webClientConfig;
137     }
138
139     public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException {
140         ControllerConfig controllerConfig = this.controllerConfigs.get(name);
141         if (controllerConfig == null) {
142             throw new ServiceException("Could not find controller config: " + name);
143         }
144         return controllerConfig;
145     }
146
147     public synchronized RicConfig getRic(String ricId) throws ServiceException {
148         RicConfig ricConfig = this.ricConfigs.get(ricId);
149         if (ricConfig == null) {
150             throw new ServiceException("Could not find ric configuration: " + ricId);
151         }
152         return ricConfig;
153     }
154
155     public static class RicConfigUpdate {
156         public enum Type {
157             ADDED, CHANGED, REMOVED
158         }
159
160         @Getter
161         private final RicConfig ricConfig;
162         @Getter
163         private final Type type;
164
165         public RicConfigUpdate(RicConfig config, Type event) {
166             this.ricConfig = config;
167             this.type = event;
168         }
169     }
170
171     public synchronized Flux<RicConfigUpdate> setConfiguration(
172             ApplicationConfigParser.ConfigParserResult parserResult) {
173
174         Collection<RicConfigUpdate> modifications = new ArrayList<>();
175         this.controllerConfigs = parserResult.getControllerConfigs();
176
177         this.dmaapConsumerTopicUrl = parserResult.getDmaapConsumerTopicUrl();
178         this.dmaapProducerTopicUrl = parserResult.getDmaapProducerTopicUrl();
179
180         Map<String, RicConfig> newRicConfigs = new HashMap<>();
181         for (RicConfig newConfig : parserResult.getRicConfigs()) {
182             RicConfig oldConfig = this.ricConfigs.get(newConfig.getRicId());
183             this.ricConfigs.remove(newConfig.getRicId());
184             if (oldConfig == null) {
185                 newRicConfigs.put(newConfig.getRicId(), newConfig);
186                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
187             } else if (!newConfig.equals(oldConfig)) {
188                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
189                 newRicConfigs.put(newConfig.getRicId(), newConfig);
190             } else {
191                 newRicConfigs.put(oldConfig.getRicId(), oldConfig);
192             }
193         }
194         for (RicConfig deletedConfig : this.ricConfigs.values()) {
195             modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
196         }
197         this.ricConfigs = newRicConfigs;
198
199         return Flux.fromIterable(modifications);
200     }
201
202     public boolean isS3Enabled() {
203         return !(Strings.isNullOrEmpty(s3EndpointOverride) || Strings.isNullOrEmpty(s3Bucket));
204     }
205
206 }