fb8070759e29220768c502e825034f08a44ce931
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / controller / ConfigLoader.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2018 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.dcae.controller;
22
23 import static org.onap.dcae.commonFunction.event.publishing.DMaaPConfigurationParser.parseToDomainMapping;
24 import static org.onap.dcae.controller.ConfigParsing.getDMaaPConfig;
25 import static org.onap.dcae.controller.ConfigParsing.getProperties;
26 import static org.onap.dcae.controller.EnvPropertiesReader.readEnvProps;
27
28 import io.vavr.Function0;
29 import io.vavr.Function1;
30 import io.vavr.collection.HashMap;
31 import io.vavr.collection.Map;
32 import io.vavr.control.Try;
33 import java.nio.file.Path;
34 import java.util.function.Consumer;
35 import org.json.JSONObject;
36 import org.onap.dcae.commonFunction.event.publishing.EventPublisher;
37 import org.onap.dcae.commonFunction.event.publishing.PublisherConfig;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class ConfigLoader {
42
43     private static final String SKIP_MSG = "Skipping dynamic configuration update";
44     private static Logger log = LoggerFactory.getLogger(ConfigLoader.class);
45     private final Consumer<Map<String, PublisherConfig>> eventPublisherReconfigurer;
46     private final ConfigFilesFacade configFilesFacade;
47     private final Function1<EnvProps, Try<JSONObject>> configurationSource;
48     private final Function0<Map<String, String>> envVariablesSupplier;
49
50     ConfigLoader(Consumer<Map<String, PublisherConfig>> eventPublisherReconfigurer,
51                  ConfigFilesFacade configFilesFacade,
52                  Function1<EnvProps, Try<JSONObject>> configurationSource,
53                  Function0<Map<String, String>> envVariablesSupplier) {
54         this.eventPublisherReconfigurer = eventPublisherReconfigurer;
55         this.configFilesFacade = configFilesFacade;
56         this.configurationSource = configurationSource;
57         this.envVariablesSupplier = envVariablesSupplier;
58     }
59
60     public static ConfigLoader create(Consumer<Map<String, PublisherConfig>> eventPublisherReconfigurer,
61                                       Path dMaaPConfigFile, Path propertiesConfigFile) {
62         return new ConfigLoader(eventPublisherReconfigurer,
63             new ConfigFilesFacade(dMaaPConfigFile, propertiesConfigFile),
64             ConfigSource::getAppConfig,
65             () -> HashMap.ofAll(System.getenv()));
66     }
67
68     public void updateConfig() {
69         log.info("Trying to dynamically update config from Config Binding Service");
70         readEnvProps(envVariablesSupplier.get())
71             .onEmpty(() -> log.warn(SKIP_MSG))
72             .forEach(props -> updateConfig(props));
73     }
74
75     private void updateConfig(EnvProps props) {
76         configurationSource.apply(props)
77             .onFailure(logSkip())
78             .onSuccess(newConf -> {
79                     updateConfigurationProperties(newConf);
80                     updateDMaaPProperties(newConf);
81                 }
82             );
83     }
84
85     private void updateDMaaPProperties(JSONObject newConf) {
86         configFilesFacade.readDMaaPConfiguration()
87             .onFailure(logSkip())
88             .onSuccess(oldDMaaPConf -> getDMaaPConfig(newConf)
89                 .onEmpty(() -> log.warn(SKIP_MSG))
90                 .forEach(newDMaaPConf -> compareAndOverwriteDMaaPConfig(oldDMaaPConf, newDMaaPConf)));
91     }
92
93
94     private void updateConfigurationProperties(JSONObject newConf) {
95         configFilesFacade.readCollectorProperties()
96             .onFailure(logSkip())
97             .onSuccess(oldProps -> compareAndOverwritePropertiesConfig(newConf, oldProps));
98     }
99
100     private void compareAndOverwritePropertiesConfig(JSONObject newConf, Map<String, String> oldProps) {
101         Map<String, String> newProperties = getProperties(newConf);
102         if (!oldProps.equals(newProperties)) {
103             configFilesFacade.writeProperties(newProperties)
104                 .onSuccess(__ -> log.info("New properties configuration written to file"))
105                 .onFailure(logSkip());
106         } else {
107             log.info("Collector properties from CBS are the same as currently used ones. " + SKIP_MSG);
108         }
109     }
110
111     private void compareAndOverwriteDMaaPConfig(JSONObject oldDMaaPConf, JSONObject newDMaaPConf) {
112         if (!oldDMaaPConf.toString().equals(newDMaaPConf.toString())) {
113             parseToDomainMapping(newDMaaPConf)
114                 .onFailure(exc -> log.error(SKIP_MSG, exc))
115                 .onSuccess(eventPublisherReconfigurer)
116                 .onSuccess(parsedConfig ->
117                     configFilesFacade.writeDMaaPConfiguration(newDMaaPConf)
118                         .onFailure(logSkip())
119                         .onSuccess(__ -> log.info("New dMaaP configuration written to file")));
120         } else {
121             log.info("DMaaP config from CBS is the same as currently used one. " + SKIP_MSG);
122         }
123     }
124
125     private Consumer<Throwable> logSkip() {
126         return __ -> log.error(SKIP_MSG);
127     }
128 }