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