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