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.
25 package org.onap.aai.sparky.dal;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
32 import javax.ws.rs.core.MediaType;
34 import org.onap.aai.restclient.client.OperationResult;
35 import org.onap.aai.restclient.client.RestClient;
36 import org.onap.aai.sparky.dal.rest.RestClientConstructionException;
37 import org.onap.aai.sparky.dal.rest.RestClientFactory;
38 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
41 * The Class ElasticSearchAdapter.
44 public class ElasticSearchAdapter {
46 private static final String BULK_IMPORT_INDEX_TEMPLATE =
47 "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\", \"_version\":\"%s\"}}\n";
49 private static final String BULK_API = "_bulk";
51 private static final String DEFAULT_TYPE = "default";
53 private RestClient restClient;
54 private RestEndpointConfig endpointConfig;
57 * Instantiates a new elastic search adapter.
58 * @throws RestClientConstructionException
60 public ElasticSearchAdapter(RestEndpointConfig endpointConfig) throws RestClientConstructionException {
62 this.restClient = RestClientFactory.buildClient(endpointConfig);
63 this.endpointConfig = endpointConfig;
67 protected Map<String, List<String>> getMessageHeaders() {
68 Map<String, List<String>> headers = new HashMap<String, List<String>>();
69 // insert mandatory headers if there are any
73 public OperationResult doGet(String url, MediaType acceptContentType) {
74 return restClient.get(url, getMessageHeaders(), acceptContentType);
77 public OperationResult doDelete(String url, MediaType acceptContentType) {
78 return restClient.delete(url, getMessageHeaders(), acceptContentType);
81 public OperationResult doPost(String url, String jsonPayload, MediaType acceptContentType) {
82 return restClient.post(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
86 public OperationResult doPut(String url, String jsonPayload, MediaType acceptContentType) {
87 return restClient.put(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
91 public OperationResult doPatch(String url, String jsonPayload, MediaType acceptContentType) {
93 Map<String,List<String>> headers = getMessageHeaders();
94 headers.putIfAbsent("X-HTTP-Method-Override", new ArrayList<String>());
95 headers.get("X-HTTP-Method-Override").add("PATCH");
97 return restClient.post(url, jsonPayload, headers, MediaType.APPLICATION_JSON_TYPE, acceptContentType);
100 public OperationResult doHead(String url, MediaType acceptContentType) {
101 return restClient.head(url, getMessageHeaders(), acceptContentType);
104 public OperationResult doBulkOperation(String url, String payload) {
105 return restClient.put(url, payload, getMessageHeaders(),
106 MediaType.APPLICATION_FORM_URLENCODED_TYPE, MediaType.APPLICATION_JSON_TYPE);
109 public String buildBulkImportOperationRequest(String index, String type, String id,
110 String version, String payload) {
112 StringBuilder requestPayload = new StringBuilder(128);
114 requestPayload.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, index, type, id, version));
115 requestPayload.append(payload).append("\n");
117 return requestPayload.toString();
121 public OperationResult retrieveEntityById(String host, String port, String indexName,
122 String docType, String resourceUrl) {
124 String.format("http://%s:%s/%s/%s/%s", host, port, indexName, docType, resourceUrl);
125 return doGet(esUrl, MediaType.APPLICATION_JSON_TYPE);
128 public String buildElasticSearchUrlForApi(String indexName, String api) {
129 return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
130 endpointConfig.getEndpointServerPort(), indexName, api);
133 public String buildElasticSearchUrl(String indexName, String docType) {
134 return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
135 endpointConfig.getEndpointServerPort(), indexName, docType);
138 public String buildElasticSearchGetDocUrl(String indexName, String docType, String docId) {
139 return String.format("http://%s:%s/%s/%s/%s", endpointConfig.getEndpointIpAddress(),
140 endpointConfig.getEndpointServerPort(), indexName, docType, docId);
143 public String buildElasticSearchGetDocUrl(String indexName, String docId) {
144 return buildElasticSearchGetDocUrl(indexName, DEFAULT_TYPE, docId);
147 public String buildElasticSearchPostUrl(String indexName) {
148 return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
149 endpointConfig.getEndpointServerPort(), indexName, DEFAULT_TYPE);
152 public String getBulkUrl() {
153 return String.format("http://%s:%s/%s", endpointConfig.getEndpointIpAddress(),
154 endpointConfig.getEndpointServerPort(), BULK_API);