Adding UI extensibility
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / elasticsearch / config / ElasticSearchConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.dal.elasticsearch.config;
24
25 import java.util.Properties;
26
27 import org.onap.aai.sparky.util.ConfigHelper;
28 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
29
30
31 /**
32  * The Class ElasticSearchConfig.
33  */
34 public class ElasticSearchConfig {
35
36   public static final String CONFIG_FILE =
37       TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION + "elasticsearch.properties";
38
39   private static ElasticSearchConfig instance;
40
41   private String ipAddress;
42
43   private String httpPort;
44
45   private String javaApiPort;
46
47   private String indexName;
48
49   private String type;
50
51   private String clusterName;
52
53   private String mappingsFileName;
54
55   private String settingsFileName;
56
57   private String topographicalSearchIndex;
58
59   private String entityCountHistoryIndex;
60
61   private String autosuggestIndexname;
62
63   private String entityCountHistoryMappingsFileName;
64
65   private String autoSuggestSettingsFileName;
66
67   private String autoSuggestMappingsFileName;
68
69   private String dynamicMappingsFileName;
70
71   private static final String IP_ADDRESS_DEFAULT = "localhost";
72
73   private static final String HTTP_PORT_DEFAULT = "9200";
74
75   private static final String JAVA_API_PORT_DEFAULT = "9300";
76
77   private static final String TYPE_DEFAULT = "aaiEntities";
78
79   private static final String CLUSTER_NAME_DEFAULT = "elasticsearch";
80
81   private static final String INDEX_NAME_DEFAULT = "entitySearchIndex";
82
83   private static final String AUDIT_INDEX_NAME_DEFAULT = "auditdataindex";
84
85   private static final String TOPOGRAPHICAL_INDEX_NAME_DEFAULT = "topographicalSearchIndex";
86
87   private static final String ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT = "entityCountHistory";
88
89   private static final String ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT =
90       TierSupportUiConstants.ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT;
91
92   private static final String ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT =
93       TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
94
95   private static final String ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT =
96       TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
97
98   private static final String ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT =
99       TierSupportUiConstants.ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT;
100
101   private static final String BULK_API = "_bulk";
102
103   public static ElasticSearchConfig getConfig() throws Exception {
104
105     if (instance == null) {
106       instance = new ElasticSearchConfig();
107       instance.initializeProperties();
108     }
109
110     return instance;
111   }
112
113   public static void setConfig(ElasticSearchConfig config) {
114     /*
115      * Explicitly allow setting the configuration singleton. This will be useful for automation.
116      */
117
118     ElasticSearchConfig.instance = config;
119   }
120
121   /**
122    * Instantiates a new elastic search config.
123    */
124   public ElasticSearchConfig() {
125     // test method
126   }
127
128   public String getElasticFullUrl(String resourceUrl, String indexName, String indexType)
129       throws Exception {
130     final String host = getIpAddress();
131     final String port = getHttpPort();
132     return String.format("http://%s:%s/%s/%s%s", host, port, indexName, indexType, resourceUrl);
133   }
134
135   public String getElasticFullUrl(String resourceUrl, String indexName) throws Exception {
136     final String host = getIpAddress();
137     final String port = getHttpPort();
138     return String.format("http://%s:%s/%s/%s%s", host, port, indexName,
139         ElasticSearchConfig.getConfig().getType(), resourceUrl);
140   }
141
142   public String getElasticFullUrl(String resourceUrl) throws Exception {
143     final String host = getIpAddress();
144     final String port = getHttpPort();
145     final String indexName = getIndexName();
146     return String.format("http://%s:%s/%s/%s%s", host, port, indexName, getType(), resourceUrl);
147   }
148
149   /**
150    * Initialize properties.
151    */
152   private void initializeProperties() {
153     Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE);
154
155     if (props == null || props.isEmpty()) {
156       return;
157     }
158
159     ipAddress = props.getProperty("elasticsearch.ipAddress", IP_ADDRESS_DEFAULT);
160     httpPort = props.getProperty("elasticsearch.httpPort", "" + HTTP_PORT_DEFAULT);
161     javaApiPort = props.getProperty("elasticsearch.javaApiPort", "" + JAVA_API_PORT_DEFAULT);
162     type = props.getProperty("elasticsearch.type", TYPE_DEFAULT);
163     clusterName = props.getProperty("elasticsearch.clusterName", CLUSTER_NAME_DEFAULT);
164     indexName = props.getProperty("elasticsearch.indexName", INDEX_NAME_DEFAULT);
165     mappingsFileName = props.getProperty("elasticsearch.mappingsFileName");
166     settingsFileName = props.getProperty("elasticsearch.settingsFileName");
167     topographicalSearchIndex =
168         props.getProperty("elasticsearch.topographicalIndexName", TOPOGRAPHICAL_INDEX_NAME_DEFAULT);
169     entityCountHistoryIndex = props.getProperty("elasticsearch.entityCountHistoryIndexName",
170         ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT);
171
172     entityCountHistoryMappingsFileName =
173         props.getProperty("elasticsearch.entityCountHistoryMappingsFileName");
174
175     autosuggestIndexname = props.getProperty("elasticsearch.autosuggestIndexname",
176         ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT);
177     autoSuggestSettingsFileName = props.getProperty("elasticsearch.autosuggestSettingsFileName",
178         ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT);
179     autoSuggestMappingsFileName = props.getProperty("elasticsearch.autosuggestMappingsFileName",
180         ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT);
181     dynamicMappingsFileName = props.getProperty("elasticsearch.dynamicMappingsFileName",
182         ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT);
183
184   }
185
186   public String getIpAddress() {
187     return ipAddress;
188   }
189
190   public void setIpAddress(String ipAddress) {
191     this.ipAddress = ipAddress;
192   }
193
194   public String getHttpPort() {
195     return httpPort;
196   }
197
198   public void setHttpPort(String httpPort) {
199     this.httpPort = httpPort;
200   }
201
202   public String getJavaApiPort() {
203     return javaApiPort;
204   }
205
206   public void setJavaApiPort(String javaApiPort) {
207     this.javaApiPort = javaApiPort;
208   }
209
210   public String getIndexName() {
211     return indexName;
212   }
213
214   public void setIndexName(String indexName) {
215     this.indexName = indexName;
216   }
217
218   public String getType() {
219     return type;
220   }
221
222   public void setType(String type) {
223     this.type = type;
224   }
225
226   public String getClusterName() {
227     return clusterName;
228   }
229
230   public void setClusterName(String clusterName) {
231     this.clusterName = clusterName;
232   }
233
234   public String getMappingsFileName() {
235     return mappingsFileName;
236   }
237
238   public void setMappingsFileName(String mappingsFileName) {
239     this.mappingsFileName = mappingsFileName;
240   }
241
242   public String getSettingsFileName() {
243     return settingsFileName;
244   }
245
246   public void setSettingsFileName(String settingsFileName) {
247     this.settingsFileName = settingsFileName;
248   }
249
250   public String getTopographicalSearchIndex() {
251     return topographicalSearchIndex;
252   }
253
254   public void setTopographicalSearchIndex(String topographicalSearchIndex) {
255     this.topographicalSearchIndex = topographicalSearchIndex;
256   }
257
258   public String getEntityCountHistoryIndex() {
259     return entityCountHistoryIndex;
260   }
261
262   public void setEntityCountHistoryIndex(String entityCountHistoryIndex) {
263     this.entityCountHistoryIndex = entityCountHistoryIndex;
264   }
265
266
267   public String getEntityCountHistoryMappingsFileName() {
268     return entityCountHistoryMappingsFileName;
269   }
270
271   public void setEntityCountHistoryMappingsFileName(String entityCountHistoryMappingsFileName) {
272     this.entityCountHistoryMappingsFileName = entityCountHistoryMappingsFileName;
273   }
274
275   public String getBulkUrl() {
276     String url = this.getIpAddress();
277     String port = this.getHttpPort();
278     return String.format("http://%s:%s/%s", url, port, BULK_API);
279   }
280
281   public String getAutosuggestIndexname() {
282     return autosuggestIndexname;
283   }
284
285   public void setAutosuggestIndexname(String autosuggestIndexname) {
286     this.autosuggestIndexname = autosuggestIndexname;
287   }
288
289   public String getAutoSuggestSettingsFileName() {
290     return autoSuggestSettingsFileName;
291   }
292
293   public void setAutoSuggestSettingsFileName(String autoSuggestSettingsFileName) {
294     this.autoSuggestSettingsFileName = autoSuggestSettingsFileName;
295   }
296
297   public String getAutoSuggestMappingsFileName() {
298     return autoSuggestMappingsFileName;
299   }
300
301   public void setAutoSuggestMappingsFileName(String autoSuggestMappingsFileName) {
302     this.autoSuggestMappingsFileName = autoSuggestMappingsFileName;
303   }
304
305 }