Config fetch for VESCollector through DCAE-SDK (CBS Client)
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / configuration / ConfigLoader.java
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.configuration;
21
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;
28
29 public class ConfigLoader {
30
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;
36
37     ConfigLoader(ConfigFilesFacade configFilesFacade, CbsConfigResolver cbsConfigResolver, Runnable applicationRestarter) {
38         this.configFilesFacade = configFilesFacade;
39         this.cbsConfigResolver = cbsConfigResolver;
40         this.applicationRestarter = applicationRestarter;
41         this.isApplicationRestartNeeded = false;
42     }
43
44     public synchronized void updateConfig() {
45         Option<JSONObject> appConfig = cbsConfigResolver.getAppConfig();
46         appConfig.peek(this::handleUpdate).onEmpty(logSkipMessage());
47     }
48
49     private Runnable logSkipMessage() {
50         return () -> log.info("Skipping dynamic configuration");
51     }
52
53     private void handleUpdate(JSONObject appConfig) {
54         updatePropertiesIfChanged(appConfig);
55         updateDmaapConfigIfChanged(appConfig);
56         restartApplicationIfNeeded();
57     }
58
59     private void updatePropertiesIfChanged(JSONObject appConfig) {
60         Map<String, String> newProperties = ConfigParsing.getProperties(appConfig);
61         Map<String, String> oldProperties = configFilesFacade.readCollectorProperties().get();
62
63         if (!areCommonPropertiesSame(oldProperties, newProperties)) {
64             configFilesFacade.writeProperties(newProperties);
65             isApplicationRestartNeeded = true;
66         }
67     }
68
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);
72     }
73
74     private Map<String, String> filterIntersectingKeys(Map<String, String> primaryProperties,
75         Map<String, String> otherProperties) {
76         return primaryProperties.filterKeys(key -> containsKey(key, otherProperties));
77     }
78
79     private boolean containsKey(String key, Map<String, String> properties) {
80         return properties.keySet().contains(key);
81     }
82
83     private void updateDmaapConfigIfChanged(JSONObject appConfig) {
84         JSONObject oldDmaapConfig = configFilesFacade.readDMaaPConfiguration().get();
85         JSONObject newDmaapConfig = ConfigParsing.getDMaaPConfig(appConfig).get();
86
87         if (!oldDmaapConfig.similar(newDmaapConfig)) {
88             configFilesFacade.writeDMaaPConfiguration(newDmaapConfig);
89             isApplicationRestartNeeded = true;
90         }
91     }
92
93     private void restartApplicationIfNeeded() {
94         if (isApplicationRestartNeeded) {
95             applicationRestarter.run();
96             isApplicationRestartNeeded = false;
97         }
98     }
99 }