aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/controller/ConfigLoader.java
blob: dbf52823c5cd0a3398b13e69e71c3f7ddf5e3096 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dcaegen2.collectors.ves
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2018 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dcae.controller;

import static org.onap.dcae.common.publishing.DMaaPConfigurationParser.parseToDomainMapping;
import static org.onap.dcae.controller.ConfigParsing.getDMaaPConfig;
import static org.onap.dcae.controller.ConfigParsing.getProperties;
import static org.onap.dcae.controller.EnvPropertiesReader.readEnvProps;

import io.vavr.Function0;
import io.vavr.Function1;
import io.vavr.collection.HashMap;
import io.vavr.collection.Map;
import io.vavr.control.Try;
import java.nio.file.Path;
import java.util.function.Consumer;
import org.json.JSONObject;
import org.onap.dcae.VesApplication;
import org.onap.dcae.common.publishing.PublisherConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigLoader {

    private static final String SKIP_MSG = "Skipping dynamic configuration update";
    private static Logger log = LoggerFactory.getLogger(ConfigLoader.class);
    private final Consumer<Map<String, PublisherConfig>> eventPublisherReconfigurer;
    private final ConfigFilesFacade configFilesFacade;
    private final Function1<EnvProps, Try<JSONObject>> configurationSource;
    private final Function0<Map<String, String>> envVariablesSupplier;
    private boolean toRestart = false;

    ConfigLoader(Consumer<Map<String, PublisherConfig>> eventPublisherReconfigurer,
        ConfigFilesFacade configFilesFacade,
        Function1<EnvProps, Try<JSONObject>> configurationSource,
        Function0<Map<String, String>> envVariablesSupplier) {
        this.eventPublisherReconfigurer = eventPublisherReconfigurer;
        this.configFilesFacade = configFilesFacade;
        this.configurationSource = configurationSource;
        this.envVariablesSupplier = envVariablesSupplier;
    }

    public static ConfigLoader create(
        Consumer<Map<String, PublisherConfig>> eventPublisherReconfigurer,
        Path dMaaPConfigFile, Path propertiesConfigFile) {
        return new ConfigLoader(eventPublisherReconfigurer,
            new ConfigFilesFacade(dMaaPConfigFile, propertiesConfigFile),
            ConfigSource::getAppConfig,
            () -> HashMap.ofAll(System.getenv()));
    }

    public void updateConfig() {
        log.info("Trying to dynamically update config from Config Binding Service");
        readEnvProps(envVariablesSupplier.get())
            .onEmpty(() -> log.warn(SKIP_MSG)).forEach(this::updateConfig);
    }

    private void updateConfig(EnvProps props) {
        configurationSource.apply(props)
            .onFailure(logSkip())
            .onSuccess(newConf -> {
                        updateConfigurationProperties(newConf);
                        updateDMaaPProperties(newConf);
                        reloadApplication();
                }
            );
    }

    private void reloadApplication() {
        if(toRestart){
            log.info("New app config - Application will be restarted");
            VesApplication.restartApplication();
        }
    }

    private void updateDMaaPProperties(JSONObject newConf) {
        configFilesFacade.readDMaaPConfiguration()
            .onFailure(logSkip())
            .onSuccess(oldDMaaPConf -> getDMaaPConfig(newConf)
                .onEmpty(() -> log.warn(SKIP_MSG))
                .forEach(newDMaaPConf -> compareAndOverwriteDMaaPConfig(oldDMaaPConf, newDMaaPConf)));
    }


    private void updateConfigurationProperties(JSONObject newConf) {
        configFilesFacade.readCollectorProperties()
            .onFailure(logSkip())
            .onSuccess(oldProps -> compareAndOverwritePropertiesConfig(newConf, oldProps));
    }

    private void compareAndOverwritePropertiesConfig(JSONObject newConf, Map<String, String> oldProps) {
        Map<String, String> newProperties = getProperties(newConf);
        Map<String, String> result = oldProps.filterKeys((s) ->  newProperties.keySet().contains(s));
        if (!result.equals(newProperties)) {
            configFilesFacade.writeProperties(newProperties)
                .onSuccess(__ -> {
                    toRestart= true;
                    log.info("New properties configuration written to file");
                 })
                .onFailure(logSkip());
        } else {
            log.info("Collector properties from CBS are the same as currently used ones. " + SKIP_MSG);
        }
    }

    private void compareAndOverwriteDMaaPConfig(JSONObject oldDMaaPConf, JSONObject newDMaaPConf) {
        if (!oldDMaaPConf.toString().equals(newDMaaPConf.toString())) {
            parseToDomainMapping(newDMaaPConf)
                .onFailure(exc -> log.error(SKIP_MSG, exc))
                .onSuccess(eventPublisherReconfigurer)
                .onSuccess(parsedConfig ->
                    configFilesFacade.writeDMaaPConfiguration(newDMaaPConf)
                        .onFailure(logSkip())
                        .onSuccess(__ -> {
                            toRestart= true;
                            log.info("New dMaaP configuration written to file");
                        }));
        } else {
            log.info("DMaaP config from CBS is the same as currently used one. " + SKIP_MSG);
        }
    }

    private Consumer<Throwable> logSkip() {
        return __ -> log.error(SKIP_MSG);
    }
}