981d9633b0907a7f18ef7ac0512e147534761eee
[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             GeneratedAppConfigObject.StreamsObject reRegObject =
178                     getStreamsObject(newConfiguration.streamSubscribesMap(), newConfiguration.reRegConfigKey(),
179                             "PNF Re-Registration");
180             TopicUrlInfo topicUrlInfo = parseTopicUrl(reRegObject.dmaapInfo().topicUrl());
181             dmaapReRegistrationConsumerProperties.setDmaapHostName(topicUrlInfo.getHost());
182             dmaapReRegistrationConsumerProperties.setDmaapPortNumber(topicUrlInfo.getPort());
183             dmaapReRegistrationConsumerProperties.setDmaapProtocol(newConfiguration.dmaapProtocol());
184             dmaapReRegistrationConsumerProperties.setDmaapContentType(newConfiguration.dmaapContentType());
185             dmaapReRegistrationConsumerProperties.setDmaapTopicName(topicUrlInfo.getTopicName());
186             dmaapReRegistrationConsumerProperties.setConsumerId(newConfiguration.dmaapConsumerConsumerId());
187             dmaapReRegistrationConsumerProperties.setConsumerGroup(newConfiguration.dmaapConsumerConsumerGroup());
188             dmaapReRegistrationConsumerProperties.setMessageLimit(newConfiguration.dmaapMessageLimit());
189             dmaapReRegistrationConsumerProperties.setTimeoutMs(newConfiguration.dmaapTimeoutMs());
190             constructDmaapReRegistrationConfiguration();
191
192             GeneratedAppConfigObject.StreamsObject cpeAuthObject =
193                     getStreamsObject(newConfiguration.streamSubscribesMap(), newConfiguration.cpeAuthConfigKey(),
194                             "CPE Authentication");
195             topicUrlInfo = parseTopicUrl(cpeAuthObject.dmaapInfo().topicUrl());
196             dmaapCpeAuthenticationConsumerProperties.setDmaapHostName(topicUrlInfo.getHost());
197             dmaapCpeAuthenticationConsumerProperties.setDmaapPortNumber(topicUrlInfo.getPort());
198             dmaapCpeAuthenticationConsumerProperties.setDmaapProtocol(newConfiguration.dmaapProtocol());
199             dmaapCpeAuthenticationConsumerProperties.setDmaapContentType(newConfiguration.dmaapContentType());
200             dmaapCpeAuthenticationConsumerProperties.setDmaapTopicName(topicUrlInfo.getTopicName());
201             dmaapCpeAuthenticationConsumerProperties.setConsumerId(newConfiguration.dmaapConsumerConsumerId());
202             dmaapCpeAuthenticationConsumerProperties.setConsumerGroup(newConfiguration.dmaapConsumerConsumerGroup());
203             dmaapCpeAuthenticationConsumerProperties.setMessageLimit(newConfiguration.dmaapMessageLimit());
204             dmaapCpeAuthenticationConsumerProperties.setTimeoutMs(newConfiguration.dmaapTimeoutMs());
205             constructDmaapCpeAuthenticationConfiguration();
206
207             GeneratedAppConfigObject.StreamsObject closeLoopObject =
208                     getStreamsObject(newConfiguration.streamPublishesMap(), newConfiguration.closeLoopConfigKey(),
209                             "Close Loop");
210             topicUrlInfo = parseTopicUrl(closeLoopObject.dmaapInfo().topicUrl());
211             dmaapProducerProperties.setDmaapHostName(topicUrlInfo.getHost());
212             dmaapProducerProperties.setDmaapPortNumber(topicUrlInfo.getPort());
213             dmaapProducerProperties.setDmaapProtocol(newConfiguration.dmaapProtocol());
214             dmaapProducerProperties.setDmaapContentType(newConfiguration.dmaapContentType());
215             dmaapProducerProperties.setDmaapTopicName(topicUrlInfo.getTopicName());
216             constructDmaapProducerConfiguration();
217
218             aaiClientProperties.setAaiHost(newConfiguration.aaiHost());
219             aaiClientProperties.setAaiPort(newConfiguration.aaiPort());
220             aaiClientProperties.setAaiProtocol(newConfiguration.aaiProtocol());
221             aaiClientProperties.setAaiUserName(newConfiguration.aaiUsername());
222             aaiClientProperties.setAaiUserPassword(newConfiguration.aaiPassword());
223             aaiClientProperties.setAaiIgnoreSslCertificateErrors(newConfiguration.aaiIgnoreSslCertificateErrors());
224             constructAaiConfiguration();
225
226             genericProperties.setPipelinesPollingIntervalSec(newConfiguration.pipelinesPollingIntervalSec());
227             genericProperties.setPipelinesTimeoutSec(newConfiguration.pipelinesTimeoutSec());
228             genericProperties.setPolicyVersion(newConfiguration.policyVersion());
229             genericProperties.setClTargetType(newConfiguration.closeLoopTargetType());
230             genericProperties.setClEventStatus(newConfiguration.closeLoopEventStatus());
231             genericProperties.setClVersion(newConfiguration.closeLoopVersion());
232             genericProperties.setClTarget(newConfiguration.closeLoopTarget());
233             genericProperties.setClOriginator(newConfiguration.closeLoopOriginator());
234             genericProperties.getReRegistration().setPolicyScope(newConfiguration.reRegistrationPolicyScope());
235             genericProperties.getReRegistration().setClControlName(newConfiguration.reRegistrationClControlName());
236             genericProperties.getCpeAuthentication().setPolicyScope(newConfiguration.cpeAuthPolicyScope());
237             genericProperties.getCpeAuthentication().setClControlName(newConfiguration.cpeAuthClControlName());
238
239             LoggingUtil.changeLoggingLevel(newConfiguration.loggingLevel());
240         }
241
242         notifyObservers();
243     }
244
245     @NotNull
246     private GeneratedAppConfigObject.StreamsObject getStreamsObject(
247             Map<String, GeneratedAppConfigObject.StreamsObject> map, String configKey, String messageName) {
248         GeneratedAppConfigObject.StreamsObject streamsObject = map.get(configKey);
249         if (!STREAMS_TYPE.equals(streamsObject.type())) {
250             throw new ApplicationEnvironmentException(String.format("%s requires information about"
251                     + " message-router topic in ONAP", messageName));
252         }
253         return streamsObject;
254     }
255
256     private void constructConfigurationObjects() {
257         constructDmaapReRegistrationConfiguration();
258         constructDmaapCpeAuthenticationConfiguration();
259         constructDmaapProducerConfiguration();
260         constructAaiConfiguration();
261     }
262
263     private void constructDmaapReRegistrationConfiguration() {
264         dmaapReRegistrationConsumerConfiguration = new ImmutableDmaapConsumerConfiguration.Builder()
265                 .dmaapHostName(dmaapReRegistrationConsumerProperties.getDmaapHostName())
266                 .dmaapPortNumber(dmaapReRegistrationConsumerProperties.getDmaapPortNumber())
267                 .dmaapProtocol(dmaapReRegistrationConsumerProperties.getDmaapProtocol())
268                 .dmaapTopicName(dmaapReRegistrationConsumerProperties.getDmaapTopicName())
269                 .dmaapUserName(
270                         dmaapReRegistrationConsumerProperties.getDmaapUserName() == null ? "" :
271                                 dmaapReRegistrationConsumerProperties.getDmaapUserName())
272                 .dmaapUserPassword(
273                         dmaapReRegistrationConsumerProperties.getDmaapUserPassword() == null ? "" :
274                                 dmaapReRegistrationConsumerProperties.getDmaapUserPassword())
275                 .dmaapContentType(dmaapReRegistrationConsumerProperties.getDmaapContentType())
276                 .consumerId(dmaapReRegistrationConsumerProperties.getConsumerId())
277                 .consumerGroup(dmaapReRegistrationConsumerProperties.getConsumerGroup())
278                 .timeoutMs(dmaapReRegistrationConsumerProperties.getTimeoutMs())
279                 .messageLimit(dmaapReRegistrationConsumerProperties.getMessageLimit())
280                 .enableDmaapCertAuth(securityProperties.isEnableDmaapCertAuth())
281                 .keyStorePath(securityProperties.getKeyStorePath())
282                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
283                 .trustStorePath(securityProperties.getTrustStorePath())
284                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
285                 .build();
286     }
287
288     private void constructDmaapCpeAuthenticationConfiguration() {
289         dmaapCpeAuthenticationConsumerConfiguration = new ImmutableDmaapConsumerConfiguration.Builder()
290                 .dmaapHostName(dmaapCpeAuthenticationConsumerProperties.getDmaapHostName())
291                 .dmaapPortNumber(dmaapCpeAuthenticationConsumerProperties.getDmaapPortNumber())
292                 .dmaapProtocol(dmaapCpeAuthenticationConsumerProperties.getDmaapProtocol())
293                 .dmaapTopicName(dmaapCpeAuthenticationConsumerProperties.getDmaapTopicName())
294                 .dmaapUserName(
295                         dmaapCpeAuthenticationConsumerProperties.getDmaapUserName() == null ? "" :
296                                 dmaapCpeAuthenticationConsumerProperties.getDmaapUserName())
297                 .dmaapUserPassword(
298                         dmaapCpeAuthenticationConsumerProperties.getDmaapUserPassword() == null ? "" :
299                                 dmaapCpeAuthenticationConsumerProperties.getDmaapUserPassword())
300                 .dmaapContentType(dmaapCpeAuthenticationConsumerProperties.getDmaapContentType())
301                 .consumerId(dmaapCpeAuthenticationConsumerProperties.getConsumerId())
302                 .consumerGroup(dmaapCpeAuthenticationConsumerProperties.getConsumerGroup())
303                 .timeoutMs(dmaapCpeAuthenticationConsumerProperties.getTimeoutMs())
304                 .messageLimit(dmaapCpeAuthenticationConsumerProperties.getMessageLimit())
305                 .enableDmaapCertAuth(securityProperties.isEnableDmaapCertAuth())
306                 .keyStorePath(securityProperties.getKeyStorePath())
307                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
308                 .trustStorePath(securityProperties.getTrustStorePath())
309                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
310                 .build();
311     }
312
313     private void constructDmaapProducerConfiguration() {
314         dmaapPublisherConfiguration = new ImmutableDmaapPublisherConfiguration.Builder()
315                 .dmaapHostName(dmaapProducerProperties.getDmaapHostName())
316                 .dmaapPortNumber(dmaapProducerProperties.getDmaapPortNumber())
317                 .dmaapProtocol(dmaapProducerProperties.getDmaapProtocol())
318                 .dmaapTopicName(dmaapProducerProperties.getDmaapTopicName())
319                 .dmaapUserName(
320                         dmaapProducerProperties.getDmaapUserName() == null ? "" :
321                                 dmaapProducerProperties.getDmaapUserName())
322                 .dmaapUserPassword(
323                         dmaapProducerProperties.getDmaapUserPassword() == null ? "" :
324                                 dmaapProducerProperties.getDmaapUserPassword())
325                 .dmaapContentType(dmaapProducerProperties.getDmaapContentType())
326                 .enableDmaapCertAuth(securityProperties.isEnableDmaapCertAuth())
327                 .keyStorePath(securityProperties.getKeyStorePath())
328                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
329                 .trustStorePath(securityProperties.getTrustStorePath())
330                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
331                 .build();
332     }
333
334     private void constructAaiConfiguration() {
335         aaiClientConfiguration = new ImmutableAaiClientConfiguration.Builder()
336                 .aaiHost(aaiClientProperties.getAaiHost())
337                 .aaiPort(aaiClientProperties.getAaiPort())
338                 .aaiProtocol(aaiClientProperties.getAaiProtocol())
339                 .aaiUserName(aaiClientProperties.getAaiUserName())
340                 .aaiUserPassword(aaiClientProperties.getAaiUserPassword())
341                 .aaiHeaders(aaiClientProperties.getAaiHeaders())
342                 .aaiIgnoreSslCertificateErrors(aaiClientProperties.isAaiIgnoreSslCertificateErrors())
343                 .enableAaiCertAuth(securityProperties.isEnableAaiCertAuth())
344                 .keyStorePath(securityProperties.getKeyStorePath())
345                 .keyStorePasswordPath(securityProperties.getKeyStorePasswordPath())
346                 .trustStorePath(securityProperties.getTrustStorePath())
347                 .trustStorePasswordPath(securityProperties.getTrustStorePasswordPath())
348                 .build();
349     }
350
351     private TopicUrlInfo parseTopicUrl(String topicUrl) {
352         String[] urlTokens = topicUrl.split(":");
353         if (urlTokens.length != 3) {
354             throw new ConfigurationParsingException("Wrong topic URL format");
355         }
356         TopicUrlInfo topicUrlInfo = new TopicUrlInfo();
357         topicUrlInfo.setHost(urlTokens[1].replace("/", ""));
358
359         String[] tokensAfterHost = urlTokens[2].split("/events/");
360         if (tokensAfterHost.length != 2) {
361             throw new ConfigurationParsingException("Wrong topic name structure");
362         }
363         topicUrlInfo.setPort(Integer.valueOf(tokensAfterHost[0]));
364         topicUrlInfo.setTopicName("/events/" + tokensAfterHost[1]);
365
366         return topicUrlInfo;
367     }
368
369     private static class TopicUrlInfo {
370         private String host;
371         private int port;
372         private String topicName;
373
374         String getHost() {
375             return host;
376         }
377
378         void setHost(String host) {
379             this.host = host;
380         }
381
382         int getPort() {
383             return port;
384         }
385
386         void setPort(int port) {
387             this.port = port;
388         }
389
390         String getTopicName() {
391             return topicName;
392         }
393
394         void setTopicName(String topicName) {
395             this.topicName = topicName;
396         }
397     }
398 }