enable suggestions to use search data service
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / search / SearchServiceAdapter.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.search;
24
25 import java.util.Arrays;
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.cl.mdc.MdcContext;
33 import org.onap.aai.restclient.client.Headers;
34 import org.onap.aai.restclient.client.OperationResult;
35 import org.onap.aai.restclient.client.RestClient;
36 import org.onap.aai.sparky.dal.rest.RestClientFactory;
37 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
38 import org.slf4j.MDC;
39
40
41 /**
42  * The Class SearchServiceAdapter.
43  */
44 public class SearchServiceAdapter {
45
46   private static final String VALUE_QUERY = "query";
47   private static final String SUGGEST_QUERY = "suggest";
48   
49   private RestClient client;
50   private RestEndpointConfig endpointConfig;
51   private String serviceApiVersion;
52
53   private Map<String, List<String>> commonHeaders;
54
55   /**
56    * Instantiates a new search adapter.
57    * @throws Exception 
58    */
59   public SearchServiceAdapter(RestEndpointConfig endpointConfig, String serviceApiVersion) throws Exception {
60
61     client = RestClientFactory.buildClient(endpointConfig);
62
63     commonHeaders = new HashMap<String, List<String>>();
64     commonHeaders.put("Accept", Arrays.asList("application/json"));
65     commonHeaders.put(Headers.FROM_APP_ID, Arrays.asList("AAI-UI"));
66     
67     this.serviceApiVersion = serviceApiVersion;
68     this.endpointConfig = endpointConfig;
69   }
70   
71   public String getServiceApiVersion() {
72     return serviceApiVersion;
73   }
74
75   public void setServiceApiVersion(String serviceApiVersion) {
76     this.serviceApiVersion = serviceApiVersion;
77   }
78
79   public RestEndpointConfig getEndpointConfig() {
80     return endpointConfig;
81   }
82
83   public void setEndpointConfig(RestEndpointConfig endpointConfig) {
84     this.endpointConfig = endpointConfig;
85   }
86
87   public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
88     OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
89         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
90     return new OperationResult(or.getResultCode(), or.getResult());
91   }
92
93   public OperationResult doGet(String url, String acceptContentType) {
94     OperationResult or =
95         client.get(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
96     return new OperationResult(or.getResultCode(), or.getResult());
97   }
98
99   public OperationResult doPut(String url, String payload, String acceptContentType) {
100     OperationResult or = client.put(url, payload, getTxnHeader(),
101         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
102     return new OperationResult(or.getResultCode(), or.getResult());
103   }
104
105   public OperationResult doDelete(String url, String acceptContentType) {
106
107     OperationResult or =
108         client.delete(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
109     return new OperationResult(or.getResultCode(), or.getResult());
110   }
111
112   public Map<String, List<String>> getTxnHeader() {
113     HashMap<String, List<String>> headers = new HashMap<String, List<String>>();
114     headers.putAll(this.commonHeaders);
115     headers.put("X-TransactionId", Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
116     headers.put("X-FromAppId", Arrays.asList(MDC.get(MdcContext.MDC_PARTNER_NAME)));
117     return headers;
118   }
119
120   /**
121    * Get Full URL for search
122    *
123    * @param api the api
124    * @param indexName
125    * @return the full url
126    */
127   public String buildSearchServiceQueryUrl(String indexName) {
128     return buildSearchServiceUrlForApi(indexName, VALUE_QUERY);
129   }
130   
131   /**
132    * Get Full URL for search
133    *
134    * @param api the api
135    * @param indexName
136    * @return the full url
137    */
138   public String buildSuggestServiceQueryUrl(String indexName) {
139     return buildSearchServiceUrlForApi(indexName, SUGGEST_QUERY);
140   }
141
142   public String buildSearchServiceUrlForApi(String indexName, String api) {
143     return String.format("https://%s:%s/services/search-data-service/%s/search/indexes/%s/%s",
144         endpointConfig.getEndpointIpAddress(), endpointConfig.getEndpointServerPort(),
145         serviceApiVersion, indexName, api);
146   }
147
148
149 }