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.datastax.driver.core.Cluster;
24 import com.datastax.driver.core.SSLOptions;
25 import com.datastax.driver.core.Session;
26 import com.google.common.base.Optional;
27 import org.openecomp.core.nosqldb.util.CassandraUtils;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
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 {
45 private static final Logger log = (Logger) LoggerFactory.getLogger
46 (CassandraSessionFactory.class.getName());
48 public static Session getSession() {
49 return ReferenceHolder.CASSANDRA;
53 * New cassandra session session.
57 public static Session newCassandraSession() {
58 Cluster.Builder builder = Cluster.builder();
59 String[] addresses = CassandraUtils.getAddresses();
60 for (String address : addresses) {
61 builder.addContactPoint(address);
65 Boolean isSsl = CassandraUtils.isSsl();
67 builder.withSSL(getSslOptions().get());
69 int port = CassandraUtils.getCassandraPort();
71 builder.withPort(port);
74 Boolean isAuthenticate = CassandraUtils.isAuthenticate();
76 builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword());
78 Cluster cluster = builder.build();
79 String keyStore = CassandraUtils.getKeySpace();
80 return cluster.connect(keyStore);
83 private static Optional<SSLOptions> getSslOptions() {
84 Optional<String> truststorePath = Optional.of(CassandraUtils.getTruststore());
85 Optional<String> truststorePassword = Optional.of(CassandraUtils.getTruststorePassword());
87 if (truststorePath.isPresent() && truststorePassword.isPresent()) {
90 context = getSslContext(truststorePath.get(), truststorePassword.get());
91 } catch (UnrecoverableKeyException | KeyManagementException
92 | NoSuchAlgorithmException | KeyStoreException | CertificateException
93 | IOException exception) {
94 throw new RuntimeException(exception);
96 String[] css = new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"};
97 return Optional.of(new SSLOptions(context, css));
99 return Optional.absent();
102 private static SSLContext getSslContext(String truststorePath, String truststorePassword)
103 throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException,
104 UnrecoverableKeyException, KeyManagementException {
105 FileInputStream tsf = null;
106 SSLContext ctx = null;
108 tsf = new FileInputStream(truststorePath);
109 ctx = SSLContext.getInstance("SSL");
111 KeyStore ts = KeyStore.getInstance("JKS");
112 ts.load(tsf, truststorePassword.toCharArray());
113 TrustManagerFactory tmf =
114 TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
117 ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
118 } catch (Exception exception) {
119 log.debug("",exception);
128 private static class ReferenceHolder {
129 private static final Session CASSANDRA = newCassandraSession();