[SDC-29] Amdocs OnBoard 1707 initial commit.
[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 javax.net.ssl.SSLContext;
40 import javax.net.ssl.TrustManagerFactory;
41
42 public class CassandraSessionFactory {
43   public static Session getSession() {
44     return ReferenceHolder.CASSANDRA;
45   }
46
47   /**
48    * New cassandra session session.
49    *
50    * @return the session
51    */
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);
57     }
58
59     //Check if ssl
60     Boolean isSsl = CassandraUtils.isSsl();
61     if (isSsl) {
62       builder.withSSL(getSslOptions().get());
63     }
64     int port = CassandraUtils.getCassandraPort();
65     if (port > 0) {
66       builder.withPort(port);
67     }
68     //Check if user/pass
69     Boolean isAuthenticate = CassandraUtils.isAuthenticate();
70     if (isAuthenticate) {
71       builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword());
72     }
73     Cluster cluster = builder.build();
74     String keyStore = CassandraUtils.getKeySpace();
75     return cluster.connect(keyStore);
76   }
77
78   private static Optional<SSLOptions> getSslOptions() {
79     Optional<String> truststorePath = Optional.of(CassandraUtils.getTruststore());
80     Optional<String> truststorePassword = Optional.of(CassandraUtils.getTruststorePassword());
81
82     if (truststorePath.isPresent() && truststorePassword.isPresent()) {
83       SSLContext context;
84       try {
85         context = getSslContext(truststorePath.get(), truststorePassword.get());
86       } catch (UnrecoverableKeyException | KeyManagementException
87           | NoSuchAlgorithmException | KeyStoreException | CertificateException
88           | IOException exception) {
89         throw new RuntimeException(exception);
90       }
91       String[] css = new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"};
92       return Optional.of(new SSLOptions(context, css));
93     }
94     return Optional.absent();
95   }
96
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;
102     try {
103       tsf = new FileInputStream(truststorePath);
104       ctx = SSLContext.getInstance("SSL");
105
106       KeyStore ts = KeyStore.getInstance("JKS");
107       ts.load(tsf, truststorePassword.toCharArray());
108       TrustManagerFactory tmf =
109           TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
110       tmf.init(ts);
111
112       ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
113     } catch (Exception exception) {
114       exception.printStackTrace();
115     } finally {
116       tsf.close();
117
118     }
119     return ctx;
120   }
121
122   private static class ReferenceHolder {
123     private static final Session CASSANDRA = newCassandraSession();
124   }
125
126
127 }