2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.dal.elasticsearch.config;
28 import java.io.BufferedReader;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.Properties;
33 import org.openecomp.sparky.dal.exception.ElasticSearchOperationException;
34 import org.openecomp.sparky.synchronizer.config.TaskProcessorConfig;
35 import org.openecomp.sparky.util.ConfigHelper;
36 import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants;
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.JsonNode;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.node.ObjectNode;
45 * The Class ElasticSearchConfig.
47 public class ElasticSearchConfig {
49 public static final String CONFIG_FILE =
50 TierSupportUiConstants.DYNAMIC_CONFIG_APP_LOCATION + "elasticsearch.properties";
52 private static ElasticSearchConfig instance;
54 private String ipAddress;
56 private String httpPort;
58 private String javaApiPort;
60 private String indexName;
64 private String clusterName;
66 private String mappingsFileName;
68 private String settingsFileName;
70 private int syncAdapterMaxConcurrentWorkers;
72 private String auditIndexName;
74 private String topographicalSearchIndex;
76 private String entityCountHistoryIndex;
78 private String autosuggestIndexname;
80 private String entityCountHistoryMappingsFileName;
82 private String autoSuggestSettingsFileName;
84 private String autoSuggestMappingsFileName;
86 private String dynamicMappingsFileName;
88 private static final String IP_ADDRESS_DEFAULT = "localhost";
90 private static final String HTTP_PORT_DEFAULT = "9200";
92 private static final String JAVA_API_PORT_DEFAULT = "9300";
94 private static final String TYPE_DEFAULT = "aaiEntities";
96 private static final String CLUSTER_NAME_DEFAULT = "elasticsearch";
98 private static final String INDEX_NAME_DEFAULT = "entitySearchIndex";
100 private static final String AUDIT_INDEX_NAME_DEFAULT = "auditdataindex";
102 private static final String TOPOGRAPHICAL_INDEX_NAME_DEFAULT = "topographicalSearchIndex";
104 private static final String ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT = "entityCountHistory";
106 private static final String ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT =
107 TierSupportUiConstants.ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT;
109 private static final String ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT =
110 TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
112 private static final String ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT =
113 TierSupportUiConstants.ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT;
115 private static final String ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT =
116 TierSupportUiConstants.ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT;
118 private static final String BULK_API = "_bulk";
120 private TaskProcessorConfig processorConfig;
122 public TaskProcessorConfig getProcessorConfig() {
123 return processorConfig;
126 public void setProcessorConfig(TaskProcessorConfig processorConfig) {
127 this.processorConfig = processorConfig;
130 public static ElasticSearchConfig getConfig() throws Exception {
132 if (instance == null) {
133 instance = new ElasticSearchConfig();
134 instance.initializeProperties();
140 public static void setConfig(ElasticSearchConfig config) {
142 * Explicitly allow setting the configuration singleton. This will be useful for automation.
145 ElasticSearchConfig.instance = config;
149 * Instantiates a new elastic search config.
151 public ElasticSearchConfig() {
155 public String getElasticFullUrl(String resourceUrl, String indexName, String indexType)
157 final String host = getIpAddress();
158 final String port = getHttpPort();
159 return String.format("http://%s:%s/%s/%s%s", host, port, indexName, indexType, resourceUrl);
162 public String getElasticFullUrl(String resourceUrl, String indexName) throws Exception {
163 final String host = getIpAddress();
164 final String port = getHttpPort();
165 return String.format("http://%s:%s/%s/%s%s", host, port, indexName,
166 ElasticSearchConfig.getConfig().getType(), resourceUrl);
169 public String getElasticFullUrl(String resourceUrl) throws Exception {
170 final String host = getIpAddress();
171 final String port = getHttpPort();
172 final String indexName = getIndexName();
173 return String.format("http://%s:%s/%s/%s%s", host, port, indexName, getType(), resourceUrl);
177 * Initialize properties.
179 private void initializeProperties() {
180 Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE);
182 ipAddress = props.getProperty("elasticsearch.ipAddress", IP_ADDRESS_DEFAULT);
183 httpPort = props.getProperty("elasticsearch.httpPort", "" + HTTP_PORT_DEFAULT);
184 javaApiPort = props.getProperty("elasticsearch.javaApiPort", "" + JAVA_API_PORT_DEFAULT);
185 type = props.getProperty("elasticsearch.type", TYPE_DEFAULT);
186 clusterName = props.getProperty("elasticsearch.clusterName", CLUSTER_NAME_DEFAULT);
187 indexName = props.getProperty("elasticsearch.indexName", INDEX_NAME_DEFAULT);
188 mappingsFileName = props.getProperty("elasticsearch.mappingsFileName");
189 settingsFileName = props.getProperty("elasticsearch.settingsFileName");
190 auditIndexName = props.getProperty("elasticsearch.auditIndexName", AUDIT_INDEX_NAME_DEFAULT);
191 topographicalSearchIndex =
192 props.getProperty("elasticsearch.topographicalIndexName", TOPOGRAPHICAL_INDEX_NAME_DEFAULT);
193 entityCountHistoryIndex = props.getProperty("elasticsearch.entityCountHistoryIndexName",
194 ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT);
195 entityCountHistoryMappingsFileName =
196 props.getProperty("elasticsearch.entityCountHistoryMappingsFileName");
198 autosuggestIndexname = props.getProperty("elasticsearch.autosuggestIndexname",
199 ENTITY_AUTO_SUGGEST_INDEX_NAME_DEFAULT);
200 autoSuggestSettingsFileName = props.getProperty("elasticsearch.autosuggestSettingsFileName",
201 ENTITY_AUTO_SUGGEST_SETTINGS_FILE_DEFAULT);
202 autoSuggestMappingsFileName = props.getProperty("elasticsearch.autosuggestMappingsFileName",
203 ENTITY_AUTO_SUGGEST_MAPPINGS_FILE_DEFAULT);
204 dynamicMappingsFileName = props.getProperty("elasticsearch.dynamicMappingsFileName",
205 ENTITY_DYNAMIC_MAPPINGS_FILE_DEFAULT);
207 syncAdapterMaxConcurrentWorkers =
208 Integer.parseInt(props.getProperty("elasticsearch.syncAdapter.maxConcurrentWorkers", "5"));
210 processorConfig = new TaskProcessorConfig();
211 processorConfig.initializeFromProperties(
212 ConfigHelper.getConfigWithPrefix("elasticsearch.taskProcessor", props));
216 public String getIpAddress() {
220 public void setIpAddress(String ipAddress) {
221 this.ipAddress = ipAddress;
224 public String getHttpPort() {
228 public void setHttpPort(String httpPort) {
229 this.httpPort = httpPort;
232 public String getJavaApiPort() {
236 public void setJavaApiPort(String javaApiPort) {
237 this.javaApiPort = javaApiPort;
240 public String getIndexName() {
244 public void setIndexName(String indexName) {
245 this.indexName = indexName;
248 public String getType() {
252 public void setType(String type) {
256 public String getClusterName() {
260 public void setClusterName(String clusterName) {
261 this.clusterName = clusterName;
264 public String getMappingsFileName() {
265 return mappingsFileName;
268 public void setMappingsFileName(String mappingsFileName) {
269 this.mappingsFileName = mappingsFileName;
272 public String getSettingsFileName() {
273 return settingsFileName;
276 public int getSyncAdapterMaxConcurrentWorkers() {
277 return syncAdapterMaxConcurrentWorkers;
280 public void setSyncAdapterMaxConcurrentWorkers(int syncAdapterMaxConcurrentWorkers) {
281 this.syncAdapterMaxConcurrentWorkers = syncAdapterMaxConcurrentWorkers;
284 public void setSettingsFileName(String settingsFileName) {
285 this.settingsFileName = settingsFileName;
288 public String getAuditIndexName() {
289 return auditIndexName;
292 public void setAuditIndexName(String auditIndexName) {
293 this.auditIndexName = auditIndexName;
296 public String getTopographicalSearchIndex() {
297 return topographicalSearchIndex;
300 public void setTopographicalSearchIndex(String topographicalSearchIndex) {
301 this.topographicalSearchIndex = topographicalSearchIndex;
304 public String getEntityCountHistoryIndex() {
305 return entityCountHistoryIndex;
308 public void setEntityCountHistoryIndex(String entityCountHistoryIndex) {
309 this.entityCountHistoryIndex = entityCountHistoryIndex;
313 public String getEntityCountHistoryMappingsFileName() {
314 return entityCountHistoryMappingsFileName;
317 public void setEntityCountHistoryMappingsFileName(String entityCountHistoryMappingsFileName) {
318 this.entityCountHistoryMappingsFileName = entityCountHistoryMappingsFileName;
321 public String getBulkUrl() {
322 String url = this.getIpAddress();
323 String port = this.getHttpPort();
324 return String.format("http://%s:%s/%s", url, port, BULK_API);
327 public String getConfigAsString(String configItem, String configFileName)
328 throws ElasticSearchOperationException {
329 String indexConfig = null;
332 indexConfig = ConfigHelper.getFileContents(configFileName);
333 } catch (IOException exc) {
334 throw new ElasticSearchOperationException(
335 "Failed to read index " + configItem + " from file = " + configFileName + ".", exc);
338 if (indexConfig == null) {
339 throw new ElasticSearchOperationException(
340 "Failed to load index " + configItem + " with filename = " + configFileName + ".");
345 public String getElasticSearchSettings() throws ElasticSearchOperationException {
346 return getConfigAsString("settings",
347 TierSupportUiConstants.getConfigPath(this.getSettingsFileName()));
350 public String getDynamicMappings() throws ElasticSearchOperationException{
351 return getConfigAsString("mapping",
352 TierSupportUiConstants.getConfigPath(this.getDynamicMappingsFileName()));
354 public String getElasticSearchMappings() throws ElasticSearchOperationException {
355 return getConfigAsString("mapping",
356 TierSupportUiConstants.getConfigPath(this.getMappingsFileName()));
359 public String getElasticSearchEntityCountHistoryMappings()
360 throws ElasticSearchOperationException {
361 return getConfigAsString("mapping",
362 TierSupportUiConstants.getConfigPath(this.getEntityCountHistoryMappingsFileName()));
365 public String getAutosuggestIndexSettings() throws ElasticSearchOperationException {
366 return getConfigAsString("setting",
367 TierSupportUiConstants.getConfigPath(this.getAutoSuggestSettingsFileName()));
370 public String getAutosuggestIndexMappings() throws ElasticSearchOperationException {
371 return getConfigAsString("mapping",
372 TierSupportUiConstants.getConfigPath(this.getAutoSuggestMappingsFileName()));
375 public String getAutosuggestIndexname() {
376 return autosuggestIndexname;
379 public void setAutosuggestIndexname(String autosuggestIndexname) {
380 this.autosuggestIndexname = autosuggestIndexname;
383 public String getAutoSuggestSettingsFileName() {
384 return autoSuggestSettingsFileName;
387 public void setAutoSuggestSettingsFileName(String autoSuggestSettingsFileName) {
388 this.autoSuggestSettingsFileName = autoSuggestSettingsFileName;
391 public String getAutoSuggestMappingsFileName() {
392 return autoSuggestMappingsFileName;
395 public void setAutoSuggestMappingsFileName(String autoSuggestMappingsFileName) {
396 this.autoSuggestMappingsFileName = autoSuggestMappingsFileName;
399 public String getDynamicMappingsFileName() {
400 return dynamicMappingsFileName;
403 public void setDynamicMappingsFileName(String dynamicMappingsFileName) {
404 this.dynamicMappingsFileName = dynamicMappingsFileName;
408 * Builds the elastic search table config.
411 * @throws ElasticSearchOperationException the elastic search operation exception
413 public String buildElasticSearchTableConfig() throws ElasticSearchOperationException {
415 JsonNode esSettingsNode;
416 JsonNode esMappingsNodes;
417 ObjectMapper mapper = new ObjectMapper();
420 esSettingsNode = mapper.readTree(getElasticSearchSettings());
421 esMappingsNodes = mapper.readTree(getElasticSearchMappings());
422 } catch (IOException e1) {
423 throw new ElasticSearchOperationException("Caught an exception building initial ES index");
426 ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
427 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
429 esConfig.set("mappings", mappings);
432 return mapper.writeValueAsString(esConfig);
433 } catch (JsonProcessingException exc) {
434 throw new ElasticSearchOperationException("Error getting object node as string", exc);
440 * Builds the elastic search entity count history table config.
443 * @throws ElasticSearchOperationException the elastic search operation exception
445 public String buildElasticSearchEntityCountHistoryTableConfig()
446 throws ElasticSearchOperationException {
448 JsonNode esSettingsNode;
449 JsonNode esMappingsNodes;
450 ObjectMapper mapper = new ObjectMapper();
453 esSettingsNode = mapper.readTree(getElasticSearchSettings());
454 esMappingsNodes = mapper.readTree(getElasticSearchEntityCountHistoryMappings());
455 } catch (IOException e1) {
456 throw new ElasticSearchOperationException("Caught an exception building initial ES index");
459 ObjectNode esConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
460 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
462 esConfig.set("mappings", mappings);
465 return mapper.writeValueAsString(esConfig);
466 } catch (JsonProcessingException exc) {
467 throw new ElasticSearchOperationException("Error getting object node as string", exc);
472 public String buildAggregationTableConfig() throws ElasticSearchOperationException {
474 JsonNode esMappingsNodes;
475 ObjectMapper mapper = new ObjectMapper();
478 esMappingsNodes = mapper.readTree(this.getDynamicMappings());
479 } catch (IOException e1) {
480 throw new ElasticSearchOperationException(
481 "Caught an exception building Aggreagation ES index");
484 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
486 ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("mappings", mappings);
489 return mapper.writeValueAsString(indexConfig);
490 } catch (JsonProcessingException exc) {
491 throw new ElasticSearchOperationException("Error getting object node as string", exc);
496 public String buildAutosuggestionTableConfig() throws ElasticSearchOperationException {
498 JsonNode esSettingsNode;
499 JsonNode esMappingsNodes;
500 ObjectMapper mapper = new ObjectMapper();
503 esSettingsNode = mapper.readTree(this.getAutosuggestIndexSettings());
504 esMappingsNodes = mapper.readTree(this.getAutosuggestIndexMappings());
505 } catch (IOException e1) {
506 throw new ElasticSearchOperationException(
507 "Caught an exception building Autosuggestion ES index");
510 ObjectNode indexConfig = (ObjectNode) mapper.createObjectNode().set("settings", esSettingsNode);
511 ObjectNode mappings = (ObjectNode) mapper.createObjectNode().set(getType(), esMappingsNodes);
513 indexConfig.set("mappings", mappings);
516 return mapper.writeValueAsString(indexConfig);
517 } catch (JsonProcessingException exc) {
518 throw new ElasticSearchOperationException("Error getting object node as string", exc);
526 * @see java.lang.Object#toString()
529 public String toString() {
530 return "ElasticSearchConfig [ipAddress=" + ipAddress + ", httpPort=" + httpPort
531 + ", javaApiPort=" + javaApiPort + ", indexName=" + indexName + ", type=" + type
532 + ", clusterName=" + clusterName + ", mappingsFileName=" + mappingsFileName
533 + ", settingsFileName=" + settingsFileName + ", syncAdapterMaxConcurrentWorkers="
534 + syncAdapterMaxConcurrentWorkers + ", auditIndexName=" + auditIndexName
535 + ", topographicalSearchIndex=" + topographicalSearchIndex + ", entityCountHistoryIndex="
536 + entityCountHistoryIndex + ", autosuggestIndexname=" + autosuggestIndexname
537 + ", entityCountHistoryMappingsFileName=" + entityCountHistoryMappingsFileName
538 + ", autoSuggestSettingsFileName=" + autoSuggestSettingsFileName
539 + ", autoSuggestMappingsFileName=" + autoSuggestMappingsFileName
540 + ", dynamicMappingsFileName=" + dynamicMappingsFileName + ", processorConfig="
541 + processorConfig + "]";