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=========================================================
21 package org.openecomp.sdc.notification.config;
23 import org.onap.sdc.tosca.services.YamlUtil;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.LinkedHashMap;
32 import java.util.function.BiConsumer;
34 public class ConfigurationManager {
36 private static final String CONFIGURATION_YAML_FILE = "onboarding_configuration.yaml";
37 private static final String NOTIFICATIONS_CONFIG = "notifications";
39 private LinkedHashMap<String, Object> notificationsConfiguration;
40 private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationManager.class);
41 private static final ConfigurationManager SINGLETON = new ConfigurationManager();
43 public static ConfigurationManager getInstance() {
47 private ConfigurationManager() {
51 private void initConfiguration() {
53 YamlUtil yamlUtil = new YamlUtil();
54 readConfigurationFromStream(yamlUtil, (filename, stream) -> {
57 LOGGER.warn("Configuration not found: " + filename + ". Using defaults");
61 Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream);
62 if (configurationMap == null) {
63 LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults");
67 notificationsConfiguration = configurationMap.get(NOTIFICATIONS_CONFIG);
68 if (notificationsConfiguration == null) {
69 LOGGER.error(NOTIFICATIONS_CONFIG +
70 " is missing in configuration file '" + filename + "'. Using defaults");
75 private void readConfigurationFromStream(YamlUtil yamlUtil,
76 BiConsumer<String, InputStream> reader) {
78 String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE);
82 if (configurationYamlFile == null) {
84 try (InputStream inputStream =
85 yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE)) {
86 reader.accept(CONFIGURATION_YAML_FILE, inputStream);
91 try (InputStream inputStream = new FileInputStream(configurationYamlFile)) {
92 reader.accept(configurationYamlFile, inputStream);
96 } catch (IOException e) {
97 LOGGER.error("Failed to read configuration", e);
101 public <T> T getConfigValue(String name, T defaultValue) {
103 Object value = notificationsConfiguration.get(name);
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);