summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/sparky/dal/ElasticSearchAdapter.java
blob: 3072b91005213ed75757632e2f6d9ccf1031e7fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.sparky.dal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.onap.aai.restclient.client.OperationResult;
import org.onap.aai.restclient.client.RestClient;
import org.onap.aai.sparky.dal.rest.RestClientConstructionException;
import org.onap.aai.sparky.dal.rest.RestClientFactory;
import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;

/**
 * The Class ElasticSearchAdapter.

 */
public class ElasticSearchAdapter {

  private static final String BULK_IMPORT_INDEX_TEMPLATE =
      "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\", \"_version\":\"%s\"}}\n";

  private static final String BULK_API = "_bulk";
  
  private static final String DEFAULT_TYPE = "default";
  
  private RestClient restClient;
  private RestEndpointConfig endpointConfig;
  
  /**
   * Instantiates a new elastic search adapter.
   * @throws RestClientConstructionException 
   */
  public ElasticSearchAdapter(RestEndpointConfig endpointConfig) throws RestClientConstructionException {

    this.restClient = RestClientFactory.buildClient(endpointConfig);
    this.endpointConfig = endpointConfig;

  }
  
  protected Map<String, List<String>> getMessageHeaders() {
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    // insert mandatory headers if there are any
    return headers;
  }

  public OperationResult doGet(String url, MediaType acceptContentType) {
    return restClient.get(url, getMessageHeaders(), acceptContentType);
  }

  public OperationResult doDelete(String url, MediaType acceptContentType) {
    return restClient.delete(url, getMessageHeaders(), acceptContentType);
  }

  public OperationResult doPost(String url, String jsonPayload, MediaType acceptContentType) {
    return restClient.post(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
        acceptContentType);
  }

  public OperationResult doPut(String url, String jsonPayload, MediaType acceptContentType) {
    return restClient.put(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
        acceptContentType);
  }

  public OperationResult doPatch(String url, String jsonPayload, MediaType acceptContentType) {

    Map<String,List<String>> headers = getMessageHeaders();
    headers.putIfAbsent("X-HTTP-Method-Override", new ArrayList<String>());
    headers.get("X-HTTP-Method-Override").add("PATCH");
    
    return restClient.post(url, jsonPayload, headers, MediaType.APPLICATION_JSON_TYPE, acceptContentType);
  }

  public OperationResult doHead(String url, MediaType acceptContentType) {
    return restClient.head(url, getMessageHeaders(), acceptContentType);
  }
  
  public OperationResult doBulkOperation(String url, String payload) {
    return restClient.put(url, payload, getMessageHeaders(),
        MediaType.APPLICATION_FORM_URLENCODED_TYPE, MediaType.APPLICATION_JSON_TYPE);
  }

  public String buildBulkImportOperationRequest(String index, String type, String id,
      String version, String payload) {

    StringBuilder requestPayload = new StringBuilder(128);

    requestPayload.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, index, type, id, version));
    requestPayload.append(payload).append("\n");

    return requestPayload.toString();

  }
  
  public OperationResult retrieveEntityById(String host, String port, String indexName,
      String docType, String resourceUrl) {
    String esUrl =
        String.format("http://%s:%s/%s/%s/%s", host, port, indexName, docType, resourceUrl);
    return doGet(esUrl, MediaType.APPLICATION_JSON_TYPE);
  }

  public String buildElasticSearchUrlForApi(String indexName, String api) {
    return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
        endpointConfig.getEndpointServerPort(), indexName, api);
  }
  
  public String buildElasticSearchUrl(String indexName, String docType) {
    return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
        endpointConfig.getEndpointServerPort(), indexName, docType);
  }

  public String buildElasticSearchGetDocUrl(String indexName, String docType, String docId) {
    return String.format("http://%s:%s/%s/%s/%s", endpointConfig.getEndpointIpAddress(),
        endpointConfig.getEndpointServerPort(), indexName, docType, docId);
  }

  public String buildElasticSearchGetDocUrl(String indexName, String docId) {
    return buildElasticSearchGetDocUrl(indexName, DEFAULT_TYPE, docId);
  }

  public String buildElasticSearchPostUrl(String indexName) {
    return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
        endpointConfig.getEndpointServerPort(), indexName, DEFAULT_TYPE);
  }
  
  public String getBulkUrl() {
    return String.format("http://%s:%s/%s", endpointConfig.getEndpointIpAddress(),
        endpointConfig.getEndpointServerPort(), BULK_API);
  }
  
}