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=========================================================
21 package org.openecomp.sdc.be.dao.cassandra.schema;
23 import com.datastax.driver.core.Cluster;
24 import com.datastax.driver.core.Session;
25 import com.datastax.driver.core.SocketOptions;
26 import org.openecomp.sdc.be.config.ConfigurationManager;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
29 import java.util.List;
31 public class SdcSchemaUtils {
33 private static Logger log = Logger.getLogger(SdcSchemaUtils.class.getName());
36 * the method creates the cluster object using the supplied cassandra nodes
37 * in the configuration
39 * @return cluster object our null in case of an invalid configuration
41 public static Cluster createCluster() {
42 List<String> nodes = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getCassandraHosts();
44 log.info("no nodes were supplied in configuration.");
47 log.info("connecting to node:{}.", nodes);
48 Cluster.Builder clusterBuilder = Cluster.builder();
49 nodes.forEach(clusterBuilder::addContactPoint);
51 clusterBuilder.withMaxSchemaAgreementWaitSeconds(60);
53 boolean authenticate = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isAuthenticate();
55 String username = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getUsername();
56 String password = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getPassword();
57 if (username == null || password == null) {
58 log.info("authentication is enabled but username or password were not supplied.");
61 clusterBuilder.withCredentials(username, password);
63 boolean ssl = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isSsl();
65 String truststorePath = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePath();
66 String truststorePassword = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePassword();
67 if (truststorePath == null || truststorePassword == null) {
68 log.info("ssl is enabled but truststorePath or truststorePassword were not supplied.");
71 System.setProperty("javax.net.ssl.trustStore", truststorePath);
72 System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
73 clusterBuilder.withSSL();
75 SocketOptions socketOptions =new SocketOptions();
76 Integer socketConnectTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketConnectTimeout();
77 if( socketConnectTimeout!=null ){
78 log.info("SocketConnectTimeout was provided, setting Cassandra client to use SocketConnectTimeout: {} .",socketConnectTimeout);
79 socketOptions.setConnectTimeoutMillis(socketConnectTimeout);
81 Integer socketReadTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketReadTimeout();
82 if( socketReadTimeout != null ){
83 log.info("SocketReadTimeout was provided, setting Cassandra client to use SocketReadTimeout: {} .",socketReadTimeout);
84 socketOptions.setReadTimeoutMillis(socketReadTimeout);
86 clusterBuilder.withSocketOptions(socketOptions);
87 return clusterBuilder.build();
90 public static boolean executeStatement(String statement) {
91 return executeStatements(statement);
94 public static boolean executeStatements(String ... statements) {
95 Cluster cluster = null;
96 Session session = null;
98 cluster = createCluster();
99 if (cluster == null) {
102 session = cluster.connect();
103 for (String statement : statements) {
104 session.execute(statement);
107 } catch (RuntimeException e) {
108 log.error(String.format("could not execute statements"), e);
111 if (session != null) {
114 if (cluster != null) {