2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.nosqldb.impl.cassandra;
23 import com.google.common.base.Optional;
25 import com.datastax.driver.core.Cluster;
26 import com.datastax.driver.core.SSLOptions;
27 import com.datastax.driver.core.Session;
28 import org.openecomp.core.nosqldb.util.CassandraUtils;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.security.KeyManagementException;
33 import java.security.KeyStore;
34 import java.security.KeyStoreException;
35 import java.security.NoSuchAlgorithmException;
36 import java.security.SecureRandom;
37 import java.security.UnrecoverableKeyException;
38 import java.security.cert.CertificateException;
39 import javax.net.ssl.SSLContext;
40 import javax.net.ssl.TrustManagerFactory;
42 public class CassandraSessionFactory {
43 public static Session getSession() {
44 return ReferenceHolder.CASSANDRA;
48 * New cassandra session session.
52 public static Session newCassandraSession() {
53 Cluster.Builder builder = Cluster.builder();
54 String[] addresses = CassandraUtils.getAddresses();
55 for (String address : addresses) {
56 builder.addContactPoint(address);
60 Boolean isSsl = CassandraUtils.isSsl();
62 builder.withSSL(getSslOptions().get());
64 int port = CassandraUtils.getCassandraPort();
66 builder.withPort(port);
69 Boolean isAuthenticate = CassandraUtils.isAuthenticate();
71 builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword());
73 Cluster cluster = builder.build();
74 String keyStore = CassandraUtils.getKeySpace();
75 return cluster.connect(keyStore);
78 private static Optional<SSLOptions> getSslOptions() {
79 Optional<String> truststorePath = Optional.of(CassandraUtils.getTruststore());
80 Optional<String> truststorePassword = Optional.of(CassandraUtils.getTruststorePassword());
82 if (truststorePath.isPresent() && truststorePassword.isPresent()) {
85 context = getSslContext(truststorePath.get(), truststorePassword.get());
86 } catch (UnrecoverableKeyException | KeyManagementException
87 | NoSuchAlgorithmException | KeyStoreException | CertificateException
88 | IOException exception) {
89 throw new RuntimeException(exception);
91 String[] css = new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"};
92 return Optional.of(new SSLOptions(context, css));
94 return Optional.absent();
97 private static SSLContext getSslContext(String truststorePath, String truststorePassword)
98 throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException,
99 UnrecoverableKeyException, KeyManagementException {
100 FileInputStream tsf = null;
101 SSLContext ctx = null;
103 tsf = new FileInputStream(truststorePath);
104 ctx = SSLContext.getInstance("SSL");
106 KeyStore ts = KeyStore.getInstance("JKS");
107 ts.load(tsf, truststorePassword.toCharArray());
108 TrustManagerFactory tmf =
109 TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
112 ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
113 } catch (Exception exception) {
114 exception.printStackTrace();
122 private static class ReferenceHolder {
123 private static final Session CASSANDRA = newCassandraSession();