2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.sparky.dal;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
28 import javax.ws.rs.core.MediaType;
30 import org.onap.aai.restclient.client.OperationResult;
31 import org.onap.aai.restclient.client.RestClient;
32 import org.onap.aai.sparky.dal.rest.RestClientConstructionException;
33 import org.onap.aai.sparky.dal.rest.RestClientFactory;
34 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
37 * The Class ElasticSearchAdapter.
40 public class ElasticSearchAdapter {
42 private static final String BULK_IMPORT_INDEX_TEMPLATE =
43 "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\", \"_version\":\"%s\"}}\n";
45 private static final String BULK_API = "_bulk";
47 private static final String DEFAULT_TYPE = "default";
49 private RestClient restClient;
50 private RestEndpointConfig endpointConfig;
53 * Instantiates a new elastic search adapter.
54 * @throws RestClientConstructionException
56 public ElasticSearchAdapter(RestEndpointConfig endpointConfig) throws RestClientConstructionException {
58 this.restClient = RestClientFactory.buildClient(endpointConfig);
59 this.endpointConfig = endpointConfig;
63 protected Map<String, List<String>> getMessageHeaders() {
64 Map<String, List<String>> headers = new HashMap<String, List<String>>();
65 // insert mandatory headers if there are any
69 public OperationResult doGet(String url, MediaType acceptContentType) {
70 return restClient.get(url, getMessageHeaders(), acceptContentType);
73 public OperationResult doDelete(String url, MediaType acceptContentType) {
74 return restClient.delete(url, getMessageHeaders(), acceptContentType);
77 public OperationResult doPost(String url, String jsonPayload, MediaType acceptContentType) {
78 return restClient.post(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
82 public OperationResult doPut(String url, String jsonPayload, MediaType acceptContentType) {
83 return restClient.put(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
87 public OperationResult doPatch(String url, String jsonPayload, MediaType acceptContentType) {
89 Map<String,List<String>> headers = getMessageHeaders();
90 headers.putIfAbsent("X-HTTP-Method-Override", new ArrayList<String>());
91 headers.get("X-HTTP-Method-Override").add("PATCH");
93 return restClient.post(url, jsonPayload, headers, MediaType.APPLICATION_JSON_TYPE, acceptContentType);
96 public OperationResult doHead(String url, MediaType acceptContentType) {
97 return restClient.head(url, getMessageHeaders(), acceptContentType);
100 public OperationResult doBulkOperation(String url, String payload) {
101 return restClient.put(url, payload, getMessageHeaders(),
102 MediaType.APPLICATION_FORM_URLENCODED_TYPE, MediaType.APPLICATION_JSON_TYPE);
105 public String buildBulkImportOperationRequest(String index, String type, String id,
106 String version, String payload) {
108 StringBuilder requestPayload = new StringBuilder(128);
110 requestPayload.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, index, type, id, version));
111 requestPayload.append(payload).append("\n");
113 return requestPayload.toString();
117 public OperationResult retrieveEntityById(String host, String port, String indexName,
118 String docType, String resourceUrl) {
120 String.format("http://%s:%s/%s/%s/%s", host, port, indexName, docType, resourceUrl);
121 return doGet(esUrl, MediaType.APPLICATION_JSON_TYPE);
124 public String buildElasticSearchUrlForApi(String indexName, String api) {
125 return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
126 endpointConfig.getEndpointServerPort(), indexName, api);
129 public String buildElasticSearchUrl(String indexName, String docType) {
130 return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
131 endpointConfig.getEndpointServerPort(), indexName, docType);
134 public String buildElasticSearchGetDocUrl(String indexName, String docType, String docId) {
135 return String.format("http://%s:%s/%s/%s/%s", endpointConfig.getEndpointIpAddress(),
136 endpointConfig.getEndpointServerPort(), indexName, docType, docId);
139 public String buildElasticSearchGetDocUrl(String indexName, String docId) {
140 return buildElasticSearchGetDocUrl(indexName, DEFAULT_TYPE, docId);
143 public String buildElasticSearchPostUrl(String indexName) {
144 return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
145 endpointConfig.getEndpointServerPort(), indexName, DEFAULT_TYPE);
148 public String getBulkUrl() {
149 return String.format("http://%s:%s/%s", endpointConfig.getEndpointIpAddress(),
150 endpointConfig.getEndpointServerPort(), BULK_API);