aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/music/datastore/CassandraClusterBuilder.java
blob: f6c9da15d4ed229dd33c34d6f3c8e1d68ac9ccbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package org.onap.music.datastore;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.eelf.logging.format.AppMessages;
import org.onap.music.eelf.logging.format.ErrorSeverity;
import org.onap.music.eelf.logging.format.ErrorTypes;
import org.onap.music.exceptions.MusicServiceException;
import org.onap.music.main.MusicUtil;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;

public class CassandraClusterBuilder {
    private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CassandraClusterBuilder.class);

    private static Cluster createCassandraCluster(String address) throws NoHostAvailableException {
        Cluster cluster = Cluster.builder().withPort(9042)
                .withCredentials(MusicUtil.getCassName(), MusicUtil.getCassPwd())
                .addContactPoint(address).build();
        Metadata metadata = cluster.getMetadata();
        logger.info(EELFLoggerDelegate.applicationLogger, "Connected to cassa cluster "
                + metadata.getClusterName() + " at " + address);
        return cluster;
    }
    /**
     *
     * @return
     */
    private static ArrayList<String> getAllPossibleLocalIps() {
        ArrayList<String> allPossibleIps = new ArrayList<String>();
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) en.nextElement();
                Enumeration<InetAddress> ee = ni.getInetAddresses();
                while (ee.hasMoreElements()) {
                    InetAddress ia = (InetAddress) ee.nextElement();
                    allPossibleIps.add(ia.getHostAddress());
                }
            }
        } catch (SocketException e) {
            logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.CONNCECTIVITYERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR);
        }catch(Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR);
        }
        return allPossibleIps;
    }

    /**
     * This method iterates through all available local IP addresses and tries to connect to first successful one
     */
    public static Cluster connectToLocalCassandraCluster() {
        ArrayList<String> localAddrs = getAllPossibleLocalIps();
        localAddrs.add(0, "localhost");
        logger.info(EELFLoggerDelegate.applicationLogger,
                "Connecting to cassa cluster: Iterating through possible ips:"
                        + getAllPossibleLocalIps());
        for (String address: localAddrs) {
            try {
                return createCassandraCluster(address);
            } catch (NoHostAvailableException e) {
                logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.HOSTUNAVAILABLE, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR);
            }
        }
        return null;
    }

    /**
     * This method connects to cassandra cluster on specific address.
     *
     * @param address
     */
    public static Cluster connectToRemoteCassandraCluster(String address) throws MusicServiceException {
        try {
            return createCassandraCluster(address);
        } catch (Exception ex) {
            logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.CASSANDRACONNECTIVITY, ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE);
            throw new MusicServiceException(
                    "Error while connecting to Cassandra cluster.. " + ex.getMessage());
        }
    }

    public static Cluster connectSmart(String cassaHost) throws MusicServiceException {
        if (cassaHost.equals("localhost")) {
            Cluster cluster = CassandraClusterBuilder.connectToLocalCassandraCluster();
            return cluster;
        } else {
            Cluster cluster = CassandraClusterBuilder.connectToRemoteCassandraCluster(MusicUtil.getMyCassaHost());
            return cluster;
        }

    }
}