5277f3cd8c5f1ef5a6bfca89a0f9f57770f82914
[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(o -> o.updateConfiguration(this));
97     }
98
99     public DmaapConsumerConfiguration getDmaapReRegistrationConsumerConfiguration() {
100         return dmaapReRegistrationConsumerConfiguration;
101     }
102
103     public DmaapConsumerConfiguration getDmaapCpeAuthenticationConsumerConfiguration() {
104         return dmaapCpeAuthenticationConsumerConfiguration;
105     }
106
107     public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
108         return dmaapPublisherConfiguration;
109     }
110
111     public AaiClientConfiguration getAaiClientConfiguration() {
112         return aaiClientConfiguration;
113     }
114
115     public int getPipelinesPollingIntervalInSeconds() {
116         return genericProperties.getPipelinesPollingIntervalSec();
117     }
118
119     public int getPipelinesTimeoutInSeconds() {
120         return genericProperties.getPipelinesTimeoutSec();
121     }
122
123     public int getCbsPollingInterval() {
124         return cbsPollingInterval;
125     }
126
127     public String getReRegistrationCloseLoopPolicyScope() {
128
129         return genericProperties.getReRegistration().getPolicyScope();
130     }
131
132     public String getReRegistrationCloseLoopControlName() {
133         return genericProperties.getReRegistration().getClControlName();
134     }
135
136     public String getCpeAuthenticationCloseLoopPolicyScope() {
137         return genericProperties.getCpeAuthentication().getPolicyScope();
138     }
139
140     public String getCpeAuthenticationCloseLoopControlName() {
141         return genericProperties.getCpeAuthentication().getClControlName();
142     }
143
144     /**
145      * Update current configuration based on the new configuration object fetched from Consul via CBS service of DCAE.
146      * @param newConfiguration updated configuration object
147      */
148     public void updateCurrentConfiguration(GeneratedAppConfigObject newConfiguration) {
149
150         cbsPollingInterval = newConfiguration.cbsPollingIntervalSec();
151
152         GeneratedAppConfigObject.StreamsObject reRegObject = getStreamsObject(newConfiguration.streamSubscribesMap(),
153                 newConfiguration.reRegConfigKey(), "PNF Re-Registration");
154         TopicUrlInfo topicUrlInfo = parseTopicUrl(reRegObject.dmaapInfo().topicUrl());
155         dmaapReRegistrationConsumerProperties.setDmaapHostName(topicUrlInfo.getHost());
156         dmaapReRegistrationConsumerProperties.setDmaapPortNumber(topicUrlInfo.getPort());
157         dmaapReRegistrationConsumerProperties.setDmaapProtocol(newConfiguration.dmaapProtocol());
158         dmaapReRegistrationConsumerProperties.setDmaapContentType(newConfiguration.dmaapContentType());
159         dmaapReRegistrationConsumerProperties.setDmaapTopicName(topicUrlInfo.getTopicName());
160         dmaapReRegistrationConsumerProperties.setConsumerId(newConfiguration.dmaapConsumerConsumerId());
161         dmaapReRegistrationConsumerProperties.setConsumerGroup(newConfiguration.dmaapConsumerConsumerGroup());
162         dmaapReRegistrationConsumerProperties.setMessageLimit(newConfiguration.dmaapMessageLimit());
163         dmaapReRegistrationConsumerProperties.setTimeoutMs(newConfiguration.dmaapTimeoutMs());
164         constructDmaapReRegistrationConfiguration();
165
166         GeneratedAppConfigObject.StreamsObject cpeAuthObject = getStreamsObject(newConfiguration.streamSubscribesMap(),
167                 newConfiguration.cpeAuthConfigKey(), "CPE Authentication");
168         topicUrlInfo = parseTopicUrl(cpeAuthObject.dmaapInfo().topicUrl());
169         dmaapCpeAuthenticationConsumerProperties.setDmaapHostName(topicUrlInfo.getHost());
170         dmaapCpeAuthenticationConsumerProperties.setDmaapPortNumber(topicUrlInfo.getPort());
171         dmaapCpeAuthenticationConsumerProperties.setDmaapProtocol(newConfiguration.dmaapProtocol());
172         dmaapCpeAuthenticationConsumerProperties.setDmaapContentType(newConfiguration.dmaapContentType());
173         dmaapCpeAuthenticationConsumerProperties.setDmaapTopicName(topicUrlInfo.getTopicName());
174         dmaapCpeAuthenticationConsumerProperties.setConsumerId(newConfiguration.dmaapConsumerConsumerId());
175         dmaapCpeAuthenticationConsumerProperties.setConsumerGroup(newConfiguration.dmaapConsumerConsumerGroup());
176         dmaapCpeAuthenticationConsumerProperties.setMessageLimit(newConfiguration.dmaapMessageLimit());
177         dmaapCpeAuthenticationConsumerProperties.setTimeoutMs(newConfiguration.dmaapTimeoutMs());
178         constructDmaapCpeAuthenticationConfiguration();
179
180         GeneratedAppConfigObject.StreamsObject closeLoopObject = getStreamsObject(newConfiguration.streamPublishesMap(),
181                 newConfiguration.closeLoopConfigKey(), "Close Loop");
182         topicUrlInfo = parseTopicUrl(closeLoopObject.dmaapInfo().topicUrl());
183         dmaapProducerProperties.setDmaapHostName(topicUrlInfo.getHost());
184         dmaapProducerProperties.setDmaapPortNumber(topicUrlInfo.getPort());
185         dmaapProducerProperties.setDmaapProtocol(newConfiguration.dmaapProtocol());
186         dmaapProducerProperties.setDmaapContentType(newConfiguration.dmaapContentType());
187         dmaapProducerProperties.setDmaapTopicName(topicUrlInfo.getTopicName());
188         constructDmaapProducerConfiguration();
189
190         aaiClientProperties.setAaiHost(newConfiguration.aaiHost());
191         aaiClientProperties.setAaiPort(newConfiguration.aaiPort());
192         aaiClientProperties.setAaiProtocol(newConfiguration.aaiProtocol());
193         aaiClientProperties.setAaiUserName(newConfiguration.aaiUsername());
194         aaiClientProperties.setAaiUserPassword(newConfiguration.aaiPassword());
195         aaiClientProperties.setAaiIgnoreSslCertificateErrors(newConfiguration.aaiIgnoreSslCertificateErrors());
196         constructAaiConfiguration();
197
198
199         genericProperties.setPipelinesPollingIntervalSec(newConfiguration.pipelinesPollingIntervalSec());
200         genericProperties.setPipelinesTimeoutSec(newConfiguration.pipelinesTimeoutSec());
201         genericProperties.getReRegistration().setPolicyScope(newConfiguration.reRegistrationPolicyScope());
202         genericProperties.getReRegistration().setClControlName(newConfiguration.reRegistrationClControlName());
203         genericProperties.getCpeAuthentication().setPolicyScope(newConfiguration.cpeAuthPolicyScope());
204         genericProperties.getCpeAuthentication().setClControlName(newConfiguration.cpeAuthClControlName());
205
206         notifyObservers();
207     }
208
209     @NotNull
210     private GeneratedAppConfigObject.StreamsObject getStreamsObject(
211             Map<String, GeneratedAppConfigObject.StreamsObject> map, String configKey, String messageName) {
212         GeneratedAppConfigObject.StreamsObject streamsObject = map.get(configKey);
213         if (!streamsObject.type().equals(STREAMS_TYPE)) {
214             throw new ApplicationEnvironmentException(String.format("%s requires information about"
215                     + " message-router topic in ONAP", messageName));
216         }
217         return streamsObject;
218     }
219
220     private void constructConfigurationObjects() {
221         constructDmaapReRegistrationConfiguration();
222         constructDmaapCpeAuthenticationConfiguration();
223         constructDmaapProducerConfiguration();
224         constructAaiConfiguration();
225     }
226
227     private void constructDmaapReRegistrationConfiguration() {
228         dmaapReRegistrationConsumerConfiguration = new ImmutableDmaapConsumerConfiguration.Builder()
229                 .dmaapHostName(dmaapReRegistrationConsumerProperties.getDmaapHostName())
230                 .dmaapPortNumber(dmaapReRegistrationConsumerProperties.getDmaapPortNumber())
231                 .dmaapProtocol(dmaapReRegistrationConsumerProperties.getDmaapProtocol())
232                 .dmaapTopicName(dmaapReRegistrationConsumerProperties.getDmaapTopicName())
233                 .dmaapUserName(
234                         dmaapReRegistrationConsumerProperties.getDmaapUserName() == null ? "" :
235                                 dmaapReRegistrationConsumerProperties.getDmaapUserName())
236                 .dmaapUserPassword(
237                         dmaapReRegistrationConsumerProperties.getDmaapUserPassword() == null ? "" :
238                                 dmaapReRegistrationConsumerProperties.getDmaapUserPassword())
239                 .dmaapContentType(dmaapReRegistrationConsumerProperties.getDmaapContentType())
240                 .consumerId(dmaapReRegistrationConsumerProperties.getConsumerId())
241                 .consumerGroup(dmaapReRegistrationConsumerProperties.getConsumerGroup())
242                 .timeoutMs(dmaapReRegistrationConsumerProperties.getTimeoutMs())
243                 .messageLimit(dmaapReRegistrationConsumerProperties.getMessageLimit())
244                 .enableDmaapCertAuth(securityProperties.isEnableDmaapCertAuth())
245                 .keyStorePath(securityProperties.getKeyStorePath())
246                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
247                 .trustStorePath(securityProperties.getTrustStorePath())
248                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
249                 .build();
250     }
251
252     private void constructDmaapCpeAuthenticationConfiguration() {
253         dmaapCpeAuthenticationConsumerConfiguration = new ImmutableDmaapConsumerConfiguration.Builder()
254                 .dmaapHostName(dmaapCpeAuthenticationConsumerProperties.getDmaapHostName())
255                 .dmaapPortNumber(dmaapCpeAuthenticationConsumerProperties.getDmaapPortNumber())
256                 .dmaapProtocol(dmaapCpeAuthenticationConsumerProperties.getDmaapProtocol())
257                 .dmaapTopicName(dmaapCpeAuthenticationConsumerProperties.getDmaapTopicName())
258                 .dmaapUserName(
259                         dmaapCpeAuthenticationConsumerProperties.getDmaapUserName() == null ? "" :
260                                 dmaapCpeAuthenticationConsumerProperties.getDmaapUserName())
261                 .dmaapUserPassword(
262                         dmaapCpeAuthenticationConsumerProperties.getDmaapUserPassword() == null ? "" :
263                                 dmaapCpeAuthenticationConsumerProperties.getDmaapUserPassword())
264                 .dmaapContentType(dmaapCpeAuthenticationConsumerProperties.getDmaapContentType())
265                 .consumerId(dmaapCpeAuthenticationConsumerProperties.getConsumerId())
266                 .consumerGroup(dmaapCpeAuthenticationConsumerProperties.getConsumerGroup())
267                 .timeoutMs(dmaapCpeAuthenticationConsumerProperties.getTimeoutMs())
268                 .messageLimit(dmaapCpeAuthenticationConsumerProperties.getMessageLimit())
269                 .enableDmaapCertAuth(securityProperties.isEnableDmaapCertAuth())
270                 .keyStorePath(securityProperties.getKeyStorePath())
271                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
272                 .trustStorePath(securityProperties.getTrustStorePath())
273                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
274                 .build();
275     }
276
277     private void constructDmaapProducerConfiguration() {
278         dmaapPublisherConfiguration = new ImmutableDmaapPublisherConfiguration.Builder()
279                 .dmaapHostName(dmaapProducerProperties.getDmaapHostName())
280                 .dmaapPortNumber(dmaapProducerProperties.getDmaapPortNumber())
281                 .dmaapProtocol(dmaapProducerProperties.getDmaapProtocol())
282                 .dmaapTopicName(dmaapProducerProperties.getDmaapTopicName())
283                 .dmaapUserName(
284                         dmaapProducerProperties.getDmaapUserName() == null ? "" :
285                                 dmaapProducerProperties.getDmaapUserName())
286                 .dmaapUserPassword(
287                         dmaapProducerProperties.getDmaapUserPassword() == null ? "" :
288                                 dmaapProducerProperties.getDmaapUserPassword())
289                 .dmaapContentType(dmaapProducerProperties.getDmaapContentType())
290                 .enableDmaapCertAuth(securityProperties.isEnableDmaapCertAuth())
291                 .keyStorePath(securityProperties.getKeyStorePath())
292                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
293                 .trustStorePath(securityProperties.getTrustStorePath())
294                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
295                 .build();
296     }
297
298     private void constructAaiConfiguration() {
299         aaiClientConfiguration = new ImmutableAaiClientConfiguration.Builder()
300                 .aaiHost(aaiClientProperties.getAaiHost())
301                 .aaiPort(aaiClientProperties.getAaiPort())
302                 .aaiProtocol(aaiClientProperties.getAaiProtocol())
303                 .aaiUserName(aaiClientProperties.getAaiUserName())
304                 .aaiUserPassword(aaiClientProperties.getAaiUserPassword())
305                 .aaiHeaders(aaiClientProperties.getAaiHeaders())
306                 .aaiIgnoreSslCertificateErrors(aaiClientProperties.isAaiIgnoreSslCertificateErrors())
307                 .enableAaiCertAuth(securityProperties.isEnableAaiCertAuth())
308                 .keyStorePath(securityProperties.getKeyStorePath())
309                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
310                 .trustStorePath(securityProperties.getTrustStorePath())
311                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
312                 .build();
313     }
314
315     private TopicUrlInfo parseTopicUrl(String topicUrl) {
316         String[] urlTokens = topicUrl.split(":");
317         if (urlTokens.length != 3) {
318             throw new ConfigurationParsingException("Wrong topic URL format");
319         }
320         TopicUrlInfo topicUrlInfo = new TopicUrlInfo();
321         topicUrlInfo.setHost(urlTokens[1].replace("/", ""));
322
323         String[] tokensAfterHost = urlTokens[2].split("/events/");
324         if (tokensAfterHost.length != 2) {
325             throw new ConfigurationParsingException("Wrong topic name structure");
326         }
327         topicUrlInfo.setPort(Integer.valueOf(tokensAfterHost[0]));
328         topicUrlInfo.setTopicName("/events/" + tokensAfterHost[1]);
329
330         return topicUrlInfo;
331     }
332
333     private static class TopicUrlInfo {
334         private String host;
335         private int port;
336         private String topicName;
337
338         String getHost() {
339             return host;
340         }
341
342         void setHost(String host) {
343             this.host = host;
344         }
345
346         int getPort() {
347             return port;
348         }
349
350         void setPort(int port) {
351             this.port = port;
352         }
353
354         String getTopicName() {
355             return topicName;
356         }
357
358         void setTopicName(String topicName) {
359             this.topicName = topicName;
360         }
361     }
362 }