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 org.json.JSONArray;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class MyProperties {
37 private static Logger LOG = LoggerFactory.getLogger(MyProperties.class);
38 public static final String PROPFILE = "etc/apigateway.properties";
39 private static final String DEFAULT_AAI_HEADERS = "[\"X-FromAppId:SDNR\",\"Authorization:Basic QUFJOkFBSQ==\"]";
40 private static final String DEFAULT_CORSENABLED = "0";
41 private static final String DEFAULT_TRUSTINSECURE = "0";
42 private static final String DEFAULT_ESDATABASE = "http://sdnrdb:9200";
43 private static final String DEFAULT_AAI = "off";
45 private static MyProperties mObj;
47 private String aaiBase;
48 private Map<String, String> aaiHeaders;
49 private String esBase;
51 private boolean trustInsecure;
53 private boolean corsEnabled;
55 public boolean isAAIOff() {
56 return this.aaiBase == null ? true : this.aaiBase.equals("off");
59 public boolean isEsOff() {
60 return this.esBase == null ? true : this.esBase.equals("off");
63 public String getAAIBaseUrl() {
67 public String getEsBaseUrl() {
71 public Map<String, String> getAAIHeaders() {
72 return this.aaiHeaders;
75 public boolean trustInsecure() {
76 return this.trustInsecure;
79 public boolean corsEnabled() {
80 return this.corsEnabled;
83 public static MyProperties Instantiate() throws IOException, NumberFormatException {
84 return Instantiate(new File(PROPFILE));
87 public static MyProperties Instantiate(File file) throws IOException, NumberFormatException {
89 return Instantiate(file, false);
92 public static MyProperties Instantiate(File file, boolean force) throws IOException, NumberFormatException {
93 if (mObj == null || force) {
94 mObj = new MyProperties(file);
95 LOG.debug("instantiated: {}", mObj.toString());
100 private MyProperties(File file) throws IOException, NumberFormatException {
101 this.aaiBase = "off";
102 this.trustInsecure = false;
103 if (!file.exists()) {
104 this.writeDefaults(file);
106 this.load(new FileInputStream(file));
109 public void load(InputStream in) throws IOException, NumberFormatException {
111 Properties defaultProps = new Properties();
112 defaultProps.load(in);
115 this.aaiBase = defaultProps.getProperty("aai", DEFAULT_AAI);
116 this.aaiHeaders = _parseHeadersMap(defaultProps.getProperty("aaiHeaders", DEFAULT_AAI_HEADERS));
117 this.esBase = defaultProps.getProperty("database", DEFAULT_ESDATABASE);
118 this.trustInsecure = Integer.parseInt(defaultProps.getProperty("insecure", DEFAULT_TRUSTINSECURE)) == 1;
119 this.corsEnabled = Integer.parseInt(defaultProps.getProperty("cors", DEFAULT_CORSENABLED)) == 1;
122 private static Map<String, String> _parseHeadersMap(String s) {
123 Map<String, String> r = new HashMap<>();
125 JSONArray a = new JSONArray(s);
126 if (a.length() > 0) {
127 for (int i = 0; i < a.length(); i++) {
128 String item = a.getString(i);
129 String[] hlp = item.split(":");
130 if (hlp.length > 1) {
131 r.put(hlp[0], hlp[1]);
135 } catch (Exception e) {
136 LOG.warn("problem loading headers map: {}", e.getMessage());
141 private String writeDefaults(File f) throws IOException {
142 StringBuilder sb = new StringBuilder();
143 final String LR = "\n";
144 FileWriter fw = new FileWriter(f);
145 sb.append("aai=" + DEFAULT_AAI + LR);
146 sb.append("aaiHeaders=" + DEFAULT_AAI_HEADERS + LR);
147 sb.append("database=" + DEFAULT_ESDATABASE + LR);
148 sb.append("insecure=" + DEFAULT_TRUSTINSECURE + LR);
149 sb.append("cors=" + DEFAULT_CORSENABLED);
151 fw.write(sb.toString());
152 } catch (Exception e) {
153 LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath(), e.getMessage());
157 return sb.toString();
160 public static MyProperties getInstance() {
165 public String toString() {
166 return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
167 + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";