changed the header license to new license
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / elasticsearch / SearchAdapter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.sparky.dal.elasticsearch;
22
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.ws.rs.core.MediaType;
29
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
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.sas.config.SearchServiceConfig;
37 import org.onap.aai.sparky.util.Encryptor;
38 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
39 import org.slf4j.MDC;
40
41
42 /**
43  * The Class SearchAdapter.
44  */
45 public class SearchAdapter {
46
47   private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchAdapter.class);
48
49   private RestClient client;
50
51   private Map<String, List<String>> commonHeaders;
52   private SearchServiceConfig sasConfig;
53
54   /**
55    * Instantiates a new search adapter.
56    * 
57    * @throws Exception
58    */
59   public SearchAdapter() throws Exception {
60     sasConfig = SearchServiceConfig.getConfig();
61     Encryptor encryptor = new Encryptor();
62
63     client = new RestClient().validateServerHostname(false).validateServerCertChain(false)
64         .clientCertFile(SparkyConstants.CONFIG_AUTH_LOCATION + sasConfig.getCertName())
65         .clientCertPassword(encryptor.decryptValue(sasConfig.getKeystorePassword()))
66         .trustStore(SparkyConstants.CONFIG_AUTH_LOCATION + sasConfig.getKeystore());
67
68     commonHeaders = new HashMap<String, List<String>>();
69     commonHeaders.put("Accept", Arrays.asList("application/json"));
70     commonHeaders.put(Headers.FROM_APP_ID, Arrays.asList("AAI-UI"));
71   }
72
73   public SearchServiceConfig getSasConfig() {
74     return sasConfig;
75   }
76
77   public void setSasConfig(SearchServiceConfig sasConfig) {
78     this.sasConfig = sasConfig;
79   }
80
81   public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
82     OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
83         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
84     return new OperationResult(or.getResultCode(), or.getResult());
85   }
86
87   public OperationResult doGet(String url, String acceptContentType) {
88     OperationResult or = client.get(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
89     return new OperationResult(or.getResultCode(), or.getResult());
90   }
91
92   public OperationResult doPut(String url, String payload, String acceptContentType) {
93     OperationResult or = client.put(url, payload, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE,
94         MediaType.APPLICATION_JSON_TYPE);
95     return new OperationResult(or.getResultCode(), or.getResult());
96   }
97
98   public OperationResult doDelete(String url, String acceptContentType) {
99
100     OperationResult or = client.delete(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
101     return new OperationResult(or.getResultCode(), or.getResult());
102   }
103
104   public Map<String, List<String>> getTxnHeader() {
105     Map headers = new HashMap<String, List<String>>();
106     headers.putAll(this.commonHeaders);
107     headers.put("X-TransactionId", Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
108     headers.put("X-FromAppId", Arrays.asList(MDC.get(MdcContext.MDC_PARTNER_NAME)));
109     return headers;
110   }
111
112
113 }