2 * ============LICENSE_START=======================================================
3 * ONAP : CCSDK.apps.sdnr.wt.apigateway
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.features.sdnr.wt.apigateway;
24 import java.io.FileInputStream;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.HashMap;
30 import java.util.Properties;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 import org.json.JSONArray;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class MyProperties {
39 private static Logger LOG = LoggerFactory.getLogger(MyProperties.class);
40 public static final String PROPFILE = "etc/apigateway.properties";
41 private static final String DEFAULT_AAI_HEADERS = "[\"X-FromAppId:SDNR\",\"Authorization:Basic QUFJOkFBSQ==\"]";
42 private static final String DEFAULT_CORSENABLED = "0";
43 private static final String DEFAULT_TRUSTINSECURE = "0";
44 private static final String DEFAULT_ESDATABASE = "http://sdnrdb:9200";
45 private static final String DEFAULT_AAI = "off";
46 private static final String DEFAULT_URL_OFF = "off";
47 private static final String DEFAULT_TILES = "${TILEURL}";
48 private static final String DEFAULT_TOPOLOGY = "${TOPOURL}";
49 private static MyProperties mObj;
50 private static final String ENVVARIABLE = "${";
51 private static final String REGEXENVVARIABLE = "(\\$\\{[A-Z0-9_-]+\\})";
52 private static final Pattern ENV_PATTERN = Pattern.compile(REGEXENVVARIABLE);
55 private String aaiBase;
56 private Map<String, String> aaiHeaders;
57 private String esBase;
58 private String tilesBase;
59 private String topologyBase;
61 private boolean trustInsecure;
63 private boolean corsEnabled;
65 public boolean isAAIOff() {
66 return this.aaiBase == null ? true : this.aaiBase.equals("off");
69 public boolean isEsOff() {
70 return this.esBase == null ? true : this.esBase.equals("off");
73 public boolean isTilesOff() {
74 return this.tilesBase == null ? true : this.tilesBase.equals("off");
77 public boolean isTopologyOff() {
78 return this.topologyBase == null ? true : this.topologyBase.equals("off");
81 public String getAAIBaseUrl() {
85 public String getEsBaseUrl() {
89 public String getTilesBaseUrl() {
90 return this.tilesBase;
93 public String getTopologyBaseUrl() {
94 return this.topologyBase;
97 public Map<String, String> getAAIHeaders() {
98 return this.aaiHeaders;
101 public boolean trustInsecure() {
102 return this.trustInsecure;
105 public boolean corsEnabled() {
106 return this.corsEnabled;
109 public static MyProperties Instantiate() throws IOException, NumberFormatException {
110 return Instantiate(new File(PROPFILE));
113 public static MyProperties Instantiate(File file) throws IOException, NumberFormatException {
115 return Instantiate(file, false);
118 public static MyProperties Instantiate(File file, boolean force) throws IOException, NumberFormatException {
119 if (mObj == null || force) {
120 mObj = new MyProperties(file);
121 LOG.debug("instantiated: {}", mObj.toString());
126 private MyProperties(File file) throws IOException, NumberFormatException {
127 this.aaiBase = DEFAULT_AAI;
128 this.trustInsecure = false;
129 if (!file.exists()) {
130 this.writeDefaults(file);
132 this.load(new FileInputStream(file));
135 public void load(InputStream in) throws IOException, NumberFormatException {
137 Properties defaultProps = new Properties();
138 defaultProps.load(in);
141 this.aaiBase = getProperty(defaultProps,"aai", DEFAULT_AAI);
142 this.aaiHeaders = _parseHeadersMap(getProperty(defaultProps,"aaiHeaders", DEFAULT_AAI_HEADERS));
143 this.esBase = getProperty(defaultProps,"database", DEFAULT_ESDATABASE);
144 this.tilesBase = getProperty(defaultProps,"tiles", DEFAULT_TILES, DEFAULT_URL_OFF);
145 this.topologyBase = getProperty(defaultProps,"topology", DEFAULT_TOPOLOGY, DEFAULT_URL_OFF);
146 this.trustInsecure = Integer.parseInt(getProperty(defaultProps,"insecure", DEFAULT_TRUSTINSECURE)) == 1;
147 this.corsEnabled = Integer.parseInt(getProperty(defaultProps,"cors", DEFAULT_CORSENABLED)) == 1;
149 private static String getProperty(Properties props,final String key, final String defValue) {
150 return getProperty(props, key, defValue, null);
152 private static String getProperty(Properties props,final String key, final String defValue, final String valueIfEmpty) {
154 LOG.debug("try to get property for {} with def {}", key, defValue);
155 String value = props.getProperty(key,defValue);
156 //try to read env var
157 if (value != null && value.contains(ENVVARIABLE)) {
159 LOG.debug("try to find env var(s) for {}", value);
160 final Matcher matcher = ENV_PATTERN.matcher(value);
161 String tmp = new String(value);
162 while (matcher.find() && matcher.groupCount() > 0) {
163 final String mkey = matcher.group(1);
166 LOG.debug("match found for v={} and env key={}", tmp, mkey);
167 //String env=System.getenv(mkey.substring(2,mkey.length()-1));
168 String env = System.getenv(mkey.substring(2, mkey.length() - 1));
169 tmp = tmp.replace(mkey, env == null ? "" : env);
170 } catch (SecurityException e) {
171 LOG.warn("unable to read env {}: {}", value, e);
177 if((value==null || value == "") && valueIfEmpty!=null) {
178 value = valueIfEmpty;
182 private static Map<String, String> _parseHeadersMap(String s) {
183 Map<String, String> r = new HashMap<>();
185 JSONArray a = new JSONArray(s);
186 if (a.length() > 0) {
187 for (int i = 0; i < a.length(); i++) {
188 String item = a.getString(i);
189 String[] hlp = item.split(":");
190 if (hlp.length > 1) {
191 r.put(hlp[0], hlp[1]);
195 } catch (Exception e) {
196 LOG.warn("problem loading headers map: {}", e.getMessage());
201 private String writeDefaults(File f) throws IOException {
202 StringBuilder sb = new StringBuilder();
203 final String LR = "\n";
204 FileWriter fw = new FileWriter(f);
205 sb.append("aai=" + DEFAULT_AAI + LR);
206 sb.append("aaiHeaders=" + DEFAULT_AAI_HEADERS + LR);
207 sb.append("database=" + DEFAULT_ESDATABASE + LR);
208 sb.append("tiles=" + DEFAULT_TILES + LR);
209 sb.append("topology=" + DEFAULT_TOPOLOGY + LR);
210 sb.append("insecure=" + DEFAULT_TRUSTINSECURE + LR);
211 sb.append("cors=" + DEFAULT_CORSENABLED);
213 fw.write(sb.toString());
214 } catch (Exception e) {
215 LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath(), e.getMessage());
219 return sb.toString();
222 public static MyProperties getInstance() {
227 public String toString() {
228 return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
229 + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";