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