Reduce the number of problems in aai-common by removing unused imports
[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 java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.InetAddress;
27 import java.util.Properties;
28
29 import org.eclipse.jetty.util.security.Password;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.logging.ErrorLogHelper;
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.equals("")) {
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 =
94                     Thread.currentThread().getContextClassLoader().getResourceAsStream("aaiconfig.properties");
95             LOGGER.info("Unable to find the aaiconfig.properties from filesystem so using file in jar");
96             if (is != null) {
97                 try {
98                     newServerProps.load(is);
99                     serverProps = newServerProps;
100                 } catch (IOException e) {
101                     LOGGER.warn("Encountered IO Exception during loading of aaiconfig props from inputstream", e);
102                 }
103             } else {
104                 LOGGER.error("Expected to find the error.properties in the jar but unable to find it");
105             }
106         }
107     }
108
109     /**
110      * Gets the.
111      *
112      * @param key the key
113      * @param defaultValue the default value
114      * @return the string
115      */
116     public static String get(String key, String defaultValue) {
117         String result = defaultValue;
118         try {
119             result = get(key);
120         } catch (AAIException a) {
121         }
122         if (result == null || result.isEmpty()) {
123             result = defaultValue;
124         }
125         return (result);
126     }
127
128     /**
129      * Gets the.
130      *
131      * @param key the key
132      * @return the string
133      * @throws AAIException the AAI exception
134      */
135     public static String get(String key) throws AAIException {
136         String response = null;
137
138         if (key.equals(AAIConstants.AAI_NODENAME)) {
139             // Get this from InetAddress rather than the properties file
140             String nodeName = getNodeName();
141             if (nodeName != null) {
142                 return nodeName;
143             }
144             // else get from property file
145         }
146
147         if (!propsInitialized || (serverProps == null)) {
148             reloadConfig();
149         }
150
151         if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret"))
152                 && serverProps.containsKey(key + ".x")) {
153             String valx = serverProps.getProperty(key + ".x");
154             return Password.deobfuscate(valx);
155         }
156
157         if (!serverProps.containsKey(key)) {
158             throw new AAIException("AAI_4005", "Property key " + key + " cannot be found");
159         } else {
160             response = serverProps.getProperty(key);
161             if (response == null || response.isEmpty()) {
162                 throw new AAIException("AAI_4005", "Property key " + key + " is null or empty");
163             }
164         }
165         return response;
166     }
167
168     /**
169      * Gets the int.
170      *
171      * @param key the key
172      * @return the int
173      * @throws AAIException the AAI exception
174      */
175     public static int getInt(String key) throws AAIException {
176         return Integer.parseInt(AAIConfig.get(key));
177     }
178
179     /**
180      * Gets the int.
181      *
182      * @param key the key
183      * @return the int
184      */
185     public static int getInt(String key, String value) {
186         return Integer.parseInt(AAIConfig.get(key, value));
187     }
188
189     /**
190      * Gets the server props.
191      *
192      * @return the server props
193      */
194     public static Properties getServerProps() {
195         return serverProps;
196     }
197
198     /**
199      * Gets the node name.
200      *
201      * @return the node name
202      */
203     public static String getNodeName() {
204         try {
205             InetAddress ip = InetAddress.getLocalHost();
206             if (ip != null) {
207                 String hostname = ip.getHostName();
208                 if (hostname != null) {
209                     return hostname;
210                 }
211             }
212         } catch (Exception e) {
213             return null;
214         }
215         return null;
216     }
217
218     /**
219      * Check if a null or an Empty string is passed in.
220      *
221      * @param s the s
222      * @return boolean
223      */
224     public static boolean isEmpty(String s) {
225         return (s == null || s.length() == 0);
226     }
227 }