Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / elasticsearch / config / ElasticSearchConfig.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.dal.elasticsearch.config;
27
28 import java.io.BufferedReader;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.Properties;
32
33 import org.openecomp.sparky.dal.exception.ElasticSearchOperationException;
34 import org.openecomp.sparky.synchronizer.config.TaskProcessorConfig;
35 import org.openecomp.sparky.util.ConfigHelper;
36 import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants;
37
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.JsonNode;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.node.ObjectNode;
42
43
44 /**
45  * The Class ElasticSearchConfig.
46  */
47 public class ElasticSearchConfig {
48
49   public static final String CONFIG_FILE =
50       TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION + "elasticsearch.properties";
51
52   private static ElasticSearchConfig instance;
53
54   private String ipAddress;
55
56   private String httpPort;
57
58   private String javaApiPort;
59
60   private String indexName;
61
62   private String type;
63
64   private String clusterName;
65
66   private String mappingsFileName;
67
68   private String settingsFileName;
69
70   private int syncAdapterMaxConcurrentWorkers;
71
72   private String auditIndexName;
73
74   private String topographicalSearchIndex;
75
76   private String entityCountHistoryIndex;
77
78   private String autosuggestIndexname;
79
80   private String entityCountHistoryMappingsFileName;
81
82   private String autoSuggestSettingsFileName;
83
84   private String autoSuggestMappingsFileName;
85   
86   private String dynamicMappingsFileName;
87
88   private static final String IP_ADDRESS_DEFAULT = "localhost";
89
90   private static final String HTTP_PORT_DEFAULT = "9200";
91
92   private static final String JAVA_API_PORT_DEFAULT = "9300";
93
94   private static final String TYPE_DEFAULT = "aaiEntities";
95
96   private static final String CLUSTER_NAME_DEFAULT = "elasticsearch";
97
98   private static final String INDEX_NAME_DEFAULT = "entitySearchIndex";
99
100   private static final String AUDIT_INDEX_NAME_DEFAULT = "auditdataindex";
101
102   private static final String TOPOGRAPHICAL_INDEX_NAME_DEFAULT = "topographicalSearchIndex";
103
104   private static final String ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT = "entityCountHistory";
105
106   private static final String ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT =
107       TierSupportUiConstants.ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT;
108
109   private static final String ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT =
110       TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
111
112   private static final String ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT =
113       TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
114   
115   private static final String ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT =
116       TierSupportUiConstants.ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT;
117
118   private static final String BULK_API = "_bulk";
119
120   private TaskProcessorConfig processorConfig;
121
122   public TaskProcessorConfig getProcessorConfig() {
123     return processorConfig;
124   }
125
126   public void setProcessorConfig(TaskProcessorConfig processorConfig) {
127     this.processorConfig = processorConfig;
128   }
129
130   public static ElasticSearchConfig getConfig() throws Exception {
131
132     if (instance == null) {
133       instance = new ElasticSearchConfig();
134       instance.initializeProperties();
135     }
136
137     return instance;
138   }
139
140   public static void setConfig(ElasticSearchConfig config) {
141     /*
142      * Explicitly allow setting the configuration singleton. This will be useful for automation.
143      */
144
145     ElasticSearchConfig.instance = config;
146   }
147
148   /**
149    * Instantiates a new elastic search config.
150    */
151   public ElasticSearchConfig() {
152     // test method
153   }
154
155   public String getElasticFullUrl(String resourceUrl, String indexName, String indexType)
156       throws Exception {
157     final String host = getIpAddress();
158     final String port = getHttpPort();
159     return String.format("http://%s:%s/%s/%s%s", host, port, indexName, indexType, resourceUrl);
160   }
161
162   public String getElasticFullUrl(String resourceUrl, String indexName) throws Exception {
163     final String host = getIpAddress();
164     final String port = getHttpPort();
165     return String.format("http://%s:%s/%s/%s%s", host, port, indexName,
166         ElasticSearchConfig.getConfig().getType(), resourceUrl);
167   }
168
169   public String getElasticFullUrl(String resourceUrl) throws Exception {
170     final String host = getIpAddress();
171     final String port = getHttpPort();
172     final String indexName = getIndexName();
173     return String.format("http://%s:%s/%s/%s%s", host, port, indexName, getType(), resourceUrl);
174   }
175
176   /**
177    * Initialize properties.
178    */
179   private void initializeProperties() {
180     Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE);
181
182     ipAddress = props.getProperty("elasticsearch.ipAddress", IP_ADDRESS_DEFAULT);
183     httpPort = props.getProperty("elasticsearch.httpPort", "" + HTTP_PORT_DEFAULT);
184     javaApiPort = props.getProperty("elasticsearch.javaApiPort", "" + JAVA_API_PORT_DEFAULT);
185     type = props.getProperty("elasticsearch.type", TYPE_DEFAULT);
186     clusterName = props.getProperty("elasticsearch.clusterName", CLUSTER_NAME_DEFAULT);
187     indexName = props.getProperty("elasticsearch.indexName", INDEX_NAME_DEFAULT);
188     mappingsFileName = props.getProperty("elasticsearch.mappingsFileName");
189     settingsFileName = props.getProperty("elasticsearch.settingsFileName");
190     auditIndexName = props.getProperty("elasticsearch.auditIndexName", AUDIT_INDEX_NAME_DEFAULT);
191     topographicalSearchIndex =
192         props.getProperty("elasticsearch.topographicalIndexName", TOPOGRAPHICAL_INDEX_NAME_DEFAULT);
193     entityCountHistoryIndex = props.getProperty("elasticsearch.entityCountHistoryIndexName",
194         ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT);
195     entityCountHistoryMappingsFileName =
196         props.getProperty("elasticsearch.entityCountHistoryMappingsFileName");
197
198     autosuggestIndexname = props.getProperty("elasticsearch.autosuggestIndexname",
199         ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT);
200     autoSuggestSettingsFileName = props.getProperty("elasticsearch.autosuggestSettingsFileName",
201         ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT);
202     autoSuggestMappingsFileName = props.getProperty("elasticsearch.autosuggestMappingsFileName",
203         ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT);
204     dynamicMappingsFileName = props.getProperty("elasticsearch.dynamicMappingsFileName",
205         ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT);
206
207     syncAdapterMaxConcurrentWorkers =
208         Integer.parseInt(props.getProperty("elasticsearch.syncAdapter.maxConcurrentWorkers", "5"));
209
210     processorConfig = new TaskProcessorConfig();
211     processorConfig.initializeFromProperties(
212         ConfigHelper.getConfigWithPrefix("elasticsearch.taskProcessor", props));
213
214   }
215
216   public String getIpAddress() {
217     return ipAddress;
218   }
219
220   public void setIpAddress(String ipAddress) {
221     this.ipAddress = ipAddress;
222   }
223
224   public String getHttpPort() {
225     return httpPort;
226   }
227
228   public void setHttpPort(String httpPort) {
229     this.httpPort = httpPort;
230   }
231
232   public String getJavaApiPort() {
233     return javaApiPort;
234   }
235
236   public void setJavaApiPort(String javaApiPort) {
237     this.javaApiPort = javaApiPort;
238   }
239
240   public String getIndexName() {
241     return indexName;
242   }
243
244   public void setIndexName(String indexName) {
245     this.indexName = indexName;
246   }
247
248   public String getType() {
249     return type;
250   }
251
252   public void setType(String type) {
253     this.type = type;
254   }
255
256   public String getClusterName() {
257     return clusterName;
258   }
259
260   public void setClusterName(String clusterName) {
261     this.clusterName = clusterName;
262   }
263
264   public String getMappingsFileName() {
265     return mappingsFileName;
266   }
267
268   public void setMappingsFileName(String mappingsFileName) {
269     this.mappingsFileName = mappingsFileName;
270   }
271
272   public String getSettingsFileName() {
273     return settingsFileName;
274   }
275
276   public int getSyncAdapterMaxConcurrentWorkers() {
277     return syncAdapterMaxConcurrentWorkers;
278   }
279
280   public void setSyncAdapterMaxConcurrentWorkers(int syncAdapterMaxConcurrentWorkers) {
281     this.syncAdapterMaxConcurrentWorkers = syncAdapterMaxConcurrentWorkers;
282   }
283
284   public void setSettingsFileName(String settingsFileName) {
285     this.settingsFileName = settingsFileName;
286   }
287
288   public String getAuditIndexName() {
289     return auditIndexName;
290   }
291
292   public void setAuditIndexName(String auditIndexName) {
293     this.auditIndexName = auditIndexName;
294   }
295
296   public String getTopographicalSearchIndex() {
297     return topographicalSearchIndex;
298   }
299
300   public void setTopographicalSearchIndex(String topographicalSearchIndex) {
301     this.topographicalSearchIndex = topographicalSearchIndex;
302   }
303
304   public String getEntityCountHistoryIndex() {
305     return entityCountHistoryIndex;
306   }
307
308   public void setEntityCountHistoryIndex(String entityCountHistoryIndex) {
309     this.entityCountHistoryIndex = entityCountHistoryIndex;
310   }
311
312
313   public String getEntityCountHistoryMappingsFileName() {
314     return entityCountHistoryMappingsFileName;
315   }
316
317   public void setEntityCountHistoryMappingsFileName(String entityCountHistoryMappingsFileName) {
318     this.entityCountHistoryMappingsFileName = entityCountHistoryMappingsFileName;
319   }
320
321   public String getBulkUrl() {
322     String url = this.getIpAddress();
323     String port = this.getHttpPort();
324     return String.format("http://%s:%s/%s", url, port, BULK_API);
325   }
326
327   public String getConfigAsString(String configItem, String configFileName)
328       throws ElasticSearchOperationException {
329     String indexConfig = null;
330
331     try {
332       indexConfig = ConfigHelper.getFileContents(configFileName);
333     } catch (IOException exc) {
334       throw new ElasticSearchOperationException(
335           "Failed to read index " + configItem + " from file = " + configFileName + ".", exc);
336     }
337
338     if (indexConfig == null) {
339       throw new ElasticSearchOperationException(
340           "Failed to load index " + configItem + " with filename = " + configFileName + ".");
341     }
342     return indexConfig;
343   }
344
345   public String getElasticSearchSettings() throws ElasticSearchOperationException {
346     return getConfigAsString("settings",
347         TierSupportUiConstants.getConfigPath(this.getSettingsFileName()));
348   }
349
350   public String getDynamicMappings() throws ElasticSearchOperationException{
351     return getConfigAsString("mapping",
352         TierSupportUiConstants.getConfigPath(this.getDynamicMappingsFileName()));
353   }
354   public String getElasticSearchMappings() throws ElasticSearchOperationException {
355     return getConfigAsString("mapping",
356         TierSupportUiConstants.getConfigPath(this.getMappingsFileName()));
357   }
358
359   public String getElasticSearchEntityCountHistoryMappings() 
360       throws ElasticSearchOperationException {
361     return getConfigAsString("mapping",
362         TierSupportUiConstants.getConfigPath(this.getEntityCountHistoryMappingsFileName()));
363   }
364
365   public String getAutosuggestIndexSettings() throws ElasticSearchOperationException {
366     return getConfigAsString("setting",
367         TierSupportUiConstants.getConfigPath(this.getAutoSuggestSettingsFileName()));
368   }
369
370   public String getAutosuggestIndexMappings() throws ElasticSearchOperationException {
371     return getConfigAsString("mapping",
372         TierSupportUiConstants.getConfigPath(this.getAutoSuggestMappingsFileName()));
373   }
374
375   public String getAutosuggestIndexname() {
376     return autosuggestIndexname;
377   }
378
379   public void setAutosuggestIndexname(String autosuggestIndexname) {
380     this.autosuggestIndexname = autosuggestIndexname;
381   }
382
383   public String getAutoSuggestSettingsFileName() {
384     return autoSuggestSettingsFileName;
385   }
386
387   public void setAutoSuggestSettingsFileName(String autoSuggestSettingsFileName) {
388     this.autoSuggestSettingsFileName = autoSuggestSettingsFileName;
389   }
390
391   public String getAutoSuggestMappingsFileName() {
392     return autoSuggestMappingsFileName;
393   }
394
395   public void setAutoSuggestMappingsFileName(String autoSuggestMappingsFileName) {
396     this.autoSuggestMappingsFileName = autoSuggestMappingsFileName;
397   }
398
399   public String getDynamicMappingsFileName() {
400     return dynamicMappingsFileName;
401   }
402
403   public void setDynamicMappingsFileName(String dynamicMappingsFileName) {
404     this.dynamicMappingsFileName = dynamicMappingsFileName;
405   }
406
407   /**
408    * Builds the elastic search table config.
409    *
410    * @return the string
411    * @throws ElasticSearchOperationException the elastic search operation exception
412    */
413   public String buildElasticSearchTableConfig() throws ElasticSearchOperationException {
414
415     JsonNode esSettingsNode;
416     JsonNode esMappingsNodes;
417     ObjectMapper mapper = new ObjectMapper();
418
419     try {
420       esSettingsNode = mapper.readTree(getElasticSearchSettings());
421       esMappingsNodes = mapper.readTree(getElasticSearchMappings());
422     } catch (IOException e1) {
423       throw new ElasticSearchOperationException("Caught an exception building initial ES index");
424     }
425
426     ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
427     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
428
429     esConfig.set("mappings", mappings);
430
431     try {
432       return mapper.writeValueAsString(esConfig);
433     } catch (JsonProcessingException exc) {
434       throw new ElasticSearchOperationException("Error getting object node as string", exc);
435     }
436
437   }
438
439   /**
440    * Builds the elastic search entity count history table config.
441    *
442    * @return the string
443    * @throws ElasticSearchOperationException the elastic search operation exception
444    */
445   public String buildElasticSearchEntityCountHistoryTableConfig()
446       throws ElasticSearchOperationException {
447
448     JsonNode esSettingsNode;
449     JsonNode esMappingsNodes;
450     ObjectMapper mapper = new ObjectMapper();
451
452     try {
453       esSettingsNode = mapper.readTree(getElasticSearchSettings());
454       esMappingsNodes = mapper.readTree(getElasticSearchEntityCountHistoryMappings());
455     } catch (IOException e1) {
456       throw new ElasticSearchOperationException("Caught an exception building initial ES index");
457     }
458
459     ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
460     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
461
462     esConfig.set("mappings", mappings);
463
464     try {
465       return mapper.writeValueAsString(esConfig);
466     } catch (JsonProcessingException exc) {
467       throw new ElasticSearchOperationException("Error getting object node as string", exc);
468     }
469
470   }
471
472   public String buildAggregationTableConfig() throws ElasticSearchOperationException {
473
474     JsonNode esMappingsNodes;
475     ObjectMapper mapper = new ObjectMapper();
476
477     try {
478       esMappingsNodes = mapper.readTree(this.getDynamicMappings());
479     } catch (IOException e1) {
480       throw new ElasticSearchOperationException(
481           "Caught an exception building Aggreagation ES index");
482     }
483
484     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
485
486     ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("mappings", mappings);
487
488     try {
489       return mapper.writeValueAsString(indexConfig);
490     } catch (JsonProcessingException exc) {
491       throw new ElasticSearchOperationException("Error getting object node as string", exc);
492     }
493
494   }
495   
496   public String buildAutosuggestionTableConfig() throws ElasticSearchOperationException {
497
498     JsonNode esSettingsNode;
499     JsonNode esMappingsNodes;
500     ObjectMapper mapper = new ObjectMapper();
501
502     try {
503       esSettingsNode = mapper.readTree(this.getAutosuggestIndexSettings());
504       esMappingsNodes = mapper.readTree(this.getAutosuggestIndexMappings());
505     } catch (IOException e1) {
506       throw new ElasticSearchOperationException(
507           "Caught an exception building Autosuggestion ES index");
508     }
509
510     ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
511     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
512
513     indexConfig.set("mappings", mappings);
514
515     try {
516       return mapper.writeValueAsString(indexConfig);
517     } catch (JsonProcessingException exc) {
518       throw new ElasticSearchOperationException("Error getting object node as string", exc);
519     }
520
521   }
522   
523   /*
524    * (non-Javadoc)
525    * 
526    * @see java.lang.Object#toString()
527    */
528   @Override
529   public String toString() {
530     return "ElasticSearchConfig [ipAddress=" + ipAddress + ", httpPort=" + httpPort
531         + ", javaApiPort=" + javaApiPort + ", indexName=" + indexName + ", type=" + type
532         + ", clusterName=" + clusterName + ", mappingsFileName=" + mappingsFileName
533         + ", settingsFileName=" + settingsFileName + ", syncAdapterMaxConcurrentWorkers="
534         + syncAdapterMaxConcurrentWorkers + ", auditIndexName=" + auditIndexName
535         + ", topographicalSearchIndex=" + topographicalSearchIndex + ", entityCountHistoryIndex="
536         + entityCountHistoryIndex + ", autosuggestIndexname=" + autosuggestIndexname
537         + ", entityCountHistoryMappingsFileName=" + entityCountHistoryMappingsFileName
538         + ", autoSuggestSettingsFileName=" + autoSuggestSettingsFileName
539         + ", autoSuggestMappingsFileName=" + autoSuggestMappingsFileName
540         + ", dynamicMappingsFileName=" + dynamicMappingsFileName + ", processorConfig="
541         + processorConfig + "]";
542   }
543 }