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