AAI-common sonar fixes
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / util / AAIConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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
21 package org.onap.aai.util;
22
23 import org.eclipse.jetty.util.security.Password;
24 import org.onap.aai.exceptions.AAIException;
25 import org.onap.aai.logging.ErrorLogHelper;
26
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.InetAddress;
31 import java.util.Properties;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class AAIConfig {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(AAIConfig.class);
38     private static final String GLOBAL_PROP_FILE_NAME = AAIConstants.AAI_CONFIG_FILENAME;
39     private static Properties serverProps;
40     private static boolean propsInitialized = false;
41
42     /**
43      * Instantiates a new AAI config.
44      */
45     // Don't instantiate
46     private AAIConfig() {
47     }
48
49     /**
50      * Inits the.
51      *
52      * @throws AAIException the AAI exception
53      */
54     public synchronized static void init() throws AAIException {
55
56         LOGGER.info("Initializing AAIConfig");
57
58         AAIConfig.getConfigFile();
59         AAIConfig.reloadConfig();
60
61         if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {
62             ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
63         } else {
64             LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
65         }
66     }
67
68     /**
69      * Gets the config file.
70      *
71      * @return the config file
72      */
73     public static String getConfigFile() {
74         return GLOBAL_PROP_FILE_NAME;
75     }
76
77     /**
78      * Reload config.
79      */
80     public synchronized static void reloadConfig() {
81
82         String propFileName = GLOBAL_PROP_FILE_NAME;
83         Properties newServerProps = new Properties();
84
85         LOGGER.debug("Reloading config from " + propFileName);
86
87         try (InputStream is = new FileInputStream(propFileName)) {
88             LOGGER.info("Found the aaiconfig.properties in the following location: {}", GLOBAL_PROP_FILE_NAME);
89             newServerProps.load(is);
90             propsInitialized = true;
91             serverProps = newServerProps;
92         } catch (Exception fnfe) {
93             final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("aaiconfig.properties");
94             LOGGER.info("Unable to find the aaiconfig.properties from filesystem so using file in jar");
95             if (is != null) {
96                 try {
97                     newServerProps.load(is);
98                     serverProps = newServerProps;
99                 } catch (IOException e) {
100                     LOGGER.warn("Encountered IO Exception during loading of aaiconfig props from inputstream", e);
101                 }
102             } else {
103                 LOGGER.error("Expected to find the error.properties in the jar but unable to find it");
104             }
105         }
106     }
107
108     /**
109      * Gets the.
110      *
111      * @param key the key
112      * @param defaultValue the default value
113      * @return the string
114      */
115     public static String get(String key, String defaultValue) {
116         String result = defaultValue;
117         try {
118             result = get(key);
119         } catch (AAIException a) {
120         }
121         if (result == null || result.isEmpty()) {
122             result = defaultValue;
123         }
124         return (result);
125     }
126
127     /**
128      * Gets the.
129      *
130      * @param key the key
131      * @return the string
132      * @throws AAIException the AAI exception
133      */
134     public static String get(String key) throws AAIException {
135         String response = null;
136
137         if (key.equals(AAIConstants.AAI_NODENAME)) {
138             // Get this from InetAddress rather than the properties file
139             String nodeName = getNodeName();
140             if (nodeName != null) {
141                 return nodeName;
142             }
143             // else get from property file
144         }
145
146         if (!propsInitialized || (serverProps == null)) {
147             reloadConfig();
148         }
149
150         if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret"))
151                 && serverProps.containsKey(key + ".x")) {
152             String valx = serverProps.getProperty(key + ".x");
153             return Password.deobfuscate(valx);
154         }
155
156         if (!serverProps.containsKey(key)) {
157             throw new AAIException("AAI_4005", "Property key " + key + " cannot be found");
158         } else {
159             response = serverProps.getProperty(key);
160             if (response == null || response.isEmpty()) {
161                 throw new AAIException("AAI_4005", "Property key " + key + " is null or empty");
162             }
163         }
164         return response;
165     }
166
167     /**
168      * Gets the int.
169      *
170      * @param key the key
171      * @return the int
172      * @throws AAIException the AAI exception
173      */
174     public static int getInt(String key) throws AAIException {
175         return Integer.parseInt(AAIConfig.get(key));
176     }
177
178     /**
179      * Gets the int.
180      *
181      * @param key the key
182      * @return the int
183      */
184     public static int getInt(String key, String value) {
185         return Integer.parseInt(AAIConfig.get(key, value));
186     }
187
188     /**
189      * Gets the server props.
190      *
191      * @return the server props
192      */
193     public static Properties getServerProps() {
194         return serverProps;
195     }
196
197     /**
198      * Gets the node name.
199      *
200      * @return the node name
201      */
202     public static String getNodeName() {
203         try {
204             InetAddress ip = InetAddress.getLocalHost();
205             if (ip != null) {
206                 String hostname = ip.getHostName();
207                 if (hostname != null) {
208                     return hostname;
209                 }
210             }
211         } catch (Exception e) {
212             return null;
213         }
214         return null;
215     }
216
217     /**
218      * Check if a null or an Empty string is passed in.
219      *
220      * @param s the s
221      * @return boolean
222      */
223     public static boolean isEmpty(String s) {
224         return (s == null || s.length() == 0);
225     }
226 }