Changing the license and trademark
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / elasticsearch / SearchAdapter.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.openecomp.sparky.dal.elasticsearch;
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.openecomp.cl.api.Logger;
33 import org.openecomp.cl.eelf.LoggerFactory;
34 import org.openecomp.sparky.dal.rest.OperationResult;
35 import org.openecomp.sparky.dal.sas.config.SearchServiceConfig;
36 import org.openecomp.sparky.util.Encryptor;
37 import org.openecomp.sparky.viewandinspect.config.TierSupportUiConstants;
38 import org.slf4j.MDC;
39
40 import org.openecomp.restclient.client.RestClient;
41 import org.openecomp.restclient.enums.RestAuthenticationMode;
42 import org.openecomp.restclient.client.Headers;
43 import org.openecomp.cl.mdc.MdcContext;
44
45 import org.openecomp.cl.mdc.MdcContext;
46
47 /**
48  * The Class SearchAdapter.
49  */
50 public class SearchAdapter {
51
52   private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchAdapter.class);
53   
54   private RestClient client;
55
56   private Map<String, List<String>> commonHeaders;
57   private SearchServiceConfig sasConfig;
58
59   /**
60    * Instantiates a new search adapter.
61    * @throws Exception 
62    */
63   public SearchAdapter() throws Exception {
64     sasConfig = SearchServiceConfig.getConfig();
65     Encryptor encryptor = new Encryptor();
66     client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT)
67         .validateServerHostname(false).validateServerCertChain(false)
68         .clientCertFile(TierSupportUiConstants.CONFIG_AUTH_LOCATION + sasConfig.getCertName())
69         .clientCertPassword(encryptor.decryptValue(sasConfig.getKeystorePassword()))
70         .trustStore(TierSupportUiConstants.CONFIG_AUTH_LOCATION + sasConfig.getKeystore());
71
72     commonHeaders = new HashMap<String, List<String>>();
73     commonHeaders.put("Accept", Arrays.asList("application/json"));
74     commonHeaders.put(Headers.FROM_APP_ID, Arrays.asList("AAI-UI"));
75   }
76
77   public SearchServiceConfig getSasConfig() {
78     return sasConfig;
79   }
80
81   public void setSasConfig(SearchServiceConfig sasConfig) {
82     this.sasConfig = sasConfig;
83   }
84
85   public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
86     org.openecomp.restclient.client.OperationResult or = client.post(url, jsonPayload, getTxnHeader(),
87         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
88     return new OperationResult(or.getResultCode(), or.getResult());
89   }
90
91   public OperationResult doGet(String url, String acceptContentType) {
92     org.openecomp.restclient.client.OperationResult or =
93         client.get(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
94     return new OperationResult(or.getResultCode(), or.getResult());
95   }
96
97   public OperationResult doPut(String url, String payload, String acceptContentType) {
98     org.openecomp.restclient.client.OperationResult or = client.put(url, payload, getTxnHeader(),
99         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
100     return new OperationResult(or.getResultCode(), or.getResult());
101   }
102
103   public OperationResult doDelete(String url, String acceptContentType) {
104
105     org.openecomp.restclient.client.OperationResult or =
106         client.delete(url, getTxnHeader(), MediaType.APPLICATION_JSON_TYPE);
107     return new OperationResult(or.getResultCode(), or.getResult());
108   }
109
110   public Map<String, List<String>> getTxnHeader() {
111     Map headers = new HashMap<String, List<String>>();
112     headers.putAll(this.commonHeaders);
113     headers.put("X-TransactionId", Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
114     headers.put("X-FromAppId", Arrays.asList(MDC.get(MdcContext.MDC_PARTNER_NAME)));
115     return headers;
116   }
117
118
119 }