d644c63a3421e372990ecd95356d1523c518124b
[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 org.json.JSONArray;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class MyProperties {
36
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";
44
45     private static MyProperties mObj;
46
47     private String aaiBase;
48     private Map<String, String> aaiHeaders;
49     private String esBase;
50
51     private boolean trustInsecure;
52
53     private boolean corsEnabled;
54
55     public boolean isAAIOff() {
56         return this.aaiBase == null ? true : this.aaiBase.equals("off");
57     }
58
59     public boolean isEsOff() {
60         return this.esBase == null ? true : this.esBase.equals("off");
61     }
62
63     public String getAAIBaseUrl() {
64         return this.aaiBase;
65     }
66
67     public String getEsBaseUrl() {
68         return this.esBase;
69     }
70
71     public Map<String, String> getAAIHeaders() {
72         return this.aaiHeaders;
73     }
74
75     public boolean trustInsecure() {
76         return this.trustInsecure;
77     }
78
79     public boolean corsEnabled() {
80         return this.corsEnabled;
81     }
82
83     public static MyProperties Instantiate() throws IOException, NumberFormatException {
84         return Instantiate(new File(PROPFILE));
85     }
86
87     public static MyProperties Instantiate(File file) throws IOException, NumberFormatException {
88
89         return Instantiate(file, false);
90     }
91
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());
96         }
97         return mObj;
98     }
99
100     private MyProperties(File file) throws IOException, NumberFormatException {
101         this.aaiBase = "off";
102         this.trustInsecure = false;
103         if (!file.exists()) {
104             this.writeDefaults(file);
105         }
106         this.load(new FileInputStream(file));
107     }
108
109     public void load(InputStream in) throws IOException, NumberFormatException {
110
111         Properties defaultProps = new Properties();
112         defaultProps.load(in);
113         in.close();
114
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;
120     }
121
122     private static Map<String, String> _parseHeadersMap(String s) {
123         Map<String, String> r = new HashMap<>();
124         try {
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]);
132                     }
133                 }
134             }
135         } catch (Exception e) {
136             LOG.warn("problem loading headers map: {}", e.getMessage());
137         }
138         return r;
139     }
140
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);
150         try {
151             fw.write(sb.toString());
152         } catch (Exception e) {
153             LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath(), e.getMessage());
154         } finally {
155             fw.close();
156         }
157         return sb.toString();
158     }
159
160     public static MyProperties getInstance() {
161         return mObj;
162     }
163
164     @Override
165     public String toString() {
166         return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
167                 + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";
168     }
169 }