* Copyright (c) 2017 AT&T Intellectual Property
* ===================================================================
* Modifications Copyright (c) 2018-2019 IBM
+ * Modifications Copyright (c) 2019 Samsung
* ===================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.util.Iterator;
import java.util.Map;
-import org.apache.commons.jcs.access.CacheAccess;
-import org.onap.music.authentication.CachingUtil;
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.MusicQueryException;
import org.onap.music.exceptions.MusicServiceException;
+import org.onap.music.lockingservice.cassandra.LockType;
+import org.onap.music.main.CipherUtil;
import org.onap.music.main.MusicUtil;
-
-import com.codahale.metrics.JmxReporter;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ColumnDefinitions;
import com.datastax.driver.core.ColumnDefinitions.Definition;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.PoolingOptions;
-import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
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.sun.jersey.core.util.Base64;
+import com.datastax.driver.extras.codecs.enums.EnumNameCodec;
+import com.datastax.driver.extras.codecs.enums.EnumOrdinalCodec;
/**
* @author nelson24
public void setCluster(Cluster cluster) {
this.cluster = cluster;
}
+
+ public Cluster getCluster() {
+ return this.cluster;
+ }
private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStore.class);
/**
- *
+ * Connect to default Cassandra address
*/
public MusicDataStore() {
- connectToCassaCluster();
+ try {
+ connectToCassaCluster(MusicUtil.getMyCassaHost());
+ } catch (MusicServiceException e) {
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), e);
+ }
}
try {
connectToCassaCluster(remoteIp);
} catch (MusicServiceException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
- }
- }
-
- /**
- *
- * @return
- */
- private ArrayList<String> getAllPossibleLocalIps() {
- ArrayList<String> allPossibleIps = new ArrayList<>();
- try {
- Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
- while (en.hasMoreElements()) {
- NetworkInterface ni = en.nextElement();
- Enumeration<InetAddress> ee = ni.getInetAddresses();
- while (ee.hasMoreElements()) {
- InetAddress ia = 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 IP addresses and connects to multiple cassandra
- * clusters.
- */
- private void connectToCassaCluster() {
- Iterator<String> it = getAllPossibleLocalIps().iterator();
- String address = "localhost";
- String[] addresses = null;
- address = MusicUtil.getMyCassaHost();
- addresses = address.split(",");
-
- logger.info(EELFLoggerDelegate.applicationLogger,
- "Connecting to cassa cluster: Iterating through possible ips:"
- + getAllPossibleLocalIps());
- PoolingOptions poolingOptions = new PoolingOptions();
- poolingOptions
- .setConnectionsPerHost(HostDistance.LOCAL, 4, 10)
- .setConnectionsPerHost(HostDistance.REMOTE, 2, 4);
- while (it.hasNext()) {
- try {
- if(MusicUtil.getCassName() != null && MusicUtil.getCassPwd() != null) {
- logger.info(EELFLoggerDelegate.applicationLogger,
- "Building with credentials "+MusicUtil.getCassName()+" & "+MusicUtil.getCassPwd());
- cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
- .withCredentials(MusicUtil.getCassName(), MusicUtil.getCassPwd())
- //.withLoadBalancingPolicy(new RoundRobinPolicy())
- .withoutJMXReporting()
- .withPoolingOptions(poolingOptions)
- .addContactPoints(addresses).build();
- }
- else
- cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
- //.withLoadBalancingPolicy(new RoundRobinPolicy())
- .addContactPoints(addresses).build();
-
- Metadata metadata = cluster.getMetadata();
- logger.info(EELFLoggerDelegate.applicationLogger, "Connected to cassa cluster "
- + metadata.getClusterName() + " at " + address);
- session = cluster.connect();
-
- break;
- } catch (NoHostAvailableException e) {
- address = it.next();
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.HOSTUNAVAILABLE, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR);
- }
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), e);
}
}
poolingOptions
.setConnectionsPerHost(HostDistance.LOCAL, 4, 10)
.setConnectionsPerHost(HostDistance.REMOTE, 2, 4);
+
if(MusicUtil.getCassName() != null && MusicUtil.getCassPwd() != null) {
+ String cassPwd = CipherUtil.decryptPKC(MusicUtil.getCassPwd());
logger.info(EELFLoggerDelegate.applicationLogger,
- "Building with credentials "+MusicUtil.getCassName()+" & "+MusicUtil.getCassPwd());
- cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
- .withCredentials(MusicUtil.getCassName(), MusicUtil.getCassPwd())
- //.withLoadBalancingPolicy(new RoundRobinPolicy())
- .withoutJMXReporting()
- .withPoolingOptions(poolingOptions)
- .addContactPoints(addresses).build();
- }
- else {
+ "Building with credentials "+MusicUtil.getCassName()+" & "+ MusicUtil.getCassPwd());
cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
+ .withCredentials(MusicUtil.getCassName(), cassPwd)
//.withLoadBalancingPolicy(new RoundRobinPolicy())
.withoutJMXReporting()
.withPoolingOptions(poolingOptions)
.addContactPoints(addresses).build();
+ } else {
+ cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
+ .withoutJMXReporting()
+ .withPoolingOptions(poolingOptions)
+ .addContactPoints(addresses)
+ .build();
}
- // JmxReporter reporter =
- // JmxReporter.forRegistry(cluster.getMetrics().getRegistry())
- // .inDomain(cluster.getClusterName() + "-metrics")
- // .build();
-
- // reporter.start();
-
+
Metadata metadata = 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();
} catch (Exception ex) {
- logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.CASSANDRACONNECTIVITY, ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE);
+ logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.CASSANDRACONNECTIVITY,
+ ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE, ex);
throw new MusicServiceException(
"Error while connecting to Cassandra cluster.. " + ex.getMessage());
}
KeyspaceMetadata ks = cluster.getMetadata().getKeyspace(keyspace);
return ks.getTable(tableName);
}
+
+ /**
+ *
+ * @param keyspace
+ * @param tableName
+ * @return TableMetadata
+ */
+ public KeyspaceMetadata returnKeyspaceMetadata(String keyspace) {
+ return cluster.getMetadata().getKeyspace(keyspace);
+ }
/**
if(definition.getType().toString().toLowerCase().contains("blob")) {
resultOutput.put(definition.getName(),
getBlobValue(row, definition.getName(), definition.getType()));
- }
- else
+ } else {
resultOutput.put(definition.getName(),
getColValue(row, definition.getName(), definition.getType()));
+ }
}
}
resultMap.put("row " + counter, resultOutput);
throw new MusicQueryException("Ill formed queryObject for the request = " + "["
+ queryObject.getQuery() + "]");
}
- logger.info(EELFLoggerDelegate.applicationLogger,
+ logger.debug(EELFLoggerDelegate.applicationLogger,
"In preprared Execute Put: the actual insert query:"
+ queryObject.getQuery() + "; the values"
+ queryObject.getValues());
-/*<<<<<<< HEAD
- PreparedStatement preparedInsert = null;
- try {
-
- preparedInsert = session.prepare(queryObject.getQuery());
-
- } catch(InvalidQueryException iqe) {
- logger.error("Exception", iqe);
- logger.error(EELFLoggerDelegate.errorLogger, iqe.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
- throw new MusicQueryException(iqe.getMessage());
- }catch(Exception e) {
- logger.error("Exception", e);
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
- throw new MusicQueryException(e.getMessage());
- }
-
-=======*/
SimpleStatement preparedInsert = null;
try {
logger.info(EELFLoggerDelegate.applicationLogger, "Executing simple put query");
if(queryObject.getConsistency() == null)
preparedInsert.setConsistencyLevel(ConsistencyLevel.ONE);
- else
+ else
preparedInsert.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency()));
} else if (consistency.equalsIgnoreCase(MusicUtil.ONE)) {
preparedInsert.setConsistencyLevel(ConsistencyLevel.ONE);
}
catch (AlreadyExistsException ae) {
- logger.error(EELFLoggerDelegate.errorLogger, ae.getMessage(),AppMessages.SESSIONFAILED+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
- throw new MusicServiceException(ae.getMessage());
- }
- catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.SESSIONFAILED+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
- throw new MusicQueryException("Executing Session Failure for Request = " + "["
- + queryObject.getQuery() + "]" + " Reason = " + e.getMessage());
+ // logger.error(EELFLoggerDelegate.errorLogger,"AlreadExistsException: " + ae.getMessage(),AppMessages.QUERYERROR,
+ // ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ throw new MusicQueryException("AlreadyExistsException: " + ae.getMessage(),ae);
+ } catch ( InvalidQueryException e ) {
+ // logger.error(EELFLoggerDelegate.errorLogger,"InvalidQueryException: " + e.getMessage(),AppMessages.SESSIONFAILED + " ["
+ // + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ throw new MusicQueryException("InvalidQueryException: " + e.getMessage(),e);
+ } catch (Exception e) {
+ // logger.error(EELFLoggerDelegate.errorLogger,e.getClass().toString() + ":" + e.getMessage(),AppMessages.SESSIONFAILED + " ["
+ // + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR, e);
+ throw new MusicServiceException("Executing Session Failure for Request = " + "["
+ + queryObject.getQuery() + "]" + " Reason = " + e.getMessage(),e);
}
-
-
return result;
}
}
if(queryObject.getConsistency() == null) {
preparedEventualGet.setConsistencyLevel(ConsistencyLevel.ONE);
+ } else {
+ preparedEventualGet.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency()));
}
- else {
- preparedEventualGet.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency()));
- }
- results = session.execute(preparedEventualGet.bind(queryObject.getValues().toArray()));
+ results = session.execute(preparedEventualGet.bind(queryObject.getValues().toArray()));
} catch (Exception ex) {
logger.error("Exception", ex);
if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_ONE)) {
if(queryObject.getConsistency() == null) {
statement.setConsistencyLevel(ConsistencyLevel.ONE);
+ } else {
+ statement.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency()));
}
- else {
- statement.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency()));
- }
}
else if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_QUORUM)) {
statement.setConsistencyLevel(ConsistencyLevel.QUORUM);
results = session.execute(statement);
} catch (Exception ex) {
- logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
- throw new MusicServiceException(ex.getMessage());
+ logger.error(EELFLoggerDelegate.errorLogger, "Execute Get Error" + ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject
+ .getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR, ex);
+ throw new MusicServiceException("Execute Get Error" + ex.getMessage());
}
return results;