push addional code
[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
29 import org.openecomp.core.nosqldb.util.CassandraUtils;
30
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;
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     Boolean isSsl = CassandraUtils.isSsl();
60     if (isSsl) {
61       builder.withSSL(getSslOptions().get());
62     }
63     int port = CassandraUtils.getCassandraPort();
64     if (port > 0) {
65       builder.withPort(port);
66     }
67     //Check if user/pass
68     Boolean isAuthenticate = CassandraUtils.isAuthenticate();
69     if (isAuthenticate) {
70       builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword());
71     }
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 | IOException e0) {
88         throw new RuntimeException(e0);
89       }
90       String[] css = new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"};
91       return Optional.of(new SSLOptions(context, css));
92     }
93     return Optional.absent();
94   }
95
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;
101     try {
102       tsf = new FileInputStream(truststorePath);
103       ctx = SSLContext.getInstance("SSL");
104
105       KeyStore ts = KeyStore.getInstance("JKS");
106       ts.load(tsf, truststorePassword.toCharArray());
107       TrustManagerFactory tmf =
108           TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
109       tmf.init(ts);
110
111       ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
112     } catch (Exception e0) {
113       e0.printStackTrace();
114     } finally {
115       tsf.close();
116
117     }
118     return ctx;
119   }
120
121   private static class ReferenceHolder {
122     private static final Session CASSANDRA = newCassandraSession();
123   }
124 }