2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.aai.util;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.InetAddress;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.Properties;
31 import java.util.Timer;
33 import org.eclipse.jetty.util.security.Password;
35 import org.openecomp.aai.exceptions.AAIException;
36 import org.openecomp.aai.logging.ErrorLogHelper;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
41 public class AAIConfig {
43 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIConfig.class);
44 private static final String GLOBAL_PROP_FILE_NAME = AAIConstants.AAI_CONFIG_FILENAME;
45 private static Properties serverProps;
46 private static boolean propsInitialized = false;
48 // this (probably) won't change between releases, put it in the config if it gets annoying...
49 private static HashMap<String,ArrayList<String>> defaultBools = new HashMap<String,ArrayList<String>>();
50 private static Timer timer = new Timer();
53 * Instantiates a new AAI config.
56 private AAIConfig() {}
61 * @throws AAIException the AAI exception
63 public synchronized static void init() throws AAIException{
64 LOGGER.info("Initializing AAIConfig");
66 ArrayList<String> genericVnfBools = new ArrayList<String>();
67 ArrayList<String> l3NetworkBools = new ArrayList<String>();
68 ArrayList<String> pserverBools = new ArrayList<String>();
69 ArrayList<String> subnetBools = new ArrayList<String>();
70 ArrayList<String> vserverBools = new ArrayList<String>();
71 ArrayList<String> vnfcBools = new ArrayList<String>();
73 genericVnfBools.add("in-maint");
74 genericVnfBools.add("is-closed-loop-disabled");
75 l3NetworkBools.add("is-bound-to-vpn");
76 pserverBools.add("in-maint");
77 subnetBools.add("dhcp-enabled");
78 vserverBools.add("in-maint");
79 vserverBools.add("is-closed-loop-disabled");
80 vnfcBools.add("in-maint");
81 vnfcBools.add("is-closed-loop-disabled");
83 defaultBools.put("generic-vnf", genericVnfBools);
84 defaultBools.put("l3-network", l3NetworkBools);
85 defaultBools.put("pserver", pserverBools);
86 defaultBools.put("subnet", subnetBools);
87 defaultBools.put("vserver", vserverBools);
88 defaultBools.put("vnfc", vnfcBools);
90 AAIConfig.getConfigFile();
91 AAIConfig.reloadConfig();
93 if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {
94 ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
96 LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
101 * Gets the default bools.
103 * @return the default bools
105 public static HashMap<String,ArrayList<String>> getDefaultBools() {
112 public static void cleanup() {
117 * Gets the config file.
119 * @return the config file
121 public static String getConfigFile() {
122 // if (GlobalPropFileName == null) {
123 // String nc = System.getProperty("aaiconfig");
124 // if (nc == null) nc = "/home/aaiadmin/etc/aaiconfig.props";
125 // logger.info( "aaiconfig = " + nc==null?"null":nc);
126 // GlobalPropFileName = nc;
128 return GLOBAL_PROP_FILE_NAME;
134 public synchronized static void reloadConfig() {
136 String propFileName = GLOBAL_PROP_FILE_NAME;
137 Properties newServerProps = null;
139 LOGGER.info("Reloading config from " + propFileName);
142 InputStream is = new FileInputStream(propFileName);
143 newServerProps = new Properties();
144 newServerProps.load(is);
145 propsInitialized = true;
147 serverProps = newServerProps;
148 newServerProps = null;
150 } catch (FileNotFoundException fnfe) {
151 ErrorLogHelper.logError("AAI_4001", " " + propFileName + ". Exception: "+fnfe.getMessage());
152 } catch (IOException e) {
153 ErrorLogHelper.logError("AAI_4002", " " + propFileName + ". IOException: "+e.getMessage());
161 * @param defaultValue the default value
164 public static String get(String key, String defaultValue) {
165 String result = defaultValue;
169 catch ( AAIException a ) {
180 * @throws AAIException the AAI exception
182 public static String get(String key) throws AAIException {
183 String response = null;
185 if (key.equals(AAIConstants.AAI_NODENAME)) {
186 // Get this from InetAddress rather than the properties file
187 String nodeName = getNodeName();
188 if (nodeName != null) {
191 // else get from property file
194 if (!propsInitialized || (serverProps == null)) {
198 if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret")) && serverProps.containsKey(key+".x")) {
199 String valx = serverProps.getProperty(key+".x");
200 return Password.deobfuscate(valx);
203 if (!serverProps.containsKey(key)) {
204 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
206 response = serverProps.getProperty(key);
207 if (response == null || response.isEmpty()) {
208 throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
219 * @throws AAIException the AAI exception
221 public static int getInt(String key) throws AAIException{
222 return Integer.valueOf(AAIConfig.get(key));
226 * Gets the server props.
228 * @return the server props
230 public static Properties getServerProps() {
235 * Gets the node name.
237 * @return the node name
239 public static String getNodeName() {
241 InetAddress ip = InetAddress.getLocalHost();
243 String hostname = ip.getHostName();
244 if (hostname != null) {
248 } catch (Exception e) {
256 * Check if a null or an Empty string is passed in.
261 public static boolean isEmpty(String s)
263 return (s == null || s.length() == 0);