d3b51461c241d947176aa26d549f3e91ed76f70d
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6  * Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved.
7  * ======================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21
22 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
23
24 import com.google.common.base.Strings;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import lombok.Getter;
32 import lombok.Setter;
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
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     @Getter
104     @Setter
105     @Value("${app.authorization-provider:}")
106     private String authProviderUrl;
107
108     @Getter
109     @Setter
110     @Value("${app.database-enabled:}")
111     private boolean databaseEnabled;
112
113     private Map<String, RicConfig> ricConfigs = new HashMap<>();
114
115     private WebClientConfig webClientConfig = null;
116
117     public synchronized Collection<RicConfig> getRicConfigs() {
118         return this.ricConfigs.values();
119     }
120
121     public WebClientConfig getWebClientConfig() {
122         if (this.webClientConfig == null) {
123             HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
124                     .httpProxyHost(this.httpProxyHost) //
125                     .httpProxyPort(this.httpProxyPort) //
126                     .httpProxyType(ProxyProvider.Proxy.valueOf(this.httpProxyType)) //
127                     .build();
128
129             this.webClientConfig = WebClientConfig.builder() //
130                     .keyStoreType(this.sslKeyStoreType) //
131                     .keyStorePassword(this.sslKeyStorePassword) //
132                     .keyStore(this.sslKeyStore) //
133                     .keyPassword(this.sslKeyPassword) //
134                     .isTrustStoreUsed(this.sslTrustStoreUsed) //
135                     .trustStore(this.sslTrustStore) //
136                     .trustStorePassword(this.sslTrustStorePassword) //
137                     .httpProxyConfig(httpProxyConfig) //
138                     .build();
139         }
140         return this.webClientConfig;
141     }
142
143     public synchronized RicConfig getRic(String ricId) throws ServiceException {
144         RicConfig ricConfig = this.ricConfigs.get(ricId);
145         if (ricConfig == null) {
146             throw new ServiceException("Could not find ric configuration: " + ricId);
147         }
148         return ricConfig;
149     }
150
151     public static class RicConfigUpdate {
152         public enum Type {
153             ADDED, CHANGED, REMOVED
154         }
155
156         @Getter
157         private final RicConfig ricConfig;
158         @Getter
159         private final Type type;
160
161         public RicConfigUpdate(RicConfig config, Type event) {
162             this.ricConfig = config;
163             this.type = event;
164         }
165     }
166
167     public synchronized Flux<RicConfigUpdate> setConfiguration(
168             ApplicationConfigParser.ConfigParserResult parserResult) {
169
170         Collection<RicConfigUpdate> modifications = new ArrayList<>();
171
172         Map<String, RicConfig> newRicConfigs = new HashMap<>();
173         for (RicConfig newConfig : parserResult.getRicConfigs()) {
174             RicConfig oldConfig = this.ricConfigs.get(newConfig.getRicId());
175             this.ricConfigs.remove(newConfig.getRicId());
176             if (oldConfig == null) {
177                 newRicConfigs.put(newConfig.getRicId(), newConfig);
178                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
179             } else if (!newConfig.equals(oldConfig)) {
180                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
181                 newRicConfigs.put(newConfig.getRicId(), newConfig);
182             } else {
183                 newRicConfigs.put(oldConfig.getRicId(), oldConfig);
184             }
185         }
186         for (RicConfig deletedConfig : this.ricConfigs.values()) {
187             modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
188         }
189         this.ricConfigs = newRicConfigs;
190
191         return Flux.fromIterable(modifications);
192     }
193
194     public boolean isS3Enabled() {
195         return !(Strings.isNullOrEmpty(s3EndpointOverride) || Strings.isNullOrEmpty(s3Bucket));
196     }
197
198     public boolean isDatabaseEnabled() {
199         return databaseEnabled;
200     }
201 }