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