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