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.*;
24 import org.openecomp.sdc.be.config.ConfigurationManager;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import java.util.List;
30 public class SdcSchemaUtils {
32 private static Logger log = LoggerFactory.getLogger(SdcSchemaUtils.class.getName());
35 * the method creates the cluster object using the supplied cassandra nodes
36 * in the configuration
38 * @return cluster object our null in case of an invalid configuration
40 public static Cluster createCluster() {
41 List<String> nodes = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getCassandraHosts();
43 log.info("no nodes were supplied in configuration.");
46 log.info("connecting to node:{}.", nodes);
47 Cluster.Builder clusterBuilder = Cluster.builder();
48 nodes.forEach(host -> clusterBuilder.addContactPoint(host));
50 clusterBuilder.withMaxSchemaAgreementWaitSeconds(60);
52 boolean authenticate = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isAuthenticate();
54 String username = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getUsername();
55 String password = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getPassword();
56 if (username == null || password == null) {
57 log.info("authentication is enabled but username or password were not supplied.");
60 clusterBuilder.withCredentials(username, password);
62 boolean ssl = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isSsl();
64 String truststorePath = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePath();
65 String truststorePassword = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePassword();
66 if (truststorePath == null || truststorePassword == null) {
67 log.info("ssl is enabled but truststorePath or truststorePassword were not supplied.");
70 System.setProperty("javax.net.ssl.trustStore", truststorePath);
71 System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
72 clusterBuilder.withSSL();
74 return clusterBuilder.build();
77 public static boolean executeStatement(String statement) {
78 return executeStatements(statement);
81 public static boolean executeStatements(String ... statements) {
82 Cluster cluster = null;
83 Session session = null;
85 cluster = createCluster();
86 if (cluster == null) {
89 session = cluster.connect();
90 for (String statement : statements) {
91 session.execute(statement);
94 } catch (RuntimeException e) {
95 log.error(String.format("could not execute statements"), e);
98 if (session != null) {
101 if (cluster != null) {