Adding back-end support for UI filters
[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 entityCountHistoryIndex;
72
73   private String autosuggestIndexname;
74
75   private String entityCountHistoryMappingsFileName;
76
77   private String autoSuggestSettingsFileName;
78
79   private String autoSuggestMappingsFileName;
80
81   private String dynamicMappingsFileName;
82
83   private static final String IP_ADDRESS_DEFAULT = "localhost";
84
85   private static final String HTTP_PORT_DEFAULT = "9200";
86
87   private static final String JAVA_API_PORT_DEFAULT = "9300";
88
89   private static final String TYPE_DEFAULT = "aaiEntities";
90
91   private static final String CLUSTER_NAME_DEFAULT = "elasticsearch";
92
93   private static final String INDEX_NAME_DEFAULT = "entitySearchIndex";
94
95   private static final String AUDIT_INDEX_NAME_DEFAULT = "auditdataindex";
96
97   private static final String TOPOGRAPHICAL_INDEX_NAME_DEFAULT = "topographicalSearchIndex";
98
99   private static final String ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT = "entityCountHistory";
100
101   private static final String ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT =
102       TierSupportUiConstants.ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT;
103
104   private static final String ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT =
105       TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
106
107   private static final String ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT =
108       TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
109
110   private static final String ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT =
111       TierSupportUiConstants.ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT;
112
113   private static final String BULK_API = "_bulk";
114
115   private TaskProcessorConfig processorConfig;
116
117   public TaskProcessorConfig getProcessorConfig() {
118     return processorConfig;
119   }
120
121   public void setProcessorConfig(TaskProcessorConfig processorConfig) {
122     this.processorConfig = processorConfig;
123   }
124
125   public static ElasticSearchConfig getConfig() throws Exception {
126
127     if (instance == null) {
128       instance = new ElasticSearchConfig();
129       instance.initializeProperties();
130     }
131
132     return instance;
133   }
134
135   public static void setConfig(ElasticSearchConfig config) {
136     /*
137      * Explicitly allow setting the configuration singleton. This will be useful for automation.
138      */
139
140     ElasticSearchConfig.instance = config;
141   }
142
143   /**
144    * Instantiates a new elastic search config.
145    */
146   public ElasticSearchConfig() {
147     // test method
148   }
149
150   public String getElasticFullUrl(String resourceUrl, String indexName, String indexType)
151       throws Exception {
152     final String host = getIpAddress();
153     final String port = getHttpPort();
154     return String.format("http://%s:%s/%s/%s%s", host, port, indexName, indexType, resourceUrl);
155   }
156
157   public String getElasticFullUrl(String resourceUrl, String indexName) throws Exception {
158     final String host = getIpAddress();
159     final String port = getHttpPort();
160     return String.format("http://%s:%s/%s/%s%s", host, port, indexName,
161         ElasticSearchConfig.getConfig().getType(), resourceUrl);
162   }
163
164   public String getElasticFullUrl(String resourceUrl) throws Exception {
165     final String host = getIpAddress();
166     final String port = getHttpPort();
167     final String indexName = getIndexName();
168     return String.format("http://%s:%s/%s/%s%s", host, port, indexName, getType(), resourceUrl);
169   }
170
171   /**
172    * Initialize properties.
173    */
174   private void initializeProperties() {
175     Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE);
176
177     ipAddress = props.getProperty("elasticsearch.ipAddress", IP_ADDRESS_DEFAULT);
178     httpPort = props.getProperty("elasticsearch.httpPort", "" + HTTP_PORT_DEFAULT);
179     javaApiPort = props.getProperty("elasticsearch.javaApiPort", "" + JAVA_API_PORT_DEFAULT);
180     type = props.getProperty("elasticsearch.type", TYPE_DEFAULT);
181     clusterName = props.getProperty("elasticsearch.clusterName", CLUSTER_NAME_DEFAULT);
182     indexName = props.getProperty("elasticsearch.indexName", INDEX_NAME_DEFAULT);
183     mappingsFileName = props.getProperty("elasticsearch.mappingsFileName");
184     settingsFileName = props.getProperty("elasticsearch.settingsFileName");
185     auditIndexName = props.getProperty("elasticsearch.auditIndexName", AUDIT_INDEX_NAME_DEFAULT);
186     entityCountHistoryIndex = props.getProperty("elasticsearch.entityCountHistoryIndexName",
187         ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT);
188     entityCountHistoryMappingsFileName =
189         props.getProperty("elasticsearch.entityCountHistoryMappingsFileName");
190
191     autosuggestIndexname = props.getProperty("elasticsearch.autosuggestIndexname",
192         ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT);
193     autoSuggestSettingsFileName = props.getProperty("elasticsearch.autosuggestSettingsFileName",
194         ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT);
195     autoSuggestMappingsFileName = props.getProperty("elasticsearch.autosuggestMappingsFileName",
196         ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT);
197     dynamicMappingsFileName = props.getProperty("elasticsearch.dynamicMappingsFileName",
198         ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT);
199
200     syncAdapterMaxConcurrentWorkers =
201         Integer.parseInt(props.getProperty("elasticsearch.syncAdapter.maxConcurrentWorkers", "5"));
202
203     processorConfig = new TaskProcessorConfig();
204     processorConfig.initializeFromProperties(
205         ConfigHelper.getConfigWithPrefix("elasticsearch.taskProcessor", props));
206
207   }
208
209   public String getIpAddress() {
210     return ipAddress;
211   }
212
213   public void setIpAddress(String ipAddress) {
214     this.ipAddress = ipAddress;
215   }
216
217   public String getHttpPort() {
218     return httpPort;
219   }
220
221   public void setHttpPort(String httpPort) {
222     this.httpPort = httpPort;
223   }
224
225   public String getJavaApiPort() {
226     return javaApiPort;
227   }
228
229   public void setJavaApiPort(String javaApiPort) {
230     this.javaApiPort = javaApiPort;
231   }
232
233   public String getIndexName() {
234     return indexName;
235   }
236
237   public void setIndexName(String indexName) {
238     this.indexName = indexName;
239   }
240
241   public String getType() {
242     return type;
243   }
244
245   public void setType(String type) {
246     this.type = type;
247   }
248
249   public String getClusterName() {
250     return clusterName;
251   }
252
253   public void setClusterName(String clusterName) {
254     this.clusterName = clusterName;
255   }
256
257   public String getMappingsFileName() {
258     return mappingsFileName;
259   }
260
261   public void setMappingsFileName(String mappingsFileName) {
262     this.mappingsFileName = mappingsFileName;
263   }
264
265   public String getSettingsFileName() {
266     return settingsFileName;
267   }
268
269   public int getSyncAdapterMaxConcurrentWorkers() {
270     return syncAdapterMaxConcurrentWorkers;
271   }
272
273   public void setSyncAdapterMaxConcurrentWorkers(int syncAdapterMaxConcurrentWorkers) {
274     this.syncAdapterMaxConcurrentWorkers = syncAdapterMaxConcurrentWorkers;
275   }
276
277   public void setSettingsFileName(String settingsFileName) {
278     this.settingsFileName = settingsFileName;
279   }
280
281   public String getAuditIndexName() {
282     return auditIndexName;
283   }
284
285   public void setAuditIndexName(String auditIndexName) {
286     this.auditIndexName = auditIndexName;
287   }
288
289   public String getEntityCountHistoryIndex() {
290     return entityCountHistoryIndex;
291   }
292
293   public void setEntityCountHistoryIndex(String entityCountHistoryIndex) {
294     this.entityCountHistoryIndex = entityCountHistoryIndex;
295   }
296
297
298   public String getEntityCountHistoryMappingsFileName() {
299     return entityCountHistoryMappingsFileName;
300   }
301
302   public void setEntityCountHistoryMappingsFileName(String entityCountHistoryMappingsFileName) {
303     this.entityCountHistoryMappingsFileName = entityCountHistoryMappingsFileName;
304   }
305
306   public String getBulkUrl() {
307     String url = this.getIpAddress();
308     String port = this.getHttpPort();
309     return String.format("http://%s:%s/%s", url, port, BULK_API);
310   }
311
312   public String getConfigAsString(String configItem, String configFileName)
313       throws ElasticSearchOperationException {
314     String indexConfig = null;
315
316     try {
317       indexConfig = ConfigHelper.getFileContents(configFileName);
318     } catch (IOException exc) {
319       throw new ElasticSearchOperationException(
320           "Failed to read index " + configItem + " from file = " + configFileName + ".", exc);
321     }
322
323     if (indexConfig == null) {
324       throw new ElasticSearchOperationException(
325           "Failed to load index " + configItem + " with filename = " + configFileName + ".");
326     }
327     return indexConfig;
328   }
329
330   public String getElasticSearchSettings() throws ElasticSearchOperationException {
331     return getConfigAsString("settings",
332         TierSupportUiConstants.getConfigPath(this.getSettingsFileName()));
333   }
334
335   public String getDynamicMappings() throws ElasticSearchOperationException {
336     return getConfigAsString("mapping",
337         TierSupportUiConstants.getConfigPath(this.getDynamicMappingsFileName()));
338   }
339
340   public String getElasticSearchMappings() throws ElasticSearchOperationException {
341     return getConfigAsString("mapping",
342         TierSupportUiConstants.getConfigPath(this.getMappingsFileName()));
343   }
344
345   public String getElasticSearchEntityCountHistoryMappings()
346       throws ElasticSearchOperationException {
347     return getConfigAsString("mapping",
348         TierSupportUiConstants.getConfigPath(this.getEntityCountHistoryMappingsFileName()));
349   }
350
351   public String getAutosuggestIndexSettings() throws ElasticSearchOperationException {
352     return getConfigAsString("setting",
353         TierSupportUiConstants.getConfigPath(this.getAutoSuggestSettingsFileName()));
354   }
355
356   public String getAutosuggestIndexMappings() throws ElasticSearchOperationException {
357     return getConfigAsString("mapping",
358         TierSupportUiConstants.getConfigPath(this.getAutoSuggestMappingsFileName()));
359   }
360
361   public String getAutosuggestIndexname() {
362     return autosuggestIndexname;
363   }
364
365   public void setAutosuggestIndexname(String autosuggestIndexname) {
366     this.autosuggestIndexname = autosuggestIndexname;
367   }
368
369   public String getAutoSuggestSettingsFileName() {
370     return autoSuggestSettingsFileName;
371   }
372
373   public void setAutoSuggestSettingsFileName(String autoSuggestSettingsFileName) {
374     this.autoSuggestSettingsFileName = autoSuggestSettingsFileName;
375   }
376
377   public String getAutoSuggestMappingsFileName() {
378     return autoSuggestMappingsFileName;
379   }
380
381   public void setAutoSuggestMappingsFileName(String autoSuggestMappingsFileName) {
382     this.autoSuggestMappingsFileName = autoSuggestMappingsFileName;
383   }
384
385   public String getDynamicMappingsFileName() {
386     return dynamicMappingsFileName;
387   }
388
389   public void setDynamicMappingsFileName(String dynamicMappingsFileName) {
390     this.dynamicMappingsFileName = dynamicMappingsFileName;
391   }
392
393   /**
394    * Builds the elastic search table config.
395    *
396    * @return the string
397    * @throws ElasticSearchOperationException the elastic search operation exception
398    */
399   public String buildElasticSearchTableConfig() throws ElasticSearchOperationException {
400
401     JsonNode esSettingsNode;
402     JsonNode esMappingsNodes;
403     ObjectMapper mapper = new ObjectMapper();
404
405     try {
406       esSettingsNode = mapper.readTree(getElasticSearchSettings());
407       esMappingsNodes = mapper.readTree(getElasticSearchMappings());
408     } catch (IOException e1) {
409       throw new ElasticSearchOperationException("Caught an exception building initial ES index");
410     }
411
412     ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
413     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
414
415     esConfig.set("mappings", mappings);
416
417     try {
418       return mapper.writeValueAsString(esConfig);
419     } catch (JsonProcessingException exc) {
420       throw new ElasticSearchOperationException("Error getting object node as string", exc);
421     }
422
423   }
424
425   /**
426    * Builds the elastic search entity count history table config.
427    *
428    * @return the string
429    * @throws ElasticSearchOperationException the elastic search operation exception
430    */
431   public String buildElasticSearchEntityCountHistoryTableConfig()
432       throws ElasticSearchOperationException {
433
434     JsonNode esSettingsNode;
435     JsonNode esMappingsNodes;
436     ObjectMapper mapper = new ObjectMapper();
437
438     try {
439       esSettingsNode = mapper.readTree(getElasticSearchSettings());
440       esMappingsNodes = mapper.readTree(getElasticSearchEntityCountHistoryMappings());
441     } catch (IOException e1) {
442       throw new ElasticSearchOperationException("Caught an exception building initial ES index");
443     }
444
445     ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
446     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
447
448     esConfig.set("mappings", mappings);
449
450     try {
451       return mapper.writeValueAsString(esConfig);
452     } catch (JsonProcessingException exc) {
453       throw new ElasticSearchOperationException("Error getting object node as string", exc);
454     }
455
456   }
457
458   public String buildAggregationTableConfig() throws ElasticSearchOperationException {
459
460     JsonNode esMappingsNodes;
461     ObjectMapper mapper = new ObjectMapper();
462
463     try {
464       esMappingsNodes = mapper.readTree(this.getDynamicMappings());
465     } catch (IOException e1) {
466       throw new ElasticSearchOperationException(
467           "Caught an exception building Aggreagation ES index");
468     }
469
470     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
471
472     ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("mappings", mappings);
473
474     try {
475       return mapper.writeValueAsString(indexConfig);
476     } catch (JsonProcessingException exc) {
477       throw new ElasticSearchOperationException("Error getting object node as string", exc);
478     }
479
480   }
481
482   public String buildAutosuggestionTableConfig() throws ElasticSearchOperationException {
483
484     JsonNode esSettingsNode;
485     JsonNode esMappingsNodes;
486     ObjectMapper mapper = new ObjectMapper();
487
488     try {
489       esSettingsNode = mapper.readTree(this.getAutosuggestIndexSettings());
490       esMappingsNodes = mapper.readTree(this.getAutosuggestIndexMappings());
491     } catch (IOException e1) {
492       throw new ElasticSearchOperationException(
493           "Caught an exception building Autosuggestion ES index");
494     }
495
496     ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
497     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
498
499     indexConfig.set("mappings", mappings);
500
501     try {
502       return mapper.writeValueAsString(indexConfig);
503     } catch (JsonProcessingException exc) {
504       throw new ElasticSearchOperationException("Error getting object node as string", exc);
505     }
506
507   }
508
509   /**
510    * @return the instance
511    */
512   public static ElasticSearchConfig getInstance() {
513     return instance;
514   }
515
516   /**
517    * @param instance the instance to set
518    */
519   public static void setInstance(ElasticSearchConfig instance) {
520     ElasticSearchConfig.instance = instance;
521   }
522
523   /**
524    * @return the configFile
525    */
526   public static String getConfigFile() {
527     return CONFIG_FILE;
528   }
529
530   /**
531    * @return the ipAddressDefault
532    */
533   public static String getIpAddressDefault() {
534     return IP_ADDRESS_DEFAULT;
535   }
536
537   /**
538    * @return the httpPortDefault
539    */
540   public static String getHttpPortDefault() {
541     return HTTP_PORT_DEFAULT;
542   }
543
544   /**
545    * @return the javaApiPortDefault
546    */
547   public static String getJavaApiPortDefault() {
548     return JAVA_API_PORT_DEFAULT;
549   }
550
551   /**
552    * @return the typeDefault
553    */
554   public static String getTypeDefault() {
555     return TYPE_DEFAULT;
556   }
557
558   /**
559    * @return the clusterNameDefault
560    */
561   public static String getClusterNameDefault() {
562     return CLUSTER_NAME_DEFAULT;
563   }
564
565   /**
566    * @return the indexNameDefault
567    */
568   public static String getIndexNameDefault() {
569     return INDEX_NAME_DEFAULT;
570   }
571
572   /**
573    * @return the auditIndexNameDefault
574    */
575   public static String getAuditIndexNameDefault() {
576     return AUDIT_INDEX_NAME_DEFAULT;
577   }
578
579   /**
580    * @return the topographicalIndexNameDefault
581    */
582   public static String getTopographicalIndexNameDefault() {
583     return TOPOGRAPHICAL_INDEX_NAME_DEFAULT;
584   }
585
586   /**
587    * @return the entityCountHistoryIndexNameDefault
588    */
589   public static String getEntityCountHistoryIndexNameDefault() {
590     return ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT;
591   }
592
593   /**
594    * @return the entityAutoSuggestIndexNameDefault
595    */
596   public static String getEntityAutoSuggestIndexNameDefault() {
597     return ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT;
598   }
599
600   /**
601    * @return the entityAutoSuggestSettingsFileDefault
602    */
603   public static String getEntityAutoSuggestSettingsFileDefault() {
604     return ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
605   }
606
607   /**
608    * @return the entityAutoSuggestMappingsFileDefault
609    */
610   public static String getEntityAutoSuggestMappingsFileDefault() {
611     return ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT;
612   }
613
614   /**
615    * @return the entityDynamicMappingsFileDefault
616    */
617   public static String getEntityDynamicMappingsFileDefault() {
618     return ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT;
619   }
620
621   /**
622    * @return the bulkApi
623    */
624   public static String getBulkApi() {
625     return BULK_API;
626   }
627
628   /*
629    * (non-Javadoc)
630    * 
631    * @see java.lang.Object#toString()
632    */
633   @Override
634   public String toString() {
635     return "ElasticSearchConfig [ipAddress=" + ipAddress + ", httpPort=" + httpPort
636         + ", javaApiPort=" + javaApiPort + ", indexName=" + indexName + ", type=" + type
637         + ", clusterName=" + clusterName + ", mappingsFileName=" + mappingsFileName
638         + ", settingsFileName=" + settingsFileName + ", syncAdapterMaxConcurrentWorkers="
639         + syncAdapterMaxConcurrentWorkers + ", auditIndexName=" + auditIndexName
640         + ", entityCountHistoryIndex=" + entityCountHistoryIndex + ", autosuggestIndexname="
641         + autosuggestIndexname + ", entityCountHistoryMappingsFileName="
642         + entityCountHistoryMappingsFileName + ", autoSuggestSettingsFileName="
643         + autoSuggestSettingsFileName + ", autoSuggestMappingsFileName="
644         + autoSuggestMappingsFileName + ", dynamicMappingsFileName=" + dynamicMappingsFileName
645         + ", processorConfig=" + processorConfig + "]";
646   }
647 }