2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.util;
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;
34 import org.eclipse.jetty.util.security.Password;
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;
42 public class AAIConfig {
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;
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();
54 * Instantiates a new AAI config.
57 private AAIConfig() {}
62 * @throws AAIException the AAI exception
64 public synchronized static void init() throws AAIException{
65 LOGGER.info("Initializing AAIConfig");
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>();
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");
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);
91 AAIConfig.getConfigFile();
92 AAIConfig.reloadConfig();
94 if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {
95 ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
97 LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
102 * Gets the default bools.
104 * @return the default bools
106 public static HashMap<String,ArrayList<String>> getDefaultBools() {
113 public static void cleanup() {
118 * Gets the config file.
120 * @return the config file
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;
129 return GLOBAL_PROP_FILE_NAME;
135 public synchronized static void reloadConfig() {
137 String propFileName = GLOBAL_PROP_FILE_NAME;
138 Properties newServerProps = null;
140 LOGGER.info("Reloading config from " + propFileName);
143 InputStream is = new FileInputStream(propFileName);
144 newServerProps = new Properties();
145 newServerProps.load(is);
146 propsInitialized = true;
148 serverProps = newServerProps;
149 newServerProps = null;
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());
162 * @param defaultValue the default value
165 public static String get(String key, String defaultValue) {
166 String result = defaultValue;
170 catch ( AAIException a ) {
181 * @throws AAIException the AAI exception
183 public static String get(String key) throws AAIException {
184 String response = null;
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) {
192 // else get from property file
195 if (!propsInitialized || (serverProps == null)) {
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);
204 if (!serverProps.containsKey(key)) {
205 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
207 response = serverProps.getProperty(key);
208 if (response == null || response.isEmpty()) {
209 throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
220 * @throws AAIException the AAI exception
222 public static int getInt(String key) throws AAIException{
223 return Integer.valueOf(AAIConfig.get(key));
227 * Gets the server props.
229 * @return the server props
231 public static Properties getServerProps() {
236 * Gets the node name.
238 * @return the node name
240 public static String getNodeName() {
242 InetAddress ip = InetAddress.getLocalHost();
244 String hostname = ip.getHostName();
245 if (hostname != null) {
249 } catch (Exception e) {
257 * Check if a null or an Empty string is passed in.
262 public static boolean isEmpty(String s)
264 return (s == null || s.length() == 0);