5022a6933daf14fe14a2c6a3dc05059fce78a321
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA Intellectual Property. 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.bbs.event.processor.config;
22
23 import static org.onap.bbs.event.processor.config.ApplicationConstants.STREAMS_TYPE;
24
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.jetbrains.annotations.NotNull;
30 import org.onap.bbs.event.processor.exceptions.ApplicationEnvironmentException;
31 import org.onap.bbs.event.processor.exceptions.ConfigurationParsingException;
32 import org.onap.bbs.event.processor.model.GeneratedAppConfigObject;
33 import org.onap.bbs.event.processor.utilities.LoggingUtil;
34 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
35 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapConsumerConfiguration;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapPublisherConfiguration;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.Configuration;
40
41 @Configuration
42 public class ApplicationConfiguration implements ConfigurationChangeObservable {
43
44     private final AaiClientProperties aaiClientProperties;
45     private final DmaapReRegistrationConsumerProperties dmaapReRegistrationConsumerProperties;
46     private final DmaapCpeAuthenticationConsumerProperties dmaapCpeAuthenticationConsumerProperties;
47     private final DmaapProducerProperties dmaapProducerProperties;
48     private final SecurityProperties securityProperties;
49     private final GenericProperties genericProperties;
50
51     private DmaapConsumerConfiguration dmaapReRegistrationConsumerConfiguration;
52     private DmaapConsumerConfiguration dmaapCpeAuthenticationConsumerConfiguration;
53     private DmaapPublisherConfiguration dmaapPublisherConfiguration;
54     private AaiClientConfiguration aaiClientConfiguration;
55     private Set<ConfigurationChangeObserver> observers;
56
57     private int cbsPollingInterval;
58
59     /**
60      * Construct BBS event processor application configuration object.
61      * @param aaiClientProperties Properties for AAI client setup
62      * @param dmaapReRegistrationConsumerProperties Properties for DMaaP client setup (PNF re-registration)
63      * @param dmaapCpeAuthenticationConsumerProperties Properties for DMaaP client setup (CPE authentication)
64      * @param dmaapProducerProperties Properties for DMaaP client setup (Close Loop)
65      * @param securityProperties General security properties
66      * @param genericProperties General application properties
67      */
68     @Autowired
69     public ApplicationConfiguration(AaiClientProperties aaiClientProperties,
70                                     DmaapReRegistrationConsumerProperties dmaapReRegistrationConsumerProperties,
71                                     DmaapCpeAuthenticationConsumerProperties dmaapCpeAuthenticationConsumerProperties,
72                                     DmaapProducerProperties dmaapProducerProperties,
73                                     SecurityProperties securityProperties,
74                                     GenericProperties genericProperties) {
75         this.aaiClientProperties = aaiClientProperties;
76         this.dmaapReRegistrationConsumerProperties = dmaapReRegistrationConsumerProperties;
77         this.dmaapCpeAuthenticationConsumerProperties = dmaapCpeAuthenticationConsumerProperties;
78         this.dmaapProducerProperties = dmaapProducerProperties;
79         this.securityProperties = securityProperties;
80         this.genericProperties = genericProperties;
81         observers = new HashSet<>();
82         constructConfigurationObjects();
83     }
84
85     @Override
86     public synchronized void register(ConfigurationChangeObserver observer) {
87         observers.add(observer);
88     }
89
90     @Override
91     public synchronized void unRegister(ConfigurationChangeObserver observer) {
92         observers.remove(observer);
93     }
94
95     @Override
96     public synchronized void notifyObservers() {
97         observers.forEach(ConfigurationChangeObserver::updateConfiguration);
98     }
99
100     public synchronized DmaapConsumerConfiguration getDmaapReRegistrationConsumerConfiguration() {
101         return dmaapReRegistrationConsumerConfiguration;
102     }
103
104     public synchronized DmaapConsumerConfiguration getDmaapCpeAuthenticationConsumerConfiguration() {
105         return dmaapCpeAuthenticationConsumerConfiguration;
106     }
107
108     public synchronized DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
109         return dmaapPublisherConfiguration;
110     }
111
112     public synchronized AaiClientConfiguration getAaiClientConfiguration() {
113         return aaiClientConfiguration;
114     }
115
116     public synchronized int getPipelinesPollingIntervalInSeconds() {
117         return genericProperties.getPipelinesPollingIntervalSec();
118     }
119
120     public synchronized int getPipelinesTimeoutInSeconds() {
121         return genericProperties.getPipelinesTimeoutSec();
122     }
123
124     public synchronized String getPolicyVersion() {
125         return genericProperties.getPolicyVersion();
126     }
127
128     public synchronized String getCloseLoopTargetType() {
129         return genericProperties.getClTargetType();
130     }
131
132     public synchronized String getCloseLoopEventStatus() {
133         return genericProperties.getClEventStatus();
134     }
135
136     public synchronized String getCloseLoopVersion() {
137         return genericProperties.getClVersion();
138     }
139
140     public synchronized String getCloseLoopTarget() {
141         return genericProperties.getClTarget();
142     }
143
144     public String getCloseLoopOriginator() {
145         return genericProperties.getClOriginator();
146     }
147
148     public synchronized int getCbsPollingInterval() {
149         return cbsPollingInterval;
150     }
151
152     public synchronized String getReRegistrationCloseLoopPolicyScope() {
153         return genericProperties.getReRegistration().getPolicyScope();
154     }
155
156     public synchronized String getReRegistrationCloseLoopControlName() {
157         return genericProperties.getReRegistration().getClControlName();
158     }
159
160     public synchronized String getCpeAuthenticationCloseLoopPolicyScope() {
161         return genericProperties.getCpeAuthentication().getPolicyScope();
162     }
163
164     public synchronized String getCpeAuthenticationCloseLoopControlName() {
165         return genericProperties.getCpeAuthentication().getClControlName();
166     }
167
168     /**
169      * Update current configuration based on the new configuration object fetched from Consul via CBS service of DCAE.
170      * @param newConfiguration updated configuration object
171      */
172     public void updateCurrentConfiguration(GeneratedAppConfigObject newConfiguration) {
173
174         synchronized (this) {
175             cbsPollingInterval = newConfiguration.cbsPollingIntervalSec();
176
177             securityProperties.setEnableAaiCertAuth(newConfiguration.enableAaiCertAuth());
178             securityProperties.setEnableDmaapCertAuth(newConfiguration.enableDmaapCertAuth());
179             securityProperties.setKeyStorePath(newConfiguration.keyStorePath());
180             securityProperties.setKeyStorePasswordPath(newConfiguration.keyStorePasswordPath());
181             securityProperties.setTrustStorePath(newConfiguration.trustStorePath());
182             securityProperties.setTrustStorePasswordPath(newConfiguration.trustStorePasswordPath());
183
184             GeneratedAppConfigObject.StreamsObject reRegObject =
185                     getStreamsObject(newConfiguration.streamSubscribesMap(), newConfiguration.reRegConfigKey(),
186                             "PNF Re-Registration");
187             TopicUrlInfo topicUrlInfo = parseTopicUrl(reRegObject.dmaapInfo().topicUrl());
188             dmaapReRegistrationConsumerProperties.setDmaapHostName(topicUrlInfo.getHost());
189             dmaapReRegistrationConsumerProperties.setDmaapPortNumber(topicUrlInfo.getPort());
190             dmaapReRegistrationConsumerProperties.setDmaapProtocol(newConfiguration.dmaapProtocol());
191             dmaapReRegistrationConsumerProperties.setDmaapUserName(reRegObject.aafUsername());
192             dmaapReRegistrationConsumerProperties.setDmaapUserPassword(reRegObject.aafPassword());
193             dmaapReRegistrationConsumerProperties.setDmaapContentType(newConfiguration.dmaapContentType());
194             dmaapReRegistrationConsumerProperties.setDmaapTopicName(topicUrlInfo.getTopicName());
195             dmaapReRegistrationConsumerProperties.setConsumerId(newConfiguration.dmaapConsumerConsumerId());
196             dmaapReRegistrationConsumerProperties.setConsumerGroup(newConfiguration.dmaapConsumerConsumerGroup());
197             dmaapReRegistrationConsumerProperties.setMessageLimit(newConfiguration.dmaapMessageLimit());
198             dmaapReRegistrationConsumerProperties.setTimeoutMs(newConfiguration.dmaapTimeoutMs());
199             constructDmaapReRegistrationConfiguration();
200
201             GeneratedAppConfigObject.StreamsObject cpeAuthObject =
202                     getStreamsObject(newConfiguration.streamSubscribesMap(), newConfiguration.cpeAuthConfigKey(),
203                             "CPE Authentication");
204             topicUrlInfo = parseTopicUrl(cpeAuthObject.dmaapInfo().topicUrl());
205             dmaapCpeAuthenticationConsumerProperties.setDmaapHostName(topicUrlInfo.getHost());
206             dmaapCpeAuthenticationConsumerProperties.setDmaapPortNumber(topicUrlInfo.getPort());
207             dmaapCpeAuthenticationConsumerProperties.setDmaapProtocol(newConfiguration.dmaapProtocol());
208             dmaapCpeAuthenticationConsumerProperties.setDmaapUserName(cpeAuthObject.aafUsername());
209             dmaapCpeAuthenticationConsumerProperties.setDmaapUserPassword(cpeAuthObject.aafPassword());
210             dmaapCpeAuthenticationConsumerProperties.setDmaapContentType(newConfiguration.dmaapContentType());
211             dmaapCpeAuthenticationConsumerProperties.setDmaapTopicName(topicUrlInfo.getTopicName());
212             dmaapCpeAuthenticationConsumerProperties.setConsumerId(newConfiguration.dmaapConsumerConsumerId());
213             dmaapCpeAuthenticationConsumerProperties.setConsumerGroup(newConfiguration.dmaapConsumerConsumerGroup());
214             dmaapCpeAuthenticationConsumerProperties.setMessageLimit(newConfiguration.dmaapMessageLimit());
215             dmaapCpeAuthenticationConsumerProperties.setTimeoutMs(newConfiguration.dmaapTimeoutMs());
216             constructDmaapCpeAuthenticationConfiguration();
217
218             GeneratedAppConfigObject.StreamsObject closeLoopObject =
219                     getStreamsObject(newConfiguration.streamPublishesMap(), newConfiguration.closeLoopConfigKey(),
220                             "Close Loop");
221             topicUrlInfo = parseTopicUrl(closeLoopObject.dmaapInfo().topicUrl());
222             dmaapProducerProperties.setDmaapHostName(topicUrlInfo.getHost());
223             dmaapProducerProperties.setDmaapPortNumber(topicUrlInfo.getPort());
224             dmaapProducerProperties.setDmaapProtocol(newConfiguration.dmaapProtocol());
225             dmaapProducerProperties.setDmaapUserName(closeLoopObject.aafUsername());
226             dmaapProducerProperties.setDmaapUserPassword(closeLoopObject.aafPassword());
227             dmaapProducerProperties.setDmaapContentType(newConfiguration.dmaapContentType());
228             dmaapProducerProperties.setDmaapTopicName(topicUrlInfo.getTopicName());
229             constructDmaapProducerConfiguration();
230
231             aaiClientProperties.setAaiHost(newConfiguration.aaiHost());
232             aaiClientProperties.setAaiPort(newConfiguration.aaiPort());
233             aaiClientProperties.setAaiProtocol(newConfiguration.aaiProtocol());
234             aaiClientProperties.setAaiUserName(newConfiguration.aaiUsername());
235             aaiClientProperties.setAaiUserPassword(newConfiguration.aaiPassword());
236             aaiClientProperties.setAaiIgnoreSslCertificateErrors(newConfiguration.aaiIgnoreSslCertificateErrors());
237             constructAaiConfiguration();
238
239             genericProperties.setPipelinesPollingIntervalSec(newConfiguration.pipelinesPollingIntervalSec());
240             genericProperties.setPipelinesTimeoutSec(newConfiguration.pipelinesTimeoutSec());
241             genericProperties.setPolicyVersion(newConfiguration.policyVersion());
242             genericProperties.setClTargetType(newConfiguration.closeLoopTargetType());
243             genericProperties.setClEventStatus(newConfiguration.closeLoopEventStatus());
244             genericProperties.setClVersion(newConfiguration.closeLoopVersion());
245             genericProperties.setClTarget(newConfiguration.closeLoopTarget());
246             genericProperties.setClOriginator(newConfiguration.closeLoopOriginator());
247             genericProperties.getReRegistration().setPolicyScope(newConfiguration.reRegistrationPolicyScope());
248             genericProperties.getReRegistration().setClControlName(newConfiguration.reRegistrationClControlName());
249             genericProperties.getCpeAuthentication().setPolicyScope(newConfiguration.cpeAuthPolicyScope());
250             genericProperties.getCpeAuthentication().setClControlName(newConfiguration.cpeAuthClControlName());
251
252             LoggingUtil.changeLoggingLevel(newConfiguration.loggingLevel());
253         }
254
255         notifyObservers();
256     }
257
258     @NotNull
259     private GeneratedAppConfigObject.StreamsObject getStreamsObject(
260             Map<String, GeneratedAppConfigObject.StreamsObject> map, String configKey, String messageName) {
261         GeneratedAppConfigObject.StreamsObject streamsObject = map.get(configKey);
262         if (!STREAMS_TYPE.equals(streamsObject.type())) {
263             throw new ApplicationEnvironmentException(String.format("%s requires information about"
264                     + " message-router topic in ONAP", messageName));
265         }
266         return streamsObject;
267     }
268
269     private void constructConfigurationObjects() {
270         constructDmaapReRegistrationConfiguration();
271         constructDmaapCpeAuthenticationConfiguration();
272         constructDmaapProducerConfiguration();
273         constructAaiConfiguration();
274     }
275
276     private void constructDmaapReRegistrationConfiguration() {
277         dmaapReRegistrationConsumerConfiguration = new ImmutableDmaapConsumerConfiguration.Builder()
278                 .dmaapHostName(dmaapReRegistrationConsumerProperties.getDmaapHostName())
279                 .dmaapPortNumber(dmaapReRegistrationConsumerProperties.getDmaapPortNumber())
280                 .dmaapProtocol(dmaapReRegistrationConsumerProperties.getDmaapProtocol())
281                 .dmaapTopicName(dmaapReRegistrationConsumerProperties.getDmaapTopicName())
282                 .dmaapUserName(
283                         dmaapReRegistrationConsumerProperties.getDmaapUserName() == null ? "" :
284                                 dmaapReRegistrationConsumerProperties.getDmaapUserName())
285                 .dmaapUserPassword(
286                         dmaapReRegistrationConsumerProperties.getDmaapUserPassword() == null ? "" :
287                                 dmaapReRegistrationConsumerProperties.getDmaapUserPassword())
288                 .dmaapContentType(dmaapReRegistrationConsumerProperties.getDmaapContentType())
289                 .consumerId(dmaapReRegistrationConsumerProperties.getConsumerId())
290                 .consumerGroup(dmaapReRegistrationConsumerProperties.getConsumerGroup())
291                 .timeoutMs(dmaapReRegistrationConsumerProperties.getTimeoutMs())
292                 .messageLimit(dmaapReRegistrationConsumerProperties.getMessageLimit())
293                 .enableDmaapCertAuth(securityProperties.isEnableDmaapCertAuth())
294                 .keyStorePath(securityProperties.getKeyStorePath())
295                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
296                 .trustStorePath(securityProperties.getTrustStorePath())
297                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
298                 .build();
299     }
300
301     private void constructDmaapCpeAuthenticationConfiguration() {
302         dmaapCpeAuthenticationConsumerConfiguration = new ImmutableDmaapConsumerConfiguration.Builder()
303                 .dmaapHostName(dmaapCpeAuthenticationConsumerProperties.getDmaapHostName())
304                 .dmaapPortNumber(dmaapCpeAuthenticationConsumerProperties.getDmaapPortNumber())
305                 .dmaapProtocol(dmaapCpeAuthenticationConsumerProperties.getDmaapProtocol())
306                 .dmaapTopicName(dmaapCpeAuthenticationConsumerProperties.getDmaapTopicName())
307                 .dmaapUserName(
308                         dmaapCpeAuthenticationConsumerProperties.getDmaapUserName() == null ? "" :
309                                 dmaapCpeAuthenticationConsumerProperties.getDmaapUserName())
310                 .dmaapUserPassword(
311                         dmaapCpeAuthenticationConsumerProperties.getDmaapUserPassword() == null ? "" :
312                                 dmaapCpeAuthenticationConsumerProperties.getDmaapUserPassword())
313                 .dmaapContentType(dmaapCpeAuthenticationConsumerProperties.getDmaapContentType())
314                 .consumerId(dmaapCpeAuthenticationConsumerProperties.getConsumerId())
315                 .consumerGroup(dmaapCpeAuthenticationConsumerProperties.getConsumerGroup())
316                 .timeoutMs(dmaapCpeAuthenticationConsumerProperties.getTimeoutMs())
317                 .messageLimit(dmaapCpeAuthenticationConsumerProperties.getMessageLimit())
318                 .enableDmaapCertAuth(securityProperties.isEnableDmaapCertAuth())
319                 .keyStorePath(securityProperties.getKeyStorePath())
320                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
321                 .trustStorePath(securityProperties.getTrustStorePath())
322                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
323                 .build();
324     }
325
326     private void constructDmaapProducerConfiguration() {
327         dmaapPublisherConfiguration = new ImmutableDmaapPublisherConfiguration.Builder()
328                 .dmaapHostName(dmaapProducerProperties.getDmaapHostName())
329                 .dmaapPortNumber(dmaapProducerProperties.getDmaapPortNumber())
330                 .dmaapProtocol(dmaapProducerProperties.getDmaapProtocol())
331                 .dmaapTopicName(dmaapProducerProperties.getDmaapTopicName())
332                 .dmaapUserName(
333                         dmaapProducerProperties.getDmaapUserName() == null ? "" :
334                                 dmaapProducerProperties.getDmaapUserName())
335                 .dmaapUserPassword(
336                         dmaapProducerProperties.getDmaapUserPassword() == null ? "" :
337                                 dmaapProducerProperties.getDmaapUserPassword())
338                 .dmaapContentType(dmaapProducerProperties.getDmaapContentType())
339                 .enableDmaapCertAuth(securityProperties.isEnableDmaapCertAuth())
340                 .keyStorePath(securityProperties.getKeyStorePath())
341                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
342                 .trustStorePath(securityProperties.getTrustStorePath())
343                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
344                 .build();
345     }
346
347     private void constructAaiConfiguration() {
348         aaiClientConfiguration = new ImmutableAaiClientConfiguration.Builder()
349                 .aaiHost(aaiClientProperties.getAaiHost())
350                 .aaiPort(aaiClientProperties.getAaiPort())
351                 .aaiProtocol(aaiClientProperties.getAaiProtocol())
352                 .aaiUserName(aaiClientProperties.getAaiUserName())
353                 .aaiUserPassword(aaiClientProperties.getAaiUserPassword())
354                 .aaiHeaders(aaiClientProperties.getAaiHeaders())
355                 .aaiIgnoreSslCertificateErrors(aaiClientProperties.isAaiIgnoreSslCertificateErrors())
356                 .enableAaiCertAuth(securityProperties.isEnableAaiCertAuth())
357                 .keyStorePath(securityProperties.getKeyStorePath())
358                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
359                 .trustStorePath(securityProperties.getTrustStorePath())
360                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
361                 .build();
362     }
363
364     private TopicUrlInfo parseTopicUrl(String topicUrl) {
365         String[] urlTokens = topicUrl.split(":");
366         if (urlTokens.length != 3) {
367             throw new ConfigurationParsingException("Wrong topic URL format");
368         }
369         TopicUrlInfo topicUrlInfo = new TopicUrlInfo();
370         topicUrlInfo.setHost(urlTokens[1].replace("/", ""));
371
372         String[] tokensAfterHost = urlTokens[2].split("/events/");
373         if (tokensAfterHost.length != 2) {
374             throw new ConfigurationParsingException("Wrong topic name structure");
375         }
376         topicUrlInfo.setPort(Integer.valueOf(tokensAfterHost[0]));
377         topicUrlInfo.setTopicName("events/" + tokensAfterHost[1]);
378
379         return topicUrlInfo;
380     }
381
382     private static class TopicUrlInfo {
383         private String host;
384         private int port;
385         private String topicName;
386
387         String getHost() {
388             return host;
389         }
390
391         void setHost(String host) {
392             this.host = host;
393         }
394
395         int getPort() {
396             return port;
397         }
398
399         void setPort(int port) {
400             this.port = port;
401         }
402
403         String getTopicName() {
404             return topicName;
405         }
406
407         void setTopicName(String topicName) {
408             this.topicName = topicName;
409         }
410     }
411 }