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