2 * ============LICENSE_START=======================================================
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
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.openecomp.sdc.notification.config;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.LinkedHashMap;
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;
32 public class ConfigurationManager {
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;
40 private ConfigurationManager() {
44 public static ConfigurationManager getInstance() {
48 private void initConfiguration() {
49 YamlUtil yamlUtil = new YamlUtil();
50 readConfigurationFromStream(yamlUtil, (filename, stream) -> {
52 LOGGER.warn("Configuration not found: " + filename + ". Using defaults");
55 Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream);
56 if (configurationMap == null) {
57 LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults");
60 notificationsConfiguration = configurationMap.get(NOTIFICATIONS_CONFIG);
61 if (notificationsConfiguration == null) {
62 LOGGER.error(NOTIFICATIONS_CONFIG + " is missing in configuration file '" + filename + "'. Using defaults");
67 private void readConfigurationFromStream(YamlUtil yamlUtil, BiConsumer<String, InputStream> reader) {
68 String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
70 if (configurationYamlFile == null) {
71 try (InputStream inputStream = yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE)) {
72 reader.accept(CONFIGURATION_YAML_FILE, inputStream);
75 try (InputStream inputStream = new FileInputStream(configurationYamlFile)) {
76 reader.accept(configurationYamlFile, inputStream);
79 } catch (IOException e) {
80 LOGGER.error("Failed to read configuration", e);
84 public <T> T getConfigValue(String name, T defaultValue) {
85 Object value = notificationsConfiguration.get(name);
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);