3a245190ffa55d8084b406c89bfb376cb7903c2c
[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 java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.validation.constraints.NotEmpty;
29
30 import lombok.Getter;
31
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
37 import reactor.core.publisher.Flux;
38 import reactor.netty.transport.ProxyProvider;
39
40 @EnableConfigurationProperties
41 public class ApplicationConfig {
42     @NotEmpty
43     @Getter
44     @Value("${app.filepath}")
45     private String localConfigurationFilePath;
46
47     @Getter
48     @Value("${app.vardata-directory:null}")
49     private String vardataDirectory;
50
51     @Value("${server.ssl.key-store-type}")
52     private String sslKeyStoreType = "";
53
54     @Value("${server.ssl.key-store-password}")
55     private String sslKeyStorePassword = "";
56
57     @Value("${server.ssl.key-store}")
58     private String sslKeyStore = "";
59
60     @Value("${server.ssl.key-password}")
61     private String sslKeyPassword = "";
62
63     @Value("${app.webclient.trust-store-used}")
64     private boolean sslTrustStoreUsed = false;
65
66     @Value("${app.webclient.trust-store-password}")
67     private String sslTrustStorePassword = "";
68
69     @Value("${app.webclient.trust-store}")
70     private String sslTrustStore = "";
71
72     @Value("${app.webclient.http.proxy-host:\"\"}")
73     private String httpProxyHost = "";
74
75     @Value("${app.webclient.http.proxy-port:0}")
76     private int httpProxyPort = 0;
77
78     @Value("${app.webclient.http.proxy-type:HTTP}")
79     private String httpProxyType = "HTTP";
80
81     private Map<String, RicConfig> ricConfigs = new HashMap<>();
82
83     @Getter
84     private String dmaapConsumerTopicUrl;
85
86     @Getter
87     private String dmaapProducerTopicUrl;
88
89     private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
90
91     private WebClientConfig webClientConfig = null;
92
93     public synchronized Collection<RicConfig> getRicConfigs() {
94         return this.ricConfigs.values();
95     }
96
97     public WebClientConfig getWebClientConfig() {
98         if (this.webClientConfig == null) {
99             HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
100                     .httpProxyHost(this.httpProxyHost) //
101                     .httpProxyPort(this.httpProxyPort) //
102                     .httpProxyType(ProxyProvider.Proxy.valueOf(this.httpProxyType)) //
103                     .build();
104
105             this.webClientConfig = ImmutableWebClientConfig.builder() //
106                     .keyStoreType(this.sslKeyStoreType) //
107                     .keyStorePassword(this.sslKeyStorePassword) //
108                     .keyStore(this.sslKeyStore) //
109                     .keyPassword(this.sslKeyPassword) //
110                     .isTrustStoreUsed(this.sslTrustStoreUsed) //
111                     .trustStore(this.sslTrustStore) //
112                     .trustStorePassword(this.sslTrustStorePassword) //
113                     .httpProxyConfig(httpProxyConfig) //
114                     .build();
115         }
116         return this.webClientConfig;
117     }
118
119     public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException {
120         ControllerConfig controllerConfig = this.controllerConfigs.get(name);
121         if (controllerConfig == null) {
122             throw new ServiceException("Could not find controller config: " + name);
123         }
124         return controllerConfig;
125     }
126
127     public synchronized RicConfig getRic(String ricId) throws ServiceException {
128         RicConfig ricConfig = this.ricConfigs.get(ricId);
129         if (ricConfig == null) {
130             throw new ServiceException("Could not find ric configuration: " + ricId);
131         }
132         return ricConfig;
133     }
134
135     public static class RicConfigUpdate {
136         public enum Type {
137             ADDED, CHANGED, REMOVED
138         }
139
140         @Getter
141         private final RicConfig ricConfig;
142         @Getter
143         private final Type type;
144
145         public RicConfigUpdate(RicConfig config, Type event) {
146             this.ricConfig = config;
147             this.type = event;
148         }
149     }
150
151     public synchronized Flux<RicConfigUpdate> setConfiguration(
152             ApplicationConfigParser.ConfigParserResult parserResult) {
153
154         Collection<RicConfigUpdate> modifications = new ArrayList<>();
155         this.controllerConfigs = parserResult.controllerConfigs();
156
157         this.dmaapConsumerTopicUrl = parserResult.dmaapConsumerTopicUrl();
158         this.dmaapProducerTopicUrl = parserResult.dmaapProducerTopicUrl();
159
160         Map<String, RicConfig> newRicConfigs = new HashMap<>();
161         for (RicConfig newConfig : parserResult.ricConfigs()) {
162             RicConfig oldConfig = this.ricConfigs.get(newConfig.ricId());
163             this.ricConfigs.remove(newConfig.ricId());
164             if (oldConfig == null) {
165                 newRicConfigs.put(newConfig.ricId(), newConfig);
166                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
167             } else if (!newConfig.equals(oldConfig)) {
168                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
169                 newRicConfigs.put(newConfig.ricId(), newConfig);
170             } else {
171                 newRicConfigs.put(oldConfig.ricId(), oldConfig);
172             }
173         }
174         for (RicConfig deletedConfig : this.ricConfigs.values()) {
175             modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
176         }
177         this.ricConfigs = newRicConfigs;
178
179         return Flux.fromIterable(modifications);
180     }
181 }