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