2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.search;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
32 import javax.ws.rs.core.MediaType;
34 import org.onap.aai.cl.mdc.MdcContext;
35 import org.onap.aai.restclient.client.Headers;
36 import org.onap.aai.restclient.client.OperationResult;
37 import org.onap.aai.restclient.client.RestClient;
38 import org.onap.aai.sparky.dal.rest.RestClientFactory;
39 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
44 * The Class SearchServiceAdapter.
46 public class SearchServiceAdapter {
48 private static final String VALUE_QUERY = "query";
50 private RestClient client;
51 private RestEndpointConfig endpointConfig;
52 private String serviceApiVersion;
54 private Map<String, List<String>> commonHeaders;
57 * Instantiates a new search adapter.
60 public SearchServiceAdapter(RestEndpointConfig endpointConfig, String serviceApiVersion) throws Exception {
62 client = RestClientFactory.buildClient(endpointConfig);
64 commonHeaders = new HashMap<String, List<String>>();
65 commonHeaders.put("Accept", Arrays.asList("application/json"));
66 commonHeaders.put(Headers.FROM_APP_ID, Arrays.asList("AAI-UI"));
68 this.serviceApiVersion = serviceApiVersion;
69 this.endpointConfig = endpointConfig;
72 public String getServiceApiVersion() {
73 return serviceApiVersion;
76 public void setServiceApiVersion(String serviceApiVersion) {
77 this.serviceApiVersion = serviceApiVersion;
80 public RestEndpointConfig getEndpointConfig() {
81 return endpointConfig;
84 public void setEndpointConfig(RestEndpointConfig endpointConfig) {
85 this.endpointConfig = endpointConfig;
88 public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
89 OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
90 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
91 return new OperationResult(or.getResultCode(), or.getResult());
94 public OperationResult doGet(String url, String acceptContentType) {
96 client.get(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
97 return new OperationResult(or.getResultCode(), or.getResult());
100 public OperationResult doPut(String url, String payload, String acceptContentType) {
101 OperationResult or = client.put(url, payload, getTxnHeader(),
102 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
103 return new OperationResult(or.getResultCode(), or.getResult());
106 public OperationResult doDelete(String url, String acceptContentType) {
109 client.delete(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
110 return new OperationResult(or.getResultCode(), or.getResult());
113 public Map<String, List<String>> getTxnHeader() {
114 HashMap<String, List<String>> headers = new HashMap<String, List<String>>();
115 headers.putAll(this.commonHeaders);
116 headers.put("X-TransactionId", Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
117 headers.put("X-FromAppId", Arrays.asList(MDC.get(MdcContext.MDC_PARTNER_NAME)));
122 * Get Full URL for search
126 * @return the full url
128 public String buildSearchServiceQueryUrl(String indexName) {
129 return buildSearchServiceUrlForApi(indexName, VALUE_QUERY);
132 public String buildSearchServiceUrlForApi(String indexName, String api) {
133 return String.format("https://%s:%s/services/search-data-service/%s/search/indexes/%s/%s",
134 endpointConfig.getEndpointIpAddress(), endpointConfig.getEndpointServerPort(),
135 serviceApiVersion, indexName, api);