Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / elasticsearch / SearchAdapter.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
26 package org.openecomp.sparky.dal.elasticsearch;
27
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import javax.ws.rs.core.MediaType;
34
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;
41 import org.slf4j.MDC;
42
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;
47
48 import org.openecomp.cl.mdc.MdcContext;
49
50 /**
51  * The Class SearchAdapter.
52  */
53 public class SearchAdapter {
54
55   private static final Logger LOG = LoggerFactory.getInstance().getLogger(SearchAdapter.class);
56   
57   private RestClient client;
58
59   private Map<String, List<String>> commonHeaders;
60   private SearchServiceConfig sasConfig;
61
62   /**
63    * Instantiates a new search adapter.
64    * @throws Exception 
65    */
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());
74
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"));
78   }
79
80   public SearchServiceConfig getSasConfig() {
81     return sasConfig;
82   }
83
84   public void setSasConfig(SearchServiceConfig sasConfig) {
85     this.sasConfig = sasConfig;
86   }
87
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());
92   }
93
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());
98   }
99
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());
104   }
105
106   public OperationResult doDelete(String url, String acceptContentType) {
107
108     org.openecomp.restclient.client.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     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)));
118     return headers;
119   }
120
121
122 }