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