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.
26 package org.openecomp.sparky.dal.elasticsearch;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.List;
33 import javax.ws.rs.core.MediaType;
35 import org.openecomp.cl.api.Logger;
36 import org.openecomp.cl.eelf.LoggerFactory;
37 import org.openecomp.sparky.dal.rest.OperationResult;
38 import org.openecomp.sparky.dal.sas.config.SearchServiceConfig;
39 import org.openecomp.sparky.util.Encryptor;
40 import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants;
43 import org.openecomp.restclient.client.RestClient;
44 import org.openecomp.restclient.enums.RestAuthenticationMode;
45 import org.openecomp.restclient.client.Headers;
46 import org.openecomp.cl.mdc.MdcContext;
48 import org.openecomp.cl.mdc.MdcContext;
51 * The Class SearchAdapter.
53 public class SearchAdapter {
55 private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchAdapter.class);
57 private RestClient client;
59 private Map<String, List<String>> commonHeaders;
60 private SearchServiceConfig sasConfig;
63 * Instantiates a new search adapter.
66 public SearchAdapter() throws Exception {
67 sasConfig = SearchServiceConfig.getConfig();
68 Encryptor encryptor = new Encryptor();
69 client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT)
70 .validateServerHostname(false).validateServerCertChain(false)
71 .clientCertFile(TierSupportUiConstants.CONFIG_AUTH_LOCATION + sasConfig.getCertName())
72 .clientCertPassword(encryptor.decryptValue(sasConfig.getKeystorePassword()))
73 .trustStore(TierSupportUiConstants.CONFIG_AUTH_LOCATION + sasConfig.getKeystore());
75 commonHeaders = new HashMap<String, List<String>>();
76 commonHeaders.put("Accept", Arrays.asList("application/json"));
77 commonHeaders.put(Headers.FROM_APP_ID, Arrays.asList("AAI-UI"));
80 public SearchServiceConfig getSasConfig() {
84 public void setSasConfig(SearchServiceConfig sasConfig) {
85 this.sasConfig = sasConfig;
88 public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
89 org.openecomp.restclient.client.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) {
95 org.openecomp.restclient.client.OperationResult or =
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 org.openecomp.restclient.client.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) {
108 org.openecomp.restclient.client.OperationResult or =
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 Map 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)));