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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.bbs.event.processor.config;
 
  23 import static org.onap.bbs.event.processor.config.ApplicationConstants.STREAMS_TYPE;
 
  25 import java.util.HashSet;
 
  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;
 
  41 public class ApplicationConfiguration implements ConfigurationChangeObservable {
 
  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;
 
  50     private DmaapConsumerConfiguration dmaapReRegistrationConsumerConfiguration;
 
  51     private DmaapConsumerConfiguration dmaapCpeAuthenticationConsumerConfiguration;
 
  52     private DmaapPublisherConfiguration dmaapPublisherConfiguration;
 
  53     private AaiClientConfiguration aaiClientConfiguration;
 
  54     private Set<ConfigurationChangeObserver> observers;
 
  56     private int cbsPollingInterval;
 
  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
 
  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();
 
  85     public synchronized void register(ConfigurationChangeObserver observer) {
 
  86         observers.add(observer);
 
  90     public synchronized void unRegister(ConfigurationChangeObserver observer) {
 
  91         observers.remove(observer);
 
  95     public synchronized void notifyObservers() {
 
  96         observers.forEach(o -> o.updateConfiguration(this));
 
  99     public DmaapConsumerConfiguration getDmaapReRegistrationConsumerConfiguration() {
 
 100         return dmaapReRegistrationConsumerConfiguration;
 
 103     public DmaapConsumerConfiguration getDmaapCpeAuthenticationConsumerConfiguration() {
 
 104         return dmaapCpeAuthenticationConsumerConfiguration;
 
 107     public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
 
 108         return dmaapPublisherConfiguration;
 
 111     public AaiClientConfiguration getAaiClientConfiguration() {
 
 112         return aaiClientConfiguration;
 
 115     public int getPipelinesPollingIntervalInSeconds() {
 
 116         return genericProperties.getPipelinesPollingIntervalSec();
 
 119     public int getPipelinesTimeoutInSeconds() {
 
 120         return genericProperties.getPipelinesTimeoutSec();
 
 123     public int getCbsPollingInterval() {
 
 124         return cbsPollingInterval;
 
 127     public String getReRegistrationCloseLoopPolicyScope() {
 
 129         return genericProperties.getReRegistration().getPolicyScope();
 
 132     public String getReRegistrationCloseLoopControlName() {
 
 133         return genericProperties.getReRegistration().getClControlName();
 
 136     public String getCpeAuthenticationCloseLoopPolicyScope() {
 
 137         return genericProperties.getCpeAuthentication().getPolicyScope();
 
 140     public String getCpeAuthenticationCloseLoopControlName() {
 
 141         return genericProperties.getCpeAuthentication().getClControlName();
 
 145      * Update current configuration based on the new configuration object fetched from Consul via CBS service of DCAE.
 
 146      * @param newConfiguration updated configuration object
 
 148     public void updateCurrentConfiguration(GeneratedAppConfigObject newConfiguration) {
 
 150         cbsPollingInterval = newConfiguration.cbsPollingIntervalSec();
 
 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();
 
 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();
 
 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();
 
 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();
 
 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());
 
 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));
 
 217         return streamsObject;
 
 220     private void constructConfigurationObjects() {
 
 221         constructDmaapReRegistrationConfiguration();
 
 222         constructDmaapCpeAuthenticationConfiguration();
 
 223         constructDmaapProducerConfiguration();
 
 224         constructAaiConfiguration();
 
 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())
 
 234                         dmaapReRegistrationConsumerProperties.getDmaapUserName() == null ? "" :
 
 235                                 dmaapReRegistrationConsumerProperties.getDmaapUserName())
 
 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())
 
 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())
 
 259                         dmaapCpeAuthenticationConsumerProperties.getDmaapUserName() == null ? "" :
 
 260                                 dmaapCpeAuthenticationConsumerProperties.getDmaapUserName())
 
 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())
 
 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())
 
 284                         dmaapProducerProperties.getDmaapUserName() == null ? "" :
 
 285                                 dmaapProducerProperties.getDmaapUserName())
 
 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())
 
 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())
 
 315     private TopicUrlInfo parseTopicUrl(String topicUrl) {
 
 316         String[] urlTokens = topicUrl.split(":");
 
 317         if (urlTokens.length != 3) {
 
 318             throw new ConfigurationParsingException("Wrong topic URL format");
 
 320         TopicUrlInfo topicUrlInfo = new TopicUrlInfo();
 
 321         topicUrlInfo.setHost(urlTokens[1].replace("/", ""));
 
 323         String[] tokensAfterHost = urlTokens[2].split("/events/");
 
 324         if (tokensAfterHost.length != 2) {
 
 325             throw new ConfigurationParsingException("Wrong topic name structure");
 
 327         topicUrlInfo.setPort(Integer.valueOf(tokensAfterHost[0]));
 
 328         topicUrlInfo.setTopicName("/events/" + tokensAfterHost[1]);
 
 333     private static class TopicUrlInfo {
 
 336         private String topicName;
 
 342         void setHost(String host) {
 
 350         void setPort(int port) {
 
 354         String getTopicName() {
 
 358         void setTopicName(String topicName) {
 
 359             this.topicName = topicName;