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.restclient.enums.RestAuthenticationMode;
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 RestClient restClient;
48 * Instantiates a new elastic search adapter.
50 public ElasticSearchAdapter(RestAuthenticationMode restAuthenticationMode, int connectTimeoutInMs,
51 int readTimeoutInMs) {
53 this.restClient = new RestClient().authenticationMode(restAuthenticationMode)
54 .connectTimeoutMs(connectTimeoutInMs).readTimeoutMs(readTimeoutInMs);
58 protected Map<String, List<String>> getMessageHeaders() {
59 Map<String, List<String>> headers = new HashMap<String, List<String>>();
60 // insert mandatory headers if there are any
64 public OperationResult doGet(String url, MediaType acceptContentType) {
65 return restClient.get(url, getMessageHeaders(), acceptContentType);
68 public OperationResult doDelete(String url, MediaType acceptContentType) {
69 return restClient.delete(url, getMessageHeaders(), acceptContentType);
72 public OperationResult doPost(String url, String jsonPayload, MediaType acceptContentType) {
73 return restClient.post(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
77 public OperationResult doPut(String url, String jsonPayload, MediaType acceptContentType) {
78 return restClient.put(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
82 public OperationResult doPatch(String url, String jsonPayload, MediaType acceptContentType) {
84 Map<String, List<String>> headers = getMessageHeaders();
85 headers.putIfAbsent("X-HTTP-Method-Override", new ArrayList<String>());
86 headers.get("X-HTTP-Method-Override").add("PATCH");
88 return restClient.post(url, jsonPayload, headers, MediaType.APPLICATION_JSON_TYPE,
92 public OperationResult doHead(String url, MediaType acceptContentType) {
93 return restClient.head(url, getMessageHeaders(), acceptContentType);
96 public OperationResult doBulkOperation(String url, String payload) {
97 return restClient.put(url, payload, getMessageHeaders(),
98 MediaType.APPLICATION_FORM_URLENCODED_TYPE, MediaType.APPLICATION_JSON_TYPE);
101 public String buildBulkImportOperationRequest(String index, String type, String id,
102 String version, String payload) {
104 StringBuilder requestPayload = new StringBuilder(128);
106 requestPayload.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, index, type, id, version));
107 requestPayload.append(payload).append("\n");
109 return requestPayload.toString();
113 public OperationResult retrieveEntityById(String host, String port, String indexName,
114 String docType, String resourceUrl) {
116 String.format("http://%s:%s/%s/%s/%s", host, port, indexName, docType, resourceUrl);
117 return doGet(esUrl, MediaType.APPLICATION_JSON_TYPE);