6d8d52dea8a301b0ef170f557e32723305ec5f9b
[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. 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 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.Map;
29 import lombok.Getter;
30 import lombok.Setter;
31 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig.HttpProxyConfig;
32 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.boot.context.properties.EnableConfigurationProperties;
35 import reactor.core.publisher.Flux;
36 import reactor.netty.transport.ProxyProvider;
37
38 @EnableConfigurationProperties
39 public class ApplicationConfig {
40
41     @Getter
42     @Value("${app.filepath}")
43     private String localConfigurationFilePath;
44
45     @Getter
46     @Value("${app.config-file-schema-path:}")
47     private String configurationFileSchemaPath;
48
49     @Getter
50     @Value("${app.vardata-directory:null}")
51     private String vardataDirectory;
52
53     @Value("${server.ssl.key-store-type}")
54     private String sslKeyStoreType = "";
55
56     @Value("${server.ssl.key-store-password}")
57     private String sslKeyStorePassword = "";
58
59     @Value("${server.ssl.key-store}")
60     private String sslKeyStore = "";
61
62     @Value("${server.ssl.key-password}")
63     private String sslKeyPassword = "";
64
65     @Value("${app.webclient.trust-store-used}")
66     private boolean sslTrustStoreUsed = false;
67
68     @Value("${app.webclient.trust-store-password}")
69     private String sslTrustStorePassword = "";
70
71     @Value("${app.webclient.trust-store}")
72     private String sslTrustStore = "";
73
74     @Value("${app.webclient.http.proxy-host:}")
75     private String httpProxyHost = "";
76
77     @Value("${app.webclient.http.proxy-port:0}")
78     private int httpProxyPort = 0;
79
80     @Value("${app.webclient.http.proxy-type:HTTP}")
81     private String httpProxyType = "HTTP";
82
83     @Getter
84     @Value("${app.s3.endpointOverride:}")
85     private String s3EndpointOverride;
86
87     @Getter
88     @Value("${app.s3.accessKeyId:}")
89     private String s3AccessKeyId;
90
91     @Getter
92     @Value("${app.s3.secretAccessKey:}")
93     private String s3SecretAccessKey;
94
95     @Getter
96     @Value("${app.s3.bucket:}")
97     private String s3Bucket;
98
99     @Getter
100     @Setter
101     @Value("${app.authorization-provider:}")
102     private String authProviderUrl;
103
104     @Getter
105     @Setter
106     @Value("${app.database-enabled:}")
107     private boolean databaseEnabled;
108
109     public enum ValidateSchema {
110         NONE,
111         INFO,
112         WARN,
113         FAIL
114     }
115
116     @Getter
117     @Setter
118     @Value("${app.validate-policy-instance-schema:NONE}")
119     private ValidateSchema validatePolicyInstanceSchema;
120
121     private Map<String, RicConfig> ricConfigs = new HashMap<>();
122
123     private WebClientConfig webClientConfig = null;
124
125     public synchronized Collection<RicConfig> getRicConfigs() {
126         return this.ricConfigs.values();
127     }
128
129     public WebClientConfig getWebClientConfig() {
130         if (this.webClientConfig == null) {
131             HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
132                     .httpProxyHost(this.httpProxyHost) //
133                     .httpProxyPort(this.httpProxyPort) //
134                     .httpProxyType(ProxyProvider.Proxy.valueOf(this.httpProxyType)) //
135                     .build();
136
137             this.webClientConfig = WebClientConfig.builder() //
138                     .keyStoreType(this.sslKeyStoreType) //
139                     .keyStorePassword(this.sslKeyStorePassword) //
140                     .keyStore(this.sslKeyStore) //
141                     .keyPassword(this.sslKeyPassword) //
142                     .isTrustStoreUsed(this.sslTrustStoreUsed) //
143                     .trustStore(this.sslTrustStore) //
144                     .trustStorePassword(this.sslTrustStorePassword) //
145                     .httpProxyConfig(httpProxyConfig) //
146                     .build();
147         }
148         return this.webClientConfig;
149     }
150
151     public synchronized RicConfig getRic(String ricId) throws ServiceException {
152         RicConfig ricConfig = this.ricConfigs.get(ricId);
153         if (ricConfig == null) {
154             throw new ServiceException("Could not find ric configuration: " + ricId);
155         }
156         return ricConfig;
157     }
158
159     public static class RicConfigUpdate {
160         public enum Type {
161             ADDED, CHANGED, REMOVED
162         }
163
164         @Getter
165         private final RicConfig ricConfig;
166         @Getter
167         private final Type type;
168
169         public RicConfigUpdate(RicConfig config, Type event) {
170             this.ricConfig = config;
171             this.type = event;
172         }
173     }
174
175     public synchronized Flux<RicConfigUpdate> setConfiguration(
176             ApplicationConfigParser.ConfigParserResult parserResult) {
177
178         Collection<RicConfigUpdate> modifications = new ArrayList<>();
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     public boolean isDatabaseEnabled() {
207         return databaseEnabled;
208     }
209 }