Update junits
[music.git] / music-core / src / main / java / org / onap / music / datastore / MusicDataStore.java
index 5a65868..2e17670 100755 (executable)
 
 package org.onap.music.datastore;
 
-import java.net.InetAddress;
-import java.net.NetworkInterface;
-import java.net.SocketException;
 import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 
 import org.onap.music.eelf.logging.EELFLoggerDelegate;
@@ -58,25 +52,41 @@ import com.datastax.driver.core.Row;
 import com.datastax.driver.core.Session;
 import com.datastax.driver.core.SimpleStatement;
 import com.datastax.driver.core.TableMetadata;
-import com.datastax.driver.core.TypeCodec;
 import com.datastax.driver.core.exceptions.AlreadyExistsException;
 import com.datastax.driver.core.exceptions.InvalidQueryException;
-import com.datastax.driver.core.exceptions.NoHostAvailableException;
 import com.datastax.driver.extras.codecs.enums.EnumNameCodec;
-import com.datastax.driver.extras.codecs.enums.EnumOrdinalCodec;
 
 /**
  * @author nelson24
  *
  */
 public class MusicDataStore {
-
-    public static final String CONSISTENCY_LEVEL_ONE = "ONE";
-    public static final String CONSISTENCY_LEVEL_QUORUM = "QUORUM";
     private Session session;
     private Cluster cluster;
 
 
+    /**
+     * Connect to default Cassandra address
+     */
+    public MusicDataStore() {
+        try {
+            connectToCassaCluster(MusicUtil.getMyCassaHost());
+        } catch (MusicServiceException e) {
+            logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), e);
+        }
+    }
+
+
+    /**
+     * @param cluster
+     * @param session
+     */
+    public MusicDataStore(Cluster cluster, Session session) {
+        this.session = session;
+        setCluster(cluster);
+    }
+
+    
     /**
      * @param session
      */
@@ -95,6 +105,9 @@ public class MusicDataStore {
      * @param cluster
      */
     public void setCluster(Cluster cluster) {
+        EnumNameCodec<LockType> lockTypeCodec = new EnumNameCodec<LockType>(LockType.class);
+        cluster.getConfiguration().getCodecRegistry().register(lockTypeCodec);
+        
         this.cluster = cluster;
     }
     
@@ -105,27 +118,7 @@ public class MusicDataStore {
 
     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStore.class);
 
-    /**
-     * Connect to default Cassandra address
-     */
-    public MusicDataStore() {
-        try {
-            connectToCassaCluster(MusicUtil.getMyCassaHost());
-        } catch (MusicServiceException e) {
-            logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), e);
-        }
-    }
-
-
-    /**
-     * @param cluster
-     * @param session
-     */
-    public MusicDataStore(Cluster cluster, Session session) {
-        this.session = session;
-        this.cluster = cluster;
-    }
-
+    
     /**
      *
      * @param remoteIp
@@ -159,6 +152,7 @@ public class MusicDataStore {
         .setConnectionsPerHost(HostDistance.LOCAL,  4, 10)
         .setConnectionsPerHost(HostDistance.REMOTE, 2, 4);
         
+        Cluster cluster;
         if(MusicUtil.getCassName() != null && MusicUtil.getCassPwd() != null) {
             String cassPwd = CipherUtil.decryptPKC(MusicUtil.getCassPwd());
             logger.info(EELFLoggerDelegate.applicationLogger,
@@ -177,16 +171,13 @@ public class MusicDataStore {
                         .build();
         }
         
-        
-        Metadata metadata = cluster.getMetadata();
+        this.setCluster(cluster);
+        Metadata metadata = this.cluster.getMetadata();
         logger.info(EELFLoggerDelegate.applicationLogger, "Connected to cassa cluster "
                         + metadata.getClusterName() + " at " + address);
-        
-        EnumNameCodec<LockType> lockTypeCodec = new EnumNameCodec<LockType>(LockType.class);
-        cluster.getConfiguration().getCodecRegistry().register(lockTypeCodec);
 
         try {
-            session = cluster.connect();
+            session = this.cluster.connect();
         } catch (Exception ex) {
             logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.CASSANDRACONNECTIVITY,
                 ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE, ex);
@@ -476,15 +467,16 @@ public class MusicDataStore {
         try {
             SimpleStatement statement = new SimpleStatement(queryObject.getQuery(), queryObject.getValues().toArray());
 
-            if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_ONE)) {
+            if (consistencyLevel.equalsIgnoreCase(MusicUtil.ONE)) {
                 if(queryObject.getConsistency() == null) {
                     statement.setConsistencyLevel(ConsistencyLevel.ONE);
                 } else {
                     statement.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency()));
                 }
-            }
-            else if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_QUORUM)) {
+            } else if (consistencyLevel.equalsIgnoreCase(MusicUtil.QUORUM)) {
                 statement.setConsistencyLevel(ConsistencyLevel.QUORUM);
+            } else if (consistencyLevel.equalsIgnoreCase(MusicUtil.LOCAL_QUORUM)) {
+                statement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
             }
 
             results = session.execute(statement);
@@ -506,9 +498,20 @@ public class MusicDataStore {
      */
     public ResultSet executeOneConsistencyGet(PreparedQueryObject queryObject)
                     throws MusicServiceException, MusicQueryException {
-        return executeGet(queryObject, CONSISTENCY_LEVEL_ONE);
+        return executeGet(queryObject, MusicUtil.ONE);
     }
-
+    
+    /**
+     * 
+     * This method performs DDL operation on Cassandra using consistency level LOCAL_QUORUM.
+     * 
+     * @param queryObject Object containing cassandra prepared query and values.
+     */
+    public ResultSet executeLocalQuorumConsistencyGet(PreparedQueryObject queryObject)
+                    throws MusicServiceException, MusicQueryException {
+        return executeGet(queryObject, MusicUtil.LOCAL_QUORUM);
+    }
+    
     /**
      * 
      * This method performs DDL operation on Cassandra using consistency level QUORUM.
@@ -517,7 +520,7 @@ public class MusicDataStore {
      */
     public ResultSet executeQuorumConsistencyGet(PreparedQueryObject queryObject)
                     throws MusicServiceException, MusicQueryException {
-        return executeGet(queryObject, CONSISTENCY_LEVEL_QUORUM);
+        return executeGet(queryObject, MusicUtil.QUORUM);
     }
-
+    
 }