3d66f526eb1e0273afe5dc4bcbba03f7cec8137f
[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
32 import org.json.JSONArray;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class MyProperties {
37
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://localhost:9200";
44         private static final String DEFAULT_AAI = "off";
45
46         @Override
47         public String toString() {
48                 return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase
49                                 + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]";
50         }
51
52         private static MyProperties mObj;
53
54         private String aaiBase;
55         private Map<String, String> aaiHeaders;
56         private String esBase;
57
58         private boolean trustInsecure;
59
60         private boolean corsEnabled;
61
62         public boolean isAAIOff() {
63                 return this.aaiBase == null ? true : this.aaiBase.equals("off");
64         }
65
66         public boolean isEsOff() {
67                 return this.esBase == null ? true : this.esBase.equals("off");
68         }
69
70         public String getAAIBaseUrl() {
71                 return this.aaiBase;
72         }
73
74         public String getEsBaseUrl() {
75                 return this.esBase;
76         }
77
78         public Map<String, String> getAAIHeaders() {
79                 return this.aaiHeaders;
80         }
81
82         public boolean trustInsecure() {
83                 return this.trustInsecure;
84         }
85
86         public boolean corsEnabled() {
87                 return this.corsEnabled;
88         }
89
90         public static MyProperties Instantiate() throws IOException, NumberFormatException {
91                 return Instantiate(new File(PROPFILE));
92         }
93
94         public static MyProperties Instantiate(File file) throws IOException, NumberFormatException {
95
96                 return Instantiate(file, false);
97         }
98
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());
103                 }
104                 return mObj;
105         }
106
107         private MyProperties(File file) throws IOException, NumberFormatException {
108                 this.aaiBase = "off";
109                 this.trustInsecure = false;
110                 if (!file.exists()) {
111                         this.writeDefaults(file);
112                 }
113                 this.load(new FileInputStream(file));
114         }
115
116         public void load(InputStream in) throws IOException, NumberFormatException {
117
118                 Properties defaultProps = new Properties();
119                 defaultProps.load(in);
120                 in.close();
121
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;
127         }
128
129         private static Map<String, String> _parseHeadersMap(String s) {
130                 Map<String, String> r = new HashMap<>();
131                 try {
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]);
139                                         }
140                                 }
141                         }
142                 } catch (Exception e) {
143                         LOG.warn("problem loading headers map: {}",e.getMessage());
144                 }
145                 return r;
146         }
147
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);
157                 try {
158                         fw.write(sb.toString());
159                 } catch (Exception e) {
160                         LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath() , e.getMessage());
161                 } finally {
162                         fw.close();
163                 }
164                 return sb.toString();
165         }
166
167         public static MyProperties getInstance() {
168                 return mObj;
169         }
170
171 }