Sync Integ to Master
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / cassandra / schema / SdcSchemaUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  */
20
21 package org.openecomp.sdc.be.dao.cassandra.schema;
22
23 import com.datastax.driver.core.*;
24 import org.openecomp.sdc.be.config.ConfigurationManager;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.List;
29
30 public class SdcSchemaUtils {
31
32     private static Logger log = LoggerFactory.getLogger(SdcSchemaUtils.class.getName());
33
34     /**
35      * the method creates the cluster object using the supplied cassandra nodes
36      * in the configuration
37      *
38      * @return cluster object our null in case of an invalid configuration
39      */
40     public static Cluster createCluster() {
41         List<String> nodes = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getCassandraHosts();
42         if (nodes == null) {
43             log.info("no nodes were supplied in configuration.");
44             return null;
45         }
46         log.info("connecting to node:{}.", nodes);
47         Cluster.Builder clusterBuilder = Cluster.builder();
48         nodes.forEach(host -> clusterBuilder.addContactPoint(host));
49
50         clusterBuilder.withMaxSchemaAgreementWaitSeconds(60);
51
52         boolean authenticate = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isAuthenticate();
53         if (authenticate) {
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.");
58                 return null;
59             }
60             clusterBuilder.withCredentials(username, password);
61         }
62         boolean ssl = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isSsl();
63         if (ssl) {
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.");
68                 return null;
69             }
70             System.setProperty("javax.net.ssl.trustStore", truststorePath);
71             System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
72             clusterBuilder.withSSL();
73         }
74         return clusterBuilder.build();
75     }
76
77     public static boolean executeStatement(String statement) {
78         return executeStatements(statement);
79     }
80
81     public static boolean executeStatements(String ... statements) {
82         Cluster cluster = null;
83         Session session = null;
84         try {
85             cluster = createCluster();
86             if (cluster == null) {
87                 return false;
88             }
89             session = cluster.connect();
90             for (String statement : statements) {
91                 session.execute(statement);
92             }
93             return true;
94         } catch (RuntimeException e) {
95             log.error(String.format("could not execute statements"), e);
96             return false;
97         } finally {
98             if (session != null) {
99                 session.close();
100             }
101             if (cluster != null) {
102                 cluster.close();
103             }
104
105         }
106     }
107
108
109
110 }