e597de0f2023f2a82ae68458ba03071952499dbd
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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.openecomp.sdc.notification.config;
21
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.function.BiConsumer;
28 import org.onap.sdc.tosca.services.YamlUtil;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31
32 public class ConfigurationManager {
33
34     private static final String CONFIGURATION_YAML_FILE = "onboarding_configuration.yaml";
35     private static final String NOTIFICATIONS_CONFIG = "notifications";
36     private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationManager.class);
37     private static final ConfigurationManager SINGLETON = new ConfigurationManager();
38     private LinkedHashMap<String, Object> notificationsConfiguration;
39
40     private ConfigurationManager() {
41         initConfiguration();
42     }
43
44     public static ConfigurationManager getInstance() {
45         return SINGLETON;
46     }
47
48     private void initConfiguration() {
49         YamlUtil yamlUtil = new YamlUtil();
50         readConfigurationFromStream(yamlUtil, (filename, stream) -> {
51             if (stream == null) {
52                 LOGGER.warn("Configuration not found: " + filename + ". Using defaults");
53                 return;
54             }
55             Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream);
56             if (configurationMap == null) {
57                 LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults");
58                 return;
59             }
60             notificationsConfiguration = configurationMap.get(NOTIFICATIONS_CONFIG);
61             if (notificationsConfiguration == null) {
62                 LOGGER.error(NOTIFICATIONS_CONFIG + " is missing in configuration file '" + filename + "'. Using defaults");
63             }
64         });
65     }
66
67     private void readConfigurationFromStream(YamlUtil yamlUtil, BiConsumer<String, InputStream> reader) {
68         String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
69         try {
70             if (configurationYamlFile == null) {
71                 try (InputStream inputStream = yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE)) {
72                     reader.accept(CONFIGURATION_YAML_FILE, inputStream);
73                 }
74             } else {
75                 try (InputStream inputStream = new FileInputStream(configurationYamlFile)) {
76                     reader.accept(configurationYamlFile, inputStream);
77                 }
78             }
79         } catch (IOException e) {
80             LOGGER.error("Failed to read configuration", e);
81         }
82     }
83
84     public <T> T getConfigValue(String name, T defaultValue) {
85         Object value = notificationsConfiguration.get(name);
86         try {
87             return value == null ? defaultValue : (T) value;
88         } catch (ClassCastException e) {
89             LOGGER.warn(String.format("Failed to read configuration property '%s' as requested type. Using default '%s'", name, defaultValue), e);
90             return defaultValue;
91         }
92     }
93 }