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 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;
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;
40 public class AAIConfig {
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;
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();
52 * Instantiates a new AAI config.
55 private AAIConfig() {}
60 * @throws AAIException the AAI exception
62 public synchronized static void init() throws AAIException {
63 LOGGER.info("Initializing AAIConfig");
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>();
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");
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);
89 AAIConfig.getConfigFile();
90 AAIConfig.reloadConfig();
92 if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {
93 ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
95 LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
100 * Gets the default bools.
102 * @return the default bools
104 public static HashMap<String,ArrayList<String>> getDefaultBools() {
111 public static void cleanup() {
116 * Gets the config file.
118 * @return the config file
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;
127 return GLOBAL_PROP_FILE_NAME;
133 public synchronized static void reloadConfig() {
135 String propFileName = GLOBAL_PROP_FILE_NAME;
136 Properties newServerProps = null;
138 LOGGER.info("Reloading config from " + propFileName);
141 InputStream is = new FileInputStream(propFileName);
142 newServerProps = new Properties();
143 newServerProps.load(is);
144 propsInitialized = true;
146 serverProps = newServerProps;
147 newServerProps = null;
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());
160 * @param defaultValue the default value
163 public static String get(String key, String defaultValue) {
164 String result = defaultValue;
168 catch ( AAIException a ) {
179 * @throws AAIException the AAI exception
181 public static String get(String key) throws AAIException {
182 String response = null;
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) {
190 // else get from property file
193 if (!propsInitialized || (serverProps == null)) {
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);
202 if (!serverProps.containsKey(key)) {
203 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
205 response = serverProps.getProperty(key);
206 if (response == null || response.isEmpty()) {
207 throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
218 * @throws AAIException the AAI exception
220 public static int getInt(String key) throws AAIException {
221 return Integer.valueOf(AAIConfig.get(key));
225 * Gets the server props.
227 * @return the server props
229 public static Properties getServerProps() {
234 * Gets the node name.
236 * @return the node name
238 public static String getNodeName() {
240 InetAddress ip = InetAddress.getLocalHost();
242 String hostname = ip.getHostName();
243 if (hostname != null) {
247 } catch (Exception e) {
255 * Check if a null or an Empty string is passed in.
260 public static boolean isEmpty(String s)
262 return (s == null || s.length() == 0);