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;
51 // this (probably) won't change between releases, put it in the config if it gets annoying...
52 private static HashMap<String,ArrayList<String>> defaultBools = new HashMap<String,ArrayList<String>>();
54 * Instantiates a new AAI config.
57 private AAIConfig() {}
62 * @throws AAIException the AAI exception
64 public synchronized static void init() throws AAIException{
66 LoggingContext.save();
67 LoggingContext.component("config");
68 LoggingContext.partnerName("NA");
69 LoggingContext.targetEntity("AAI");
70 LoggingContext.requestId(UUID.randomUUID().toString());
71 LoggingContext.serviceName("AAI");
72 LoggingContext.targetServiceName("init");
73 LoggingContext.statusCode(StatusCode.COMPLETE);
75 LOGGER.info("Initializing AAIConfig");
77 ArrayList<String> genericVnfBools = new ArrayList<String>();
78 ArrayList<String> l3NetworkBools = new ArrayList<String>();
79 ArrayList<String> pserverBools = new ArrayList<String>();
80 ArrayList<String> subnetBools = new ArrayList<String>();
81 ArrayList<String> vserverBools = new ArrayList<String>();
82 ArrayList<String> vnfcBools = new ArrayList<String>();
84 genericVnfBools.add("in-maint");
85 genericVnfBools.add("is-closed-loop-disabled");
86 l3NetworkBools.add("is-bound-to-vpn");
87 pserverBools.add("in-maint");
88 subnetBools.add("dhcp-enabled");
89 vserverBools.add("in-maint");
90 vserverBools.add("is-closed-loop-disabled");
91 vnfcBools.add("in-maint");
92 vnfcBools.add("is-closed-loop-disabled");
94 defaultBools.put("generic-vnf", genericVnfBools);
95 defaultBools.put("l3-network", l3NetworkBools);
96 defaultBools.put("pserver", pserverBools);
97 defaultBools.put("subnet", subnetBools);
98 defaultBools.put("vserver", vserverBools);
99 defaultBools.put("vnfc", vnfcBools);
101 AAIConfig.getConfigFile();
102 AAIConfig.reloadConfig();
104 if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {
105 ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
107 LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
109 LoggingContext.restore();
113 * Gets the default bools.
115 * @return the default bools
117 public static HashMap<String,ArrayList<String>> getDefaultBools() {
122 * Gets the config file.
124 * @return the config file
126 public static String getConfigFile() {
127 // if (GlobalPropFileName == null) {
128 // String nc = System.getProperty("aaiconfig");
129 // if (nc == null) nc = "/home/aaiadmin/etc/aaiconfig.props";
130 // logger.info( "aaiconfig = " + nc==null?"null":nc);
131 // GlobalPropFileName = nc;
133 return GLOBAL_PROP_FILE_NAME;
139 public synchronized static void reloadConfig() {
141 String propFileName = GLOBAL_PROP_FILE_NAME;
142 Properties newServerProps = null;
144 LOGGER.debug("Reloading config from " + propFileName);
147 InputStream is = new FileInputStream(propFileName);
148 newServerProps = new Properties();
149 newServerProps.load(is);
150 propsInitialized = true;
152 serverProps = newServerProps;
153 newServerProps = null;
155 } catch (FileNotFoundException fnfe) {
156 ErrorLogHelper.logError("AAI_4001", " " + propFileName + ". Exception: "+fnfe.getMessage());
157 } catch (IOException e) {
158 ErrorLogHelper.logError("AAI_4002", " " + propFileName + ". IOException: "+e.getMessage());
166 * @param defaultValue the default value
169 public static String get(String key, String defaultValue) {
170 String result = defaultValue;
174 catch ( AAIException a ) {
185 * @throws AAIException the AAI exception
187 public static String get(String key) throws AAIException {
188 String response = null;
190 if (key.equals(AAIConstants.AAI_NODENAME)) {
191 // Get this from InetAddress rather than the properties file
192 String nodeName = getNodeName();
193 if (nodeName != null) {
196 // else get from property file
199 if (!propsInitialized || (serverProps == null)) {
203 if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret")) && serverProps.containsKey(key+".x")) {
204 String valx = serverProps.getProperty(key+".x");
205 return Password.deobfuscate(valx);
208 if (!serverProps.containsKey(key)) {
209 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
211 response = serverProps.getProperty(key);
212 if (response == null || response.isEmpty()) {
213 throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
224 * @throws AAIException the AAI exception
226 public static int getInt(String key) throws AAIException{
227 return Integer.valueOf(AAIConfig.get(key));
231 * Gets the server props.
233 * @return the server props
235 public static Properties getServerProps() {
240 * Gets the node name.
242 * @return the node name
244 public static String getNodeName() {
246 InetAddress ip = InetAddress.getLocalHost();
248 String hostname = ip.getHostName();
249 if (hostname != null) {
253 } catch (Exception e) {
261 * Check if a null or an Empty string is passed in.
266 public static boolean isEmpty(String s)
268 return (s == null || s.length() == 0);