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 org.eclipse.jetty.util.security.Password;
24 import org.onap.aai.exceptions.AAIException;
25 import org.onap.aai.logging.ErrorLogHelper;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.InetAddress;
31 import java.util.Properties;
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 == "") {
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 = Thread.currentThread().getContextClassLoader().getResourceAsStream("aaiconfig.properties");
94 LOGGER.info("Unable to find the aaiconfig.properties from filesystem so using file in jar");
97 newServerProps.load(is);
98 serverProps = newServerProps;
99 } catch (IOException e) {
100 LOGGER.warn("Encountered IO Exception during loading of aaiconfig props from inputstream", e);
103 LOGGER.error("Expected to find the error.properties in the jar but unable to find it");
112 * @param defaultValue the default value
115 public static String get(String key, String defaultValue) {
116 String result = defaultValue;
119 } catch (AAIException a) {
121 if (result == null || result.isEmpty()) {
122 result = defaultValue;
132 * @throws AAIException the AAI exception
134 public static String get(String key) throws AAIException {
135 String response = null;
137 if (key.equals(AAIConstants.AAI_NODENAME)) {
138 // Get this from InetAddress rather than the properties file
139 String nodeName = getNodeName();
140 if (nodeName != null) {
143 // else get from property file
146 if (!propsInitialized || (serverProps == null)) {
150 if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret"))
151 && serverProps.containsKey(key + ".x")) {
152 String valx = serverProps.getProperty(key + ".x");
153 return Password.deobfuscate(valx);
156 if (!serverProps.containsKey(key)) {
157 throw new AAIException("AAI_4005", "Property key " + key + " cannot be found");
159 response = serverProps.getProperty(key);
160 if (response == null || response.isEmpty()) {
161 throw new AAIException("AAI_4005", "Property key " + key + " is null or empty");
172 * @throws AAIException the AAI exception
174 public static int getInt(String key) throws AAIException {
175 return Integer.parseInt(AAIConfig.get(key));
184 public static int getInt(String key, String value) {
185 return Integer.parseInt(AAIConfig.get(key, value));
189 * Gets the server props.
191 * @return the server props
193 public static Properties getServerProps() {
198 * Gets the node name.
200 * @return the node name
202 public static String getNodeName() {
204 InetAddress ip = InetAddress.getLocalHost();
206 String hostname = ip.getHostName();
207 if (hostname != null) {
211 } catch (Exception e) {
218 * Check if a null or an Empty string is passed in.
223 public static boolean isEmpty(String s) {
224 return (s == null || s.length() == 0);