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 com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.InetAddress;
31 import java.util.Properties;
32 import java.util.UUID;
34 import org.eclipse.jetty.util.security.Password;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.logging.ErrorLogHelper;
37 import org.onap.aai.logging.LoggingContext;
38 import org.onap.aai.logging.LoggingContext.StatusCode;
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;
48 * Instantiates a new AAI config.
57 * @throws AAIException the AAI exception
59 public synchronized static void init() throws AAIException {
61 LoggingContext.save();
62 LoggingContext.component("config");
63 LoggingContext.partnerName("NA");
64 LoggingContext.targetEntity("AAI");
65 LoggingContext.requestId(UUID.randomUUID().toString());
66 LoggingContext.serviceName("AAI");
67 LoggingContext.targetServiceName("init");
68 LoggingContext.statusCode(StatusCode.COMPLETE);
70 LOGGER.info("Initializing AAIConfig");
72 AAIConfig.getConfigFile();
73 AAIConfig.reloadConfig();
75 if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {
76 ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
78 LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
80 LoggingContext.restore();
84 * Gets the config file.
86 * @return the config file
88 public static String getConfigFile() {
89 return GLOBAL_PROP_FILE_NAME;
95 public synchronized static void reloadConfig() {
97 String propFileName = GLOBAL_PROP_FILE_NAME;
98 Properties newServerProps = null;
100 LOGGER.debug("Reloading config from " + propFileName);
102 try (InputStream is = new FileInputStream(propFileName)) {
103 newServerProps = new Properties();
104 newServerProps.load(is);
105 propsInitialized = true;
106 serverProps = newServerProps;
107 } catch (FileNotFoundException fnfe) {
108 ErrorLogHelper.logError("AAI_4001", " " + propFileName + ". Exception: " + fnfe.getMessage());
109 } catch (IOException e) {
110 ErrorLogHelper.logError("AAI_4002", " " + propFileName + ". IOException: " + e.getMessage());
118 * @param defaultValue the default value
121 public static String get(String key, String defaultValue) {
122 String result = defaultValue;
125 } catch (AAIException a) {
127 if (result == null || result.isEmpty()) {
128 result = defaultValue;
138 * @throws AAIException the AAI exception
140 public static String get(String key) throws AAIException {
141 String response = null;
143 if (key.equals(AAIConstants.AAI_NODENAME)) {
144 // Get this from InetAddress rather than the properties file
145 String nodeName = getNodeName();
146 if (nodeName != null) {
149 // else get from property file
152 if (!propsInitialized || (serverProps == null)) {
156 if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret"))
157 && serverProps.containsKey(key + ".x")) {
158 String valx = serverProps.getProperty(key + ".x");
159 return Password.deobfuscate(valx);
162 if (!serverProps.containsKey(key)) {
163 throw new AAIException("AAI_4005", "Property key " + key + " cannot be found");
165 response = serverProps.getProperty(key);
166 if (response == null || response.isEmpty()) {
167 throw new AAIException("AAI_4005", "Property key " + key + " is null or empty");
178 * @throws AAIException the AAI exception
180 public static int getInt(String key) throws AAIException {
181 return Integer.parseInt(AAIConfig.get(key));
190 public static int getInt(String key, String value) {
191 return Integer.parseInt(AAIConfig.get(key, value));
195 * Gets the server props.
197 * @return the server props
199 public static Properties getServerProps() {
204 * Gets the node name.
206 * @return the node name
208 public static String getNodeName() {
210 InetAddress ip = InetAddress.getLocalHost();
212 String hostname = ip.getHostName();
213 if (hostname != null) {
217 } catch (Exception e) {
224 * Check if a null or an Empty string is passed in.
229 public static boolean isEmpty(String s) {
230 return (s == null || s.length() == 0);