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 topographicalSearchIndex;
73 private String entityCountHistoryIndex;
75 private String autosuggestIndexname;
77 private String entityCountHistoryMappingsFileName;
79 private String autoSuggestSettingsFileName;
81 private String autoSuggestMappingsFileName;
83 private String dynamicMappingsFileName;
85 private static final String IP_ADDRESS_DEFAULT = "localhost";
87 private static final String HTTP_PORT_DEFAULT = "9200";
89 private static final String JAVA_API_PORT_DEFAULT = "9300";
91 private static final String TYPE_DEFAULT = "aaiEntities";
93 private static final String CLUSTER_NAME_DEFAULT = "elasticsearch";
95 private static final String INDEX_NAME_DEFAULT = "entitySearchIndex";
97 private static final String AUDIT_INDEX_NAME_DEFAULT = "auditdataindex";
99 private static final String TOPOGRAPHICAL_INDEX_NAME_DEFAULT = "topographicalSearchIndex";
101 private static final String ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT = "entityCountHistory";
103 private static final String ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT =
104 TierSupportUiConstants.ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT;
106 private static final String ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT =
107 TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
109 private static final String ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT =
110 TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
112 private static final String ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT =
113 TierSupportUiConstants.ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT;
115 private static final String BULK_API = "_bulk";
117 private TaskProcessorConfig processorConfig;
119 public TaskProcessorConfig getProcessorConfig() {
120 return processorConfig;
123 public void setProcessorConfig(TaskProcessorConfig processorConfig) {
124 this.processorConfig = processorConfig;
127 public static ElasticSearchConfig getConfig() throws Exception {
129 if (instance == null) {
130 instance = new ElasticSearchConfig();
131 instance.initializeProperties();
137 public static void setConfig(ElasticSearchConfig config) {
139 * Explicitly allow setting the configuration singleton. This will be useful for automation.
142 ElasticSearchConfig.instance = config;
146 * Instantiates a new elastic search config.
148 public ElasticSearchConfig() {
152 public String getElasticFullUrl(String resourceUrl, String indexName, String indexType)
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);
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);
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);
174 * Initialize properties.
176 private void initializeProperties() {
177 Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE);
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");
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);
204 syncAdapterMaxConcurrentWorkers =
205 Integer.parseInt(props.getProperty("elasticsearch.syncAdapter.maxConcurrentWorkers", "5"));
207 processorConfig = new TaskProcessorConfig();
208 processorConfig.initializeFromProperties(
209 ConfigHelper.getConfigWithPrefix("elasticsearch.taskProcessor", props));
213 public String getIpAddress() {
217 public void setIpAddress(String ipAddress) {
218 this.ipAddress = ipAddress;
221 public String getHttpPort() {
225 public void setHttpPort(String httpPort) {
226 this.httpPort = httpPort;
229 public String getJavaApiPort() {
233 public void setJavaApiPort(String javaApiPort) {
234 this.javaApiPort = javaApiPort;
237 public String getIndexName() {
241 public void setIndexName(String indexName) {
242 this.indexName = indexName;
245 public String getType() {
249 public void setType(String type) {
253 public String getClusterName() {
257 public void setClusterName(String clusterName) {
258 this.clusterName = clusterName;
261 public String getMappingsFileName() {
262 return mappingsFileName;
265 public void setMappingsFileName(String mappingsFileName) {
266 this.mappingsFileName = mappingsFileName;
269 public String getSettingsFileName() {
270 return settingsFileName;
273 public int getSyncAdapterMaxConcurrentWorkers() {
274 return syncAdapterMaxConcurrentWorkers;
277 public void setSyncAdapterMaxConcurrentWorkers(int syncAdapterMaxConcurrentWorkers) {
278 this.syncAdapterMaxConcurrentWorkers = syncAdapterMaxConcurrentWorkers;
281 public void setSettingsFileName(String settingsFileName) {
282 this.settingsFileName = settingsFileName;
285 public String getAuditIndexName() {
286 return auditIndexName;
289 public void setAuditIndexName(String auditIndexName) {
290 this.auditIndexName = auditIndexName;
293 public String getTopographicalSearchIndex() {
294 return topographicalSearchIndex;
297 public void setTopographicalSearchIndex(String topographicalSearchIndex) {
298 this.topographicalSearchIndex = topographicalSearchIndex;
301 public String getEntityCountHistoryIndex() {
302 return entityCountHistoryIndex;
305 public void setEntityCountHistoryIndex(String entityCountHistoryIndex) {
306 this.entityCountHistoryIndex = entityCountHistoryIndex;
310 public String getEntityCountHistoryMappingsFileName() {
311 return entityCountHistoryMappingsFileName;
314 public void setEntityCountHistoryMappingsFileName(String entityCountHistoryMappingsFileName) {
315 this.entityCountHistoryMappingsFileName = entityCountHistoryMappingsFileName;
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);
324 public String getConfigAsString(String configItem, String configFileName)
325 throws ElasticSearchOperationException {
326 String indexConfig = null;
329 indexConfig = ConfigHelper.getFileContents(configFileName);
330 } catch (IOException exc) {
331 throw new ElasticSearchOperationException(
332 "Failed to read index " + configItem + " from file = " + configFileName + ".", exc);
335 if (indexConfig == null) {
336 throw new ElasticSearchOperationException(
337 "Failed to load index " + configItem + " with filename = " + configFileName + ".");
342 public String getElasticSearchSettings() throws ElasticSearchOperationException {
343 return getConfigAsString("settings",
344 TierSupportUiConstants.getConfigPath(this.getSettingsFileName()));
347 public String getDynamicMappings() throws ElasticSearchOperationException{
348 return getConfigAsString("mapping",
349 TierSupportUiConstants.getConfigPath(this.getDynamicMappingsFileName()));
351 public String getElasticSearchMappings() throws ElasticSearchOperationException {
352 return getConfigAsString("mapping",
353 TierSupportUiConstants.getConfigPath(this.getMappingsFileName()));
356 public String getElasticSearchEntityCountHistoryMappings()
357 throws ElasticSearchOperationException {
358 return getConfigAsString("mapping",
359 TierSupportUiConstants.getConfigPath(this.getEntityCountHistoryMappingsFileName()));
362 public String getAutosuggestIndexSettings() throws ElasticSearchOperationException {
363 return getConfigAsString("setting",
364 TierSupportUiConstants.getConfigPath(this.getAutoSuggestSettingsFileName()));
367 public String getAutosuggestIndexMappings() throws ElasticSearchOperationException {
368 return getConfigAsString("mapping",
369 TierSupportUiConstants.getConfigPath(this.getAutoSuggestMappingsFileName()));
372 public String getAutosuggestIndexname() {
373 return autosuggestIndexname;
376 public void setAutosuggestIndexname(String autosuggestIndexname) {
377 this.autosuggestIndexname = autosuggestIndexname;
380 public String getAutoSuggestSettingsFileName() {
381 return autoSuggestSettingsFileName;
384 public void setAutoSuggestSettingsFileName(String autoSuggestSettingsFileName) {
385 this.autoSuggestSettingsFileName = autoSuggestSettingsFileName;
388 public String getAutoSuggestMappingsFileName() {
389 return autoSuggestMappingsFileName;
392 public void setAutoSuggestMappingsFileName(String autoSuggestMappingsFileName) {
393 this.autoSuggestMappingsFileName = autoSuggestMappingsFileName;
396 public String getDynamicMappingsFileName() {
397 return dynamicMappingsFileName;
400 public void setDynamicMappingsFileName(String dynamicMappingsFileName) {
401 this.dynamicMappingsFileName = dynamicMappingsFileName;
405 * Builds the elastic search table config.
408 * @throws ElasticSearchOperationException the elastic search operation exception
410 public String buildElasticSearchTableConfig() throws ElasticSearchOperationException {
412 JsonNode esSettingsNode;
413 JsonNode esMappingsNodes;
414 ObjectMapper mapper = new ObjectMapper();
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");
423 ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
424 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
426 esConfig.set("mappings", mappings);
429 return mapper.writeValueAsString(esConfig);
430 } catch (JsonProcessingException exc) {
431 throw new ElasticSearchOperationException("Error getting object node as string", exc);
437 * Builds the elastic search entity count history table config.
440 * @throws ElasticSearchOperationException the elastic search operation exception
442 public String buildElasticSearchEntityCountHistoryTableConfig()
443 throws ElasticSearchOperationException {
445 JsonNode esSettingsNode;
446 JsonNode esMappingsNodes;
447 ObjectMapper mapper = new ObjectMapper();
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");
456 ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
457 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
459 esConfig.set("mappings", mappings);
462 return mapper.writeValueAsString(esConfig);
463 } catch (JsonProcessingException exc) {
464 throw new ElasticSearchOperationException("Error getting object node as string", exc);
469 public String buildAggregationTableConfig() throws ElasticSearchOperationException {
471 JsonNode esMappingsNodes;
472 ObjectMapper mapper = new ObjectMapper();
475 esMappingsNodes = mapper.readTree(this.getDynamicMappings());
476 } catch (IOException e1) {
477 throw new ElasticSearchOperationException(
478 "Caught an exception building Aggreagation ES index");
481 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
483 ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("mappings", mappings);
486 return mapper.writeValueAsString(indexConfig);
487 } catch (JsonProcessingException exc) {
488 throw new ElasticSearchOperationException("Error getting object node as string", exc);
493 public String buildAutosuggestionTableConfig() throws ElasticSearchOperationException {
495 JsonNode esSettingsNode;
496 JsonNode esMappingsNodes;
497 ObjectMapper mapper = new ObjectMapper();
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");
507 ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
508 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
510 indexConfig.set("mappings", mappings);
513 return mapper.writeValueAsString(indexConfig);
514 } catch (JsonProcessingException exc) {
515 throw new ElasticSearchOperationException("Error getting object node as string", exc);
523 * @see java.lang.Object#toString()
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 + "]";