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