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=========================================================
20 package org.onap.aai.util;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.InetAddress;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Properties;
30 import java.util.Timer;
31 import org.onap.aai.logging.LoggingContext;
32 import org.onap.aai.logging.LoggingContext.StatusCode;
34 import java.util.UUID;
36 import org.eclipse.jetty.util.security.Password;
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.logging.ErrorLogHelper;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
44 public class AAIConfig {
46 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIConfig.class);
47 private static final String GLOBAL_PROP_FILE_NAME = AAIConstants.AAI_CONFIG_FILENAME;
48 private static Properties serverProps;
49 private static boolean propsInitialized = false;
52 * Instantiates a new AAI config.
55 private AAIConfig() {}
60 * @throws AAIException the AAI exception
62 public synchronized static void init() throws AAIException{
64 LoggingContext.save();
65 LoggingContext.component("config");
66 LoggingContext.partnerName("NA");
67 LoggingContext.targetEntity("AAI");
68 LoggingContext.requestId(UUID.randomUUID().toString());
69 LoggingContext.serviceName("AAI");
70 LoggingContext.targetServiceName("init");
71 LoggingContext.statusCode(StatusCode.COMPLETE);
73 LOGGER.info("Initializing AAIConfig");
75 AAIConfig.getConfigFile();
76 AAIConfig.reloadConfig();
78 if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {
79 ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
81 LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
83 LoggingContext.restore();
87 * Gets the config file.
89 * @return the config file
91 public static String getConfigFile() {
92 return GLOBAL_PROP_FILE_NAME;
98 public synchronized static void reloadConfig() {
100 String propFileName = GLOBAL_PROP_FILE_NAME;
101 Properties newServerProps = null;
103 LOGGER.debug("Reloading config from " + propFileName);
105 try(InputStream is = new FileInputStream(propFileName)) {
106 newServerProps = new Properties();
107 newServerProps.load(is);
108 propsInitialized = true;
109 serverProps = newServerProps;
110 } catch (FileNotFoundException fnfe) {
111 ErrorLogHelper.logError("AAI_4001", " " + propFileName + ". Exception: "+fnfe.getMessage());
112 } catch (IOException e) {
113 ErrorLogHelper.logError("AAI_4002", " " + propFileName + ". IOException: "+e.getMessage());
121 * @param defaultValue the default value
124 public static String get(String key, String defaultValue) {
125 String result = defaultValue;
128 } catch ( AAIException a ) {
130 if (result == null || result.isEmpty()) {
131 result = defaultValue;
141 * @throws AAIException the AAI exception
143 public static String get(String key) throws AAIException {
144 String response = null;
146 if (key.equals(AAIConstants.AAI_NODENAME)) {
147 // Get this from InetAddress rather than the properties file
148 String nodeName = getNodeName();
149 if (nodeName != null) {
152 // else get from property file
155 if (!propsInitialized || (serverProps == null)) {
159 if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret")) && serverProps.containsKey(key+".x")) {
160 String valx = serverProps.getProperty(key+".x");
161 return Password.deobfuscate(valx);
164 if (!serverProps.containsKey(key)) {
165 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
167 response = serverProps.getProperty(key);
168 if (response == null || response.isEmpty()) {
169 throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
180 * @throws AAIException the AAI exception
182 public static int getInt(String key) throws AAIException{
183 return Integer.valueOf(AAIConfig.get(key));
187 * Gets the server props.
189 * @return the server props
191 public static Properties getServerProps() {
196 * Gets the node name.
198 * @return the node name
200 public static String getNodeName() {
202 InetAddress ip = InetAddress.getLocalHost();
204 String hostname = ip.getHostName();
205 if (hostname != null) {
209 } catch (Exception e) {
217 * Check if a null or an Empty string is passed in.
222 public static boolean isEmpty(String s)
224 return (s == null || s.length() == 0);