Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-web / src / main / java / org / onap / dcae / analytics / web / spring / MongoAutoConfigurationPostProcessor.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.util.Arrays;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.onap.dcae.analytics.model.AnalyticsProfile;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.boot.SpringApplication;
31 import org.springframework.boot.env.EnvironmentPostProcessor;
32 import org.springframework.core.Ordered;
33 import org.springframework.core.env.ConfigurableEnvironment;
34 import org.springframework.core.env.MapPropertySource;
35 import org.springframework.core.env.MutablePropertySources;
36 import org.springframework.core.env.PropertySource;
37
38 /**
39  * Disables mongo auto configuration if {@link AnalyticsProfile#MONGO_PROFILE_NAME} is not present
40  *
41  * @author Rajiv Singla
42  */
43 public class MongoAutoConfigurationPostProcessor implements EnvironmentPostProcessor, Ordered {
44
45     private static final Logger logger = LoggerFactory.getLogger(MongoAutoConfigurationPostProcessor.class);
46
47     private static final String PROPERTY_SOURCE_NAME = "defaultProperties";
48     private static final String SPRING_AUTO_CONFIG_EXCLUDE_PROPERTY_KEY = "spring.autoconfigure.exclude";
49     private static final List<String> MONGO_AUTO_CONFIG_PROPERTIES = Arrays.asList(
50             "org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration",
51             "org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration",
52             "org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration");
53
54     @Override
55     public void postProcessEnvironment(final ConfigurableEnvironment environment, final SpringApplication application) {
56
57         final boolean isMongoProfileActive = Arrays.stream(environment.getActiveProfiles())
58                 .anyMatch(profile -> profile.equalsIgnoreCase(AnalyticsProfile.MONGO_PROFILE_NAME));
59
60         // if mongo profile is not active disable mongo auto configuration
61         if (!isMongoProfileActive) {
62             logger.info("Mongo Profile is not active - disabling Mongo Auto Configuration");
63             final Map<String, Object> mongoExcludePropsMap = new HashMap<>();
64             mongoExcludePropsMap.put(SPRING_AUTO_CONFIG_EXCLUDE_PROPERTY_KEY, MONGO_AUTO_CONFIG_PROPERTIES);
65             addMongoPropertiesIfAbsent(environment.getPropertySources(), mongoExcludePropsMap);
66         }
67     }
68
69     private void addMongoPropertiesIfAbsent(final MutablePropertySources propertySources,
70                                             final Map<String, Object> mongoPropertiesMap) {
71         MapPropertySource target = null;
72         if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
73             PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
74             if (source instanceof MapPropertySource) {
75                 target = (MapPropertySource) source;
76                 for (final Map.Entry<String, Object> entry : mongoPropertiesMap.entrySet()) {
77                     if (!target.containsProperty(entry.getKey())) {
78                         target.getSource().putIfAbsent(entry.getKey(), entry.getValue());
79                     }
80                 }
81             }
82         }
83         if (target == null) {
84             target = new MapPropertySource(PROPERTY_SOURCE_NAME, mongoPropertiesMap);
85         }
86         if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
87             propertySources.addLast(target);
88         }
89     }
90
91     @Override
92     public int getOrder() {
93         return Ordered.LOWEST_PRECEDENCE;
94     }
95 }