Adding UI extensibility
[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.restclient.enums.RestAuthenticationMode;
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 RestClient restClient;
46
47   /**
48    * Instantiates a new elastic search adapter.
49    */
50   public ElasticSearchAdapter(RestAuthenticationMode restAuthenticationMode, int connectTimeoutInMs,
51       int readTimeoutInMs) {
52
53     this.restClient = new RestClient().authenticationMode(restAuthenticationMode)
54         .connectTimeoutMs(connectTimeoutInMs).readTimeoutMs(readTimeoutInMs);
55
56   }
57
58   protected Map<String, List<String>> getMessageHeaders() {
59     Map<String, List<String>> headers = new HashMap<String, List<String>>();
60     // insert mandatory headers if there are any
61     return headers;
62   }
63
64   public OperationResult doGet(String url, MediaType acceptContentType) {
65     return restClient.get(url, getMessageHeaders(), acceptContentType);
66   }
67
68   public OperationResult doDelete(String url, MediaType acceptContentType) {
69     return restClient.delete(url, getMessageHeaders(), acceptContentType);
70   }
71
72   public OperationResult doPost(String url, String jsonPayload, MediaType acceptContentType) {
73     return restClient.post(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
74         acceptContentType);
75   }
76
77   public OperationResult doPut(String url, String jsonPayload, MediaType acceptContentType) {
78     return restClient.put(url, jsonPayload, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE,
79         acceptContentType);
80   }
81
82   public OperationResult doPatch(String url, String jsonPayload, MediaType acceptContentType) {
83
84     Map<String, List<String>> headers = getMessageHeaders();
85     headers.putIfAbsent("X-HTTP-Method-Override", new ArrayList<String>());
86     headers.get("X-HTTP-Method-Override").add("PATCH");
87
88     return restClient.post(url, jsonPayload, headers, MediaType.APPLICATION_JSON_TYPE,
89         acceptContentType);
90   }
91
92   public OperationResult doHead(String url, MediaType acceptContentType) {
93     return restClient.head(url, getMessageHeaders(), acceptContentType);
94   }
95
96   public OperationResult doBulkOperation(String url, String payload) {
97     return restClient.put(url, payload, getMessageHeaders(),
98         MediaType.APPLICATION_FORM_URLENCODED_TYPE, MediaType.APPLICATION_JSON_TYPE);
99   }
100
101   public String buildBulkImportOperationRequest(String index, String type, String id,
102       String version, String payload) {
103
104     StringBuilder requestPayload = new StringBuilder(128);
105
106     requestPayload.append(String.format(BULK_IMPORT_INDEX_TEMPLATE, index, type, id, version));
107     requestPayload.append(payload).append("\n");
108
109     return requestPayload.toString();
110
111   }
112
113   public OperationResult retrieveEntityById(String host, String port, String indexName,
114       String docType, String resourceUrl) {
115     String esUrl =
116         String.format("http://%s:%s/%s/%s/%s", host, port, indexName, docType, resourceUrl);
117     return doGet(esUrl, MediaType.APPLICATION_JSON_TYPE);
118   }
119
120 }