2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.configuration;
19 import com.google.gson.GsonBuilder;
20 import com.google.gson.JsonElement;
21 import com.google.gson.JsonObject;
22 import com.google.gson.JsonParser;
23 import com.google.gson.JsonSyntaxException;
24 import com.google.gson.TypeAdapterFactory;
26 import java.io.BufferedInputStream;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.time.Duration;
33 import java.util.Properties;
34 import java.util.ServiceLoader;
36 import javax.validation.constraints.NotEmpty;
37 import javax.validation.constraints.NotNull;
39 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
40 import org.onap.dcaegen2.collectors.datafile.model.logging.MappedDiagnosticContext;
41 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
42 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties;
43 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.beans.factory.annotation.Value;
48 import org.springframework.boot.context.properties.ConfigurationProperties;
49 import org.springframework.boot.context.properties.EnableConfigurationProperties;
50 import org.springframework.context.annotation.ComponentScan;
51 import org.springframework.stereotype.Component;
53 import reactor.core.Disposable;
54 import reactor.core.publisher.Flux;
55 import reactor.core.publisher.Mono;
58 * Holds all configuration for the DFC.
60 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18
61 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
65 @ComponentScan("org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers")
66 @EnableConfigurationProperties
67 @ConfigurationProperties("app")
68 public class AppConfig {
69 private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
71 private ConsumerConfiguration dmaapConsumerConfiguration;
72 private Map<String, PublisherConfiguration> publishingConfiguration;
73 private FtpesConfig ftpesConfiguration;
74 private CloudConfigurationProvider cloudConfigurationProvider;
75 @Value("#{systemEnvironment}")
76 Properties systemEnvironment;
77 private Disposable refreshConfigTask = null;
80 private String filepath;
83 public synchronized void setCloudConfigurationProvider(
84 CloudConfigurationProvider reactiveCloudConfigurationProvider) {
85 this.cloudConfigurationProvider = reactiveCloudConfigurationProvider;
88 public synchronized void setFilepath(String filepath) {
89 this.filepath = filepath;
93 * Reads the cloud configuration.
95 public void initialize() {
97 Map<String, String> context = MappedDiagnosticContext.initializeTraceContext();
98 loadConfigurationFromFile();
100 refreshConfigTask = Flux.interval(Duration.ZERO, Duration.ofMinutes(5))
101 .flatMap(count -> createRefreshConfigurationTask(count, context))
102 .subscribe(e -> logger.info("Refreshed configuration data"),
103 throwable -> logger.error("Configuration refresh terminated due to exception", throwable),
104 () -> logger.error("Configuration refresh terminated"));
108 if (refreshConfigTask != null) {
109 refreshConfigTask.dispose();
110 refreshConfigTask = null;
114 public synchronized ConsumerConfiguration getDmaapConsumerConfiguration() {
115 return dmaapConsumerConfiguration;
118 public synchronized boolean isFeedConfigured(String changeIdentifier) {
119 return publishingConfiguration.containsKey(changeIdentifier);
122 public synchronized PublisherConfiguration getPublisherConfiguration(String changeIdentifier)
123 throws DatafileTaskException {
125 if (publishingConfiguration == null) {
126 throw new DatafileTaskException("No PublishingConfiguration loaded, changeIdentifier: " + changeIdentifier);
128 PublisherConfiguration cfg = publishingConfiguration.get(changeIdentifier);
130 throw new DatafileTaskException(
131 "Cannot find getPublishingConfiguration for changeIdentifier: " + changeIdentifier);
136 public synchronized FtpesConfig getFtpesConfiguration() {
137 return ftpesConfiguration;
140 Flux<AppConfig> createRefreshConfigurationTask(Long counter, Map<String, String> context) {
141 return Flux.just(counter) //
142 .doOnNext(cnt -> logger.debug("Refresh config {}", cnt)) //
143 .flatMap(cnt -> readEnvironmentVariables(systemEnvironment, context)) //
144 .flatMap(this::fetchConfiguration);
147 Mono<EnvProperties> readEnvironmentVariables(Properties systemEnvironment, Map<String, String> context) {
148 return EnvironmentProcessor.readEnvironmentVariables(systemEnvironment, context)
149 .onErrorResume(AppConfig::onErrorResume);
152 private static <R> Mono<R> onErrorResume(Throwable trowable) {
153 logger.error("Could not refresh application configuration {}", trowable.toString());
157 private Mono<AppConfig> fetchConfiguration(EnvProperties env) {
158 Mono<JsonObject> serviceCfg = cloudConfigurationProvider.callForServiceConfigurationReactive(env) //
159 .onErrorResume(AppConfig::onErrorResume);
161 // Note, have to use this callForServiceConfigurationReactive with EnvProperties, since the
162 // other ones does not work
163 EnvProperties dmaapEnv = ImmutableEnvProperties.builder() //
164 .consulHost(env.consulHost()) //
165 .consulPort(env.consulPort()) //
166 .cbsName(env.cbsName()) //
167 .appName(env.appName() + ":dmaap") //
169 Mono<JsonObject> dmaapCfg = cloudConfigurationProvider.callForServiceConfigurationReactive(dmaapEnv)
170 .onErrorResume(t -> Mono.just(new JsonObject()));
172 return serviceCfg.zipWith(dmaapCfg, this::parseCloudConfig) //
173 .onErrorResume(AppConfig::onErrorResume);
177 * Parse configuration.
179 * @param serviceConfigRootObject the DFC service's configuration
180 * @param dmaapConfigRootObject if there is no dmaapConfigRootObject, the dmaap feeds are taken from the
181 * serviceConfigRootObject
182 * @return this which is updated if successful
184 private AppConfig parseCloudConfig(JsonObject serviceConfigRootObject, JsonObject dmaapConfigRootObject) {
186 CloudConfigParser parser = new CloudConfigParser(serviceConfigRootObject, dmaapConfigRootObject);
187 setConfiguration(parser.getDmaapConsumerConfig(), parser.getDmaapPublisherConfig(),
188 parser.getFtpesConfig());
189 } catch (DatafileTaskException e) {
190 logger.error("Could not parse configuration {}", e.toString(), e);
196 * Reads the configuration from file.
198 void loadConfigurationFromFile() {
199 GsonBuilder gsonBuilder = new GsonBuilder();
200 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
202 try (InputStream inputStream = createInputStream(filepath)) {
203 JsonParser parser = new JsonParser();
204 JsonObject rootObject = getJsonElement(parser, inputStream).getAsJsonObject();
205 if (rootObject == null) {
206 throw new JsonSyntaxException("Root is not a json object");
208 parseCloudConfig(rootObject, rootObject);
209 } catch (JsonSyntaxException | IOException e) {
210 logger.warn("Local configuration file not loaded: {}", filepath, e);
214 private synchronized void setConfiguration(ConsumerConfiguration consumerConfiguration,
215 Map<String, PublisherConfiguration> publisherConfiguration, FtpesConfig ftpesConfig) {
216 if (consumerConfiguration == null || publisherConfiguration == null || ftpesConfig == null) {
218 "Problem with configuration consumerConfiguration: {}, publisherConfiguration: {}, ftpesConfig: {}",
219 consumerConfiguration, publisherConfiguration, ftpesConfig);
221 this.dmaapConsumerConfiguration = consumerConfiguration;
222 this.publishingConfiguration = publisherConfiguration;
223 this.ftpesConfiguration = ftpesConfig;
227 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
228 return parser.parse(new InputStreamReader(inputStream));
231 InputStream createInputStream(@NotNull String filepath) throws IOException {
232 return new BufferedInputStream(new FileInputStream(filepath));