Implement improved MinIo client
[sdc.git] / openecomp-be / lib / openecomp-common-lib / src / main / java / org / openecomp / sdc / common / CommonConfigurationManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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 package org.openecomp.sdc.common;
21
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
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;
31
32 /**
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.
34  */
35 public class CommonConfigurationManager {
36
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;
43
44     protected CommonConfigurationManager() {
45         initConfiguration();
46     }
47
48     protected CommonConfigurationManager(String yamlSection) {
49         this();
50         this.yamlSection = yamlSection;
51     }
52
53     public static synchronized CommonConfigurationManager getInstance() {
54         if (singletonInstance == null) {
55             singletonInstance = new CommonConfigurationManager();
56         }
57         return singletonInstance;
58     }
59
60     public void reload() {
61         initConfiguration();
62     }
63
64     private void initConfiguration() {
65         YamlUtil yamlUtil = new YamlUtil();
66         readConfigurationFromStream(yamlUtil, (filename, stream) -> {
67             this.configFilename = filename;
68             if (stream == null) {
69                 LOGGER.warn("Configuration not found: " + filename + ". Using defaults");
70                 return;
71             }
72             Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream);
73             if (configurationMap == null) {
74                 LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults");
75                 return;
76             } else {
77                 this.configuration = configurationMap;
78             }
79         });
80     }
81
82     private void readConfigurationFromStream(YamlUtil yamlUtil, BiConsumer<String, InputStream> reader) {
83         String configurationYamlFile = System.getProperty(JVM_PARAM_CONFIGURATION_FILE);
84         try {
85             if (configurationYamlFile == null) {
86                 try (InputStream inputStream = yamlUtil.loadYamlFileIs("/" + JVM_PARAM_CONFIGURATION_FILE)) {
87                     reader.accept(JVM_PARAM_CONFIGURATION_FILE, inputStream);
88                 }
89             } else {
90                 try (InputStream inputStream = new FileInputStream(configurationYamlFile)) {
91                     reader.accept(configurationYamlFile, inputStream);
92                 }
93             }
94         } catch (IOException e) {
95             LOGGER.error("Failed to read configuration " + configurationYamlFile, e);
96             throw new RuntimeException("Failed to read configuration " + configurationYamlFile, e);
97         }
98     }
99
100     /**
101      * This method can be used to access any yaml section configuration.
102      *
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
108      */
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");
114             return defaultValue;
115         }
116         Object value = section.get(name);
117         try {
118             return value == null ? defaultValue : (T) value;
119         } catch (ClassCastException e) {
120             LOGGER.warn(
121                 String.format("Failed to read configuration property '%s' as requested type. Using default '%s'",
122                     name, defaultValue), e);
123             return defaultValue;
124         }
125     }
126
127     /**
128      * This method can be used to access a specific configuration parameter in the configuration in the yamlSection predefined in the constructor.
129      *
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
134      */
135     public <T> T getConfigValue(String name, T defaultValue) {
136         return this.getConfigValue(yamlSection, name, defaultValue);
137     }
138 }