2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.util;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.InetAddress;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Properties;
32 import java.util.Timer;
33 import org.onap.aai.logging.LoggingContext;
34 import org.onap.aai.logging.LoggingContext.StatusCode;
36 import java.util.UUID;
38 import org.eclipse.jetty.util.security.Password;
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.logging.ErrorLogHelper;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
46 public class AAIConfig {
48 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIConfig.class);
49 private static final String GLOBAL_PROP_FILE_NAME = AAIConstants.AAI_CONFIG_FILENAME;
50 private static Properties serverProps;
51 private static boolean propsInitialized = false;
53 // this (probably) won't change between releases, put it in the config if it gets annoying...
54 private static HashMap<String,ArrayList<String>> defaultBools = new HashMap<String,ArrayList<String>>();
55 private static Timer timer = new Timer();
58 * Instantiates a new AAI config.
61 private AAIConfig() {}
66 * @throws AAIException the AAI exception
68 public synchronized static void init() throws AAIException{
70 LoggingContext.save();
71 LoggingContext.component("config");
72 LoggingContext.partnerName("NA");
73 LoggingContext.targetEntity("AAI");
74 LoggingContext.requestId(UUID.randomUUID().toString());
75 LoggingContext.serviceName("AAI");
76 LoggingContext.targetServiceName("init");
77 LoggingContext.statusCode(StatusCode.COMPLETE);
79 LOGGER.info("Initializing AAIConfig");
81 ArrayList<String> genericVnfBools = new ArrayList<String>();
82 ArrayList<String> l3NetworkBools = new ArrayList<String>();
83 ArrayList<String> pserverBools = new ArrayList<String>();
84 ArrayList<String> subnetBools = new ArrayList<String>();
85 ArrayList<String> vserverBools = new ArrayList<String>();
86 ArrayList<String> vnfcBools = new ArrayList<String>();
88 genericVnfBools.add("in-maint");
89 genericVnfBools.add("is-closed-loop-disabled");
90 l3NetworkBools.add("is-bound-to-vpn");
91 pserverBools.add("in-maint");
92 subnetBools.add("dhcp-enabled");
93 vserverBools.add("in-maint");
94 vserverBools.add("is-closed-loop-disabled");
95 vnfcBools.add("in-maint");
96 vnfcBools.add("is-closed-loop-disabled");
98 defaultBools.put("generic-vnf", genericVnfBools);
99 defaultBools.put("l3-network", l3NetworkBools);
100 defaultBools.put("pserver", pserverBools);
101 defaultBools.put("subnet", subnetBools);
102 defaultBools.put("vserver", vserverBools);
103 defaultBools.put("vnfc", vnfcBools);
105 AAIConfig.getConfigFile();
106 AAIConfig.reloadConfig();
108 if (AAIConstants.AAI_NODENAME == null || AAIConstants.AAI_NODENAME == "") {
109 ErrorLogHelper.logError("AAI_4005", " AAI_NODENAME is not defined");
111 LOGGER.info("A&AI Server Node Name = " + AAIConstants.AAI_NODENAME);
113 LoggingContext.restore();
117 * Gets the default bools.
119 * @return the default bools
121 public static HashMap<String,ArrayList<String>> getDefaultBools() {
128 public static void cleanup() {
133 * Gets the config file.
135 * @return the config file
137 public static String getConfigFile() {
138 // if (GlobalPropFileName == null) {
139 // String nc = System.getProperty("aaiconfig");
140 // if (nc == null) nc = "/home/aaiadmin/etc/aaiconfig.props";
141 // logger.info( "aaiconfig = " + nc==null?"null":nc);
142 // GlobalPropFileName = nc;
144 return GLOBAL_PROP_FILE_NAME;
150 public synchronized static void reloadConfig() {
152 String propFileName = GLOBAL_PROP_FILE_NAME;
153 Properties newServerProps = null;
155 LOGGER.debug("Reloading config from " + propFileName);
158 InputStream is = new FileInputStream(propFileName);
159 newServerProps = new Properties();
160 newServerProps.load(is);
161 propsInitialized = true;
163 serverProps = newServerProps;
164 newServerProps = null;
166 } catch (FileNotFoundException fnfe) {
167 ErrorLogHelper.logError("AAI_4001", " " + propFileName + ". Exception: "+fnfe.getMessage());
168 } catch (IOException e) {
169 ErrorLogHelper.logError("AAI_4002", " " + propFileName + ". IOException: "+e.getMessage());
177 * @param defaultValue the default value
180 public static String get(String key, String defaultValue) {
181 String result = defaultValue;
185 catch ( AAIException a ) {
196 * @throws AAIException the AAI exception
198 public static String get(String key) throws AAIException {
199 String response = null;
201 if (key.equals(AAIConstants.AAI_NODENAME)) {
202 // Get this from InetAddress rather than the properties file
203 String nodeName = getNodeName();
204 if (nodeName != null) {
207 // else get from property file
210 if (!propsInitialized || (serverProps == null)) {
214 if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret")) && serverProps.containsKey(key+".x")) {
215 String valx = serverProps.getProperty(key+".x");
216 return Password.deobfuscate(valx);
219 if (!serverProps.containsKey(key)) {
220 throw new AAIException("AAI_4005", "Property key "+key+" cannot be found");
222 response = serverProps.getProperty(key);
223 if (response == null || response.isEmpty()) {
224 throw new AAIException("AAI_4005", "Property key "+key+" is null or empty");
235 * @throws AAIException the AAI exception
237 public static int getInt(String key) throws AAIException{
238 return Integer.valueOf(AAIConfig.get(key));
242 * Gets the server props.
244 * @return the server props
246 public static Properties getServerProps() {
251 * Gets the node name.
253 * @return the node name
255 public static String getNodeName() {
257 InetAddress ip = InetAddress.getLocalHost();
259 String hostname = ip.getHostName();
260 if (hostname != null) {
264 } catch (Exception e) {
272 * Check if a null or an Empty string is passed in.
277 public static boolean isEmpty(String s)
279 return (s == null || s.length() == 0);