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