Increase junit coverage
[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   public String getElasticSearchMappings() throws ElasticSearchOperationException {
340     return getConfigAsString("mapping",
341         TierSupportUiConstants.getConfigPath(this.getMappingsFileName()));
342   }
343
344   public String getElasticSearchEntityCountHistoryMappings() 
345       throws ElasticSearchOperationException {
346     return getConfigAsString("mapping",
347         TierSupportUiConstants.getConfigPath(this.getEntityCountHistoryMappingsFileName()));
348   }
349
350   public String getAutosuggestIndexSettings() throws ElasticSearchOperationException {
351     return getConfigAsString("setting",
352         TierSupportUiConstants.getConfigPath(this.getAutoSuggestSettingsFileName()));
353   }
354
355   public String getAutosuggestIndexMappings() throws ElasticSearchOperationException {
356     return getConfigAsString("mapping",
357         TierSupportUiConstants.getConfigPath(this.getAutoSuggestMappingsFileName()));
358   }
359
360   public String getAutosuggestIndexname() {
361     return autosuggestIndexname;
362   }
363
364   public void setAutosuggestIndexname(String autosuggestIndexname) {
365     this.autosuggestIndexname = autosuggestIndexname;
366   }
367
368   public String getAutoSuggestSettingsFileName() {
369     return autoSuggestSettingsFileName;
370   }
371
372   public void setAutoSuggestSettingsFileName(String autoSuggestSettingsFileName) {
373     this.autoSuggestSettingsFileName = autoSuggestSettingsFileName;
374   }
375
376   public String getAutoSuggestMappingsFileName() {
377     return autoSuggestMappingsFileName;
378   }
379
380   public void setAutoSuggestMappingsFileName(String autoSuggestMappingsFileName) {
381     this.autoSuggestMappingsFileName = autoSuggestMappingsFileName;
382   }
383
384   public String getDynamicMappingsFileName() {
385     return dynamicMappingsFileName;
386   }
387
388   public void setDynamicMappingsFileName(String dynamicMappingsFileName) {
389     this.dynamicMappingsFileName = dynamicMappingsFileName;
390   }
391
392   /**
393    * Builds the elastic search table config.
394    *
395    * @return the string
396    * @throws ElasticSearchOperationException the elastic search operation exception
397    */
398   public String buildElasticSearchTableConfig() throws ElasticSearchOperationException {
399
400     JsonNode esSettingsNode;
401     JsonNode esMappingsNodes;
402     ObjectMapper mapper = new ObjectMapper();
403
404     try {
405       esSettingsNode = mapper.readTree(getElasticSearchSettings());
406       esMappingsNodes = mapper.readTree(getElasticSearchMappings());
407     } catch (IOException e1) {
408       throw new ElasticSearchOperationException("Caught an exception building initial ES index");
409     }
410
411     ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
412     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
413
414     esConfig.set("mappings", mappings);
415
416     try {
417       return mapper.writeValueAsString(esConfig);
418     } catch (JsonProcessingException exc) {
419       throw new ElasticSearchOperationException("Error getting object node as string", exc);
420     }
421
422   }
423
424   /**
425    * Builds the elastic search entity count history table config.
426    *
427    * @return the string
428    * @throws ElasticSearchOperationException the elastic search operation exception
429    */
430   public String buildElasticSearchEntityCountHistoryTableConfig()
431       throws ElasticSearchOperationException {
432
433     JsonNode esSettingsNode;
434     JsonNode esMappingsNodes;
435     ObjectMapper mapper = new ObjectMapper();
436
437     try {
438       esSettingsNode = mapper.readTree(getElasticSearchSettings());
439       esMappingsNodes = mapper.readTree(getElasticSearchEntityCountHistoryMappings());
440     } catch (IOException e1) {
441       throw new ElasticSearchOperationException("Caught an exception building initial ES index");
442     }
443
444     ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
445     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
446
447     esConfig.set("mappings", mappings);
448
449     try {
450       return mapper.writeValueAsString(esConfig);
451     } catch (JsonProcessingException exc) {
452       throw new ElasticSearchOperationException("Error getting object node as string", exc);
453     }
454
455   }
456
457   public String buildAggregationTableConfig() throws ElasticSearchOperationException {
458
459     JsonNode esMappingsNodes;
460     ObjectMapper mapper = new ObjectMapper();
461
462     try {
463       esMappingsNodes = mapper.readTree(this.getDynamicMappings());
464     } catch (IOException e1) {
465       throw new ElasticSearchOperationException(
466           "Caught an exception building Aggreagation ES index");
467     }
468
469     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
470
471     ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("mappings", mappings);
472
473     try {
474       return mapper.writeValueAsString(indexConfig);
475     } catch (JsonProcessingException exc) {
476       throw new ElasticSearchOperationException("Error getting object node as string", exc);
477     }
478
479   }
480   
481   public String buildAutosuggestionTableConfig() throws ElasticSearchOperationException {
482
483     JsonNode esSettingsNode;
484     JsonNode esMappingsNodes;
485     ObjectMapper mapper = new ObjectMapper();
486
487     try {
488       esSettingsNode = mapper.readTree(this.getAutosuggestIndexSettings());
489       esMappingsNodes = mapper.readTree(this.getAutosuggestIndexMappings());
490     } catch (IOException e1) {
491       throw new ElasticSearchOperationException(
492           "Caught an exception building Autosuggestion ES index");
493     }
494
495     ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
496     ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
497
498     indexConfig.set("mappings", mappings);
499
500     try {
501       return mapper.writeValueAsString(indexConfig);
502     } catch (JsonProcessingException exc) {
503       throw new ElasticSearchOperationException("Error getting object node as string", exc);
504     }
505
506   }
507   
508   /*
509    * (non-Javadoc)
510    * 
511    * @see java.lang.Object#toString()
512    */
513   @Override
514   public String toString() {
515     return "ElasticSearchConfig [ipAddress=" + ipAddress + ", httpPort=" + httpPort
516         + ", javaApiPort=" + javaApiPort + ", indexName=" + indexName + ", type=" + type
517         + ", clusterName=" + clusterName + ", mappingsFileName=" + mappingsFileName
518         + ", settingsFileName=" + settingsFileName + ", syncAdapterMaxConcurrentWorkers="
519         + syncAdapterMaxConcurrentWorkers + ", auditIndexName=" + auditIndexName
520         + ", entityCountHistoryIndex=" 
521         + entityCountHistoryIndex + ", autosuggestIndexname=" + autosuggestIndexname
522         + ", entityCountHistoryMappingsFileName=" + entityCountHistoryMappingsFileName
523         + ", autoSuggestSettingsFileName=" + autoSuggestSettingsFileName
524         + ", autoSuggestMappingsFileName=" + autoSuggestMappingsFileName
525         + ", dynamicMappingsFileName=" + dynamicMappingsFileName + ", processorConfig="
526         + processorConfig + "]";
527   }
528 }