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