X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=music-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fmusic%2Fdatastore%2FMusicDataStore.java;h=9ce73cc831aef73bd4ead4e182c27e0f0da5d396;hb=fd5224f4aaf68e6ae186ef8a163edd7f5b2966f7;hp=2e17670f1f3cd12d5e8b608cbe046210db99056b;hpb=d4f4d573469aaa9b284313e4bd9535e6d8e3dd10;p=music.git diff --git a/music-core/src/main/java/org/onap/music/datastore/MusicDataStore.java b/music-core/src/main/java/org/onap/music/datastore/MusicDataStore.java index 2e17670f..9ce73cc8 100755 --- a/music-core/src/main/java/org/onap/music/datastore/MusicDataStore.java +++ b/music-core/src/main/java/org/onap/music/datastore/MusicDataStore.java @@ -51,6 +51,7 @@ 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.SocketOptions; import com.datastax.driver.core.TableMetadata; import com.datastax.driver.core.exceptions.AlreadyExistsException; import com.datastax.driver.core.exceptions.InvalidQueryException; @@ -61,6 +62,10 @@ import com.datastax.driver.extras.codecs.enums.EnumNameCodec; * */ public class MusicDataStore { + + public static final String CONSISTENCY_LEVEL_ONE = "ONE"; + public static final String CONSISTENCY_LEVEL_QUORUM = "QUORUM"; + public static final String CONSISTENCY_LEVEL_LOCAL_QUORUM = "LOCAL_QUORUM"; private Session session; private Cluster cluster; @@ -86,7 +91,7 @@ public class MusicDataStore { setCluster(cluster); } - + /** * @param session */ @@ -107,10 +112,10 @@ public class MusicDataStore { public void setCluster(Cluster cluster) { EnumNameCodec lockTypeCodec = new EnumNameCodec(LockType.class); cluster.getConfiguration().getCodecRegistry().register(lockTypeCodec); - + this.cluster = cluster; } - + public Cluster getCluster() { return this.cluster; } @@ -118,7 +123,7 @@ public class MusicDataStore { private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStore.class); - + /** * * @param remoteIp @@ -151,38 +156,49 @@ public class MusicDataStore { poolingOptions .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()); + String cassPwd; + if (MusicUtil.getCipherEncKey() != null && !("").equals(MusicUtil.getCipherEncKey())) { + cassPwd = CipherUtil.decryptPKC(MusicUtil.getCassPwd()); + } else { + cassPwd = MusicUtil.getCassPwd(); + } logger.info(EELFLoggerDelegate.applicationLogger, "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(); + .withCredentials(MusicUtil.getCassName(), cassPwd) + //.withLoadBalancingPolicy(new RoundRobinPolicy()) + .withoutJMXReporting() + .withPoolingOptions(poolingOptions) + .withSocketOptions( + new SocketOptions().setConnectTimeoutMillis(MusicUtil.getCassandraConnectTimeOutMS()) + .setReadTimeoutMillis(MusicUtil.getCassandraReadTimeOutMS())) + .addContactPoints(addresses).build(); } else { cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort()) - .withoutJMXReporting() - .withPoolingOptions(poolingOptions) - .addContactPoints(addresses) - .build(); + .withoutJMXReporting() + .withPoolingOptions(poolingOptions) + .withSocketOptions(new SocketOptions() + .setConnectTimeoutMillis(MusicUtil.getCassandraConnectTimeOutMS()) + .setReadTimeoutMillis(MusicUtil.getCassandraReadTimeOutMS())) + .addContactPoints(addresses) + .build(); } - + this.setCluster(cluster); Metadata metadata = this.cluster.getMetadata(); logger.info(EELFLoggerDelegate.applicationLogger, "Connected to cassa cluster " - + metadata.getClusterName() + " at " + address); + + metadata.getClusterName() + " at " + address); try { session = this.cluster.connect(); } catch (Exception ex) { logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.CASSANDRACONNECTIVITY, - ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE, ex); + ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE, ex); throw new MusicServiceException( - "Error while connecting to Cassandra cluster.. " + ex.getMessage()); + "Error while connecting to Cassandra cluster.. " + ex.getMessage()); } } @@ -210,16 +226,16 @@ public class MusicDataStore { 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); - } + * + * @param keyspace + * @param tableName + * @return TableMetadata + */ + public KeyspaceMetadata returnKeyspaceMetadata(String keyspace) { + return cluster.getMetadata().getKeyspace(keyspace); + } /** @@ -285,7 +301,7 @@ public class MusicDataStore { */ public Map> marshalData(ResultSet results) { Map> resultMap = - new HashMap<>(); + new HashMap<>(); int counter = 0; for (Row row : results) { ColumnDefinitions colInfo = row.getColumnDefinitions(); @@ -297,7 +313,7 @@ public class MusicDataStore { getBlobValue(row, definition.getName(), definition.getType())); } else { resultOutput.put(definition.getName(), - getColValue(row, definition.getName(), definition.getType())); + getColValue(row, definition.getName(), definition.getType())); } } } @@ -309,7 +325,7 @@ public class MusicDataStore { // Prepared Statements 1802 additions - + public boolean executePut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException, MusicQueryException { return executePut(queryObject, consistency, 0); @@ -325,19 +341,19 @@ public class MusicDataStore { * @throws MusicQueryException */ public boolean executePut(PreparedQueryObject queryObject, String consistency,long timeSlot) - throws MusicServiceException, MusicQueryException { + throws MusicServiceException, MusicQueryException { boolean result = false; long timeOfWrite = System.currentTimeMillis(); if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) { logger.error(EELFLoggerDelegate.errorLogger, queryObject.getQuery(),AppMessages.QUERYERROR, ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); throw new MusicQueryException("Ill formed queryObject for the request = " + "[" - + queryObject.getQuery() + "]"); + + queryObject.getQuery() + "]"); } logger.debug(EELFLoggerDelegate.applicationLogger, - "In preprared Execute Put: the actual insert query:" - + queryObject.getQuery() + "; the values" - + queryObject.getValues()); + "In preprared Execute Put: the actual insert query:" + + queryObject.getQuery() + "; the values" + + queryObject.getValues()); SimpleStatement preparedInsert = null; try { @@ -363,26 +379,29 @@ public class MusicDataStore { ResultSet rs = session.execute(preparedInsert); result = rs.wasApplied(); - - } - catch (AlreadyExistsException ae) { - // 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 (AlreadyExistsException ae) { + throw new MusicServiceException("Already Exists Exception: " + ae.getMessage()); + } catch (InvalidQueryException e) { + if (e.getMessage().contains("unconfigured table")) { + throw new MusicServiceException("Invalid Query Exception: " + e.getMessage()); + } else { + logger.info(EELFLoggerDelegate.applicationLogger, "Query Exception: " + e.getMessage(), + AppMessages.SESSIONFAILED + " [" + queryObject.getQuery() + "]", ErrorSeverity.INFO, + ErrorTypes.QUERYERROR, e); + throw new MusicServiceException("Query Exception: " + e.getMessage()); + } } 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); + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), + AppMessages.SESSIONFAILED + " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, + ErrorTypes.QUERYERROR, e); + throw new MusicServiceException("Executing Session Failure for Request = " + "[" + queryObject.getQuery() + + "]" + " Reason = " + e.getMessage()); } + return result; } - /* *//** + /* *//** * This method performs DDL operations on Cassandra using consistency level ONE. * * @param queryObject Object containing cassandra prepared query and values. @@ -425,15 +444,15 @@ public class MusicDataStore { return results; } - *//** - * - * This method performs DDL operation on Cassandra using consistency level QUORUM. - * - * @param queryObject Object containing cassandra prepared query and values. - * @return ResultSet - * @throws MusicServiceException - * @throws MusicQueryException - *//* + *//** + * + * This method performs DDL operation on Cassandra using consistency level QUORUM. + * + * @param queryObject Object containing cassandra prepared query and values. + * @return ResultSet + * @throws MusicServiceException + * @throws MusicQueryException + *//* public ResultSet executeCriticalGet(PreparedQueryObject queryObject) throws MusicServiceException, MusicQueryException { if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) { @@ -456,26 +475,21 @@ public class MusicDataStore { return results; } - */ + */ public ResultSet executeGet(PreparedQueryObject queryObject,String consistencyLevel) throws MusicQueryException, MusicServiceException { if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) { logger.error(EELFLoggerDelegate.errorLogger, "",AppMessages.QUERYERROR+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); throw new MusicQueryException("Error processing Prepared Query Object for the request = " + "[" - + queryObject.getQuery() + "]"); + + queryObject.getQuery() + "]"); } ResultSet results = null; try { SimpleStatement statement = new SimpleStatement(queryObject.getQuery(), queryObject.getValues().toArray()); - - if (consistencyLevel.equalsIgnoreCase(MusicUtil.ONE)) { - if(queryObject.getConsistency() == null) { + if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_ONE)) { statement.setConsistencyLevel(ConsistencyLevel.ONE); - } else { - statement.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency())); - } - } else if (consistencyLevel.equalsIgnoreCase(MusicUtil.QUORUM)) { + } else if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_QUORUM)) { statement.setConsistencyLevel(ConsistencyLevel.QUORUM); - } else if (consistencyLevel.equalsIgnoreCase(MusicUtil.LOCAL_QUORUM)) { + } else if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_LOCAL_QUORUM)) { statement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM); } @@ -483,24 +497,24 @@ public class MusicDataStore { } catch (Exception ex) { logger.error(EELFLoggerDelegate.errorLogger, "Execute Get Error" + ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject - .getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR, ex); + .getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR, ex); throw new MusicServiceException("Execute Get Error" + ex.getMessage()); } - + return results; - + } - + /** * This method performs DDL operations on Cassandra using consistency level ONE. * * @param queryObject Object containing cassandra prepared query and values. */ public ResultSet executeOneConsistencyGet(PreparedQueryObject queryObject) - throws MusicServiceException, MusicQueryException { - return executeGet(queryObject, MusicUtil.ONE); + throws MusicServiceException, MusicQueryException { + return executeGet(queryObject, CONSISTENCY_LEVEL_ONE); } - + /** * * This method performs DDL operation on Cassandra using consistency level LOCAL_QUORUM. @@ -508,10 +522,10 @@ public class MusicDataStore { * @param queryObject Object containing cassandra prepared query and values. */ public ResultSet executeLocalQuorumConsistencyGet(PreparedQueryObject queryObject) - throws MusicServiceException, MusicQueryException { - return executeGet(queryObject, MusicUtil.LOCAL_QUORUM); + throws MusicServiceException, MusicQueryException { + return executeGet(queryObject, CONSISTENCY_LEVEL_LOCAL_QUORUM); } - + /** * * This method performs DDL operation on Cassandra using consistency level QUORUM. @@ -519,8 +533,8 @@ public class MusicDataStore { * @param queryObject Object containing cassandra prepared query and values. */ public ResultSet executeQuorumConsistencyGet(PreparedQueryObject queryObject) - throws MusicServiceException, MusicQueryException { - return executeGet(queryObject, MusicUtil.QUORUM); + throws MusicServiceException, MusicQueryException { + return executeGet(queryObject, CONSISTENCY_LEVEL_QUORUM); } - + }