c518cff810c2d5e313bf549580b38ea42c6d0c1b
[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 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);
53
54
55     private String aaiBase;
56     private Map<String, String> aaiHeaders;
57     private String esBase;
58     private String tilesBase;
59     private String topologyBase;
60
61     private boolean trustInsecure;
62
63     private boolean corsEnabled;
64
65     public boolean isAAIOff() {
66         return this.aaiBase == null ? true : this.aaiBase.equals("off");
67     }
68
69     public boolean isEsOff() {
70         return this.esBase == null ? true : this.esBase.equals("off");
71     }
72
73     public boolean isTilesOff() {
74         return this.tilesBase == null ? true : this.tilesBase.equals("off");
75     }
76
77     public boolean isTopologyOff() {
78         return this.topologyBase == null ? true : this.topologyBase.equals("off");
79     }
80
81     public String getAAIBaseUrl() {
82         return this.aaiBase;
83     }
84
85     public String getEsBaseUrl() {
86         return this.esBase;
87     }
88
89     public String getTilesBaseUrl() {
90         return this.tilesBase;
91     }
92
93     public String getTopologyBaseUrl() {
94         return this.topologyBase;
95     }
96
97     public Map<String, String> getAAIHeaders() {
98         return this.aaiHeaders;
99     }
100
101     public boolean trustInsecure() {
102         return this.trustInsecure;
103     }
104
105     public boolean corsEnabled() {
106         return this.corsEnabled;
107     }
108
109     public static MyProperties Instantiate() throws IOException, NumberFormatException {
110         return Instantiate(new File(PROPFILE));
111     }
112
113     public static MyProperties Instantiate(File file) throws IOException, NumberFormatException {
114
115         return Instantiate(file, false);
116     }
117
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());
122         }
123         return mObj;
124     }
125
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);
131         }
132         this.load(new FileInputStream(file));
133     }
134
135     public void load(InputStream in) throws IOException, NumberFormatException {
136
137         Properties defaultProps = new Properties();
138         defaultProps.load(in);
139         in.close();
140
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;
148     }
149     private static String getProperty(Properties props,final String key, final String defValue) {
150         return getProperty(props, key, defValue, null);
151     }
152     private static String getProperty(Properties props,final String key, final String defValue, final String valueIfEmpty) {
153
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)) {
158
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);
164                 if (mkey != null) {
165                     try {
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);
172                     }
173                 }
174             }
175             value = tmp;
176         }
177         if((value==null || value == "") && valueIfEmpty!=null) {
178             value = valueIfEmpty;
179         }
180         return value;
181     }
182     private static Map<String, String> _parseHeadersMap(String s) {
183         Map<String, String> r = new HashMap<>();
184         try {
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]);
192                     }
193                 }
194             }
195         } catch (Exception e) {
196             LOG.warn("problem loading headers map: {}", e.getMessage());
197         }
198         return r;
199     }
200
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);
212         try {
213             fw.write(sb.toString());
214         } catch (Exception e) {
215             LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath(), e.getMessage());
216         } finally {
217             fw.close();
218         }
219         return sb.toString();
220     }
221
222     public static MyProperties getInstance() {
223         return mObj;
224     }
225
226     @Override
227     public String toString() {
228         return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
229                 + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";
230     }
231
232 }