aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/music/datastore
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/onap/music/datastore')
-rw-r--r--src/main/java/org/onap/music/datastore/CassandraClusterBuilder.java100
-rw-r--r--src/main/java/org/onap/music/datastore/Condition.java35
-rw-r--r--src/main/java/org/onap/music/datastore/MusicDataStore.java104
-rw-r--r--src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java37
4 files changed, 131 insertions, 145 deletions
diff --git a/src/main/java/org/onap/music/datastore/CassandraClusterBuilder.java b/src/main/java/org/onap/music/datastore/CassandraClusterBuilder.java
new file mode 100644
index 00000000..f6c9da15
--- /dev/null
+++ b/src/main/java/org/onap/music/datastore/CassandraClusterBuilder.java
@@ -0,0 +1,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;
+ }
+
+ }
+}
diff --git a/src/main/java/org/onap/music/datastore/Condition.java b/src/main/java/org/onap/music/datastore/Condition.java
index 23994900..4faafb79 100644
--- a/src/main/java/org/onap/music/datastore/Condition.java
+++ b/src/main/java/org/onap/music/datastore/Condition.java
@@ -31,24 +31,21 @@ import org.onap.music.service.impl.MusicCassaCore;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
-
-
- public class Condition {
- Map<String, Object> conditions;
- PreparedQueryObject selectQueryForTheRow;
- private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(Condition.class);
- //private static MusicCoreService musicCore = MusicCassaCore.getInstance();
-
- public Condition(Map<String, Object> conditions, PreparedQueryObject selectQueryForTheRow) {
- this.conditions = conditions;
- this.selectQueryForTheRow = selectQueryForTheRow;
- }
-
- public boolean testCondition() throws Exception {
- // first generate the row
- ResultSet results = MusicCore.quorumGet(selectQueryForTheRow);
- Row row = results.one();
- return MusicDataStoreHandle.getDSHandle().doesRowSatisfyCondition(row, conditions);
- }
+public class Condition {
+ Map<String, Object> conditions;
+ PreparedQueryObject selectQueryForTheRow;
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(Condition.class);
+ //private static MusicCoreService musicCore = MusicCassaCore.getInstance();
+
+ public Condition(Map<String, Object> conditions, PreparedQueryObject selectQueryForTheRow) {
+ this.conditions = conditions;
+ this.selectQueryForTheRow = selectQueryForTheRow;
}
+ public boolean testCondition() throws Exception {
+ // first generate the row
+ ResultSet results = MusicCore.quorumGet(selectQueryForTheRow);
+ Row row = results.one();
+ return MusicDataStore.doesRowSatisfyCondition(row, conditions);
+ }
+}
diff --git a/src/main/java/org/onap/music/datastore/MusicDataStore.java b/src/main/java/org/onap/music/datastore/MusicDataStore.java
index 1ea10054..deb65edd 100644
--- a/src/main/java/org/onap/music/datastore/MusicDataStore.java
+++ b/src/main/java/org/onap/music/datastore/MusicDataStore.java
@@ -65,13 +65,6 @@ public class MusicDataStore {
/**
- * Constructs DataStore by connecting to local Cassandra
- */
- public MusicDataStore() {
- connectToLocalCassandraCluster();
- }
-
- /**
* Constructs DataStore by providing existing cluster and session
* @param cluster
* @param session
@@ -82,94 +75,6 @@ public class MusicDataStore {
}
/**
- * Constructs DataStore by connecting to provided remote Cassandra
- * @param remoteAddress
- * @throws MusicServiceException
- */
- public MusicDataStore(String remoteAddress) {
- try {
- connectToRemoteCassandraCluster(remoteAddress);
- } catch (MusicServiceException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
- }
- }
-
- private void createCassandraSession(String address) throws NoHostAvailableException {
- 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);
- session = cluster.connect();
- }
-
- /**
- *
- * @return
- */
- private 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
- */
- private void 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 {
- createCassandraSession(address);
- break;
- } catch (NoHostAvailableException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.HOSTUNAVAILABLE, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR);
- }
- }
- }
-
- /**
- * This method connects to cassandra cluster on specific address.
- *
- * @param address
- */
- private void connectToRemoteCassandraCluster(String address) throws MusicServiceException {
- try {
- createCassandraSession(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 void close() {
- session.close();
- }
-
- /**
*
* @param keyspace
* @param tableName
@@ -203,7 +108,7 @@ public class MusicDataStore {
* @param colType
* @return
*/
- public Object getColValue(Row row, String colName, DataType colType) {
+ public static Object getColValue(Row row, String colName, DataType colType) {
switch (colType.getName()) {
case VARCHAR:
@@ -237,7 +142,7 @@ public class MusicDataStore {
return data;
}
- public boolean doesRowSatisfyCondition(Row row, Map<String, Object> condition) throws Exception {
+ public static boolean doesRowSatisfyCondition(Row row, Map<String, Object> condition) throws Exception {
ColumnDefinitions colInfo = row.getColumnDefinitions();
for (Map.Entry<String, Object> entry : condition.entrySet()) {
@@ -453,5 +358,10 @@ public class MusicDataStore {
TimeMeasureInstance.instance().exit();
}
}
+
+ @Deprecated
+ public void close() {
+ session.close();
+ }
}
diff --git a/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java b/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java
index ebd6213f..0b44df3c 100644
--- a/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java
+++ b/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java
@@ -24,10 +24,11 @@ package org.onap.music.datastore;
import java.util.HashMap;
import java.util.Map;
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.Session;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.exceptions.MusicServiceException;
import org.onap.music.main.MusicUtil;
-import org.onap.music.service.impl.MusicCassaCore;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.TableMetadata;
@@ -38,32 +39,11 @@ public class MusicDataStoreHandle {
private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStoreHandle.class);
/**
- *
- * @param remoteAddress
- * @return
- */
- public static MusicDataStore getDSHandle(String remoteAddress) {
- logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle");
- long start = System.currentTimeMillis();
- if (mDstoreHandle == null) {
- try {
- MusicUtil.loadProperties();
- } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, "No properties file defined. Falling back to default.");
- }
- mDstoreHandle = new MusicDataStore(remoteAddress);
- }
- long end = System.currentTimeMillis();
- logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
- return mDstoreHandle;
- }
-
- /**
*
* @return
* @throws MusicServiceException
*/
- public static MusicDataStore getDSHandle() throws MusicServiceException {
+ private static MusicDataStore getDSHandle() throws MusicServiceException {
logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle");
long start = System.currentTimeMillis();
@@ -73,12 +53,11 @@ public class MusicDataStoreHandle {
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger, "No properties file defined. Falling back to default.");
}
- // Quick Fix - Best to put this into every call to getDSHandle?
- if (MusicUtil.getMyCassaHost().equals("localhost")) {
- mDstoreHandle = new MusicDataStore();
- } else {
- mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost());
- }
+ // Quick Fix - Best to put this into every call to getInstanceDSHandle?
+ Cluster cluster = CassandraClusterBuilder.connectSmart(MusicUtil.getMyCassaHost());
+ Session session = cluster.connect();
+ mDstoreHandle = new MusicDataStore(cluster, session);
+
}
if(mDstoreHandle.getSession() == null) {
String message = "Connection to Cassandra has not been enstablished."