e881aa43c9afb15e51f30fac4df984e6812d1a32
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / ElasticSearchAdapter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.dal;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.MediaType;
31
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;
37
38 /**
39  * The Class ElasticSearchAdapter.
40
41  */
42 public class ElasticSearchAdapter {
43
44   private static final String BULK_IMPORT_INDEX_TEMPLATE =
45       "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\", \"_version\":\"%s\"}}\n";
46
47   private static final String BULK_API = "_bulk";
48   
49   private static final String DEFAULT_TYPE = "default";
50   
51   private RestClient restClient;
52   private RestEndpointConfig endpointConfig;
53   
54   /**
55    * Instantiates a new elastic search adapter.
56    * @throws RestClientConstructionException 
57    */
58   public ElasticSearchAdapter(RestEndpointConfig endpointConfig) throws RestClientConstructionException {
59
60     this.restClient = RestClientFactory.buildClient(endpointConfig);
61     this.endpointConfig = endpointConfig;
62
63   }
64   
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
68     return headers;
69   }
70
71   public OperationResult doGet(String url, MediaType acceptContentType) {
72     return restClient.get(url, getMessageHeaders(), acceptContentType);
73   }
74
75   public OperationResult doDelete(String url, MediaType acceptContentType) {
76     return restClient.delete(url, getMessageHeaders(), acceptContentType);
77   }
78
79   public OperationResult doPost(String url, String jsonPayload, MediaType acceptContentType) {
80     return restClient.post(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
81         acceptContentType);
82   }
83
84   public OperationResult doPut(String url, String jsonPayload, MediaType acceptContentType) {
85     return restClient.put(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
86         acceptContentType);
87   }
88
89   public OperationResult doPatch(String url, String jsonPayload, MediaType acceptContentType) {
90
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");
94     
95     return restClient.post(url, jsonPayload, headers, MediaType.APPLICATION_JSON_TYPE, acceptContentType);
96   }
97
98   public OperationResult doHead(String url, MediaType acceptContentType) {
99     return restClient.head(url, getMessageHeaders(), acceptContentType);
100   }
101   
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);
105   }
106
107   public String buildBulkImportOperationRequest(String index, String type, String id,
108       String version, String payload) {
109
110     StringBuilder requestPayload = new StringBuilder(128);
111
112     requestPayload.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, index, type, id, version));
113     requestPayload.append(payload).append("\n");
114
115     return requestPayload.toString();
116
117   }
118   
119   public OperationResult retrieveEntityById(String host, String port, String indexName,
120       String docType, String resourceUrl) {
121     String esUrl =
122         String.format("http://%s:%s/%s/%s/%s", host, port, indexName, docType, resourceUrl);
123     return doGet(esUrl, MediaType.APPLICATION_JSON_TYPE);
124   }
125
126   public String buildElasticSearchUrlForApi(String indexName, String api) {
127     return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
128         endpointConfig.getEndpointServerPort(), indexName, api);
129   }
130   
131   public String buildElasticSearchUrl(String indexName, String docType) {
132     return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
133         endpointConfig.getEndpointServerPort(), indexName, docType);
134   }
135
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);
139   }
140
141   public String buildElasticSearchGetDocUrl(String indexName, String docId) {
142     return buildElasticSearchGetDocUrl(indexName, DEFAULT_TYPE, docId);
143   }
144
145   public String buildElasticSearchPostUrl(String indexName) {
146     return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
147         endpointConfig.getEndpointServerPort(), indexName, DEFAULT_TYPE);
148   }
149   
150   public String getBulkUrl() {
151     return String.format("http://%s:%s/%s", endpointConfig.getEndpointIpAddress(),
152         endpointConfig.getEndpointServerPort(), BULK_API);
153   }
154   
155 }