Add the logging statements for when
[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 package org.onap.aai.util;
21
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.InetAddress;
27 import java.util.Properties;
28 import org.onap.aai.logging.LoggingContext;
29 import org.onap.aai.logging.LoggingContext.StatusCode;
30
31 import java.util.UUID;
32
33 import org.eclipse.jetty.util.security.Password;
34
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.logging.ErrorLogHelper;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40
41 public class AAIConfig {
42
43         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIConfig.class);
44         private static final String GLOBAL_PROP_FILE_NAME = AAIConstants.AAI_CONFIG_FILENAME;
45         private static Properties serverProps;
46     private static boolean propsInitialized = false;
47
48     /**
49      * Instantiates a new AAI config.
50      */
51     // Don't instantiate
52     private AAIConfig() {}
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")) && serverProps.containsKey(key+".x")) {
157                 String valx = serverProps.getProperty(key+".x");
158                 return Password.deobfuscate(valx);
159         }
160
161         if (!serverProps.containsKey(key)) {
162                 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
163         } else {
164                 response = serverProps.getProperty(key);
165                 if (response == null || response.isEmpty()) {
166                         throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
167                 }
168         }
169         return response;
170         }
171
172     /**
173      * Gets the int.
174      *
175      * @param key the key
176      * @return the int
177      * @throws AAIException the AAI exception
178      */
179     public static int getInt(String key) throws AAIException{
180         return Integer.parseInt(AAIConfig.get(key));
181         }
182
183     /**
184      * Gets the int.
185      *
186      * @param key the key
187      * @return the int
188      */
189     public static int getInt(String key, String value) {
190         return Integer.parseInt(AAIConfig.get(key, value));
191     }
192
193         /**
194          * Gets the server props.
195          *
196          * @return the server props
197          */
198         public static Properties getServerProps() {
199                 return serverProps;
200         }
201
202         /**
203          * Gets the node name.
204          *
205          * @return the node name
206          */
207         public static String getNodeName() {
208                 try {
209             InetAddress ip = InetAddress.getLocalHost();
210             if (ip != null) {
211                    String hostname = ip.getHostName();
212                    if (hostname != null) {
213                            return hostname;
214                    }
215             }
216                 } catch (Exception e) {
217                         return null;
218                 }
219                 return null;
220         }
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         {
231                 return (s == null || s.length() == 0);
232         }
233 }