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;
32 import org.json.JSONArray;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class MyProperties {
38 private static Logger LOG = LoggerFactory.getLogger(MyProperties.class);
39 public static final String PROPFILE = "etc/apigateway.properties";
40 private static final String DEFAULT_AAI_HEADERS = "[\"X-FromAppId:SDNR\",\"Authorization:Basic QUFJOkFBSQ==\"]";
41 private static final String DEFAULT_CORSENABLED = "0";
42 private static final String DEFAULT_TRUSTINSECURE = "0";
43 private static final String DEFAULT_ESDATABASE = "http://sdnrdb:9200";
44 private static final String DEFAULT_AAI = "off";
47 public String toString() {
48 return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
49 + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";
52 private static MyProperties mObj;
54 private String aaiBase;
55 private Map<String, String> aaiHeaders;
56 private String esBase;
58 private boolean trustInsecure;
60 private boolean corsEnabled;
62 public boolean isAAIOff() {
63 return this.aaiBase == null ? true : this.aaiBase.equals("off");
66 public boolean isEsOff() {
67 return this.esBase == null ? true : this.esBase.equals("off");
70 public String getAAIBaseUrl() {
74 public String getEsBaseUrl() {
78 public Map<String, String> getAAIHeaders() {
79 return this.aaiHeaders;
82 public boolean trustInsecure() {
83 return this.trustInsecure;
86 public boolean corsEnabled() {
87 return this.corsEnabled;
90 public static MyProperties Instantiate() throws IOException, NumberFormatException {
91 return Instantiate(new File(PROPFILE));
94 public static MyProperties Instantiate(File file) throws IOException, NumberFormatException {
96 return Instantiate(file, false);
99 public static MyProperties Instantiate(File file, boolean force) throws IOException, NumberFormatException {
100 if (mObj == null || force) {
101 mObj = new MyProperties(file);
102 LOG.debug("instantiated: {}", mObj.toString());
107 private MyProperties(File file) throws IOException, NumberFormatException {
108 this.aaiBase = "off";
109 this.trustInsecure = false;
110 if (!file.exists()) {
111 this.writeDefaults(file);
113 this.load(new FileInputStream(file));
116 public void load(InputStream in) throws IOException, NumberFormatException {
118 Properties defaultProps = new Properties();
119 defaultProps.load(in);
122 this.aaiBase = defaultProps.getProperty("aai", DEFAULT_AAI);
123 this.aaiHeaders = _parseHeadersMap(defaultProps.getProperty("aaiHeaders", DEFAULT_AAI_HEADERS));
124 this.esBase = defaultProps.getProperty("database", DEFAULT_ESDATABASE);
125 this.trustInsecure = Integer.parseInt(defaultProps.getProperty("insecure", DEFAULT_TRUSTINSECURE)) == 1;
126 this.corsEnabled = Integer.parseInt(defaultProps.getProperty("cors", DEFAULT_CORSENABLED)) == 1;
129 private static Map<String, String> _parseHeadersMap(String s) {
130 Map<String, String> r = new HashMap<>();
132 JSONArray a = new JSONArray(s);
133 if ( a.length() > 0) {
134 for (int i = 0; i < a.length(); i++) {
135 String item = a.getString(i);
136 String[] hlp = item.split(":");
137 if (hlp.length > 1) {
138 r.put(hlp[0], hlp[1]);
142 } catch (Exception e) {
143 LOG.warn("problem loading headers map: {}",e.getMessage());
148 private String writeDefaults(File f) throws IOException {
149 StringBuilder sb = new StringBuilder();
150 final String LR = "\n";
151 FileWriter fw = new FileWriter(f);
152 sb.append("aai=" + DEFAULT_AAI + LR);
153 sb.append("aaiHeaders=" + DEFAULT_AAI_HEADERS + LR);
154 sb.append("database=" + DEFAULT_ESDATABASE + LR);
155 sb.append("insecure=" + DEFAULT_TRUSTINSECURE + LR);
156 sb.append("cors=" + DEFAULT_CORSENABLED);
158 fw.write(sb.toString());
159 } catch (Exception e) {
160 LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath() , e.getMessage());
164 return sb.toString();
167 public static MyProperties getInstance() {