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.Properties;
28 import org.onap.aai.logging.LoggingContext;
29 import org.onap.aai.logging.LoggingContext.StatusCode;
31 import java.util.UUID;
33 import org.eclipse.jetty.util.security.Password;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.logging.ErrorLogHelper;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
41 public class AAIConfig {
43 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIConfig.class);
44 private static final String GLOBAL_PROP_FILE_NAME = AAIConstants.AAI_CONFIG_FILENAME;
45 private static Properties serverProps;
46 private static boolean propsInitialized = false;
49 * Instantiates a new AAI config.
52 private AAIConfig() {}
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")) && serverProps.containsKey(key+".x")) {
157 String valx = serverProps.getProperty(key+".x");
158 return Password.deobfuscate(valx);
161 if (!serverProps.containsKey(key)) {
162 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
164 response = serverProps.getProperty(key);
165 if (response == null || response.isEmpty()) {
166 throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
177 * @throws AAIException the AAI exception
179 public static int getInt(String key) throws AAIException{
180 return Integer.parseInt(AAIConfig.get(key));
189 public static int getInt(String key, String value) {
190 return Integer.parseInt(AAIConfig.get(key, value));
194 * Gets the server props.
196 * @return the server props
198 public static Properties getServerProps() {
203 * Gets the node name.
205 * @return the node name
207 public static String getNodeName() {
209 InetAddress ip = InetAddress.getLocalHost();
211 String hostname = ip.getHostName();
212 if (hostname != null) {
216 } catch (Exception e) {
224 * Check if a null or an Empty string is passed in.
229 public static boolean isEmpty(String s)
231 return (s == null || s.length() == 0);