[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-nosqldb-lib / openecomp-nosqldb-core / src / main / java / org / openecomp / core / nosqldb / impl / cassandra / CassandraSessionFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.nosqldb.impl.cassandra;
22
23 import com.google.common.base.Optional;
24
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;
29
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 java.util.Objects;
40 import javax.net.ssl.SSLContext;
41 import javax.net.ssl.TrustManagerFactory;
42
43 public class CassandraSessionFactory {
44   public static Session getSession() {
45     return ReferenceHolder.CASSANDRA;
46   }
47
48   /**
49    * New cassandra session session.
50    *
51    * @return the session
52    */
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);
58     }
59
60     //Check if ssl
61     Boolean isSsl = CassandraUtils.isSsl();
62     if (isSsl) {
63       builder.withSSL(getSslOptions().get());
64     }
65     int port = CassandraUtils.getCassandraPort();
66     if (port > 0) {
67       builder.withPort(port);
68     }
69     //Check if user/pass
70     Boolean isAuthenticate = CassandraUtils.isAuthenticate();
71     if (isAuthenticate) {
72       builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword());
73     }
74     Cluster cluster = builder.build();
75     String keyStore = CassandraUtils.getKeySpace();
76     return cluster.connect(keyStore);
77   }
78
79   private static Optional<SSLOptions> getSslOptions() {
80     Optional<String> truststorePath = Optional.of(CassandraUtils.getTruststore());
81     Optional<String> truststorePassword = Optional.of(CassandraUtils.getTruststorePassword());
82
83     if (truststorePath.isPresent() && truststorePassword.isPresent()) {
84       SSLContext context;
85       try {
86         context = getSslContext(truststorePath.get(), truststorePassword.get());
87       } catch (UnrecoverableKeyException | KeyManagementException
88           | NoSuchAlgorithmException | KeyStoreException | CertificateException
89           | IOException exception) {
90         throw new RuntimeException(exception);
91       }
92       String[] css = new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"};
93       return Optional.of(new SSLOptions(context, css));
94     }
95     return Optional.absent();
96   }
97
98   private static SSLContext getSslContext(String truststorePath, String truststorePassword)
99       throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException,
100       UnrecoverableKeyException, KeyManagementException {
101     FileInputStream tsf = null;
102     SSLContext ctx = null;
103     try {
104       tsf = new FileInputStream(truststorePath);
105       ctx = SSLContext.getInstance("SSL");
106
107       KeyStore ts = KeyStore.getInstance("JKS");
108       ts.load(tsf, truststorePassword.toCharArray());
109       TrustManagerFactory tmf =
110           TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
111       tmf.init(ts);
112
113       ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
114     } catch (Exception exception) {
115       exception.printStackTrace();
116     } finally {
117       if (tsf != null) {
118         tsf.close();
119       }
120     }
121     return ctx;
122   }
123
124   private static class ReferenceHolder {
125     private static final Session CASSANDRA = newCassandraSession();
126   }
127
128
129 }