add periodic fetch configuration
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-web / src / main / java / org / onap / dcae / analytics / web / spring / ConfigBindingServiceEnvironmentPostProcessor.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.web.spring;
21
22 import java.time.Duration;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29
30 import org.onap.dcae.analytics.model.AnalyticsProfile;
31 import org.onap.dcae.analytics.model.configbindingservice.ConfigBindingServiceConstants;
32 import org.onap.dcae.analytics.model.util.function.JsonStringToMapFunction;
33 import org.onap.dcae.analytics.web.config.SystemConfig;
34 import org.onap.dcae.analytics.web.exception.AnalyticsValidationException;
35 import org.onap.dcae.analytics.web.exception.EnvironmentLoaderException;
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
37 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
38 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
39 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
40 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
41 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
42 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.boot.SpringApplication;
46 import org.springframework.boot.env.EnvironmentPostProcessor;
47 import org.springframework.core.Ordered;
48 import org.springframework.core.env.ConfigurableEnvironment;
49 import org.springframework.core.env.MapPropertySource;
50 import org.springframework.core.env.MutablePropertySources;
51 import org.springframework.core.env.PropertySource;
52 import org.springframework.core.env.StandardEnvironment;
53 import org.springframework.util.ClassUtils;
54 import org.springframework.web.context.support.StandardServletEnvironment;
55
56 import com.google.gson.JsonElement;
57 import com.google.gson.JsonObject;
58
59 import reactor.core.Disposable;
60 import reactor.core.publisher.Flux;
61 import reactor.core.publisher.Mono;
62
63 /**
64  * A custom spring framework environment post processor which can fetch and populate spring context with
65  * Config Binding Service application properties.
66  * <p>
67  * Activated only when config binding service profile is active.
68  *
69  * @author Rajiv Singla
70  */
71 public class ConfigBindingServiceEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
72
73     private static final Logger logger = LoggerFactory.getLogger(ConfigBindingServiceEnvironmentPostProcessor.class);
74     private static final String SERVLET_ENVIRONMENT_CLASS =
75             "org.springframework.web.context.support.StandardServletEnvironment";
76
77     private static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE;
78
79     private Disposable refreshConfigTask = null;
80
81     private ConfigurableEnvironment env = null;
82
83     private Map<String, Object> filterKeyMap = null;
84
85     private String configServicePropertiesKey =
86             ConfigBindingServiceConstants.CONFIG_BINDING_SERVICE_PROPERTIES_KEY;
87
88     @Override
89     public void postProcessEnvironment(final ConfigurableEnvironment environment,
90             final SpringApplication application) {
91
92         final boolean isConfigServiceProfilePresent = Arrays.stream(environment.getActiveProfiles())
93                 .anyMatch(p -> p.equalsIgnoreCase(AnalyticsProfile.CONFIG_BINDING_SERVICE_PROFILE_NAME));
94
95         if (!isConfigServiceProfilePresent) {
96             logger.info("Config Binding Service Profile is not active. "
97                     + "Skipping Adding config binding service properties");
98             return;
99         }
100
101         logger.info("Config Binding Service Profile is active. "
102                 + "Application properties will be fetched from config binding service");
103
104         env = environment;
105         initialize();
106
107     }
108
109     @Override
110     public int getOrder() {
111         return DEFAULT_ORDER;
112     }
113
114     public synchronized void addJsonPropertySource(final MutablePropertySources sources,
115             final PropertySource<?> source) {
116         final String name = findPropertySource(sources);
117         if (sources.contains(name)) {
118             sources.addBefore(name, source);
119         } else {
120             sources.addFirst(source);
121         }
122     }
123
124     private String findPropertySource(final MutablePropertySources sources) {
125         if (ClassUtils.isPresent(SERVLET_ENVIRONMENT_CLASS, null)
126                 && sources.contains(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)) {
127             return StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME;
128
129         }
130         return StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME;
131     }
132
133     /**
134      *
135      * Fetch the configuration.
136      *
137      */
138     public void initialize() {
139         stop();
140
141         refreshConfigTask = createRefreshTask() //
142                 .subscribe(e -> logger.info("Refreshed configuration data"),
143                         throwable -> logger.error("Configuration refresh terminated due to exception", throwable),
144                         () -> logger.error("Configuration refresh terminated"));
145     }
146
147     /**
148      * Fetch the configuration task from CBS.
149      *
150      */
151     private Flux<String> createRefreshTask() {
152         return readEnvironmentVariables() //
153                 .flatMap(this::createCbsClient) //
154                 .flatMapMany(this::periodicConfigurationUpdates) //
155                 .map(this::parseTcaConfig) //
156                 .onErrorResume(this::onErrorResume);
157     }
158
159     /**
160      * periodicConfigurationUpdates.
161      *
162      * @param cbsClient cbsClient
163      * @return configuration refreshed
164      *
165      */
166     public Flux<JsonObject> periodicConfigurationUpdates(CbsClient cbsClient) {
167         final Duration initialDelay = Duration.ZERO;
168         final Duration refreshPeriod =
169                  Duration.ofMinutes(ConfigBindingServiceConstants.CONFIG_SERVICE_REFRESHPERIOD);
170         final CbsRequest getConfigRequest = CbsRequests.getAll(RequestDiagnosticContext.create());
171         return cbsClient.updates(getConfigRequest, initialDelay, refreshPeriod);
172     }
173
174     /**
175      *
176      * get environment variables.
177      *
178      * @return environment properties.
179      *
180      */
181     public Mono<EnvProperties> readEnvironmentVariables() {
182         logger.trace("Loading configuration from system environment variables");
183         EnvProperties envProperties;
184         try {
185             envProperties = ImmutableEnvProperties.builder() //
186                     .consulHost(SystemConfig.getConsulHost()) //
187                     .consulPort(SystemConfig.getConsultPort()) //
188                     .cbsName(SystemConfig.getConfigBindingService()) //
189                     .appName(SystemConfig.getService()) //
190                     .build();
191         } catch (EnvironmentLoaderException e) {
192             return Mono.error(e);
193         }
194         logger.trace("Evaluated environment system variables {}", envProperties);
195         return Mono.just(envProperties);
196     }
197
198     /**
199      * Stops the refreshing of the configuration.
200      *
201      */
202     public void stop() {
203         if (refreshConfigTask != null) {
204             refreshConfigTask.dispose();
205             refreshConfigTask = null;
206         }
207     }
208
209     /**
210      * periodicConfigurationUpdates.
211      *
212      * @param throwable throwable
213      * @return Mono
214      *
215      */
216     private <R> Mono<R> onErrorResume(Throwable throwable) {
217         logger.error("Could not refresh application configuration {}", throwable.toString());
218         return Mono.empty();
219     }
220
221     /**
222      * create CbsClient.
223      *
224      * @param env environment properties
225      * @return cbsclient
226      *
227      */
228     public Mono<CbsClient> createCbsClient(EnvProperties env) {
229         return CbsClientFactory.createCbsClient(env);
230     }
231
232     /**
233      * Parse configuration.
234      *
235      * @param jsonObject the TCA service's configuration
236      * @return this
237      *
238      */
239     public String parseTcaConfig(JsonObject jsonObject) {
240
241         JsonElement jsonConfig = jsonObject.get(ConfigBindingServiceConstants.CONFIG);
242
243         Optional<String> configServiceJsonOptional = Optional.of(jsonConfig.toString());
244         if (!configServiceJsonOptional.isPresent()) {
245             final String errorMessage =
246                     "Unable to get fetch application configuration from config binding service";
247             throw new AnalyticsValidationException(errorMessage,
248                     new IllegalStateException(errorMessage));
249         }
250
251         // convert fetch config binding service json string to Map of property key and
252         // values
253         Map<String, Object> configPropertiesMap = configServiceJsonOptional
254                 .map(new JsonStringToMapFunction(configServicePropertiesKey)).orElse(Collections.emptyMap());
255
256         if (configPropertiesMap.isEmpty()) {
257
258             logger.warn("No properties found in config binding service");
259
260         } else {
261
262             // remove config service key prefix on spring reserved property key prefixes
263             final Set<String> springKeyPrefixes =
264                     ConfigBindingServiceConstants.SPRING_RESERVED_PROPERTIES_KEY_PREFIXES;
265             final Set<String> springKeys = springKeyPrefixes.stream()
266                     .map(springKeyPrefix -> configServicePropertiesKey + "." + springKeyPrefix)
267                     .collect(Collectors.toSet());
268
269             filterKeyMap = configPropertiesMap.entrySet().stream()
270                     .collect(Collectors.toMap((Map.Entry<String, Object> e) -> springKeys.stream()
271                             .anyMatch(springKey -> e.getKey().startsWith(springKey))
272                                     ? e.getKey().substring(configServicePropertiesKey.toCharArray().length + 1)
273                                     : e.getKey(),
274                             Map.Entry::getValue));
275
276             filterKeyMap.forEach((key, value) -> logger
277                     .info("Adding property from config service in spring context: {} -> {}", key, value));
278             MutablePropertySources sources = env.getPropertySources();
279             addJsonPropertySource(sources, new MapPropertySource(configServicePropertiesKey, filterKeyMap));
280
281         }
282         return configServiceJsonOptional.get();
283     }
284
285 }