Sync the latest code changes
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.util;
23
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.InetAddress;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Properties;
32 import java.util.Timer;
33 import org.onap.aai.logging.LoggingContext;
34 import org.onap.aai.logging.LoggingContext.StatusCode;
35
36 import java.util.UUID;
37
38 import org.eclipse.jetty.util.security.Password;
39
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.logging.ErrorLogHelper;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
44
45
46 public class AAIConfig {
47
48         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIConfig.class);
49         private static final String GLOBAL_PROP_FILE_NAME = AAIConstants.AAI_CONFIG_FILENAME;
50         private static Properties serverProps;
51     private static boolean propsInitialized = false;
52     
53     // this (probably) won't change between releases, put it in the config if it gets annoying...
54     private static HashMap<String,ArrayList<String>> defaultBools = new HashMap<String,ArrayList<String>>();
55     private static Timer timer = new Timer();
56     
57     /**
58      * Instantiates a new AAI config.
59      */
60     // Don't instantiate
61     private AAIConfig() {}
62
63     /**
64      * Inits the.
65      *
66      * @throws AAIException the AAI exception
67      */
68     public synchronized static void init() throws AAIException{
69
70         LoggingContext.save();
71                 LoggingContext.component("config");
72                 LoggingContext.partnerName("NA");
73                 LoggingContext.targetEntity("AAI");
74                 LoggingContext.requestId(UUID.randomUUID().toString());
75                 LoggingContext.serviceName("AAI");
76                 LoggingContext.targetServiceName("init");
77                 LoggingContext.statusCode(StatusCode.COMPLETE);
78
79                 LOGGER.info("Initializing AAIConfig");
80                 
81                 ArrayList<String> genericVnfBools = new ArrayList<String>();
82                 ArrayList<String> l3NetworkBools = new ArrayList<String>();
83                 ArrayList<String> pserverBools = new ArrayList<String>();
84                 ArrayList<String> subnetBools = new ArrayList<String>();
85                 ArrayList<String> vserverBools = new ArrayList<String>();
86                 ArrayList<String> vnfcBools = new ArrayList<String>();
87
88                 genericVnfBools.add("in-maint");
89                 genericVnfBools.add("is-closed-loop-disabled");
90                 l3NetworkBools.add("is-bound-to-vpn");
91                 pserverBools.add("in-maint");
92                 subnetBools.add("dhcp-enabled");
93                 vserverBools.add("in-maint");
94                 vserverBools.add("is-closed-loop-disabled");
95                 vnfcBools.add("in-maint");
96                 vnfcBools.add("is-closed-loop-disabled");
97
98                 defaultBools.put("generic-vnf", genericVnfBools);
99                 defaultBools.put("l3-network",  l3NetworkBools);
100                 defaultBools.put("pserver", pserverBools);
101                 defaultBools.put("subnet", subnetBools);
102                 defaultBools.put("vserver", vserverBools);
103                 defaultBools.put("vnfc", vnfcBools);
104                 
105         AAIConfig.getConfigFile();
106         AAIConfig.reloadConfig();
107         
108         if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {      
109             ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
110         } else {
111             LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
112         }
113         LoggingContext.restore();
114     }
115
116     /**
117      * Gets the default bools.
118      *
119      * @return the default bools
120      */
121     public static HashMap<String,ArrayList<String>> getDefaultBools() { 
122         return defaultBools;
123     }
124         
125     /**
126      * Cleanup.
127      */
128     public static void cleanup() {
129                 timer.cancel();
130         }
131
132     /**
133      * Gets the config file.
134      *
135      * @return the config file
136      */
137     public static String getConfigFile() {
138 //        if (GlobalPropFileName == null) {
139 //              String nc = System.getProperty("aaiconfig");
140 //                      if (nc == null) nc = "/home/aaiadmin/etc/aaiconfig.props";
141 //                  logger.info( "aaiconfig = " + nc==null?"null":nc);
142 //                      GlobalPropFileName = nc;
143 //        }
144         return GLOBAL_PROP_FILE_NAME;
145     }
146
147     /**
148      * Reload config.
149      */
150     public synchronized static void reloadConfig() {
151
152         String propFileName = GLOBAL_PROP_FILE_NAME;
153         Properties newServerProps = null;
154         
155         LOGGER.debug("Reloading config from " + propFileName);
156         
157         try {
158             InputStream is = new FileInputStream(propFileName);
159             newServerProps = new Properties();
160             newServerProps.load(is);
161             propsInitialized = true;
162
163             serverProps = newServerProps;
164             newServerProps = null;
165                 
166         } catch (FileNotFoundException fnfe) {
167                 ErrorLogHelper.logError("AAI_4001", " " + propFileName + ". Exception: "+fnfe.getMessage());
168         } catch (IOException e) {
169                 ErrorLogHelper.logError("AAI_4002", " " + propFileName + ". IOException: "+e.getMessage());
170         }
171     }
172     
173     /**
174      * Gets the.
175      *
176      * @param key the key
177      * @param defaultValue the default value
178      * @return the string
179      */
180     public static String get(String key, String defaultValue) {
181         String result = defaultValue;
182         try {
183                         result = get (key);
184         }
185         catch ( AAIException a ) {
186                 
187         }
188         return ( result );
189     }
190
191     /**
192      * Gets the.
193      *
194      * @param key the key
195      * @return the string
196      * @throws AAIException the AAI exception
197      */
198     public static String get(String key) throws AAIException {
199         String response = null;
200         
201         if (key.equals(AAIConstants.AAI_NODENAME)) {
202                 // Get this from InetAddress rather than the properties file
203                 String nodeName = getNodeName();
204                 if (nodeName != null) {
205                         return nodeName;
206                 }
207                 // else get from property file
208         }
209         
210         if (!propsInitialized || (serverProps == null)) {
211                 reloadConfig();
212         }
213         
214         if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret")) && serverProps.containsKey(key+".x")) {
215                 String valx = serverProps.getProperty(key+".x");
216                 return Password.deobfuscate(valx);
217         }
218         
219         if (!serverProps.containsKey(key)) {
220                 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
221         } else {
222                 response = serverProps.getProperty(key);
223                 if (response == null || response.isEmpty()) {
224                         throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
225                 }
226         }
227         return response;
228         }
229
230     /**
231      * Gets the int.
232      *
233      * @param key the key
234      * @return the int
235      * @throws AAIException the AAI exception
236      */
237     public static int getInt(String key) throws AAIException{
238         return Integer.valueOf(AAIConfig.get(key));
239         }
240
241         /**
242          * Gets the server props.
243          *
244          * @return the server props
245          */
246         public static Properties getServerProps() {
247                 return serverProps;
248         }
249         
250         /**
251          * Gets the node name.
252          *
253          * @return the node name
254          */
255         public static String getNodeName() {
256                 try {
257             InetAddress ip = InetAddress.getLocalHost();
258             if (ip != null) {
259                    String hostname = ip.getHostName();
260                    if (hostname != null) {
261                            return hostname;
262                    }
263             }
264                 } catch (Exception e) {
265                         return null;
266                 }
267                 return null;
268         }
269         
270         
271         /**
272          * Check if a null or an Empty string is passed in.
273          *
274          * @param s the s
275          * @return boolean
276          */
277         public static boolean isEmpty(String s)
278         {
279                 return (s == null || s.length() == 0);
280         }
281
282 }