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