2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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.onap.aai.util;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.InetAddress;
27 import java.util.Properties;
29 import org.eclipse.jetty.util.security.Password;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.logging.ErrorLogHelper;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class AAIConfig {
37 private static final Logger LOGGER = LoggerFactory.getLogger(AAIConfig.class);
38 private static final String GLOBAL_PROP_FILE_NAME = AAIConstants.AAI_CONFIG_FILENAME;
39 private static Properties serverProps;
40 private static boolean propsInitialized = false;
43 * Instantiates a new AAI config.
52 * @throws AAIException the AAI exception
54 public synchronized static void init() throws AAIException {
56 LOGGER.info("Initializing AAIConfig");
58 AAIConfig.getConfigFile();
59 AAIConfig.reloadConfig();
61 if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME.equals("")) {
62 ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
64 LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
69 * Gets the config file.
71 * @return the config file
73 public static String getConfigFile() {
74 return GLOBAL_PROP_FILE_NAME;
80 public synchronized static void reloadConfig() {
82 String propFileName = GLOBAL_PROP_FILE_NAME;
83 Properties newServerProps = new Properties();
85 LOGGER.debug("Reloading config from " + propFileName);
87 try (InputStream is = new FileInputStream(propFileName)) {
88 LOGGER.info("Found the aaiconfig.properties in the following location: {}", GLOBAL_PROP_FILE_NAME);
89 newServerProps.load(is);
90 propsInitialized = true;
91 serverProps = newServerProps;
92 } catch (Exception fnfe) {
93 final InputStream is =
94 Thread.currentThread().getContextClassLoader().getResourceAsStream("aaiconfig.properties");
95 LOGGER.info("Unable to find the aaiconfig.properties from filesystem so using file in jar");
98 newServerProps.load(is);
99 serverProps = newServerProps;
100 } catch (IOException e) {
101 LOGGER.warn("Encountered IO Exception during loading of aaiconfig props from inputstream", e);
104 LOGGER.error("Expected to find the error.properties in the jar but unable to find it");
113 * @param defaultValue the default value
116 public static String get(String key, String defaultValue) {
117 String result = defaultValue;
120 } catch (AAIException a) {
122 if (result == null || result.isEmpty()) {
123 result = defaultValue;
133 * @throws AAIException the AAI exception
135 public static String get(String key) throws AAIException {
136 String response = null;
138 if (key.equals(AAIConstants.AAI_NODENAME)) {
139 // Get this from InetAddress rather than the properties file
140 String nodeName = getNodeName();
141 if (nodeName != null) {
144 // else get from property file
147 if (!propsInitialized || (serverProps == null)) {
151 if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret"))
152 && serverProps.containsKey(key + ".x")) {
153 String valx = serverProps.getProperty(key + ".x");
154 return Password.deobfuscate(valx);
157 if (!serverProps.containsKey(key)) {
158 throw new AAIException("AAI_4005", "Property key " + key + " cannot be found");
160 response = serverProps.getProperty(key);
161 if (response == null || response.isEmpty()) {
162 throw new AAIException("AAI_4005", "Property key " + key + " is null or empty");
173 * @throws AAIException the AAI exception
175 public static int getInt(String key) throws AAIException {
176 return Integer.parseInt(AAIConfig.get(key));
185 public static int getInt(String key, String value) {
186 return Integer.parseInt(AAIConfig.get(key, value));
190 * Gets the server props.
192 * @return the server props
194 public static Properties getServerProps() {
199 * Gets the node name.
201 * @return the node name
203 public static String getNodeName() {
205 InetAddress ip = InetAddress.getLocalHost();
207 String hostname = ip.getHostName();
208 if (hostname != null) {
212 } catch (Exception e) {
219 * Check if a null or an Empty string is passed in.
224 public static boolean isEmpty(String s) {
225 return (s == null || s.length() == 0);