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