46747fb88194dccb0736e086d03fa5921aaa2e9e
[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 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;
29 import java.util.Map;
30 import java.util.function.BiConsumer;
31
32 /**
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.
35  */
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;
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     private void initConfiguration() {
61         YamlUtil yamlUtil = new YamlUtil();
62         readConfigurationFromStream(yamlUtil, (filename, stream) -> {
63             this.configFilename = filename;
64             if (stream == null) {
65                 LOGGER.warn("Configuration not found: " + filename + ". Using defaults");
66                 return;
67             }
68             Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream);
69             if (configurationMap == null) {
70                 LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults");
71                 return;
72             } else {
73                 this.configuration = configurationMap;
74             }
75         });
76     }
77
78     private void readConfigurationFromStream(YamlUtil yamlUtil, BiConsumer<String, InputStream> reader) {
79         String configurationYamlFile = System.getProperty(JVM_PARAM_CONFIGURATION_FILE);
80         try {
81             if (configurationYamlFile == null) {
82                 try (InputStream inputStream = yamlUtil.loadYamlFileIs("/" + JVM_PARAM_CONFIGURATION_FILE)) {
83                     reader.accept(JVM_PARAM_CONFIGURATION_FILE, inputStream);
84                 }
85             } else {
86                 try (InputStream inputStream = new FileInputStream(configurationYamlFile)) {
87                     reader.accept(configurationYamlFile, inputStream);
88                 }
89             }
90         } catch (IOException e) {
91             LOGGER.error("Failed to read configuration " + configurationYamlFile, e);
92             throw new RuntimeException("Failed to read configuration " + configurationYamlFile, e);
93         }
94     }
95
96     /**
97      * This method can be used to access any yaml section configuration.
98      *
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
104      */
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");
110             return defaultValue;
111         }
112         Object value = section.get(name);
113         try {
114             return value == null ? defaultValue : (T) value;
115         } catch (ClassCastException e) {
116             LOGGER.warn(
117                     String.format("Failed to read configuration property '%s' as requested type. Using default '%s'",
118                             name, defaultValue), e);
119             return defaultValue;
120         }
121     }
122
123     /**
124      * This method can be used to access a specific configuration parameter in the configuration in the
125      * yamlSection predefined in the constructor.
126      *
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
131      */
132     public <T> T getConfigValue(String name, T defaultValue) {
133         return this.getConfigValue(yamlSection, name, defaultValue);
134     }
135 }