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 org.onap.sdc.tosca.services.YamlUtil;
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.LinkedHashMap;
30 import java.util.function.BiConsumer;
33 * This is a common class that can access the config file given in input to the JVM with the parameter
34 * -Dconfiguration.yaml=file.yaml.
36 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 private void initConfiguration() {
61 YamlUtil yamlUtil = new YamlUtil();
62 readConfigurationFromStream(yamlUtil, (filename, stream) -> {
63 this.configFilename = filename;
65 LOGGER.warn("Configuration not found: " + filename + ". Using defaults");
68 Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream);
69 if (configurationMap == null) {
70 LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults");
73 this.configuration = configurationMap;
78 private void readConfigurationFromStream(YamlUtil yamlUtil, BiConsumer<String, InputStream> reader) {
79 String configurationYamlFile = System.getProperty(JVM_PARAM_CONFIGURATION_FILE);
81 if (configurationYamlFile == null) {
82 try (InputStream inputStream = yamlUtil.loadYamlFileIs("/" + JVM_PARAM_CONFIGURATION_FILE)) {
83 reader.accept(JVM_PARAM_CONFIGURATION_FILE, inputStream);
86 try (InputStream inputStream = new FileInputStream(configurationYamlFile)) {
87 reader.accept(configurationYamlFile, inputStream);
90 } catch (IOException e) {
91 LOGGER.error("Failed to read configuration " + configurationYamlFile, e);
92 throw new RuntimeException("Failed to read configuration " + configurationYamlFile, e);
97 * This method can be used to access any yaml section configuration.
99 * @param yamlSection The yaml section that must be accessed
100 * @param name The configuration name inside that yaml section
101 * @param defaultValue A default value
102 * @param <T> The type of value to be returned
103 * @return The value found or the default value if not found
105 public <T> T getConfigValue(String yamlSection, String name, T defaultValue) {
106 Map<String, Object> section = this.configuration.get(yamlSection);
107 if (section == null) {
108 LOGGER.error("Section " + yamlSection + " is missing in configuration file '" + configFilename +
109 "'. Using defaults");
112 Object value = section.get(name);
114 return value == null ? defaultValue : (T) value;
115 } catch (ClassCastException e) {
117 String.format("Failed to read configuration property '%s' as requested type. Using default '%s'",
118 name, defaultValue), e);
124 * This method can be used to access a specific configuration parameter in the configuration in the
125 * yamlSection predefined in the constructor.
127 * @param name The name of the config
128 * @param defaultValue A default value
129 * @param <T> The type of value to be returned
130 * @return The value found or the default value if not found
132 public <T> T getConfigValue(String name, T defaultValue) {
133 return this.getConfigValue(yamlSection, name, defaultValue);