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) 2020 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcae.configuration;
23 import static io.vavr.API.Try;
24 import static org.onap.dcae.common.publishing.VavrUtils.enhanceError;
25 import static org.onap.dcae.common.publishing.VavrUtils.f;
26 import static org.onap.dcae.common.publishing.VavrUtils.logError;
27 import static org.onap.dcae.configuration.Conversions.toList;
29 import io.vavr.CheckedRunnable;
30 import io.vavr.Tuple2;
31 import io.vavr.collection.Map;
32 import io.vavr.control.Try;
33 import java.io.FileNotFoundException;
34 import java.nio.charset.StandardCharsets;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import org.apache.commons.configuration.ConfigurationException;
38 import org.apache.commons.configuration.PropertiesConfiguration;
39 import org.json.JSONObject;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 class ConfigFilesFacade {
45 private static final Logger log = LoggerFactory.getLogger(ConfigFilesFacade.class);
47 private final Path dMaaPConfigPath;
48 private final Path propertiesPath;
50 ConfigFilesFacade(Path propertiesPath, Path dMaaPConfigPath) {
51 this.propertiesPath = propertiesPath;
52 this.dMaaPConfigPath = dMaaPConfigPath;
55 Try<Map<String, String>> readCollectorProperties() {
56 log.info(f("Reading collector properties from path: '%s'", propertiesPath));
57 return Try(this::readProperties)
58 .map(prop -> toList(prop.getKeys()).toMap(k -> k, k -> (String) prop.getProperty(k)))
59 .mapFailure(enhanceError("Unable to read properties configuration from path '%s'", propertiesPath))
60 .onFailure(logError(log))
61 .peek(props -> log.info(f("Read following collector properties: '%s'", props)));
64 Try<JSONObject> readDMaaPConfiguration() {
65 log.info(f("Reading DMaaP configuration from file: '%s'", dMaaPConfigPath));
66 return readFile(dMaaPConfigPath)
67 .recover(FileNotFoundException.class, __ -> "{}")
68 .mapFailure(enhanceError("Unable to read DMaaP configuration from file '%s'", dMaaPConfigPath))
69 .flatMap(Conversions::toJson)
70 .onFailure(logError(log))
71 .peek(props -> log.info(f("Read following DMaaP properties: '%s'", props)));
74 Try<Void> writeDMaaPConfiguration(JSONObject dMaaPConfiguration) {
75 log.info(f("Writing DMaaP configuration '%s' into file '%s'", dMaaPConfiguration, dMaaPConfigPath));
76 return writeFile(dMaaPConfigPath, indentConfiguration(dMaaPConfiguration.toString()))
77 .mapFailure(enhanceError("Could not save new DMaaP configuration to path '%s'", dMaaPConfigPath))
78 .onFailure(logError(log))
79 .peek(__ -> log.info("Written successfully"));
83 Try<Void> writeProperties(Map<String, String> properties) {
84 log.info(f("Writing properties configuration '%s' into file '%s'", properties, propertiesPath));
85 return Try.run(saveProperties(properties))
86 .mapFailure(enhanceError("Could not save properties to path '%s'", properties))
87 .onFailure(logError(log))
88 .peek(__ -> log.info("Written successfully"));
91 private Try<String> readFile(Path path) {
92 return Try(() -> new String(Files.readAllBytes(path), StandardCharsets.UTF_8))
93 .mapFailure(enhanceError("Could not read content from path: '%s'", path));
96 private Try<Void> writeFile(Path path, String content) {
97 return Try.run(() -> Files.write(path, content.getBytes()))
98 .mapFailure(enhanceError("Could not write content to path: '%s'", path));
101 private PropertiesConfiguration readProperties() throws ConfigurationException {
102 PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
103 propertiesConfiguration.setDelimiterParsingDisabled(true);
104 propertiesConfiguration.load(propertiesPath.toFile());
105 return propertiesConfiguration;
108 private CheckedRunnable saveProperties(Map<String, String> properties) {
110 PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(propertiesPath.toFile());
111 propertiesConfiguration.setEncoding(null);
112 for (Tuple2<String, String> property : properties) {
113 updateProperty(propertiesConfiguration, property);
115 propertiesConfiguration.save();
119 private void updateProperty(PropertiesConfiguration propertiesConfiguration, Tuple2<String, String> property) {
120 if (propertiesConfiguration.containsKey(property._1)) {
121 propertiesConfiguration.setProperty(property._1, property._2);
123 propertiesConfiguration.addProperty(property._1, property._2);
127 private String indentConfiguration(String configuration) {
128 return new JSONObject(configuration).toString(4);