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