360369c07c30193df397aee4cb26c7afb349a2bb
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6  * Modifications Copyright (C) 2023-2025 OpenInfra Foundation Europe.
7  * All rights reserved.
8  * ======================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ========================LICENSE_END===================================
21  */
22
23 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
24
25 import com.google.common.base.Strings;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Map;
30 import lombok.Getter;
31 import lombok.Setter;
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 import reactor.core.publisher.Flux;
37 import reactor.netty.transport.ProxyProvider;
38
39 @EnableConfigurationProperties
40 public class ApplicationConfig {
41
42     @Getter
43     @Value("${app.filepath:null}")
44     private String localConfigurationFilePath;
45
46     @Getter
47     @Value("${app.config-file-schema-path:null}")
48     private String configurationFileSchemaPath;
49
50     @Getter
51     @Value("${app.vardata-directory:null}")
52     private String vardataDirectory;
53
54     @Getter
55     @Value("${server.ssl.enabled:true}")
56     private boolean sslEnabled;
57
58     @Value("${server.ssl.key-store-type:null}")
59     private String sslKeyStoreType = "";
60
61     @Value("${server.ssl.key-store-password:null}")
62     private String sslKeyStorePassword = "";
63
64     @Value("${server.ssl.key-store:null}")
65     private String sslKeyStore = "";
66
67     @Value("${server.ssl.key-password:null}")
68     private String sslKeyPassword = "";
69
70     @Value("${app.webclient.trust-store-used:false}")
71     private boolean sslTrustStoreUsed = false;
72
73     @Value("${app.webclient.trust-store-password:null}")
74     private String sslTrustStorePassword = "";
75
76     @Value("${app.webclient.trust-store:null}")
77     private String sslTrustStore = "";
78
79     @Value("${app.webclient.http.proxy-host:}")
80     private String httpProxyHost = "";
81
82     @Value("${app.webclient.http.proxy-port:0}")
83     private int httpProxyPort = 0;
84
85     @Value("${app.webclient.http.proxy-type:HTTP}")
86     private String httpProxyType = "HTTP";
87
88     @Getter
89     @Value("${app.s3.endpointOverride:}")
90     private String s3EndpointOverride;
91
92     @Getter
93     @Value("${app.s3.accessKeyId:}")
94     private String s3AccessKeyId;
95
96     @Getter
97     @Value("${app.s3.secretAccessKey:}")
98     private String s3SecretAccessKey;
99
100     @Getter
101     @Value("${app.s3.bucket:}")
102     private String s3Bucket;
103
104     @Getter
105     @Setter
106     @Value("${app.authorization-provider:}")
107     private String authProviderUrl;
108
109     @Getter
110     @Setter
111     @Value("${app.database-enabled:}")
112     private boolean databaseEnabled;
113
114     public enum ValidateSchema {
115         NONE,
116         INFO,
117         WARN,
118         FAIL
119     }
120
121     @Getter
122     @Setter
123     @Value("${app.validate-policy-instance-schema:NONE}")
124     private ValidateSchema validatePolicyInstanceSchema;
125
126     private Map<String, RicConfig> ricConfigs = new HashMap<>();
127
128     private WebClientConfig webClientConfig = null;
129
130     public synchronized Collection<RicConfig> getRicConfigs() {
131         return this.ricConfigs.values();
132     }
133
134     public WebClientConfig getWebClientConfig() {
135         if (this.webClientConfig == null) {
136             HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
137                     .httpProxyHost(this.httpProxyHost) //
138                     .httpProxyPort(this.httpProxyPort) //
139                     .httpProxyType(ProxyProvider.Proxy.valueOf(this.httpProxyType)) //
140                     .build();
141             if (sslEnabled) {
142                 this.webClientConfig = WebClientConfig.builder() //
143                         .sslEnabled(true)
144                         .keyStoreType(this.sslKeyStoreType) //
145                         .keyStorePassword(this.sslKeyStorePassword) //
146                         .keyStore(this.sslKeyStore) //
147                         .keyPassword(this.sslKeyPassword) //
148                         .isTrustStoreUsed(this.sslTrustStoreUsed) //
149                         .trustStore(this.sslTrustStore) //
150                         .trustStorePassword(this.sslTrustStorePassword) //
151                         .httpProxyConfig(httpProxyConfig) //
152                         .build();
153             } else {
154                 this.webClientConfig = WebClientConfig.builder() //
155                         .sslEnabled(false)
156                         .isTrustStoreUsed(false)
157                         .httpProxyConfig(httpProxyConfig) //
158                         .build();
159             }
160
161         }
162         return this.webClientConfig;
163     }
164
165     public synchronized RicConfig getRic(String ricId) throws ServiceException {
166         RicConfig ricConfig = this.ricConfigs.get(ricId);
167         if (ricConfig == null) {
168             throw new ServiceException("Could not find ric configuration: " + ricId);
169         }
170         return ricConfig;
171     }
172
173     public static class RicConfigUpdate {
174         public enum Type {
175             ADDED, CHANGED, REMOVED
176         }
177
178         @Getter
179         private final RicConfig ricConfig;
180         @Getter
181         private final Type type;
182
183         public RicConfigUpdate(RicConfig config, Type event) {
184             this.ricConfig = config;
185             this.type = event;
186         }
187     }
188
189     public synchronized Flux<RicConfigUpdate> setConfiguration(
190             ApplicationConfigParser.ConfigParserResult parserResult) {
191
192         Collection<RicConfigUpdate> modifications = new ArrayList<>();
193
194         Map<String, RicConfig> newRicConfigs = new HashMap<>();
195         for (RicConfig newConfig : parserResult.getRicConfigs()) {
196             RicConfig oldConfig = this.ricConfigs.get(newConfig.getRicId());
197             this.ricConfigs.remove(newConfig.getRicId());
198             if (oldConfig == null) {
199                 newRicConfigs.put(newConfig.getRicId(), newConfig);
200                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
201             } else if (!newConfig.equals(oldConfig)) {
202                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
203                 newRicConfigs.put(newConfig.getRicId(), newConfig);
204             } else {
205                 newRicConfigs.put(oldConfig.getRicId(), oldConfig);
206             }
207         }
208         for (RicConfig deletedConfig : this.ricConfigs.values()) {
209             modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
210         }
211         this.ricConfigs = newRicConfigs;
212
213         return Flux.fromIterable(modifications);
214     }
215
216     public boolean isS3Enabled() {
217         return !(Strings.isNullOrEmpty(s3EndpointOverride) || Strings.isNullOrEmpty(s3Bucket));
218     }
219
220     public boolean isDatabaseEnabled() {
221         return databaseEnabled;
222     }
223 }