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;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.ws.rs.core.MediaType;
32 import org.onap.aai.restclient.client.OperationResult;
33 import org.onap.aai.restclient.client.RestClient;
34 import org.onap.aai.sparky.dal.rest.RestClientConstructionException;
35 import org.onap.aai.sparky.dal.rest.RestClientFactory;
36 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
39 * The Class ElasticSearchAdapter.
42 public class ElasticSearchAdapter {
44 private static final String BULK_IMPORT_INDEX_TEMPLATE =
45 "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\", \"_version\":\"%s\"}}\n";
47 private static final String BULK_API = "_bulk";
49 private static final String DEFAULT_TYPE = "default";
51 private RestClient restClient;
52 private RestEndpointConfig endpointConfig;
55 * Instantiates a new elastic search adapter.
56 * @throws RestClientConstructionException
58 public ElasticSearchAdapter(RestEndpointConfig endpointConfig) throws RestClientConstructionException {
60 this.restClient = RestClientFactory.buildClient(endpointConfig);
61 this.endpointConfig = endpointConfig;
65 protected Map<String, List<String>> getMessageHeaders() {
66 Map<String, List<String>> headers = new HashMap<String, List<String>>();
67 // insert mandatory headers if there are any
71 public OperationResult doGet(String url, MediaType acceptContentType) {
72 return restClient.get(url, getMessageHeaders(), acceptContentType);
75 public OperationResult doDelete(String url, MediaType acceptContentType) {
76 return restClient.delete(url, getMessageHeaders(), acceptContentType);
79 public OperationResult doPost(String url, String jsonPayload, MediaType acceptContentType) {
80 return restClient.post(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
84 public OperationResult doPut(String url, String jsonPayload, MediaType acceptContentType) {
85 return restClient.put(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
89 public OperationResult doPatch(String url, String jsonPayload, MediaType acceptContentType) {
91 Map<String,List<String>> headers = getMessageHeaders();
92 headers.putIfAbsent("X-HTTP-Method-Override", new ArrayList<String>());
93 headers.get("X-HTTP-Method-Override").add("PATCH");
95 return restClient.post(url, jsonPayload, headers, MediaType.APPLICATION_JSON_TYPE, acceptContentType);
98 public OperationResult doHead(String url, MediaType acceptContentType) {
99 return restClient.head(url, getMessageHeaders(), acceptContentType);
102 public OperationResult doBulkOperation(String url, String payload) {
103 return restClient.put(url, payload, getMessageHeaders(),
104 MediaType.APPLICATION_FORM_URLENCODED_TYPE, MediaType.APPLICATION_JSON_TYPE);
107 public String buildBulkImportOperationRequest(String index, String type, String id,
108 String version, String payload) {
110 StringBuilder requestPayload = new StringBuilder(128);
112 requestPayload.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, index, type, id, version));
113 requestPayload.append(payload).append("\n");
115 return requestPayload.toString();
119 public OperationResult retrieveEntityById(String host, String port, String indexName,
120 String docType, String resourceUrl) {
122 String.format("http://%s:%s/%s/%s/%s", host, port, indexName, docType, resourceUrl);
123 return doGet(esUrl, MediaType.APPLICATION_JSON_TYPE);
126 public String buildElasticSearchUrlForApi(String indexName, String api) {
127 return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
128 endpointConfig.getEndpointServerPort(), indexName, api);
131 public String buildElasticSearchUrl(String indexName, String docType) {
132 return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
133 endpointConfig.getEndpointServerPort(), indexName, docType);
136 public String buildElasticSearchGetDocUrl(String indexName, String docType, String docId) {
137 return String.format("http://%s:%s/%s/%s/%s", endpointConfig.getEndpointIpAddress(),
138 endpointConfig.getEndpointServerPort(), indexName, docType, docId);
141 public String buildElasticSearchGetDocUrl(String indexName, String docId) {
142 return buildElasticSearchGetDocUrl(indexName, DEFAULT_TYPE, docId);
145 public String buildElasticSearchPostUrl(String indexName) {
146 return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
147 endpointConfig.getEndpointServerPort(), indexName, DEFAULT_TYPE);
150 public String getBulkUrl() {
151 return String.format("http://%s:%s/%s", endpointConfig.getEndpointIpAddress(),
152 endpointConfig.getEndpointServerPort(), BULK_API);