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