2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.dal.elasticsearch.config;
25 import java.io.BufferedReader;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.util.Properties;
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;
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;
42 * The Class ElasticSearchConfig.
44 public class ElasticSearchConfig {
46 public static final String CONFIG_FILE =
47 TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION + "elasticsearch.properties";
49 private static ElasticSearchConfig instance;
51 private String ipAddress;
53 private String httpPort;
55 private String javaApiPort;
57 private String indexName;
61 private String clusterName;
63 private String mappingsFileName;
65 private String settingsFileName;
67 private int syncAdapterMaxConcurrentWorkers;
69 private String auditIndexName;
71 private String entityCountHistoryIndex;
73 private String autosuggestIndexname;
75 private String entityCountHistoryMappingsFileName;
77 private String autoSuggestSettingsFileName;
79 private String autoSuggestMappingsFileName;
81 private String dynamicMappingsFileName;
83 private static final String IP_ADDRESS_DEFAULT = "localhost";
85 private static final String HTTP_PORT_DEFAULT = "9200";
87 private static final String JAVA_API_PORT_DEFAULT = "9300";
89 private static final String TYPE_DEFAULT = "aaiEntities";
91 private static final String CLUSTER_NAME_DEFAULT = "elasticsearch";
93 private static final String INDEX_NAME_DEFAULT = "entitySearchIndex";
95 private static final String AUDIT_INDEX_NAME_DEFAULT = "auditdataindex";
97 private static final String TOPOGRAPHICAL_INDEX_NAME_DEFAULT = "topographicalSearchIndex";
99 private static final String ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT = "entityCountHistory";
101 private static final String ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT =
102 TierSupportUiConstants.ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT;
104 private static final String ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT =
105 TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
107 private static final String ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT =
108 TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
110 private static final String ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT =
111 TierSupportUiConstants.ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT;
113 private static final String BULK_API = "_bulk";
115 private TaskProcessorConfig processorConfig;
117 public TaskProcessorConfig getProcessorConfig() {
118 return processorConfig;
121 public void setProcessorConfig(TaskProcessorConfig processorConfig) {
122 this.processorConfig = processorConfig;
125 public static ElasticSearchConfig getConfig() throws Exception {
127 if (instance == null) {
128 instance = new ElasticSearchConfig();
129 instance.initializeProperties();
135 public static void setConfig(ElasticSearchConfig config) {
137 * Explicitly allow setting the configuration singleton. This will be useful for automation.
140 ElasticSearchConfig.instance = config;
144 * Instantiates a new elastic search config.
146 public ElasticSearchConfig() {
150 public String getElasticFullUrl(String resourceUrl, String indexName, String indexType)
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);
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);
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);
172 * Initialize properties.
174 private void initializeProperties() {
175 Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE);
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");
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);
200 syncAdapterMaxConcurrentWorkers =
201 Integer.parseInt(props.getProperty("elasticsearch.syncAdapter.maxConcurrentWorkers", "5"));
203 processorConfig = new TaskProcessorConfig();
204 processorConfig.initializeFromProperties(
205 ConfigHelper.getConfigWithPrefix("elasticsearch.taskProcessor", props));
209 public String getIpAddress() {
213 public void setIpAddress(String ipAddress) {
214 this.ipAddress = ipAddress;
217 public String getHttpPort() {
221 public void setHttpPort(String httpPort) {
222 this.httpPort = httpPort;
225 public String getJavaApiPort() {
229 public void setJavaApiPort(String javaApiPort) {
230 this.javaApiPort = javaApiPort;
233 public String getIndexName() {
237 public void setIndexName(String indexName) {
238 this.indexName = indexName;
241 public String getType() {
245 public void setType(String type) {
249 public String getClusterName() {
253 public void setClusterName(String clusterName) {
254 this.clusterName = clusterName;
257 public String getMappingsFileName() {
258 return mappingsFileName;
261 public void setMappingsFileName(String mappingsFileName) {
262 this.mappingsFileName = mappingsFileName;
265 public String getSettingsFileName() {
266 return settingsFileName;
269 public int getSyncAdapterMaxConcurrentWorkers() {
270 return syncAdapterMaxConcurrentWorkers;
273 public void setSyncAdapterMaxConcurrentWorkers(int syncAdapterMaxConcurrentWorkers) {
274 this.syncAdapterMaxConcurrentWorkers = syncAdapterMaxConcurrentWorkers;
277 public void setSettingsFileName(String settingsFileName) {
278 this.settingsFileName = settingsFileName;
281 public String getAuditIndexName() {
282 return auditIndexName;
285 public void setAuditIndexName(String auditIndexName) {
286 this.auditIndexName = auditIndexName;
289 public String getEntityCountHistoryIndex() {
290 return entityCountHistoryIndex;
293 public void setEntityCountHistoryIndex(String entityCountHistoryIndex) {
294 this.entityCountHistoryIndex = entityCountHistoryIndex;
298 public String getEntityCountHistoryMappingsFileName() {
299 return entityCountHistoryMappingsFileName;
302 public void setEntityCountHistoryMappingsFileName(String entityCountHistoryMappingsFileName) {
303 this.entityCountHistoryMappingsFileName = entityCountHistoryMappingsFileName;
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);
312 public String getConfigAsString(String configItem, String configFileName)
313 throws ElasticSearchOperationException {
314 String indexConfig = null;
317 indexConfig = ConfigHelper.getFileContents(configFileName);
318 } catch (IOException exc) {
319 throw new ElasticSearchOperationException(
320 "Failed to read index " + configItem + " from file = " + configFileName + ".", exc);
323 if (indexConfig == null) {
324 throw new ElasticSearchOperationException(
325 "Failed to load index " + configItem + " with filename = " + configFileName + ".");
330 public String getElasticSearchSettings() throws ElasticSearchOperationException {
331 return getConfigAsString("settings",
332 TierSupportUiConstants.getConfigPath(this.getSettingsFileName()));
335 public String getDynamicMappings() throws ElasticSearchOperationException {
336 return getConfigAsString("mapping",
337 TierSupportUiConstants.getConfigPath(this.getDynamicMappingsFileName()));
340 public String getElasticSearchMappings() throws ElasticSearchOperationException {
341 return getConfigAsString("mapping",
342 TierSupportUiConstants.getConfigPath(this.getMappingsFileName()));
345 public String getElasticSearchEntityCountHistoryMappings()
346 throws ElasticSearchOperationException {
347 return getConfigAsString("mapping",
348 TierSupportUiConstants.getConfigPath(this.getEntityCountHistoryMappingsFileName()));
351 public String getAutosuggestIndexSettings() throws ElasticSearchOperationException {
352 return getConfigAsString("setting",
353 TierSupportUiConstants.getConfigPath(this.getAutoSuggestSettingsFileName()));
356 public String getAutosuggestIndexMappings() throws ElasticSearchOperationException {
357 return getConfigAsString("mapping",
358 TierSupportUiConstants.getConfigPath(this.getAutoSuggestMappingsFileName()));
361 public String getAutosuggestIndexname() {
362 return autosuggestIndexname;
365 public void setAutosuggestIndexname(String autosuggestIndexname) {
366 this.autosuggestIndexname = autosuggestIndexname;
369 public String getAutoSuggestSettingsFileName() {
370 return autoSuggestSettingsFileName;
373 public void setAutoSuggestSettingsFileName(String autoSuggestSettingsFileName) {
374 this.autoSuggestSettingsFileName = autoSuggestSettingsFileName;
377 public String getAutoSuggestMappingsFileName() {
378 return autoSuggestMappingsFileName;
381 public void setAutoSuggestMappingsFileName(String autoSuggestMappingsFileName) {
382 this.autoSuggestMappingsFileName = autoSuggestMappingsFileName;
385 public String getDynamicMappingsFileName() {
386 return dynamicMappingsFileName;
389 public void setDynamicMappingsFileName(String dynamicMappingsFileName) {
390 this.dynamicMappingsFileName = dynamicMappingsFileName;
394 * Builds the elastic search table config.
397 * @throws ElasticSearchOperationException the elastic search operation exception
399 public String buildElasticSearchTableConfig() throws ElasticSearchOperationException {
401 JsonNode esSettingsNode;
402 JsonNode esMappingsNodes;
403 ObjectMapper mapper = new ObjectMapper();
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");
412 ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
413 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
415 esConfig.set("mappings", mappings);
418 return mapper.writeValueAsString(esConfig);
419 } catch (JsonProcessingException exc) {
420 throw new ElasticSearchOperationException("Error getting object node as string", exc);
426 * Builds the elastic search entity count history table config.
429 * @throws ElasticSearchOperationException the elastic search operation exception
431 public String buildElasticSearchEntityCountHistoryTableConfig()
432 throws ElasticSearchOperationException {
434 JsonNode esSettingsNode;
435 JsonNode esMappingsNodes;
436 ObjectMapper mapper = new ObjectMapper();
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");
445 ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
446 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
448 esConfig.set("mappings", mappings);
451 return mapper.writeValueAsString(esConfig);
452 } catch (JsonProcessingException exc) {
453 throw new ElasticSearchOperationException("Error getting object node as string", exc);
458 public String buildAggregationTableConfig() throws ElasticSearchOperationException {
460 JsonNode esMappingsNodes;
461 ObjectMapper mapper = new ObjectMapper();
464 esMappingsNodes = mapper.readTree(this.getDynamicMappings());
465 } catch (IOException e1) {
466 throw new ElasticSearchOperationException(
467 "Caught an exception building Aggreagation ES index");
470 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
472 ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("mappings", mappings);
475 return mapper.writeValueAsString(indexConfig);
476 } catch (JsonProcessingException exc) {
477 throw new ElasticSearchOperationException("Error getting object node as string", exc);
482 public String buildAutosuggestionTableConfig() throws ElasticSearchOperationException {
484 JsonNode esSettingsNode;
485 JsonNode esMappingsNodes;
486 ObjectMapper mapper = new ObjectMapper();
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");
496 ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
497 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
499 indexConfig.set("mappings", mappings);
502 return mapper.writeValueAsString(indexConfig);
503 } catch (JsonProcessingException exc) {
504 throw new ElasticSearchOperationException("Error getting object node as string", exc);
510 * @return the instance
512 public static ElasticSearchConfig getInstance() {
517 * @param instance the instance to set
519 public static void setInstance(ElasticSearchConfig instance) {
520 ElasticSearchConfig.instance = instance;
524 * @return the configFile
526 public static String getConfigFile() {
531 * @return the ipAddressDefault
533 public static String getIpAddressDefault() {
534 return IP_ADDRESS_DEFAULT;
538 * @return the httpPortDefault
540 public static String getHttpPortDefault() {
541 return HTTP_PORT_DEFAULT;
545 * @return the javaApiPortDefault
547 public static String getJavaApiPortDefault() {
548 return JAVA_API_PORT_DEFAULT;
552 * @return the typeDefault
554 public static String getTypeDefault() {
559 * @return the clusterNameDefault
561 public static String getClusterNameDefault() {
562 return CLUSTER_NAME_DEFAULT;
566 * @return the indexNameDefault
568 public static String getIndexNameDefault() {
569 return INDEX_NAME_DEFAULT;
573 * @return the auditIndexNameDefault
575 public static String getAuditIndexNameDefault() {
576 return AUDIT_INDEX_NAME_DEFAULT;
580 * @return the topographicalIndexNameDefault
582 public static String getTopographicalIndexNameDefault() {
583 return TOPOGRAPHICAL_INDEX_NAME_DEFAULT;
587 * @return the entityCountHistoryIndexNameDefault
589 public static String getEntityCountHistoryIndexNameDefault() {
590 return ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT;
594 * @return the entityAutoSuggestIndexNameDefault
596 public static String getEntityAutoSuggestIndexNameDefault() {
597 return ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT;
601 * @return the entityAutoSuggestSettingsFileDefault
603 public static String getEntityAutoSuggestSettingsFileDefault() {
604 return ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
608 * @return the entityAutoSuggestMappingsFileDefault
610 public static String getEntityAutoSuggestMappingsFileDefault() {
611 return ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT;
615 * @return the entityDynamicMappingsFileDefault
617 public static String getEntityDynamicMappingsFileDefault() {
618 return ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT;
622 * @return the bulkApi
624 public static String getBulkApi() {
631 * @see java.lang.Object#toString()
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 + "]";