2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dcae.configuration;
22 import io.vavr.collection.Map;
23 import io.vavr.control.Option;
24 import org.json.JSONObject;
25 import org.onap.dcae.configuration.cbs.CbsConfigResolver;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class ConfigLoader {
31 private static final Logger log = LoggerFactory.getLogger(ConfigLoader.class);
32 private final ConfigFilesFacade configFilesFacade;
33 private final CbsConfigResolver cbsConfigResolver;
34 private final Runnable applicationRestarter;
35 private boolean isApplicationRestartNeeded;
37 ConfigLoader(ConfigFilesFacade configFilesFacade, CbsConfigResolver cbsConfigResolver, Runnable applicationRestarter) {
38 this.configFilesFacade = configFilesFacade;
39 this.cbsConfigResolver = cbsConfigResolver;
40 this.applicationRestarter = applicationRestarter;
41 this.isApplicationRestartNeeded = false;
44 public synchronized void updateConfig() {
45 Option<JSONObject> appConfig = cbsConfigResolver.getAppConfig();
46 appConfig.peek(this::handleUpdate).onEmpty(logSkipMessage());
49 private Runnable logSkipMessage() {
50 return () -> log.info("Skipping dynamic configuration");
53 private void handleUpdate(JSONObject appConfig) {
54 updatePropertiesIfChanged(appConfig);
55 updateDmaapConfigIfChanged(appConfig);
56 restartApplicationIfNeeded();
59 private void updatePropertiesIfChanged(JSONObject appConfig) {
60 Map<String, String> newProperties = ConfigParsing.getProperties(appConfig);
61 Map<String, String> oldProperties = configFilesFacade.readCollectorProperties().get();
63 if (!areCommonPropertiesSame(oldProperties, newProperties)) {
64 configFilesFacade.writeProperties(newProperties);
65 isApplicationRestartNeeded = true;
69 private boolean areCommonPropertiesSame(Map<String, String> oldProperties, Map<String, String> newProperties) {
70 Map<String, String> filteredOldProperties = filterIntersectingKeys(oldProperties, newProperties);
71 return filteredOldProperties.equals(newProperties);
74 private Map<String, String> filterIntersectingKeys(Map<String, String> primaryProperties,
75 Map<String, String> otherProperties) {
76 return primaryProperties.filterKeys(key -> containsKey(key, otherProperties));
79 private boolean containsKey(String key, Map<String, String> properties) {
80 return properties.keySet().contains(key);
83 private void updateDmaapConfigIfChanged(JSONObject appConfig) {
84 JSONObject oldDmaapConfig = configFilesFacade.readDMaaPConfiguration().get();
85 JSONObject newDmaapConfig = ConfigParsing.getDMaaPConfig(appConfig).get();
87 if (!oldDmaapConfig.similar(newDmaapConfig)) {
88 configFilesFacade.writeDMaaPConfiguration(newDmaapConfig);
89 isApplicationRestartNeeded = true;
93 private void restartApplicationIfNeeded() {
94 if (isApplicationRestartNeeded) {
95 applicationRestarter.run();
96 isApplicationRestartNeeded = false;