Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / search / SearchServiceAdapter.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.search;
26
27 import java.util.Arrays;
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.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;
40 import org.slf4j.MDC;
41
42
43 /**
44  * The Class SearchServiceAdapter.
45  */
46 public class SearchServiceAdapter {
47
48   private static final String VALUE_QUERY = "query";
49   
50   private RestClient client;
51   private RestEndpointConfig endpointConfig;
52   private String serviceApiVersion;
53
54   private Map<String, List<String>> commonHeaders;
55
56   /**
57    * Instantiates a new search adapter.
58    * @throws Exception 
59    */
60   public SearchServiceAdapter(RestEndpointConfig endpointConfig, String serviceApiVersion) throws Exception {
61
62     client = RestClientFactory.buildClient(endpointConfig);
63
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"));
67     
68     this.serviceApiVersion = serviceApiVersion;
69     this.endpointConfig = endpointConfig;
70   }
71   
72   public String getServiceApiVersion() {
73     return serviceApiVersion;
74   }
75
76   public void setServiceApiVersion(String serviceApiVersion) {
77     this.serviceApiVersion = serviceApiVersion;
78   }
79
80   public RestEndpointConfig getEndpointConfig() {
81     return endpointConfig;
82   }
83
84   public void setEndpointConfig(RestEndpointConfig endpointConfig) {
85     this.endpointConfig = endpointConfig;
86   }
87
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());
92   }
93
94   public OperationResult doGet(String url, String acceptContentType) {
95     OperationResult or =
96         client.get(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
97     return new OperationResult(or.getResultCode(), or.getResult());
98   }
99
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());
104   }
105
106   public OperationResult doDelete(String url, String acceptContentType) {
107
108     OperationResult or =
109         client.delete(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
110     return new OperationResult(or.getResultCode(), or.getResult());
111   }
112
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)));
118     return headers;
119   }
120
121   /**
122    * Get Full URL for search
123    *
124    * @param api the api
125    * @param indexName
126    * @return the full url
127    */
128   public String buildSearchServiceQueryUrl(String indexName) {
129     return buildSearchServiceUrlForApi(indexName, VALUE_QUERY);
130   }
131
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);
136   }
137
138
139 }