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