2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.search;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
28 import javax.ws.rs.core.MediaType;
30 import org.onap.aai.cl.mdc.MdcContext;
31 import org.onap.aai.restclient.client.Headers;
32 import org.onap.aai.restclient.client.OperationResult;
33 import org.onap.aai.restclient.client.RestClient;
34 import org.onap.aai.sparky.dal.rest.RestClientFactory;
35 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
40 * The Class SearchServiceAdapter.
42 public class SearchServiceAdapter {
44 private static final String VALUE_QUERY = "query";
46 private RestClient client;
47 private RestEndpointConfig endpointConfig;
48 private String serviceApiVersion;
50 private Map<String, List<String>> commonHeaders;
53 * Instantiates a new search adapter.
56 public SearchServiceAdapter(RestEndpointConfig endpointConfig, String serviceApiVersion) throws Exception {
58 client = RestClientFactory.buildClient(endpointConfig);
60 commonHeaders = new HashMap<String, List<String>>();
61 commonHeaders.put("Accept", Arrays.asList("application/json"));
62 commonHeaders.put(Headers.FROM_APP_ID, Arrays.asList("AAI-UI"));
64 this.serviceApiVersion = serviceApiVersion;
65 this.endpointConfig = endpointConfig;
68 public String getServiceApiVersion() {
69 return serviceApiVersion;
72 public void setServiceApiVersion(String serviceApiVersion) {
73 this.serviceApiVersion = serviceApiVersion;
76 public RestEndpointConfig getEndpointConfig() {
77 return endpointConfig;
80 public void setEndpointConfig(RestEndpointConfig endpointConfig) {
81 this.endpointConfig = endpointConfig;
84 public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
85 OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
86 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
87 return new OperationResult(or.getResultCode(), or.getResult());
90 public OperationResult doGet(String url, String acceptContentType) {
92 client.get(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
93 return new OperationResult(or.getResultCode(), or.getResult());
96 public OperationResult doPut(String url, String payload, String acceptContentType) {
97 OperationResult or = client.put(url, payload, getTxnHeader(),
98 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
99 return new OperationResult(or.getResultCode(), or.getResult());
102 public OperationResult doDelete(String url, String acceptContentType) {
105 client.delete(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
106 return new OperationResult(or.getResultCode(), or.getResult());
109 public Map<String, List<String>> getTxnHeader() {
110 HashMap<String, List<String>> headers = new HashMap<String, List<String>>();
111 headers.putAll(this.commonHeaders);
112 headers.put("X-TransactionId", Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
113 headers.put("X-FromAppId", Arrays.asList(MDC.get(MdcContext.MDC_PARTNER_NAME)));
118 * Get Full URL for search
122 * @return the full url
124 public String buildSearchServiceQueryUrl(String indexName) {
125 return buildSearchServiceUrlForApi(indexName, VALUE_QUERY);
128 public String buildSearchServiceUrlForApi(String indexName, String api) {
129 return String.format("https://%s:%s/services/search-data-service/%s/search/indexes/%s/%s",
130 endpointConfig.getEndpointIpAddress(), endpointConfig.getEndpointServerPort(),
131 serviceApiVersion, indexName, api);