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;
29 import org.openecomp.core.nosqldb.util.CassandraUtils;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.security.KeyManagementException;
34 import java.security.KeyStore;
35 import java.security.KeyStoreException;
36 import java.security.NoSuchAlgorithmException;
37 import java.security.SecureRandom;
38 import java.security.UnrecoverableKeyException;
39 import java.security.cert.CertificateException;
40 import javax.net.ssl.SSLContext;
41 import javax.net.ssl.TrustManagerFactory;
43 public class CassandraSessionFactory {
44 public static Session getSession() {
45 return ReferenceHolder.CASSANDRA;
49 * New cassandra session session.
53 public static Session newCassandraSession() {
54 Cluster.Builder builder = Cluster.builder();
55 String[] addresses = CassandraUtils.getAddresses();
56 for (String address : addresses) {
57 builder.addContactPoint(address);
59 Boolean isSsl = CassandraUtils.isSsl();
61 builder.withSSL(getSslOptions().get());
63 int port = CassandraUtils.getCassandraPort();
65 builder.withPort(port);
68 Boolean isAuthenticate = CassandraUtils.isAuthenticate();
70 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 | IOException e0) {
88 throw new RuntimeException(e0);
90 String[] css = new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"};
91 return Optional.of(new SSLOptions(context, css));
93 return Optional.absent();
96 private static SSLContext getSslContext(String truststorePath, String truststorePassword)
97 throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException,
98 UnrecoverableKeyException, KeyManagementException {
99 FileInputStream tsf = null;
100 SSLContext ctx = null;
102 tsf = new FileInputStream(truststorePath);
103 ctx = SSLContext.getInstance("SSL");
105 KeyStore ts = KeyStore.getInstance("JKS");
106 ts.load(tsf, truststorePassword.toCharArray());
107 TrustManagerFactory tmf =
108 TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
111 ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
112 } catch (Exception e0) {
113 e0.printStackTrace();
121 private static class ReferenceHolder {
122 private static final Session CASSANDRA = newCassandraSession();