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;
33 import org.onap.aai.logging.LoggingContext;
34 import org.onap.aai.logging.LoggingContext.StatusCode;
36 import java.util.UUID;
38 import org.eclipse.jetty.util.security.Password;
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;
46 public class AAIConfig {
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;
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>>();
56 * Instantiates a new AAI config.
59 private AAIConfig() {}
64 * @throws AAIException the AAI exception
66 public synchronized static void init() throws AAIException{
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);
77 LOGGER.info("Initializing AAIConfig");
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>();
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");
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);
103 AAIConfig.getConfigFile();
104 AAIConfig.reloadConfig();
106 if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {
107 ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
109 LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
111 LoggingContext.restore();
115 * Gets the default bools.
117 * @return the default bools
119 public static HashMap<String,ArrayList<String>> getDefaultBools() {
124 * Gets the config file.
126 * @return the config file
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;
135 return GLOBAL_PROP_FILE_NAME;
141 public synchronized static void reloadConfig() {
143 String propFileName = GLOBAL_PROP_FILE_NAME;
144 Properties newServerProps = null;
146 LOGGER.debug("Reloading config from " + propFileName);
149 InputStream is = new FileInputStream(propFileName);
150 newServerProps = new Properties();
151 newServerProps.load(is);
152 propsInitialized = true;
154 serverProps = newServerProps;
155 newServerProps = null;
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());
168 * @param defaultValue the default value
171 public static String get(String key, String defaultValue) {
172 String result = defaultValue;
176 catch ( AAIException a ) {
187 * @throws AAIException the AAI exception
189 public static String get(String key) throws AAIException {
190 String response = null;
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) {
198 // else get from property file
201 if (!propsInitialized || (serverProps == null)) {
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);
210 if (!serverProps.containsKey(key)) {
211 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
213 response = serverProps.getProperty(key);
214 if (response == null || response.isEmpty()) {
215 throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
226 * @throws AAIException the AAI exception
228 public static int getInt(String key) throws AAIException{
229 return Integer.valueOf(AAIConfig.get(key));
233 * Gets the server props.
235 * @return the server props
237 public static Properties getServerProps() {
242 * Gets the node name.
244 * @return the node name
246 public static String getNodeName() {
248 InetAddress ip = InetAddress.getLocalHost();
250 String hostname = ip.getHostName();
251 if (hostname != null) {
255 } catch (Exception e) {
263 * Check if a null or an Empty string is passed in.
268 public static boolean isEmpty(String s)
270 return (s == null || s.length() == 0);