2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2021 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.common;
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;
33 * This is a common class that can access the config file given in input to the JVM with the parameter -Dconfiguration.yaml=file.yaml.
35 public class CommonConfigurationManager {
37 public static final String JVM_PARAM_CONFIGURATION_FILE = "configuration.yaml";
38 private static final Logger LOGGER = LoggerFactory.getLogger(CommonConfigurationManager.class);
39 private static CommonConfigurationManager singletonInstance;
40 private Map<String, LinkedHashMap<String, Object>> configuration;
41 private String configFilename;
42 private String yamlSection;
44 protected CommonConfigurationManager() {
48 protected CommonConfigurationManager(String yamlSection) {
50 this.yamlSection = yamlSection;
53 public static synchronized CommonConfigurationManager getInstance() {
54 if (singletonInstance == null) {
55 singletonInstance = new CommonConfigurationManager();
57 return singletonInstance;
60 public void reload() {
64 private void initConfiguration() {
65 YamlUtil yamlUtil = new YamlUtil();
66 readConfigurationFromStream(yamlUtil, (filename, stream) -> {
67 this.configFilename = filename;
69 LOGGER.warn("Configuration not found: " + filename + ". Using defaults");
72 Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream);
73 if (configurationMap == null) {
74 LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults");
77 this.configuration = configurationMap;
82 private void readConfigurationFromStream(YamlUtil yamlUtil, BiConsumer<String, InputStream> reader) {
83 String configurationYamlFile = System.getProperty(JVM_PARAM_CONFIGURATION_FILE);
85 if (configurationYamlFile == null) {
86 try (InputStream inputStream = yamlUtil.loadYamlFileIs("/" + JVM_PARAM_CONFIGURATION_FILE)) {
87 reader.accept(JVM_PARAM_CONFIGURATION_FILE, inputStream);
90 try (InputStream inputStream = new FileInputStream(configurationYamlFile)) {
91 reader.accept(configurationYamlFile, inputStream);
94 } catch (IOException e) {
95 LOGGER.error("Failed to read configuration " + configurationYamlFile, e);
96 throw new RuntimeException("Failed to read configuration " + configurationYamlFile, e);
101 * This method can be used to access any yaml section configuration.
103 * @param yamlSection The yaml section that must be accessed
104 * @param name The configuration name inside that yaml section
105 * @param defaultValue A default value
106 * @param <T> The type of value to be returned
107 * @return The value found or the default value if not found
109 public <T> T getConfigValue(String yamlSection, String name, T defaultValue) {
110 Map<String, Object> section = this.configuration.get(yamlSection);
111 if (section == null) {
112 LOGGER.error("Section " + yamlSection + " is missing in configuration file '" + configFilename +
113 "'. Using defaults");
116 Object value = section.get(name);
118 return value == null ? defaultValue : (T) value;
119 } catch (ClassCastException e) {
121 String.format("Failed to read configuration property '%s' as requested type. Using default '%s'",
122 name, defaultValue), e);
128 * This method can be used to access a specific configuration parameter in the configuration in the yamlSection predefined in the constructor.
130 * @param name The name of the config
131 * @param defaultValue A default value
132 * @param <T> The type of value to be returned
133 * @return The value found or the default value if not found
135 public <T> T getConfigValue(String name, T defaultValue) {
136 return this.getConfigValue(yamlSection, name, defaultValue);