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 java.util.List;
25 import com.datastax.driver.core.SocketOptions;
26 import org.openecomp.sdc.be.config.ConfigurationManager;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.datastax.driver.core.Cluster;
31 import com.datastax.driver.core.Session;
33 public class SdcSchemaUtils {
35 private static Logger log = LoggerFactory.getLogger(SdcSchemaUtils.class.getName());
38 * the method creates the cluster object using the supplied cassandra nodes
39 * in the configuration
41 * @return cluster object our null in case of an invalid configuration
43 public static Cluster createCluster() {
44 List<String> nodes = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getCassandraHosts();
46 log.info("no nodes were supplied in configuration.");
49 log.info("connecting to node:{}.", nodes);
50 Cluster.Builder clusterBuilder = Cluster.builder();
51 nodes.forEach(host -> clusterBuilder.addContactPoint(host));
53 clusterBuilder.withMaxSchemaAgreementWaitSeconds(60);
55 boolean authenticate = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isAuthenticate();
57 String username = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getUsername();
58 String password = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getPassword();
59 if (username == null || password == null) {
60 log.info("authentication is enabled but username or password were not supplied.");
63 clusterBuilder.withCredentials(username, password);
65 boolean ssl = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isSsl();
67 String truststorePath = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePath();
68 String truststorePassword = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePassword();
69 if (truststorePath == null || truststorePassword == null) {
70 log.info("ssl is enabled but truststorePath or truststorePassword were not supplied.");
73 System.setProperty("javax.net.ssl.trustStore", truststorePath);
74 System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
75 clusterBuilder.withSSL();
77 SocketOptions socketOptions =new SocketOptions();
78 Integer socketConnectTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketConnectTimeout();
79 if( socketConnectTimeout!=null ){
80 log.info("SocketConnectTimeout was provided, setting Cassandra client to use SocketConnectTimeout: {} .",socketConnectTimeout);
81 socketOptions.setConnectTimeoutMillis(socketConnectTimeout);
83 Integer socketReadTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketReadTimeout();
84 if( socketReadTimeout != null ){
85 log.info("SocketReadTimeout was provided, setting Cassandra client to use SocketReadTimeout: {} .",socketReadTimeout);
86 socketOptions.setReadTimeoutMillis(socketReadTimeout);
88 clusterBuilder.withSocketOptions(socketOptions);
89 return clusterBuilder.build();
92 public static boolean executeStatement(String statement) {
93 return executeStatements(statement);
96 public static boolean executeStatements(String ... statements) {
97 Cluster cluster = null;
98 Session session = null;
100 cluster = createCluster();
101 if (cluster == null) {
104 session = cluster.connect();
105 for (String statement : statements) {
106 session.execute(statement);
109 } catch (RuntimeException e) {
110 log.error(String.format("could not execute statements"), e);
113 if (session != null) {
116 if (cluster != null) {