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