re base code
[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.Cluster;
24 import com.datastax.driver.core.Session;
25 import com.datastax.driver.core.SocketOptions;
26 import org.openecomp.sdc.be.config.ConfigurationManager;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28
29 import java.util.List;
30
31 public class SdcSchemaUtils {
32
33     private static Logger log = Logger.getLogger(SdcSchemaUtils.class.getName());
34
35     /**
36      * the method creates the cluster object using the supplied cassandra nodes
37      * in the configuration
38      *
39      * @return cluster object our null in case of an invalid configuration
40      */
41     public static Cluster createCluster() {
42         List<String> nodes = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getCassandraHosts();
43         if (nodes == null) {
44             log.info("no nodes were supplied in configuration.");
45             return null;
46         }
47         log.info("connecting to node:{}.", nodes);
48         Cluster.Builder clusterBuilder = Cluster.builder();
49         nodes.forEach(clusterBuilder::addContactPoint);
50
51         clusterBuilder.withMaxSchemaAgreementWaitSeconds(60);
52
53         boolean authenticate = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isAuthenticate();
54         if (authenticate) {
55             String username = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getUsername();
56             String password = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getPassword();
57             if (username == null || password == null) {
58                 log.info("authentication is enabled but username or password were not supplied.");
59                 return null;
60             }
61             clusterBuilder.withCredentials(username, password);
62         }
63         boolean ssl = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isSsl();
64         if (ssl) {
65             String truststorePath = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePath();
66             String truststorePassword = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePassword();
67             if (truststorePath == null || truststorePassword == null) {
68                 log.info("ssl is enabled but truststorePath or truststorePassword were not supplied.");
69                 return null;
70             }
71             System.setProperty("javax.net.ssl.trustStore", truststorePath);
72             System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword);
73             clusterBuilder.withSSL();
74         }
75         SocketOptions socketOptions =new SocketOptions();
76         Integer socketConnectTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketConnectTimeout();
77         if( socketConnectTimeout!=null ){
78             log.info("SocketConnectTimeout was provided, setting Cassandra client to use SocketConnectTimeout: {} .",socketConnectTimeout);
79             socketOptions.setConnectTimeoutMillis(socketConnectTimeout);
80         }
81         Integer socketReadTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketReadTimeout();
82         if( socketReadTimeout != null ){
83             log.info("SocketReadTimeout was provided, setting Cassandra client to use SocketReadTimeout: {} .",socketReadTimeout);
84             socketOptions.setReadTimeoutMillis(socketReadTimeout);
85         }
86         clusterBuilder.withSocketOptions(socketOptions);
87         return clusterBuilder.build();
88     }
89
90     public static boolean executeStatement(String statement) {
91         return executeStatements(statement);
92     }
93
94     public static boolean executeStatements(String ... statements) {
95         Cluster cluster = null;
96         Session session = null;
97         try {
98             cluster = createCluster();
99             if (cluster == null) {
100                 return false;
101             }
102             session = cluster.connect();
103             for (String statement : statements) {
104                 session.execute(statement);
105             }
106             return true;
107         } catch (RuntimeException e) {
108             log.error(String.format("could not execute statements"), e);
109             return false;
110         } finally {
111             if (session != null) {
112                 session.close();
113             }
114             if (cluster != null) {
115                 cluster.close();
116             }
117
118         }
119     }
120
121
122
123 }