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=========================================================
19 * Modifications copyright (c) 2018 Nokia
20 * ================================================================================
22 package org.openecomp.sdc.be.dao.cassandra.schema;
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;
31 import java.util.List;
32 import java.util.function.Supplier;
34 public class SdcSchemaUtils {
36 private static Logger log = Logger.getLogger(SdcSchemaUtils.class.getName());
39 * the method creates the cluster object using the supplied cassandra nodes
40 * in the configuration
42 * @return cluster object our null in case of an invalid configuration
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.");
52 log.info("connecting to node:{} port{}.", nodes, cassandraPort);
53 Cluster.Builder clusterBuilder = Cluster.builder();
54 nodes.forEach(node -> clusterBuilder.addContactPoint(node).withPort(cassandraPort));
56 clusterBuilder.withMaxSchemaAgreementWaitSeconds(60);
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.");
65 clusterBuilder.withCredentials(username, password);
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.");
74 System.setProperty("javax.net.ssl.trustStore", truststorePath);
75 System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
76 clusterBuilder.withSSL();
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);
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);
89 clusterBuilder.withSocketOptions(socketOptions);
90 return clusterBuilder.build();
93 public boolean executeStatement(String statement) {
94 return executeStatement(this::createCluster, statement);
97 public boolean executeStatements(String ... statements) {
98 return executeStatements(this::createCluster, statements);
101 boolean executeStatement(Supplier<Cluster> clusterSupplier, String statement) {
102 return executeStatements(clusterSupplier, statement);
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);
112 } catch (RuntimeException e) {
113 log.error("could not execute statements", e);
118 Configuration.CassandrConfig getCassandraConfig() {
119 return ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig();