Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / dal / ElasticSearchAdapter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.sparky.dal;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.ws.rs.core.MediaType;
29
30 import org.onap.aai.restclient.client.OperationResult;
31 import org.onap.aai.restclient.client.RestClient;
32 import org.onap.aai.sparky.dal.rest.RestClientConstructionException;
33 import org.onap.aai.sparky.dal.rest.RestClientFactory;
34 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
35
36 /**
37  * The Class ElasticSearchAdapter.
38
39  */
40 public class ElasticSearchAdapter {
41
42   private static final String BULK_IMPORT_INDEX_TEMPLATE =
43       "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\", \"_version\":\"%s\"}}\n";
44
45   private static final String BULK_API = "_bulk";
46   
47   private static final String DEFAULT_TYPE = "default";
48   
49   private RestClient restClient;
50   private RestEndpointConfig endpointConfig;
51   
52   /**
53    * Instantiates a new elastic search adapter.
54    * @throws RestClientConstructionException 
55    */
56   public ElasticSearchAdapter(RestEndpointConfig endpointConfig) throws RestClientConstructionException {
57
58     this.restClient = RestClientFactory.buildClient(endpointConfig);
59     this.endpointConfig = endpointConfig;
60
61   }
62   
63   protected Map<String, List<String>> getMessageHeaders() {
64     Map<String, List<String>> headers = new HashMap<String, List<String>>();
65     // insert mandatory headers if there are any
66     return headers;
67   }
68
69   public OperationResult doGet(String url, MediaType acceptContentType) {
70     return restClient.get(url, getMessageHeaders(), acceptContentType);
71   }
72
73   public OperationResult doDelete(String url, MediaType acceptContentType) {
74     return restClient.delete(url, getMessageHeaders(), acceptContentType);
75   }
76
77   public OperationResult doPost(String url, String jsonPayload, MediaType acceptContentType) {
78     return restClient.post(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
79         acceptContentType);
80   }
81
82   public OperationResult doPut(String url, String jsonPayload, MediaType acceptContentType) {
83     return restClient.put(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
84         acceptContentType);
85   }
86
87   public OperationResult doPatch(String url, String jsonPayload, MediaType acceptContentType) {
88
89     Map<String,List<String>> headers = getMessageHeaders();
90     headers.putIfAbsent("X-HTTP-Method-Override", new ArrayList<String>());
91     headers.get("X-HTTP-Method-Override").add("PATCH");
92     
93     return restClient.post(url, jsonPayload, headers, MediaType.APPLICATION_JSON_TYPE, acceptContentType);
94   }
95
96   public OperationResult doHead(String url, MediaType acceptContentType) {
97     return restClient.head(url, getMessageHeaders(), acceptContentType);
98   }
99   
100   public OperationResult doBulkOperation(String url, String payload) {
101     return restClient.put(url, payload, getMessageHeaders(),
102         MediaType.APPLICATION_FORM_URLENCODED_TYPE, MediaType.APPLICATION_JSON_TYPE);
103   }
104
105   public String buildBulkImportOperationRequest(String index, String type, String id,
106       String version, String payload) {
107
108     StringBuilder requestPayload = new StringBuilder(128);
109
110     requestPayload.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, index, type, id, version));
111     requestPayload.append(payload).append("\n");
112
113     return requestPayload.toString();
114
115   }
116   
117   public OperationResult retrieveEntityById(String host, String port, String indexName,
118       String docType, String resourceUrl) {
119     String esUrl =
120         String.format("http://%s:%s/%s/%s/%s", host, port, indexName, docType, resourceUrl);
121     return doGet(esUrl, MediaType.APPLICATION_JSON_TYPE);
122   }
123
124   public String buildElasticSearchUrlForApi(String indexName, String api) {
125     return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
126         endpointConfig.getEndpointServerPort(), indexName, api);
127   }
128   
129   public String buildElasticSearchUrl(String indexName, String docType) {
130     return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
131         endpointConfig.getEndpointServerPort(), indexName, docType);
132   }
133
134   public String buildElasticSearchGetDocUrl(String indexName, String docType, String docId) {
135     return String.format("http://%s:%s/%s/%s/%s", endpointConfig.getEndpointIpAddress(),
136         endpointConfig.getEndpointServerPort(), indexName, docType, docId);
137   }
138
139   public String buildElasticSearchGetDocUrl(String indexName, String docId) {
140     return buildElasticSearchGetDocUrl(indexName, DEFAULT_TYPE, docId);
141   }
142
143   public String buildElasticSearchPostUrl(String indexName) {
144     return String.format("http://%s:%s/%s/%s", endpointConfig.getEndpointIpAddress(),
145         endpointConfig.getEndpointServerPort(), indexName, DEFAULT_TYPE);
146   }
147   
148   public String getBulkUrl() {
149     return String.format("http://%s:%s/%s", endpointConfig.getEndpointIpAddress(),
150         endpointConfig.getEndpointServerPort(), BULK_API);
151   }
152   
153 }