2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.dal.elasticsearch;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.ws.rs.core.MediaType;
32 import org.onap.aai.sparky.dal.rest.OperationResult;
33 import org.onap.aai.sparky.dal.sas.config.SearchServiceConfig;
34 import org.onap.aai.sparky.util.Encryptor;
35 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
36 import org.onap.aai.cl.api.Logger;
37 import org.onap.aai.cl.eelf.LoggerFactory;
40 import org.onap.aai.restclient.client.RestClient;
41 import org.onap.aai.restclient.enums.RestAuthenticationMode;
42 import org.onap.aai.restclient.client.Headers;
43 import org.onap.aai.cl.mdc.MdcContext;
45 import org.onap.aai.cl.mdc.MdcContext;
48 * The Class SearchAdapter.
50 public class SearchAdapter {
52 private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchAdapter.class);
54 private RestClient client;
59 public RestClient getClient() {
64 * @param client the client to set
66 public void setClient(RestClient client) {
71 * @return the commonHeaders
73 public Map<String, List<String>> getCommonHeaders() {
78 * @param commonHeaders the commonHeaders to set
80 public void setCommonHeaders(Map<String, List<String>> commonHeaders) {
81 this.commonHeaders = commonHeaders;
87 public static Logger getLog() {
91 private Map<String, List<String>> commonHeaders;
92 private SearchServiceConfig sasConfig;
95 * Instantiates a new search adapter.
99 public SearchAdapter() throws Exception {
100 sasConfig = SearchServiceConfig.getConfig();
101 Encryptor encryptor = new Encryptor();
102 client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT)
103 .validateServerHostname(false).validateServerCertChain(false)
104 .clientCertFile(TierSupportUiConstants.CONFIG_AUTH_LOCATION + sasConfig.getCertName())
105 .clientCertPassword(encryptor.decryptValue(sasConfig.getKeystorePassword()))
106 .trustStore(TierSupportUiConstants.CONFIG_AUTH_LOCATION + sasConfig.getKeystore());
108 commonHeaders = new HashMap<String, List<String>>();
109 commonHeaders.put("Accept", Arrays.asList("application/json"));
110 commonHeaders.put(Headers.FROM_APP_ID, Arrays.asList("AAI-UI"));
113 public SearchServiceConfig getSasConfig() {
117 public void setSasConfig(SearchServiceConfig sasConfig) {
118 this.sasConfig = sasConfig;
121 public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
122 org.onap.aai.restclient.client.OperationResult or = client.post(url, jsonPayload,
123 getTxnHeader(), MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
124 return new OperationResult(or.getResultCode(), or.getResult());
127 public OperationResult doGet(String url, String acceptContentType) {
128 org.onap.aai.restclient.client.OperationResult or =
129 client.get(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
130 return new OperationResult(or.getResultCode(), or.getResult());
133 public OperationResult doPut(String url, String payload, String acceptContentType) {
134 org.onap.aai.restclient.client.OperationResult or = client.put(url, payload, getTxnHeader(),
135 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
136 return new OperationResult(or.getResultCode(), or.getResult());
139 public OperationResult doDelete(String url, String acceptContentType) {
141 org.onap.aai.restclient.client.OperationResult or =
142 client.delete(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
143 return new OperationResult(or.getResultCode(), or.getResult());
146 public Map<String, List<String>> getTxnHeader() {
147 Map headers = new HashMap<String, List<String>>();
148 headers.putAll(this.commonHeaders);
149 headers.put("X-TransactionId", Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
150 headers.put("X-FromAppId", Arrays.asList(MDC.get(MdcContext.MDC_PARTNER_NAME)));