aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/configuration/ConfigFilesFacade.java
blob: c94e6c30ca384b6e7b41906f51f0a47a1422ed52 (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
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dcaegen2.collectors.ves
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2020 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.configuration;

import static io.vavr.API.Try;
import static org.onap.dcae.common.publishing.VavrUtils.enhanceError;
import static org.onap.dcae.common.publishing.VavrUtils.f;
import static org.onap.dcae.common.publishing.VavrUtils.logError;
import static org.onap.dcae.configuration.Conversions.toList;

import io.vavr.CheckedRunnable;
import io.vavr.Tuple2;
import io.vavr.collection.Map;
import io.vavr.control.Try;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ConfigFilesFacade is used for reading and writing application properties and dmaap configuration.
 */
public class ConfigFilesFacade {

    private static final Logger log = LoggerFactory.getLogger(ConfigFilesFacade.class);

    private Path dmaapConfigPath;
    private Path propertiesPath;

    ConfigFilesFacade(Path propertiesPath, Path dMaaPConfigPath) {
        this.propertiesPath = propertiesPath;
        this.dmaapConfigPath = dMaaPConfigPath;
    }

    /**
     * Set new paths
     * @param propertiesFile application property file
     * @param dmaapConfigFile dmaap configuration file
     */
    public void setPaths(Path propertiesFile, Path dmaapConfigFile) {
        this.propertiesPath = propertiesFile;
        this.dmaapConfigPath = dmaapConfigFile;
    }

    Try<Map<String, String>> readCollectorProperties() {
        log.info(f("Reading collector properties from path: '%s'", propertiesPath));
        return Try(this::readProperties)
            .map(prop -> toList(prop.getKeys()).toMap(k -> k, k -> (String) prop.getProperty(k)))
            .mapFailure(enhanceError("Unable to read properties configuration from path '%s'", propertiesPath))
            .onFailure(logError(log))
            .peek(props -> log.info(f("Read following collector properties: '%s'", props)));
    }

    Try<JSONObject> readDMaaPConfiguration() {
        log.info(f("Reading DMaaP configuration from file: '%s'", dmaapConfigPath));
        return readFile(dmaapConfigPath)
            .recover(FileNotFoundException.class, __ -> "{}")
            .mapFailure(enhanceError("Unable to read DMaaP configuration from file '%s'", dmaapConfigPath))
            .flatMap(Conversions::toJson)
            .onFailure(logError(log))
            .peek(props -> log.info(f("Read following DMaaP properties: '%s'", props)));
    }

    Try<Void> writeDMaaPConfiguration(JSONObject dMaaPConfiguration) {
        log.info(f("Writing DMaaP configuration '%s' into file '%s'", dMaaPConfiguration, dmaapConfigPath));
        return writeFile(dmaapConfigPath, indentConfiguration(dMaaPConfiguration.toString()))
            .mapFailure(enhanceError("Could not save new DMaaP configuration to path '%s'", dmaapConfigPath))
            .onFailure(logError(log))
            .peek(__ -> log.info("Written successfully"));
    }


    Try<Void> writeProperties(Map<String, String> properties) {
        log.info(f("Writing properties configuration '%s' into file '%s'", properties, propertiesPath));
        return Try.run(saveProperties(properties))
            .mapFailure(enhanceError("Could not save properties to path '%s'", properties))
            .onFailure(logError(log))
            .peek(__ -> log.info("Written successfully"));
    }

    private Try<String> readFile(Path path) {
        return Try(() -> new String(Files.readAllBytes(path), StandardCharsets.UTF_8))
            .mapFailure(enhanceError("Could not read content from path: '%s'", path));
    }

    private Try<Void> writeFile(Path path, String content) {
        return Try.run(() -> Files.write(path, content.getBytes()))
            .mapFailure(enhanceError("Could not write content to path: '%s'", path));
    }

    private PropertiesConfiguration readProperties() throws ConfigurationException {
        PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
        propertiesConfiguration.setDelimiterParsingDisabled(true);
        propertiesConfiguration.load(propertiesPath.toFile());
        return propertiesConfiguration;
    }

    private CheckedRunnable saveProperties(Map<String, String> properties) {
        return () -> {
            PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(propertiesPath.toFile());
            propertiesConfiguration.setEncoding(null);
            for (Tuple2<String, String> property : properties) {
                updateProperty(propertiesConfiguration, property);
            }
            propertiesConfiguration.save();
        };
    }

    private void updateProperty(PropertiesConfiguration propertiesConfiguration, Tuple2<String, String> property) {
        if (propertiesConfiguration.containsKey(property._1)) {
            propertiesConfiguration.setProperty(property._1, property._2);
        } else {
            propertiesConfiguration.addProperty(property._1, property._2);
        }
    }

    private String indentConfiguration(String configuration) {
        return new JSONObject(configuration).toString(4);
    }
}