aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java
blob: f8734d5a667fafbea60bfad57ba481ad5a23b79a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.common.database;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.spec.InvalidKeySpecException;
import java.text.ParseException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.json.JSONException;
import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol;
import org.onap.ccsdk.features.sdnr.wt.common.database.data.EsVersion;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterHealthRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterSettingsRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateAliasRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateIndexRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.Delete7Request;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteAliasRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteIndexRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.Get7Request;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetIndexRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetInfoRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.Index7Request;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.IndexRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ListAliasesRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ListIndicesRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.NodeStatsRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.RefreshIndexRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.Search7Request;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.Update7Request;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.UpdateByQuery7Request;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.UpdateByQueryRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.requests.UpdateRequest;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterHealthResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterSettingsResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.CreateAliasResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.CreateIndexResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteAliasResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteByQueryResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteIndexResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.GetInfoResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.GetResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.IndexResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ListAliasesResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ListIndicesResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.NodeStatsResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.RefreshIndexResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.UpdateByQueryResponse;
import org.onap.ccsdk.features.sdnr.wt.common.database.responses.UpdateResponse;
import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExtRestClient {

    private static final Logger LOG = LoggerFactory.getLogger(ExtRestClient.class);

    private class BasicAuthHttpClientConfigCallback implements HttpClientConfigCallback {

        private final String basicAuthUsername;
        private final String basicAuthPassword;
        private final boolean trustAll;

        BasicAuthHttpClientConfigCallback(String username, String password, boolean trustAll) {
            this.basicAuthUsername = username;
            this.basicAuthPassword = password;
            this.trustAll = trustAll;
        }

        @Override
        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
            HttpAsyncClientBuilder httpAsyncClientBuilder = null;
            try {
                httpAsyncClientBuilder = httpClientBuilder.setSSLContext(BaseHTTPClient.setupSsl(this.trustAll));
                if (this.trustAll) {
                    httpAsyncClientBuilder.setSSLHostnameVerifier(new HostnameVerifier() {

                        @Override
                        public boolean verify(String hostname, SSLSession session) {
                            return true;
                        }
                    });
                }
            } catch (NoSuchAlgorithmException | KeyManagementException | UnrecoverableKeyException
                    | CertificateException | KeyStoreException | InvalidKeySpecException | IOException e) {
                LOG.warn("unable to init ssl context for db client: {}", e.getMessage());
            }
            if (basicAuthPassword == null || basicAuthUsername == null) {
                return httpAsyncClientBuilder;
            }
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(basicAuthUsername, basicAuthPassword));

            return httpAsyncClientBuilder == null ? httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
                    : httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

    }
    //    private class SSLCercAuthHttpClientConfigCallback implements HttpClientConfigCallback {
    //
    //        private final String certFilename;
    //
    //        SSLCercAuthHttpClientConfigCallback(String certfile) {
    //            this.certFilename = certfile;
    //        }
    //
    //        @Override
    //        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
    //            if (this.certFilename == null) {
    //                return httpClientBuilder;
    //            }
    //
    //            char[] keystorePass = "MY PASSWORD".toCharArray();
    //
    //            FileInputStream fis = null;
    //
    //            // Loading KEYSTORE in JKS format
    //            KeyStore keyStorePci = null;
    //            try {
    //                keyStorePci = KeyStore.getInstance(KeyStore.getDefaultType());
    //            } catch (KeyStoreException e1) {
    //                LOG.warn("unable to load keystore: {}",e1);
    //            }
    //            if (keyStorePci != null) {
    //                try {
    //                    fis = new FileInputStream(this.certFilename);
    //                    keyStorePci.load(fis, keystorePass);
    //                } catch (Exception e) {
    //                    LOG.error("Error loading keystore: " + this.certFilename);
    //                } finally {
    //                    if (fis != null) {
    //                        try {
    //                            fis.close();
    //                        } catch (IOException e) {
    //
    //                        }
    //                    }
    //                }
    //            }
    //            SSLContext sslcontext=null;
    //            try {
    //                sslcontext = SSLContexts.custom().loadKeyMaterial(keyStorePci, keystorePass).build();
    //            } catch (KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException
    //                    | KeyStoreException e) {
    //                LOG.warn("unable to load sslcontext: {}",e);
    //            }
    //            return httpClientBuilder.setSSLContext(sslcontext);
    //        }
    //    }

    private final RestClient client;
    private final boolean isES7;

    protected ExtRestClient(HostInfo[] hosts) throws Exception {
        this(hosts, null, null, false);
    }

    protected ExtRestClient(HostInfo[] hosts, String username, String password, boolean trustAll) throws Exception {
        this.client = RestClient.builder(get(hosts))
                .setHttpClientConfigCallback(new BasicAuthHttpClientConfigCallback(username, password, trustAll))
                .build();
        EsVersion tmp = autoDetectVersion();
        LOG.info("working with sdnrdb version {}", tmp.toString());
        this.isES7 = tmp.isNewerOrEqualThan(new EsVersion(7, 0, 0));
    }

    /**
     * @return
     * @throws IOException
     * @throws Exception
     */
    private EsVersion autoDetectVersion() throws IOException, Exception {
        GetInfoResponse infoResponse = this.getInfo();
        return infoResponse.getVersion();

    }

    public boolean isVersion7() {
        return this.isES7;
    }

    public ClusterHealthResponse health(ClusterHealthRequest request)
            throws UnsupportedOperationException, IOException, JSONException {
        return new ClusterHealthResponse(this.client.performRequest(request.getInner()));
    }

    public void close() throws IOException {
        this.client.close();

    }

    public boolean indicesExists(GetIndexRequest request) throws IOException {
        Response response = this.client.performRequest(request.getInner());
        return response.getStatusLine().getStatusCode() == 200;
    }

    public ClusterSettingsResponse setupClusterSettings(ClusterSettingsRequest request) throws IOException {
        return new ClusterSettingsResponse(this.client.performRequest(request.getInner()));
    }

    public CreateAliasResponse updateAliases(CreateAliasRequest request) throws IOException {
        return new CreateAliasResponse(this.client.performRequest(request.getInner()));
    }

    public CreateIndexResponse createIndex(CreateIndexRequest request) throws IOException {
        return new CreateIndexResponse(this.client.performRequest(request.getInner()));
    }

    public CreateAliasResponse createAlias(CreateAliasRequest request) throws IOException {
        return new CreateAliasResponse(this.client.performRequest(request.getInner()));
    }

    public DeleteAliasResponse deleteAlias(DeleteAliasRequest request) throws IOException {
        return new DeleteAliasResponse(this.client.performRequest(request.getInner()));
    }

    public DeleteIndexResponse deleteIndex(DeleteIndexRequest request) throws IOException {
        return new DeleteIndexResponse(this.client.performRequest(request.getInner()));
    }

    public IndexResponse index(IndexRequest request) throws IOException {
        if (this.isES7 && !(request instanceof Index7Request)) {
            request = new Index7Request(request);
        }
        return new IndexResponse(this.client.performRequest(request.getInner()));
    }

    public DeleteResponse delete(DeleteRequest request) throws IOException {
        Response response = null;
        if (this.isES7 && !(request instanceof Delete7Request)) {
            request = new Delete7Request(request);
        }
        try {
            response = this.client.performRequest(request.getInner());
        } catch (ResponseException e) {
            new DeleteResponse(e.getResponse());
        }
        return new DeleteResponse(response);
    }

    public DeleteByQueryResponse deleteByQuery(DeleteByQueryRequest request) throws IOException {
        Response response = null;
        try {
            response = this.client.performRequest(request.getInner());
        } catch (ResponseException e) {
            new DeleteResponse(e.getResponse());
        }
        return new DeleteByQueryResponse(response);
    }

    public SearchResponse search(SearchRequest request) throws IOException {
        return this.search(request, false);
    }

    /**
     * Search for database entries
     * 
     * @param request inputRequest
     * @param ignoreParseException especially for usercreated filters which may cause ES server response exceptions
     * @return Response with related entries
     * @throws IOException of client
     */
    public SearchResponse search(SearchRequest request, boolean ignoreParseException) throws IOException {
        if (this.isES7 && !(request instanceof Search7Request)) {
            request = new Search7Request(request);
        }
        if (ignoreParseException) {
            try {
                return new SearchResponse(this.client.performRequest(request.getInner()));
            } catch (ResponseException e) {
                LOG.debug("ignoring Exception for request {}: {}", request, e.getMessage());
                return new SearchResponse(e.getResponse());
            }
        } else {
            return new SearchResponse(this.client.performRequest(request.getInner()));
        }
    }

    public GetResponse get(GetRequest request) throws IOException {
        if (this.isES7 && !(request instanceof Get7Request)) {
            request = new Get7Request(request);
        }
        try {
            return new GetResponse(this.client.performRequest(request.getInner()));
        } catch (ResponseException e) {
            return new GetResponse(e.getResponse());
        }
    }

    public UpdateByQueryResponse update(UpdateByQueryRequest request) throws IOException {
        if (this.isES7 && !(request instanceof UpdateByQuery7Request)) {
            request = new UpdateByQuery7Request(request);
        }
        return new UpdateByQueryResponse(this.client.performRequest(request.getInner()));

    }

    public UpdateResponse update(UpdateRequest request) throws IOException {
        if (this.isES7 && !(request instanceof Update7Request)) {
            request = new Update7Request(request);
        }
        return new UpdateResponse(this.client.performRequest(request.getInner()));

    }

    public RefreshIndexResponse refreshIndex(RefreshIndexRequest request) throws IOException {
        return new RefreshIndexResponse(this.client.performRequest(request.getInner()));
    }

    public NodeStatsResponse stats(NodeStatsRequest request) throws IOException {
        return new NodeStatsResponse(this.client.performRequest(request.getInner()));
    }

    public ListIndicesResponse getIndices() throws ParseException, IOException {
        return new ListIndicesResponse(this.client.performRequest(new ListIndicesRequest().getInner()));
    }

    public ListAliasesResponse getAliases() throws ParseException, IOException {
        return new ListAliasesResponse(this.client.performRequest(new ListAliasesRequest().getInner()));
    }

    public GetInfoResponse getInfo() throws IOException, Exception {
        return new GetInfoResponse(this.client.performRequest(new GetInfoRequest().getInner()));
    }

    public boolean waitForYellowStatus(long timeoutms) {

        ClusterHealthRequest request = new ClusterHealthRequest();
        request.timeout(timeoutms / 1000);
        ClusterHealthResponse response = null;
        String status = "";
        try {
            response = this.health(request);

        } catch (UnsupportedOperationException | IOException | JSONException e) {
            LOG.error("Exception", e);
        }
        if (response != null) {
            status = response.getStatus();
            LOG.debug("Elasticsearch service started with status {}", response.getStatus());

        } else {
            LOG.warn("Elasticsearch service not started yet with status {}. current status is {}", status, "none");
            return false;
        }
        return response.isStatusMinimal(ClusterHealthResponse.HEALTHSTATUS_YELLOW);

    }

    private static HttpHost[] get(HostInfo[] hosts) {
        HttpHost[] httphosts = new HttpHost[hosts.length];
        for (int i = 0; i < hosts.length; i++) {
            httphosts[i] = new HttpHost(hosts[i].hostname, hosts[i].port, hosts[i].protocol.toString());
        }
        return httphosts;
    }

    public static ExtRestClient createInstance(HostInfo[] hosts) throws Exception {
        return new ExtRestClient(hosts);
    }

    public static ExtRestClient createInstance(HostInfo[] hosts, String username, String password, boolean trustAll)
            throws Exception {
        return new ExtRestClient(hosts, username, password, trustAll);
    }

    public static ExtRestClient createInstance(String hostname, int port, Protocol protocol) throws Exception {
        return createInstance(new HostInfo[] {new HostInfo(hostname, port, protocol)});

    }

}