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 org.openecomp.sdc.be.config.ConfigurationManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.datastax.driver.core.Cluster;
30 import com.datastax.driver.core.Session;
32 public class SdcSchemaUtils {
34 private static Logger log = LoggerFactory.getLogger(SdcSchemaUtils.class.getName());
37 * the method creates the cluster object using the supplied cassandra nodes
38 * in the configuration
40 * @return cluster object our null in case of an invalid configuration
42 public static Cluster createCluster() {
43 List<String> nodes = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getCassandraHosts();
45 log.info("no nodes were supplied in configuration.");
48 log.info("connecting to node:{}.", nodes);
49 Cluster.Builder clusterBuilder = Cluster.builder();
50 nodes.forEach(host -> clusterBuilder.addContactPoint(host));
52 clusterBuilder.withMaxSchemaAgreementWaitSeconds(60);
54 boolean authenticate = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isAuthenticate();
56 String username = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getUsername();
57 String password = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getPassword();
58 if (username == null || password == null) {
59 log.info("authentication is enabled but username or password were not supplied.");
62 clusterBuilder.withCredentials(username, password);
64 boolean ssl = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isSsl();
66 String truststorePath = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePath();
67 String truststorePassword = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePassword();
68 if (truststorePath == null || truststorePassword == null) {
69 log.info("ssl is enabled but truststorePath or truststorePassword were not supplied.");
72 System.setProperty("javax.net.ssl.trustStore", truststorePath);
73 System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
74 clusterBuilder.withSSL();
76 return clusterBuilder.build();
79 public static boolean executeStatement(String statement) {
80 return executeStatements(statement);
83 public static boolean executeStatements(String ... statements) {
84 Cluster cluster = null;
85 Session session = null;
87 cluster = createCluster();
88 if (cluster == null) {
91 session = cluster.connect();
92 for (String statement : statements) {
93 session.execute(statement);
96 } catch (RuntimeException e) {
97 log.error(String.format("could not execute statements"), e);
100 if (session != null) {
103 if (cluster != null) {