Make Cassandra port configurable.
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / cassandra / schema / SdcSchemaUtils.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  * Modifications copyright (c) 2018 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.be.dao.cassandra.schema;
23
24 import com.datastax.driver.core.Cluster;
25 import com.datastax.driver.core.Session;
26 import com.datastax.driver.core.SocketOptions;
27 import org.openecomp.sdc.be.config.Configuration;
28 import org.openecomp.sdc.be.config.ConfigurationManager;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30
31 import java.util.List;
32 import java.util.function.Supplier;
33
34 public class SdcSchemaUtils {
35
36     private static Logger log = Logger.getLogger(SdcSchemaUtils.class.getName());
37
38     /**
39      * the method creates the cluster object using the supplied cassandra nodes
40      * in the configuration
41      *
42      * @return cluster object our null in case of an invalid configuration
43      */
44     public Cluster createCluster() {
45         final Configuration.CassandrConfig config = getCassandraConfig();
46         List<String> nodes = config.getCassandraHosts();
47         Integer cassandraPort = config.getCassandraPort();
48         if (nodes == null || cassandraPort == null) {
49             log.info("no nodes or port were supplied in configuration.");
50             return null;
51         }
52         log.info("connecting to node:{} port{}.", nodes, cassandraPort);
53         Cluster.Builder clusterBuilder = Cluster.builder();
54         nodes.forEach(node -> clusterBuilder.addContactPoint(node).withPort(cassandraPort));
55
56         clusterBuilder.withMaxSchemaAgreementWaitSeconds(60);
57
58         if (config.isAuthenticate()) {
59             String username = config.getUsername();
60             String password = config.getPassword();
61             if (username == null || password == null) {
62                 log.info("authentication is enabled but username or password were not supplied.");
63                 return null;
64             }
65             clusterBuilder.withCredentials(username, password);
66         }
67         if (config.isSsl()) {
68             String truststorePath = config.getTruststorePath();
69             String truststorePassword = config.getTruststorePassword();
70             if (truststorePath == null || truststorePassword == null) {
71                 log.info("ssl is enabled but truststorePath or truststorePassword were not supplied.");
72                 return null;
73             }
74             System.setProperty("javax.net.ssl.trustStore", truststorePath);
75             System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
76             clusterBuilder.withSSL();
77         }
78         SocketOptions socketOptions =new SocketOptions();
79         Integer socketConnectTimeout = config.getSocketConnectTimeout();
80         if( socketConnectTimeout!=null ){
81             log.info("SocketConnectTimeout was provided, setting Cassandra client to use SocketConnectTimeout: {} .",socketConnectTimeout);
82             socketOptions.setConnectTimeoutMillis(socketConnectTimeout);
83         }
84         Integer socketReadTimeout = config.getSocketReadTimeout();
85         if( socketReadTimeout != null ){
86             log.info("SocketReadTimeout was provided, setting Cassandra client to use SocketReadTimeout: {} .",socketReadTimeout);
87             socketOptions.setReadTimeoutMillis(socketReadTimeout);
88         }
89         clusterBuilder.withSocketOptions(socketOptions);
90         return clusterBuilder.build();
91     }
92
93     public boolean executeStatement(String statement) {
94         return executeStatement(this::createCluster, statement);
95     }
96
97     public boolean executeStatements(String ... statements) {
98         return executeStatements(this::createCluster, statements);
99     }
100
101     boolean executeStatement(Supplier<Cluster> clusterSupplier, String statement) {
102         return executeStatements(clusterSupplier, statement);
103     }
104
105     boolean executeStatements(Supplier<Cluster> clusterSupplier, String ... statements) {
106         try(Cluster cluster = clusterSupplier.get();
107                 Session session = cluster.connect()) {
108             for (String statement : statements) {
109                 session.execute(statement);
110             }
111             return true;
112         } catch (RuntimeException e) {
113             log.error("could not execute statements", e);
114         }
115         return false;
116     }
117
118     Configuration.CassandrConfig getCassandraConfig() {
119         return ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig();
120     }
121
122 }