2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.dcaegen2.collectors.datafile.configuration;
21 import com.google.gson.JsonObject;
22 import java.util.Optional;
23 import java.util.Properties;
24 import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration;
25 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
26 import org.onap.dcaegen2.collectors.datafile.model.EnvProperties;
27 import org.onap.dcaegen2.collectors.datafile.service.DatafileConfigurationProvider;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.boot.context.properties.EnableConfigurationProperties;
33 import org.springframework.context.annotation.Configuration;
34 import org.springframework.context.annotation.Primary;
35 import org.springframework.scheduling.annotation.EnableScheduling;
36 import reactor.core.publisher.Flux;
37 import reactor.core.scheduler.Schedulers;
40 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
43 @EnableConfigurationProperties
46 public class CloudConfiguration extends AppConfig {
48 private static final Logger logger = LoggerFactory.getLogger(CloudConfiguration.class);
50 private DatafileConfigurationProvider datafileConfigurationProvider;
51 private DmaapPublisherConfiguration dmaapPublisherCloudConfiguration;
52 private DmaapConsumerConfiguration dmaapConsumerCloudConfiguration;
54 @Value("#{systemEnvironment}")
55 private Properties systemEnvironment;
58 public void setThreadPoolTaskScheduler(DatafileConfigurationProvider datafileConfigurationProvider) {
59 this.datafileConfigurationProvider = datafileConfigurationProvider;
63 protected void runTask() {
64 Flux.defer(() -> EnvironmentProcessor.evaluate(systemEnvironment))
65 .subscribeOn(Schedulers.parallel())
66 .subscribe(this::parsingConfigSuccess, this::parsingConfigError);
69 private void parsingConfigError(Throwable throwable) {
70 logger.warn("Error in case of processing system environment, more details below: ", throwable);
73 private void cloudConfigError(Throwable throwable) {
74 logger.warn("Exception during getting configuration from CONSUL/CONFIG_BINDING_SERVICE ", throwable);
77 private void parsingConfigSuccess(EnvProperties envProperties) {
78 logger.info("Fetching Datafile Collector configuration from ConfigBindingService/Consul");
79 datafileConfigurationProvider.callForDataFileCollectorConfiguration(envProperties)
80 .subscribe(this::parseCloudConfig, this::cloudConfigError);
83 private void parseCloudConfig(JsonObject jsonObject) {
84 logger.info("Received application configuration: {}", jsonObject);
85 CloudConfigParser cloudConfigParser = new CloudConfigParser(jsonObject);
86 dmaapPublisherCloudConfiguration = cloudConfigParser.getDmaapPublisherConfig();
87 dmaapConsumerCloudConfiguration = cloudConfigParser.getDmaapConsumerConfig();
91 public DmaapPublisherConfiguration getDmaapPublisherConfiguration() {
92 return Optional.ofNullable(dmaapPublisherCloudConfiguration).orElse(super.getDmaapPublisherConfiguration());
96 public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
97 return Optional.ofNullable(dmaapConsumerCloudConfiguration).orElse(super.getDmaapConsumerConfiguration());