66cbd02e91cb05731455da8a7bd7d356d1680104
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps.sdnr.wt.apigateway
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.ccsdk.features.sdnr.wt.apigateway;
22
23 import java.io.File;
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;
29 import java.util.Map;
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;
36
37 public class MyProperties {
38
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 final String DEFAULT_SITEDOC = "${SITEDOCURL}";
50     private static final String DEFAULT_TERRAIN = "${TERRAINURL}";
51
52     private static MyProperties mObj;
53     private static final String ENVVARIABLE = "${";
54     private static final String REGEXENVVARIABLE = "(\\$\\{[A-Z0-9_-]+\\})";
55     private static final Pattern ENV_PATTERN = Pattern.compile(REGEXENVVARIABLE);
56
57
58     private String aaiBase;
59     private Map<String, String> aaiHeaders;
60     private String esBase;
61     private String tilesBase;
62     private String topologyBase;
63     private String sitedocBase;
64     private String terrainBase;
65
66     private boolean trustInsecure;
67     private boolean corsEnabled;
68
69     public boolean isAAIOff() {
70         return this.aaiBase == null ? true : this.aaiBase.equals("off");
71     }
72
73     public boolean isEsOff() {
74         return this.esBase == null ? true : this.esBase.equals("off");
75     }
76
77     public boolean isTilesOff() {
78         return this.tilesBase == null ? true : this.tilesBase.equals("off");
79     }
80
81     public boolean isTopologyOff() {
82         return this.topologyBase == null ? true : this.topologyBase.equals("off");
83     }
84
85     public String getAAIBaseUrl() {
86         return this.aaiBase;
87     }
88
89     public String getEsBaseUrl() {
90         return this.esBase;
91     }
92
93     public String getTilesBaseUrl() {
94         return this.tilesBase;
95     }
96
97     public String getTopologyBaseUrl() {
98         return this.topologyBase;
99     }
100     public String getSitedocBaseUrl() {
101         return this.sitedocBase;
102     }
103
104     public String getTerrainBaseUrl() {
105         return this.terrainBase;
106     }
107
108     public Map<String, String> getAAIHeaders() {
109         return this.aaiHeaders;
110     }
111
112     public boolean trustInsecure() {
113         return this.trustInsecure;
114     }
115
116     public boolean corsEnabled() {
117         return this.corsEnabled;
118     }
119
120     public static MyProperties Instantiate() throws IOException, NumberFormatException {
121         return Instantiate(new File(PROPFILE));
122     }
123
124     public static MyProperties Instantiate(File file) throws IOException, NumberFormatException {
125
126         return Instantiate(file, false);
127     }
128
129     public static MyProperties Instantiate(File file, boolean force) throws IOException, NumberFormatException {
130         if (mObj == null || force) {
131             mObj = new MyProperties(file);
132             LOG.debug("instantiated: {}", mObj.toString());
133         }
134         return mObj;
135     }
136
137     private MyProperties(File file) throws IOException, NumberFormatException {
138         this.aaiBase = DEFAULT_AAI;
139         this.trustInsecure = false;
140         if (!file.exists()) {
141             this.writeDefaults(file);
142         }
143         this.load(new FileInputStream(file));
144     }
145
146     public void load(InputStream in) throws IOException, NumberFormatException {
147
148         Properties defaultProps = new Properties();
149         defaultProps.load(in);
150         in.close();
151
152         this.aaiBase = getProperty(defaultProps,"aai", DEFAULT_AAI);
153         this.aaiHeaders = _parseHeadersMap(getProperty(defaultProps,"aaiHeaders", DEFAULT_AAI_HEADERS));
154         this.esBase = getProperty(defaultProps,"database", DEFAULT_ESDATABASE);
155         this.tilesBase = getProperty(defaultProps,"tiles", DEFAULT_TILES, DEFAULT_URL_OFF);
156         this.topologyBase = getProperty(defaultProps,"topology", DEFAULT_TOPOLOGY, DEFAULT_URL_OFF);
157         this.sitedocBase = getProperty(defaultProps,"sitedoc", DEFAULT_SITEDOC, DEFAULT_URL_OFF);
158         this.terrainBase = getProperty(defaultProps,"terrain", DEFAULT_TERRAIN, DEFAULT_URL_OFF);
159         this.trustInsecure = Integer.parseInt(getProperty(defaultProps,"insecure", DEFAULT_TRUSTINSECURE)) == 1;
160         this.corsEnabled = Integer.parseInt(getProperty(defaultProps,"cors", DEFAULT_CORSENABLED)) == 1;
161     }
162     private static String getProperty(Properties props,final String key, final String defValue) {
163         return getProperty(props, key, defValue, null);
164     }
165     private static String getProperty(Properties props,final String key, final String defValue, final String valueIfEmpty) {
166
167         LOG.debug("try to get property for {} with def {}", key, defValue);
168         String value = props.getProperty(key,defValue);
169         //try to read env var
170         if (value != null && value.contains(ENVVARIABLE)) {
171
172             LOG.debug("try to find env var(s) for {}", value);
173             final Matcher matcher = ENV_PATTERN.matcher(value);
174             String tmp = new String(value);
175             while (matcher.find() && matcher.groupCount() > 0) {
176                 final String mkey = matcher.group(1);
177                 if (mkey != null) {
178                     try {
179                         LOG.debug("match found for v={} and env key={}", tmp, mkey);
180                         //String env=System.getenv(mkey.substring(2,mkey.length()-1));
181                         String env = System.getenv(mkey.substring(2, mkey.length() - 1));
182                         tmp = tmp.replace(mkey, env == null ? "" : env);
183                     } catch (SecurityException e) {
184                         LOG.warn("unable to read env {}: {}", value, e);
185                     }
186                 }
187             }
188             value = tmp;
189         }
190         if((value==null || value == "") && valueIfEmpty!=null) {
191             value = valueIfEmpty;
192         }
193         return value;
194     }
195     private static Map<String, String> _parseHeadersMap(String s) {
196         Map<String, String> r = new HashMap<>();
197         try {
198             JSONArray a = new JSONArray(s);
199             if (a.length() > 0) {
200                 for (int i = 0; i < a.length(); i++) {
201                     String item = a.getString(i);
202                     String[] hlp = item.split(":");
203                     if (hlp.length > 1) {
204                         r.put(hlp[0], hlp[1]);
205                     }
206                 }
207             }
208         } catch (Exception e) {
209             LOG.warn("problem loading headers map: {}", e.getMessage());
210         }
211         return r;
212     }
213
214     private String writeDefaults(File f) throws IOException {
215         StringBuilder sb = new StringBuilder();
216         final String LR = "\n";
217         FileWriter fw = new FileWriter(f);
218         sb.append("aai=" + DEFAULT_AAI + LR);
219         sb.append("aaiHeaders=" + DEFAULT_AAI_HEADERS + LR);
220         sb.append("database=" + DEFAULT_ESDATABASE + LR);
221         sb.append("tiles=" + DEFAULT_TILES + LR);
222         sb.append("topology=" + DEFAULT_TOPOLOGY + LR);
223         sb.append("terrain=" + DEFAULT_TERRAIN + LR);
224         sb.append("insecure=" + DEFAULT_TRUSTINSECURE + LR);
225         sb.append("cors=" + DEFAULT_CORSENABLED);
226         try {
227             fw.write(sb.toString());
228         } catch (Exception e) {
229             LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath(), e.getMessage());
230         } finally {
231             fw.close();
232         }
233         return sb.toString();
234     }
235
236     public static MyProperties getInstance() {
237         return mObj;
238     }
239
240     @Override
241     public String toString() {
242         return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
243                 + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";
244     }
245
246
247 }