From a1f03a639a2ba38ec5c19e96c3c9d703fa048a8f Mon Sep 17 00:00:00 2001 From: "Tschaen, Brendan" Date: Thu, 15 Nov 2018 10:31:53 -0500 Subject: Revert "music core implemented as a service" This reverts commit 7addd52dc73e6571028ca7f6a018e75ec1c9b0ca. Change-Id: I0875ce10521363b8e17eb6cd2cf9d8848e572bea Issue-ID: MUSIC-189 Signed-off-by: Tschaen, Brendan --- .../music/authentication/MusicAuthentication.java | 138 ---- .../conductor/conditionals/MusicConditional.java | 41 +- .../conditionals/RestMusicConditionalAPI.java | 10 +- .../org/onap/music/datastore/CassaDataStore.java | 475 ++++++++++++ .../org/onap/music/datastore/CassaLockStore.java | 173 +++++ .../java/org/onap/music/datastore/Condition.java | 53 -- .../org/onap/music/datastore/MusicDataStore.java | 475 ------------ .../onap/music/datastore/MusicDataStoreHandle.java | 115 --- .../org/onap/music/datastore/MusicLockState.java | 41 + .../music/eelf/healthcheck/MusicHealthCheck.java | 8 +- .../lockingservice/cassandra/CassaLockStore.java | 175 ----- .../lockingservice/cassandra/MusicLockState.java | 41 - src/main/java/org/onap/music/main/CachingUtil.java | 15 +- .../java/org/onap/music/main/CronJobManager.java | 13 +- src/main/java/org/onap/music/main/MusicCore.java | 826 +++++++++++++++++++++ .../java/org/onap/music/main/VotingAppJar.java | 14 +- .../music/response/jsonobjects/JsonResponse.java | 2 +- .../org/onap/music/rest/RestMusicAdminAPI.java | 29 +- .../java/org/onap/music/rest/RestMusicDataAPI.java | 88 ++- .../org/onap/music/rest/RestMusicLocksAPI.java | 49 +- .../java/org/onap/music/rest/RestMusicQAPI.java | 10 +- .../org/onap/music/service/MusicCoreService.java | 94 --- .../onap/music/service/impl/MusicCassaCore.java | 669 ----------------- .../org/onap/music/unittests/CassandraCQL.java | 256 +++++++ .../onap/music/unittests/CassandraCQLQueries.java | 256 ------- .../onap/music/unittests/MusicDataStoreTest.java | 22 +- .../onap/music/unittests/MusicLockStoreTest.java | 23 +- .../onap/music/unittests/TestCassaLockStore.java | 12 +- .../org/onap/music/unittests/TestMusicCore.java | 29 +- .../music/unittests/TestMusicCoreIntegration.java | 62 +- .../onap/music/unittests/TestRestMusicData.java | 21 +- .../onap/music/unittests/TestRestMusicQAPI.java | 14 +- .../org/onap/music/unittests/TestVotingApp.java | 5 +- 33 files changed, 1984 insertions(+), 2270 deletions(-) delete mode 100644 src/main/java/org/onap/music/authentication/MusicAuthentication.java create mode 100644 src/main/java/org/onap/music/datastore/CassaDataStore.java create mode 100644 src/main/java/org/onap/music/datastore/CassaLockStore.java delete mode 100644 src/main/java/org/onap/music/datastore/Condition.java delete mode 100644 src/main/java/org/onap/music/datastore/MusicDataStore.java delete mode 100644 src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java create mode 100644 src/main/java/org/onap/music/datastore/MusicLockState.java delete mode 100644 src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java delete mode 100644 src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java create mode 100644 src/main/java/org/onap/music/main/MusicCore.java delete mode 100644 src/main/java/org/onap/music/service/MusicCoreService.java delete mode 100644 src/main/java/org/onap/music/service/impl/MusicCassaCore.java create mode 100644 src/test/java/org/onap/music/unittests/CassandraCQL.java delete mode 100644 src/test/java/org/onap/music/unittests/CassandraCQLQueries.java diff --git a/src/main/java/org/onap/music/authentication/MusicAuthentication.java b/src/main/java/org/onap/music/authentication/MusicAuthentication.java deleted file mode 100644 index 4967e845..00000000 --- a/src/main/java/org/onap/music/authentication/MusicAuthentication.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.authentication; - -import java.util.HashMap; -import java.util.Map; - -import org.onap.music.datastore.PreparedQueryObject; -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.CachingUtil; -import org.onap.music.main.MusicUtil; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.Row; - -public class MusicAuthentication { - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicAuthentication.class); - - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); - - /** - * authenticate user logic - * - * @param nameSpace - * @param userId - * @param password - * @param keyspace - * @param aid - * @param operation - * @return - * @throws Exception - */ - public static Map authenticate(String nameSpace, String userId, - String password, String keyspace, String aid, String operation) - throws Exception { - Map resultMap = new HashMap<>(); - String uuid = null; - resultMap = CachingUtil.validateRequest(nameSpace, userId, password, keyspace, aid, - operation); - if (!resultMap.isEmpty()) - return resultMap; - String isAAFApp = null; - try { - isAAFApp= CachingUtil.isAAFApplication(nameSpace); - } catch(MusicServiceException e) { - resultMap.put("Exception", e.getMessage()); - return resultMap; - } - if(isAAFApp == null) { - resultMap.put("Exception", "Namespace: "+nameSpace+" doesn't exist. Please make sure ns(appName)" - + " is correct and Application is onboarded."); - return resultMap; - } - boolean isAAF = Boolean.valueOf(isAAFApp); - if (userId == null || password == null) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - logger.error(EELFLoggerDelegate.errorLogger,"One or more required headers is missing. userId: " + userId - + " :: password: " + password); - resultMap.put("Exception", - "UserId and Password are mandatory for the operation " + operation); - return resultMap; - } - if(!isAAF && !(operation.equals("createKeySpace"))) { - resultMap = CachingUtil.authenticateAIDUser(nameSpace, userId, password, keyspace); - if (!resultMap.isEmpty()) - return resultMap; - - } - if (isAAF && nameSpace != null && userId != null && password != null) { - boolean isValid = true; - try { - isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - logger.error(EELFLoggerDelegate.errorLogger,"Got exception while AAF authentication for namespace " + nameSpace); - resultMap.put("Exception", e.getMessage()); - } - if (!isValid) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - resultMap.put("Exception", "User not authenticated..."); - } - if (!resultMap.isEmpty()) - return resultMap; - - } - - if (operation.equals("createKeySpace")) { - logger.info(EELFLoggerDelegate.applicationLogger,"AID is not provided. Creating new UUID for keyspace."); - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "select uuid from admin.keyspace_master where application_name=? and username=? and keyspace_name=? allow filtering"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); - - try { - Row rs = musicCore.get(pQuery).one(); - uuid = rs.getUUID("uuid").toString(); - resultMap.put("uuid", "existing"); - } catch (Exception e) { - logger.info(EELFLoggerDelegate.applicationLogger,"No UUID found in DB. So creating new UUID."); - uuid = CachingUtil.generateUUID(); - resultMap.put("uuid", "new"); - } - resultMap.put("aid", uuid); - } - - return resultMap; - } - -} diff --git a/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java b/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java index 281c6ca0..8aadcba3 100644 --- a/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java +++ b/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java @@ -27,18 +27,16 @@ import java.util.HashMap; import java.util.Map; import org.codehaus.jettison.json.JSONObject; -import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; 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.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; import org.onap.music.rest.RestMusicDataAPI; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; import com.datastax.driver.core.ColumnDefinitions; import com.datastax.driver.core.DataType; @@ -48,7 +46,6 @@ import com.datastax.driver.core.TableMetadata; public class MusicConditional { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicDataAPI.class); - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); public static ReturnType conditionalInsert(String keyspace, String tablename, String casscadeColumnName, Map casscadeColumnData, String primaryKey, Map valuesMap, @@ -56,7 +53,7 @@ public class MusicConditional { Map queryBank = new HashMap<>(); TableMetadata tableInfo = null; - tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename); + tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType(); String primaryId = tableInfo.getPrimaryKey().get(0).getName(); DataType casscadeColumnType = tableInfo.getColumn(casscadeColumnName).getType(); @@ -87,15 +84,15 @@ public class MusicConditional { String key = keyspace + "." + tablename + "." + primaryKey; - String lockId = musicCore.createLockReference(key); + String lockId = MusicCore.createLockReference(key); long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = musicCore.acquireLockWithLease(key, lockId, leasePeriod); + ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod); try { if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { ReturnType criticalPutResult = conditionalInsertAtomic(lockId, keyspace, tablename, primaryKey, queryBank); - musicCore.destroyLockRef(key,lockId); + MusicCore.destroyLockRef(key,lockId); if (criticalPutResult.getMessage().contains("insert")) criticalPutResult .setMessage("Insert values: "); @@ -105,11 +102,11 @@ public class MusicConditional { return criticalPutResult; } else { - musicCore.destroyLockRef(key,lockId); + MusicCore.destroyLockRef(key,lockId); return lockAcqResult; } } catch (Exception e) { - musicCore.destroyLockRef(key,lockId); + MusicCore.destroyLockRef(key,lockId); return new ReturnType(ResultType.FAILURE, e.getMessage()); } @@ -122,18 +119,18 @@ public class MusicConditional { try { String fullyQualifiedKey = keyspace + "." + tableName + "." + primaryKey; - ReturnType lockAcqResult = musicCore.acquireLock(fullyQualifiedKey, lockId); + ReturnType lockAcqResult = MusicCore.acquireLock(fullyQualifiedKey, lockId); if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { try { - results = MusicDataStoreHandle.getDSHandle().executeCriticalGet(queryBank.get(MusicUtil.SELECT)); + results = MusicCore.getDSHandle().executeCriticalGet(queryBank.get(MusicUtil.SELECT)); } catch (Exception e) { return new ReturnType(ResultType.FAILURE, e.getMessage()); } if (results.all().isEmpty()) { - MusicDataStoreHandle.getDSHandle().executePut(queryBank.get(MusicUtil.INSERT), "critical"); + MusicCore.getDSHandle().executePut(queryBank.get(MusicUtil.INSERT), "critical"); return new ReturnType(ResultType.SUCCESS, "insert"); } else { - MusicDataStoreHandle.getDSHandle().executePut(queryBank.get(MusicUtil.UPDATE), "critical"); + MusicCore.getDSHandle().executePut(queryBank.get(MusicUtil.UPDATE), "critical"); return new ReturnType(ResultType.SUCCESS, "update"); } } else { @@ -155,20 +152,20 @@ public class MusicConditional { public static ReturnType update(Map queryBank, String keyspace, String tableName, String primaryKey,String primaryKeyValue,String planId,String cascadeColumnName,Map cascadeColumnValues) { String key = keyspace + "." + tableName + "." + primaryKeyValue; - String lockId = musicCore.createLockReference(key); + String lockId = MusicCore.createLockReference(key); long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); try { - ReturnType lockAcqResult = musicCore.acquireLockWithLease(key, lockId, leasePeriod); + ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod); if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { return updateAtomic(lockId, keyspace, tableName, primaryKey,primaryKeyValue, queryBank,planId,cascadeColumnValues,cascadeColumnName); } else { - musicCore.destroyLockRef(key,lockId); + MusicCore.destroyLockRef(key,lockId); return lockAcqResult; } } catch (Exception e) { - musicCore.destroyLockRef(key,lockId); + MusicCore.destroyLockRef(key,lockId); return new ReturnType(ResultType.FAILURE, e.getMessage()); } @@ -179,9 +176,9 @@ public class MusicConditional { String key = keyspace + "." + tableName + "." + primaryKeyValue; long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); try { - ReturnType lockAcqResult = musicCore.acquireLockWithLease(key, lockId, leasePeriod); + ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod); if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - Row row = MusicDataStoreHandle.getDSHandle().executeCriticalGet(queryBank.get(MusicUtil.SELECT)).one(); + Row row = MusicCore.getDSHandle().executeCriticalGet(queryBank.get(MusicUtil.SELECT)).one(); if(row != null) { Map updatedValues = cascadeColumnUpdateSpecific(row, cascadeColumnValues, casscadeColumnName, planId); @@ -194,14 +191,14 @@ public class MusicConditional { update.addValue(MusicUtil.convertToActualDataType(DataType.text(), vector_ts)); update.addValue(MusicUtil.convertToActualDataType(DataType.text(), primaryKeyValue)); try { - MusicDataStoreHandle.getDSHandle().executePut(update, "critical"); + MusicCore.getDSHandle().executePut(update, "critical"); } catch (Exception ex) { return new ReturnType(ResultType.FAILURE, ex.getMessage()); } }else { return new ReturnType(ResultType.FAILURE,"Cannot find data related to key: "+primaryKey); } - MusicDataStoreHandle.getDSHandle().executePut(queryBank.get(MusicUtil.UPSERT), "critical"); + MusicCore.getDSHandle().executePut(queryBank.get(MusicUtil.UPSERT), "critical"); return new ReturnType(ResultType.SUCCESS, "update success"); } else { diff --git a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java index 63c104f7..4292749c 100644 --- a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java +++ b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java @@ -38,19 +38,17 @@ import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import org.codehaus.jettison.json.JSONObject; -import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; 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.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; import org.onap.music.response.jsonobjects.JsonResponse; import org.onap.music.rest.RestMusicAdminAPI; -import org.onap.music.service.impl.MusicCassaCore; -import org.onap.music.authentication.MusicAuthentication; import org.onap.music.conductor.*; @@ -105,7 +103,7 @@ public class RestMusicConditionalAPI { Map authMap = null; try { - authMap = MusicAuthentication.authenticate(ns, userId, password, keyspace, aid, "insertIntoTable"); + authMap = MusicCore.authenticate(ns, userId, password, keyspace, aid, "insertIntoTable"); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); @@ -176,7 +174,7 @@ public class RestMusicConditionalAPI { Map authMap = null; try { - authMap = MusicAuthentication.authenticate(ns, userId, password, keyspace, aid, "updateTable"); + authMap = MusicCore.authenticate(ns, userId, password, keyspace, aid, "updateTable"); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); @@ -196,7 +194,7 @@ public class RestMusicConditionalAPI { String planId = casscadeColumnData.get("key").toString(); Map casscadeColumnValueMap = (Map) casscadeColumnData.get("value"); TableMetadata tableInfo = null; - tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename); + tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType(); String primaryId = tableInfo.getPrimaryKey().get(0).getName(); diff --git a/src/main/java/org/onap/music/datastore/CassaDataStore.java b/src/main/java/org/onap/music/datastore/CassaDataStore.java new file mode 100644 index 00000000..14934f6e --- /dev/null +++ b/src/main/java/org/onap/music/datastore/CassaDataStore.java @@ -0,0 +1,475 @@ +/* + * ============LICENSE_START========================================== + * org.onap.music + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property + * =================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================= + * ==================================================================== + */ +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 java.util.UUID; + +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.main.MusicUtil; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.ColumnDefinitions; +import com.datastax.driver.core.ColumnDefinitions.Definition; +import com.datastax.driver.core.ConsistencyLevel; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.KeyspaceMetadata; +import com.datastax.driver.core.Metadata; +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.TableMetadata; +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.core.utils.UUIDs; +import com.sun.jersey.core.util.Base64; + +/** + * @author nelson24 + * + */ +/** + * @author bharathb + * + */ +/** + * @author bharathb + * + */ +/** + * @author bharathb + * + */ +/** + * @author bharathb + * + */ +/** + * @author bharathb + * + */ +/** + * @author bharathb + * + */ +/** + * @author bharathb + * + */ +public class CassaDataStore { + + private Session session; + private Cluster cluster; + + + + /** + * @param session + */ + public void setSession(Session session) { + this.session = session; + } + + /** + * @param session + */ + public Session getSession() { + return session; + } + + /** + * @param cluster + */ + public void setCluster(Cluster cluster) { + this.cluster = cluster; + } + + + + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CassaDataStore.class); + + /** + * + */ + public CassaDataStore() { + connectToCassaCluster(); + } + + + /** + * @param cluster + * @param session + */ + public CassaDataStore(Cluster cluster, Session session) { + this.session = session; + this.cluster = cluster; + } + + /** + * + * @param remoteIp + * @throws MusicServiceException + */ + public CassaDataStore(String remoteIp) { + try { + connectToCassaCluster(remoteIp); + } catch (MusicServiceException e) { + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); + } + } + + /** + * + * @return + */ + private ArrayList getAllPossibleLocalIps() { + ArrayList allPossibleIps = new ArrayList(); + try { + Enumeration en = NetworkInterface.getNetworkInterfaces(); + while (en.hasMoreElements()) { + NetworkInterface ni = (NetworkInterface) en.nextElement(); + Enumeration 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 IP addresses and connects to multiple cassandra + * clusters. + */ + private void connectToCassaCluster() { + Iterator it = getAllPossibleLocalIps().iterator(); + String address = "localhost"; + logger.info(EELFLoggerDelegate.applicationLogger, + "Connecting to cassa cluster: Iterating through possible ips:" + + getAllPossibleLocalIps()); + while (it.hasNext()) { + try { + 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(); + + break; + } catch (NoHostAvailableException e) { + address = it.next(); + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.HOSTUNAVAILABLE, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); + } + } + } + + /** + * + */ + public void close() { + session.close(); + } + + /** + * This method connects to cassandra cluster on specific address. + * + * @param address + */ + private void connectToCassaCluster(String address) throws MusicServiceException { + 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); + try { + session = cluster.connect(); + } 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()); + } + } + + /** + * + * @param keyspace + * @param tableName + * @param columnName + * @return DataType + */ + public DataType returnColumnDataType(String keyspace, String tableName, String columnName) { + KeyspaceMetadata ks = cluster.getMetadata().getKeyspace(keyspace); + TableMetadata table = ks.getTable(tableName); + return table.getColumn(columnName).getType(); + + } + + /** + * + * @param keyspace + * @param tableName + * @return TableMetadata + */ + public TableMetadata returnColumnMetadata(String keyspace, String tableName) { + KeyspaceMetadata ks = cluster.getMetadata().getKeyspace(keyspace); + return ks.getTable(tableName); + } + + + /** + * Utility function to return the Java specific object type. + * + * @param row + * @param colName + * @param colType + * @return + */ + public Object getColValue(Row row, String colName, DataType colType) { + + switch (colType.getName()) { + case VARCHAR: + return row.getString(colName); + case UUID: + return row.getUUID(colName); + case VARINT: + return row.getVarint(colName); + case BIGINT: + return row.getLong(colName); + case INT: + return row.getInt(colName); + case FLOAT: + return row.getFloat(colName); + case DOUBLE: + return row.getDouble(colName); + case BOOLEAN: + return row.getBool(colName); + case MAP: + return row.getMap(colName, String.class, String.class); + case LIST: + return row.getList(colName, String.class); + default: + return null; + } + } + + public byte[] getBlobValue(Row row, String colName, DataType colType) { + ByteBuffer bb = row.getBytes(colName); + byte[] data = bb.array(); + return data; + } + + public boolean doesRowSatisfyCondition(Row row, Map condition) throws Exception { + ColumnDefinitions colInfo = row.getColumnDefinitions(); + + for (Map.Entry entry : condition.entrySet()) { + String colName = entry.getKey(); + DataType colType = colInfo.getType(colName); + Object columnValue = getColValue(row, colName, colType); + Object conditionValue = MusicUtil.convertToActualDataType(colType, entry.getValue()); + if (columnValue.equals(conditionValue) == false) + return false; + } + return true; + } + + /** + * Utility function to store ResultSet values in to a MAP for output. + * + * @param results + * @return MAP + */ + public Map> marshalData(ResultSet results) { + Map> resultMap = + new HashMap>(); + int counter = 0; + for (Row row : results) { + ColumnDefinitions colInfo = row.getColumnDefinitions(); + HashMap resultOutput = new HashMap(); + for (Definition definition : colInfo) { + if (!definition.getName().equals("vector_ts")) { + if(definition.getType().toString().toLowerCase().contains("blob")) { + resultOutput.put(definition.getName(), + getBlobValue(row, definition.getName(), definition.getType())); + } + else + resultOutput.put(definition.getName(), + getColValue(row, definition.getName(), definition.getType())); + } + } + resultMap.put("row " + counter, resultOutput); + counter++; + } + return resultMap; + } + + + // Prepared Statements 1802 additions + /** + * This Method performs DDL and DML operations on Cassandra using specified consistency level + * + * @param queryObject Object containing cassandra prepared query and values. + * @param consistency Specify consistency level for data synchronization across cassandra + * replicas + * @return Boolean Indicates operation success or failure + * @throws MusicServiceException + * @throws MusicQueryException + */ + public boolean executePut(PreparedQueryObject queryObject, String consistency) + throws MusicServiceException, MusicQueryException { + + boolean result = false; + + 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() + "]"); + } + logger.info(EELFLoggerDelegate.applicationLogger, + "In preprared Execute Put: the actual insert query:" + + queryObject.getQuery() + "; the values" + + queryObject.getValues()); + PreparedStatement preparedInsert = null; + try { + + preparedInsert = session.prepare(queryObject.getQuery()); + + } catch(InvalidQueryException iqe) { + logger.error(EELFLoggerDelegate.errorLogger, iqe.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR); + throw new MusicQueryException(iqe.getMessage()); + }catch(Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR); + throw new MusicQueryException(e.getMessage()); + } + + try { + if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { + logger.info(EELFLoggerDelegate.applicationLogger, "Executing critical put query"); + preparedInsert.setConsistencyLevel(ConsistencyLevel.QUORUM); + } else if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) { + logger.info(EELFLoggerDelegate.applicationLogger, "Executing simple put query"); + preparedInsert.setConsistencyLevel(ConsistencyLevel.ONE); + } + + ResultSet rs = session.execute(preparedInsert.bind(queryObject.getValues().toArray())); + result = rs.wasApplied(); + + } + 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()); + } + + + return result; + } + + /** + * This method performs DDL operations on Cassandra using consistency level ONE. + * + * @param queryObject Object containing cassandra prepared query and values. + * @return ResultSet + * @throws MusicServiceException + * @throws MusicQueryException + */ + public ResultSet executeEventualGet(PreparedQueryObject queryObject) + throws MusicServiceException, MusicQueryException { + + if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) { + logger.error(EELFLoggerDelegate.errorLogger, "",AppMessages.QUERYERROR+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); + throw new MusicQueryException("Ill formed queryObject for the request = " + "[" + + queryObject.getQuery() + "]"); + } + logger.info(EELFLoggerDelegate.applicationLogger, + "Executing Eventual get query:" + queryObject.getQuery()); + + ResultSet results = null; + try { + PreparedStatement preparedEventualGet = session.prepare(queryObject.getQuery()); + preparedEventualGet.setConsistencyLevel(ConsistencyLevel.ONE); + results = session.execute(preparedEventualGet.bind(queryObject.getValues().toArray())); + + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); + throw new MusicServiceException(ex.getMessage()); + } + 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 + */ + public ResultSet executeCriticalGet(PreparedQueryObject queryObject) + throws MusicServiceException, MusicQueryException { + 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() + "]"); + } + logger.info(EELFLoggerDelegate.applicationLogger, + "Executing Critical get query:" + queryObject.getQuery()); + PreparedStatement preparedEventualGet = session.prepare(queryObject.getQuery()); + preparedEventualGet.setConsistencyLevel(ConsistencyLevel.QUORUM); + ResultSet results = null; + try { + results = session.execute(preparedEventualGet.bind(queryObject.getValues().toArray())); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); + throw new MusicServiceException(ex.getMessage()); + } + return results; + + } +} + diff --git a/src/main/java/org/onap/music/datastore/CassaLockStore.java b/src/main/java/org/onap/music/datastore/CassaLockStore.java new file mode 100644 index 00000000..e03a1c07 --- /dev/null +++ b/src/main/java/org/onap/music/datastore/CassaLockStore.java @@ -0,0 +1,173 @@ +package org.onap.music.datastore; + +import java.util.UUID; + +import org.onap.music.eelf.logging.EELFLoggerDelegate; +import org.onap.music.exceptions.MusicQueryException; +import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.main.MusicUtil; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.utils.UUIDs; + +/* + * This is the lock store that is built on top of Cassandra that is used by MUSIC to maintain lock state. + */ + +public class CassaLockStore { + + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CassaLockStore.class); + + public class LockObject{ + public String lockRef; + public String createTime; + public String acquireTime; + public LockObject(String lockRef, String createTime, String acquireTime) { + this.lockRef = lockRef; + this.acquireTime = acquireTime; + this.createTime = createTime; + + } + } + CassaDataStore dsHandle; + public CassaLockStore() { + dsHandle = new CassaDataStore(); + } + + public CassaLockStore(CassaDataStore dsHandle) { + this.dsHandle=dsHandle; + } + + + /** + * + * This method creates a shadow locking table for every main table in Cassandra. This table tracks all information regarding locks. + * @param keyspace of the application. + * @param table of the application. + * @return true if the operation was successful. + * @throws MusicServiceException + * @throws MusicQueryException + */ + public boolean createLockQueue(String keyspace, String table) throws MusicServiceException, MusicQueryException { + logger.info(EELFLoggerDelegate.applicationLogger, + "Create lock queue/table for " + keyspace+"."+table); + table = "lockQ_"+table; + String tabQuery = "CREATE TABLE IF NOT EXISTS "+keyspace+"."+table + + " ( key text, lockReference bigint, createTime text, acquireTime text, guard bigint static, PRIMARY KEY ((key), lockReference) ) " + + "WITH CLUSTERING ORDER BY (lockReference ASC);"; + System.out.println(tabQuery); + PreparedQueryObject queryObject = new PreparedQueryObject(); + + queryObject.appendQueryString(tabQuery); + boolean result; + result = dsHandle.executePut(queryObject, "eventual"); + return result; + } + + /** + * This method creates a lock reference for each invocation. The lock references are monotonically increasing timestamps. + * @param keyspace of the locks. + * @param table of the locks. + * @param lockName is the primary key of the lock table + * @return the UUID lock reference. + * @throws MusicServiceException + * @throws MusicQueryException + */ + public String genLockRefandEnQueue(String keyspace, String table, String lockName) throws MusicServiceException, MusicQueryException { + logger.info(EELFLoggerDelegate.applicationLogger, + "Create lock reference for " + keyspace + "." + table + "." + lockName); + table = "lockQ_" + table; + long lockEpochMillis = System.currentTimeMillis(); + long lockRef = lockEpochMillis; + + logger.info(EELFLoggerDelegate.applicationLogger, + "Created lock reference for " + keyspace + "." + table + "." + lockName + ":" + lockRef); + + PreparedQueryObject queryObject = new PreparedQueryObject(); + String defaultQuery = " UPDATE " + keyspace + "." + table + " SET guard=-1 WHERE key=? IF guard = NULL;"; + + queryObject.addValue(lockName); + queryObject.appendQueryString(defaultQuery); + boolean dqResult = dsHandle.executePut(queryObject, "critical"); +// System.out.println("dqResult: " + dqResult); + + + queryObject = new PreparedQueryObject(); + String insQuery = "BEGIN BATCH" + + " UPDATE " + keyspace + "." + table + " SET guard=? WHERE key=? IF guard < ?;" + + " INSERT INTO " + keyspace + "." + table + + "(key, lockReference, createTime, acquireTime) VALUES (?,?,?,?) IF NOT EXISTS; APPLY BATCH;"; + + queryObject.addValue(lockRef); + queryObject.addValue(lockName); + queryObject.addValue(lockRef); + + queryObject.addValue(lockName); + queryObject.addValue(lockRef); + queryObject.addValue(String.valueOf(lockEpochMillis)); + queryObject.addValue("0"); + queryObject.appendQueryString(insQuery); + boolean pResult = dsHandle.executePut(queryObject, "critical"); +// System.out.println("pResult: " + pResult); + return String.valueOf(lockRef); + } + + + /** + * This method returns the top of lock table/queue for the key. + * @param keyspace of the application. + * @param table of the application. + * @param key is the primary key of the application table + * @return the UUID lock reference. + * @throws MusicServiceException + * @throws MusicQueryException + */ + public LockObject peekLockQueue(String keyspace, String table, String key) throws MusicServiceException, MusicQueryException{ + logger.info(EELFLoggerDelegate.applicationLogger, + "Peek in lock table for " + keyspace+"."+table+"."+key); + table = "lockQ_"+table; + String selectQuery = "select * from "+keyspace+"."+table+" where key='"+key+"' LIMIT 1;"; + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString(selectQuery); + ResultSet results = dsHandle.executeEventualGet(queryObject); + Row row = results.one(); + String lockReference = "" + row.getLong("lockReference"); + String createTime = row.getString("createTime"); + String acquireTime = row.getString("acquireTime"); + + return new LockObject(lockReference, createTime,acquireTime); + } + + + /** + * This method removes the lock ref from the lock table/queue for the key. + * @param keyspace of the application. + * @param table of the application. + * @param key is the primary key of the application table + * @param lockReference the lock reference that needs to be dequeued. + * @throws MusicServiceException + * @throws MusicQueryException + */ + public void deQueueLockRef(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{ + table = "lockQ_"+table; + PreparedQueryObject queryObject = new PreparedQueryObject(); + Long lockReferenceL = Long.parseLong(lockReference); + String deleteQuery = "delete from "+keyspace+"."+table+" where key='"+key+"' AND lockReference ="+lockReferenceL+" IF EXISTS;"; + queryObject.appendQueryString(deleteQuery); + dsHandle.executePut(queryObject, "critical"); + } + + + public void updateLockAcquireTime(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{ + table = "lockQ_"+table; + PreparedQueryObject queryObject = new PreparedQueryObject(); + Long lockReferenceL = Long.parseLong(lockReference); + String updateQuery = "update "+keyspace+"."+table+" set acquireTime='"+ System.currentTimeMillis()+"' where key='"+key+"' AND lockReference = "+lockReferenceL+" IF EXISTS;"; + queryObject.appendQueryString(updateQuery); + dsHandle.executePut(queryObject, "eventual"); + + } + + +} diff --git a/src/main/java/org/onap/music/datastore/Condition.java b/src/main/java/org/onap/music/datastore/Condition.java deleted file mode 100644 index f08a8754..00000000 --- a/src/main/java/org/onap/music/datastore/Condition.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.datastore; - -import java.util.Map; - -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; - - - - public class Condition { - Map conditions; - PreparedQueryObject selectQueryForTheRow; - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(Condition.class); - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); - - public Condition(Map 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); - } - } - diff --git a/src/main/java/org/onap/music/datastore/MusicDataStore.java b/src/main/java/org/onap/music/datastore/MusicDataStore.java deleted file mode 100644 index 725cefe6..00000000 --- a/src/main/java/org/onap/music/datastore/MusicDataStore.java +++ /dev/null @@ -1,475 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -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 java.util.UUID; - -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.main.MusicUtil; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.ColumnDefinitions; -import com.datastax.driver.core.ColumnDefinitions.Definition; -import com.datastax.driver.core.ConsistencyLevel; -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.KeyspaceMetadata; -import com.datastax.driver.core.Metadata; -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.TableMetadata; -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.core.utils.UUIDs; -import com.sun.jersey.core.util.Base64; - -/** - * @author nelson24 - * - */ -/** - * @author bharathb - * - */ -/** - * @author bharathb - * - */ -/** - * @author bharathb - * - */ -/** - * @author bharathb - * - */ -/** - * @author bharathb - * - */ -/** - * @author bharathb - * - */ -/** - * @author bharathb - * - */ -public class MusicDataStore { - - private Session session; - private Cluster cluster; - - - - /** - * @param session - */ - public void setSession(Session session) { - this.session = session; - } - - /** - * @param session - */ - public Session getSession() { - return session; - } - - /** - * @param cluster - */ - public void setCluster(Cluster cluster) { - this.cluster = cluster; - } - - - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStore.class); - - /** - * - */ - public MusicDataStore() { - connectToCassaCluster(); - } - - - /** - * @param cluster - * @param session - */ - public MusicDataStore(Cluster cluster, Session session) { - this.session = session; - this.cluster = cluster; - } - - /** - * - * @param remoteIp - * @throws MusicServiceException - */ - public MusicDataStore(String remoteIp) { - try { - connectToCassaCluster(remoteIp); - } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); - } - } - - /** - * - * @return - */ - private ArrayList getAllPossibleLocalIps() { - ArrayList allPossibleIps = new ArrayList(); - try { - Enumeration en = NetworkInterface.getNetworkInterfaces(); - while (en.hasMoreElements()) { - NetworkInterface ni = (NetworkInterface) en.nextElement(); - Enumeration 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 IP addresses and connects to multiple cassandra - * clusters. - */ - private void connectToCassaCluster() { - Iterator it = getAllPossibleLocalIps().iterator(); - String address = "localhost"; - logger.info(EELFLoggerDelegate.applicationLogger, - "Connecting to cassa cluster: Iterating through possible ips:" - + getAllPossibleLocalIps()); - while (it.hasNext()) { - try { - 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(); - - break; - } catch (NoHostAvailableException e) { - address = it.next(); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.HOSTUNAVAILABLE, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); - } - } - } - - /** - * - */ - public void close() { - session.close(); - } - - /** - * This method connects to cassandra cluster on specific address. - * - * @param address - */ - private void connectToCassaCluster(String address) throws MusicServiceException { - 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); - try { - session = cluster.connect(); - } 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()); - } - } - - /** - * - * @param keyspace - * @param tableName - * @param columnName - * @return DataType - */ - public DataType returnColumnDataType(String keyspace, String tableName, String columnName) { - KeyspaceMetadata ks = cluster.getMetadata().getKeyspace(keyspace); - TableMetadata table = ks.getTable(tableName); - return table.getColumn(columnName).getType(); - - } - - /** - * - * @param keyspace - * @param tableName - * @return TableMetadata - */ - public TableMetadata returnColumnMetadata(String keyspace, String tableName) { - KeyspaceMetadata ks = cluster.getMetadata().getKeyspace(keyspace); - return ks.getTable(tableName); - } - - - /** - * Utility function to return the Java specific object type. - * - * @param row - * @param colName - * @param colType - * @return - */ - public Object getColValue(Row row, String colName, DataType colType) { - - switch (colType.getName()) { - case VARCHAR: - return row.getString(colName); - case UUID: - return row.getUUID(colName); - case VARINT: - return row.getVarint(colName); - case BIGINT: - return row.getLong(colName); - case INT: - return row.getInt(colName); - case FLOAT: - return row.getFloat(colName); - case DOUBLE: - return row.getDouble(colName); - case BOOLEAN: - return row.getBool(colName); - case MAP: - return row.getMap(colName, String.class, String.class); - case LIST: - return row.getList(colName, String.class); - default: - return null; - } - } - - public byte[] getBlobValue(Row row, String colName, DataType colType) { - ByteBuffer bb = row.getBytes(colName); - byte[] data = bb.array(); - return data; - } - - public boolean doesRowSatisfyCondition(Row row, Map condition) throws Exception { - ColumnDefinitions colInfo = row.getColumnDefinitions(); - - for (Map.Entry entry : condition.entrySet()) { - String colName = entry.getKey(); - DataType colType = colInfo.getType(colName); - Object columnValue = getColValue(row, colName, colType); - Object conditionValue = MusicUtil.convertToActualDataType(colType, entry.getValue()); - if (columnValue.equals(conditionValue) == false) - return false; - } - return true; - } - - /** - * Utility function to store ResultSet values in to a MAP for output. - * - * @param results - * @return MAP - */ - public Map> marshalData(ResultSet results) { - Map> resultMap = - new HashMap>(); - int counter = 0; - for (Row row : results) { - ColumnDefinitions colInfo = row.getColumnDefinitions(); - HashMap resultOutput = new HashMap(); - for (Definition definition : colInfo) { - if (!definition.getName().equals("vector_ts")) { - if(definition.getType().toString().toLowerCase().contains("blob")) { - resultOutput.put(definition.getName(), - getBlobValue(row, definition.getName(), definition.getType())); - } - else - resultOutput.put(definition.getName(), - getColValue(row, definition.getName(), definition.getType())); - } - } - resultMap.put("row " + counter, resultOutput); - counter++; - } - return resultMap; - } - - - // Prepared Statements 1802 additions - /** - * This Method performs DDL and DML operations on Cassandra using specified consistency level - * - * @param queryObject Object containing cassandra prepared query and values. - * @param consistency Specify consistency level for data synchronization across cassandra - * replicas - * @return Boolean Indicates operation success or failure - * @throws MusicServiceException - * @throws MusicQueryException - */ - public boolean executePut(PreparedQueryObject queryObject, String consistency) - throws MusicServiceException, MusicQueryException { - - boolean result = false; - - 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() + "]"); - } - logger.info(EELFLoggerDelegate.applicationLogger, - "In preprared Execute Put: the actual insert query:" - + queryObject.getQuery() + "; the values" - + queryObject.getValues()); - PreparedStatement preparedInsert = null; - try { - - preparedInsert = session.prepare(queryObject.getQuery()); - - } catch(InvalidQueryException iqe) { - logger.error(EELFLoggerDelegate.errorLogger, iqe.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR); - throw new MusicQueryException(iqe.getMessage()); - }catch(Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR); - throw new MusicQueryException(e.getMessage()); - } - - try { - if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { - logger.info(EELFLoggerDelegate.applicationLogger, "Executing critical put query"); - preparedInsert.setConsistencyLevel(ConsistencyLevel.QUORUM); - } else if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) { - logger.info(EELFLoggerDelegate.applicationLogger, "Executing simple put query"); - preparedInsert.setConsistencyLevel(ConsistencyLevel.ONE); - } - - ResultSet rs = session.execute(preparedInsert.bind(queryObject.getValues().toArray())); - result = rs.wasApplied(); - - } - 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()); - } - - - return result; - } - - /** - * This method performs DDL operations on Cassandra using consistency level ONE. - * - * @param queryObject Object containing cassandra prepared query and values. - * @return ResultSet - * @throws MusicServiceException - * @throws MusicQueryException - */ - public ResultSet executeEventualGet(PreparedQueryObject queryObject) - throws MusicServiceException, MusicQueryException { - - if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) { - logger.error(EELFLoggerDelegate.errorLogger, "",AppMessages.QUERYERROR+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); - throw new MusicQueryException("Ill formed queryObject for the request = " + "[" - + queryObject.getQuery() + "]"); - } - logger.info(EELFLoggerDelegate.applicationLogger, - "Executing Eventual get query:" + queryObject.getQuery()); - - ResultSet results = null; - try { - PreparedStatement preparedEventualGet = session.prepare(queryObject.getQuery()); - preparedEventualGet.setConsistencyLevel(ConsistencyLevel.ONE); - results = session.execute(preparedEventualGet.bind(queryObject.getValues().toArray())); - - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); - throw new MusicServiceException(ex.getMessage()); - } - 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 - */ - public ResultSet executeCriticalGet(PreparedQueryObject queryObject) - throws MusicServiceException, MusicQueryException { - 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() + "]"); - } - logger.info(EELFLoggerDelegate.applicationLogger, - "Executing Critical get query:" + queryObject.getQuery()); - PreparedStatement preparedEventualGet = session.prepare(queryObject.getQuery()); - preparedEventualGet.setConsistencyLevel(ConsistencyLevel.QUORUM); - ResultSet results = null; - try { - results = session.execute(preparedEventualGet.bind(queryObject.getValues().toArray())); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); - throw new MusicServiceException(ex.getMessage()); - } - return results; - - } -} - diff --git a/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java b/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java deleted file mode 100644 index a2d9386b..00000000 --- a/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.datastore; - -import java.util.HashMap; -import java.util.Map; - -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; - -public class MusicDataStoreHandle { - - public static MusicDataStore mDstoreHandle = null; - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStoreHandle.class); - - /** - * - * @param remoteIp - * @return - */ - public static MusicDataStore getDSHandle(String remoteIp) { - 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(remoteIp); - } - 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 { - - 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."); - } - // Quick Fix - Best to put this into every call to getDSHandle? - if (! MusicUtil.getMyCassaHost().equals("localhost") ) { - mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost()); - } else { - mDstoreHandle = new MusicDataStore(); - } - } - if(mDstoreHandle.getSession() == null) { - String message = "Connection to Cassandra has not been enstablished." - + " Please check connection properites and reboot."; - logger.info(EELFLoggerDelegate.applicationLogger, message); - throw new MusicServiceException(message); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire data store handle:" + (end - start) + " ms"); - return mDstoreHandle; - } - - /** - * - * @param keyspace - * @param tablename - * @return - * @throws MusicServiceException - */ - public static TableMetadata returnColumnMetadata(String keyspace, String tablename) throws MusicServiceException { - return MusicDataStoreHandle.getDSHandle().returnColumnMetadata(keyspace, tablename); - } - - /** - * - * @param results - * @return - * @throws MusicServiceException - */ - public static Map> marshallResults(ResultSet results) throws MusicServiceException { - return MusicDataStoreHandle.getDSHandle().marshalData(results); - } - -} diff --git a/src/main/java/org/onap/music/datastore/MusicLockState.java b/src/main/java/org/onap/music/datastore/MusicLockState.java new file mode 100644 index 00000000..60a15b13 --- /dev/null +++ b/src/main/java/org/onap/music/datastore/MusicLockState.java @@ -0,0 +1,41 @@ +package org.onap.music.datastore; +public class MusicLockState { + public enum LockStatus { + UNLOCKED, BEING_LOCKED, LOCKED + };// captures the state of the lock + + private LockStatus lockStatus; + private String lockHolder; + private String errorMessage = null; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public MusicLockState(LockStatus lockStatus, String lockHolder) { + super(); + this.lockStatus = lockStatus; + this.lockHolder = lockHolder; + } + + public LockStatus getLockStatus() { + return lockStatus; + } + + public void setLockStatus(LockStatus lockStatus) { + this.lockStatus = lockStatus; + } + + public String getLockHolder() { + return lockHolder; + } + + public void setLockHolder(String lockHolder) { + this.lockHolder = lockHolder; + } + +} diff --git a/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java b/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java index 65e19501..5f6329a1 100644 --- a/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java +++ b/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java @@ -26,10 +26,9 @@ import java.util.UUID; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.eelf.logging.EELFLoggerDelegate; import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; import com.datastax.driver.core.ConsistencyLevel; @@ -40,7 +39,6 @@ import com.datastax.driver.core.ConsistencyLevel; public class MusicHealthCheck { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class); - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); private String cassandrHost; private String zookeeperHost; @@ -80,7 +78,7 @@ public class MusicHealthCheck { PreparedQueryObject pQuery = new PreparedQueryObject(); pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)"); pQuery.addValue(UUID.randomUUID()); - ResultType rs = musicCore.nonKeyRelatedPut(pQuery, consistency); + ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency); System.out.println(rs); if (rs != null) { return Boolean.TRUE; @@ -96,7 +94,7 @@ public class MusicHealthCheck { pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)"); ResultType rs = null ; try { - rs = musicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString()); + rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString()); } catch (MusicServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java b/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java deleted file mode 100644 index f753aab7..00000000 --- a/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java +++ /dev/null @@ -1,175 +0,0 @@ -package org.onap.music.lockingservice.cassandra; - -import java.util.UUID; - -import org.onap.music.datastore.MusicDataStore; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.exceptions.MusicQueryException; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.MusicUtil; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.core.utils.UUIDs; - -/* - * This is the lock store that is built on top of Cassandra that is used by MUSIC to maintain lock state. - */ - -public class CassaLockStore { - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CassaLockStore.class); - - public class LockObject{ - public String lockRef; - public String createTime; - public String acquireTime; - public LockObject(String lockRef, String createTime, String acquireTime) { - this.lockRef = lockRef; - this.acquireTime = acquireTime; - this.createTime = createTime; - - } - } - MusicDataStore dsHandle; - public CassaLockStore() { - dsHandle = new MusicDataStore(); - } - - public CassaLockStore(MusicDataStore dsHandle) { - this.dsHandle=dsHandle; - } - - - /** - * - * This method creates a shadow locking table for every main table in Cassandra. This table tracks all information regarding locks. - * @param keyspace of the application. - * @param table of the application. - * @return true if the operation was successful. - * @throws MusicServiceException - * @throws MusicQueryException - */ - public boolean createLockQueue(String keyspace, String table) throws MusicServiceException, MusicQueryException { - logger.info(EELFLoggerDelegate.applicationLogger, - "Create lock queue/table for " + keyspace+"."+table); - table = "lockQ_"+table; - String tabQuery = "CREATE TABLE IF NOT EXISTS "+keyspace+"."+table - + " ( key text, lockReference bigint, createTime text, acquireTime text, guard bigint static, PRIMARY KEY ((key), lockReference) ) " - + "WITH CLUSTERING ORDER BY (lockReference ASC);"; - System.out.println(tabQuery); - PreparedQueryObject queryObject = new PreparedQueryObject(); - - queryObject.appendQueryString(tabQuery); - boolean result; - result = dsHandle.executePut(queryObject, "eventual"); - return result; - } - - /** - * This method creates a lock reference for each invocation. The lock references are monotonically increasing timestamps. - * @param keyspace of the locks. - * @param table of the locks. - * @param lockName is the primary key of the lock table - * @return the UUID lock reference. - * @throws MusicServiceException - * @throws MusicQueryException - */ - public String genLockRefandEnQueue(String keyspace, String table, String lockName) throws MusicServiceException, MusicQueryException { - logger.info(EELFLoggerDelegate.applicationLogger, - "Create lock reference for " + keyspace + "." + table + "." + lockName); - table = "lockQ_" + table; - long lockEpochMillis = System.currentTimeMillis(); - long lockRef = lockEpochMillis; - - logger.info(EELFLoggerDelegate.applicationLogger, - "Created lock reference for " + keyspace + "." + table + "." + lockName + ":" + lockRef); - - PreparedQueryObject queryObject = new PreparedQueryObject(); - String defaultQuery = " UPDATE " + keyspace + "." + table + " SET guard=-1 WHERE key=? IF guard = NULL;"; - - queryObject.addValue(lockName); - queryObject.appendQueryString(defaultQuery); - boolean dqResult = dsHandle.executePut(queryObject, "critical"); -// System.out.println("dqResult: " + dqResult); - - - queryObject = new PreparedQueryObject(); - String insQuery = "BEGIN BATCH" + - " UPDATE " + keyspace + "." + table + " SET guard=? WHERE key=? IF guard < ?;" + - " INSERT INTO " + keyspace + "." + table + - "(key, lockReference, createTime, acquireTime) VALUES (?,?,?,?) IF NOT EXISTS; APPLY BATCH;"; - - queryObject.addValue(lockRef); - queryObject.addValue(lockName); - queryObject.addValue(lockRef); - - queryObject.addValue(lockName); - queryObject.addValue(lockRef); - queryObject.addValue(String.valueOf(lockEpochMillis)); - queryObject.addValue("0"); - queryObject.appendQueryString(insQuery); - boolean pResult = dsHandle.executePut(queryObject, "critical"); -// System.out.println("pResult: " + pResult); - return String.valueOf(lockRef); - } - - - /** - * This method returns the top of lock table/queue for the key. - * @param keyspace of the application. - * @param table of the application. - * @param key is the primary key of the application table - * @return the UUID lock reference. - * @throws MusicServiceException - * @throws MusicQueryException - */ - public LockObject peekLockQueue(String keyspace, String table, String key) throws MusicServiceException, MusicQueryException{ - logger.info(EELFLoggerDelegate.applicationLogger, - "Peek in lock table for " + keyspace+"."+table+"."+key); - table = "lockQ_"+table; - String selectQuery = "select * from "+keyspace+"."+table+" where key='"+key+"' LIMIT 1;"; - PreparedQueryObject queryObject = new PreparedQueryObject(); - queryObject.appendQueryString(selectQuery); - ResultSet results = dsHandle.executeEventualGet(queryObject); - Row row = results.one(); - String lockReference = "" + row.getLong("lockReference"); - String createTime = row.getString("createTime"); - String acquireTime = row.getString("acquireTime"); - - return new LockObject(lockReference, createTime,acquireTime); - } - - - /** - * This method removes the lock ref from the lock table/queue for the key. - * @param keyspace of the application. - * @param table of the application. - * @param key is the primary key of the application table - * @param lockReference the lock reference that needs to be dequeued. - * @throws MusicServiceException - * @throws MusicQueryException - */ - public void deQueueLockRef(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{ - table = "lockQ_"+table; - PreparedQueryObject queryObject = new PreparedQueryObject(); - Long lockReferenceL = Long.parseLong(lockReference); - String deleteQuery = "delete from "+keyspace+"."+table+" where key='"+key+"' AND lockReference ="+lockReferenceL+" IF EXISTS;"; - queryObject.appendQueryString(deleteQuery); - dsHandle.executePut(queryObject, "critical"); - } - - - public void updateLockAcquireTime(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{ - table = "lockQ_"+table; - PreparedQueryObject queryObject = new PreparedQueryObject(); - Long lockReferenceL = Long.parseLong(lockReference); - String updateQuery = "update "+keyspace+"."+table+" set acquireTime='"+ System.currentTimeMillis()+"' where key='"+key+"' AND lockReference = "+lockReferenceL+" IF EXISTS;"; - queryObject.appendQueryString(updateQuery); - dsHandle.executePut(queryObject, "eventual"); - - } - - -} diff --git a/src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java b/src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java deleted file mode 100644 index f5070a1d..00000000 --- a/src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.onap.music.lockingservice.cassandra; -public class MusicLockState { - public enum LockStatus { - UNLOCKED, BEING_LOCKED, LOCKED - };// captures the state of the lock - - private LockStatus lockStatus; - private String lockHolder; - private String errorMessage = null; - - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } - - public MusicLockState(LockStatus lockStatus, String lockHolder) { - super(); - this.lockStatus = lockStatus; - this.lockHolder = lockHolder; - } - - public LockStatus getLockStatus() { - return lockStatus; - } - - public void setLockStatus(LockStatus lockStatus) { - this.lockStatus = lockStatus; - } - - public String getLockHolder() { - return lockHolder; - } - - public void setLockHolder(String lockHolder) { - this.lockHolder = lockHolder; - } - -} diff --git a/src/main/java/org/onap/music/main/CachingUtil.java b/src/main/java/org/onap/music/main/CachingUtil.java index 5c253f5e..d3654118 100755 --- a/src/main/java/org/onap/music/main/CachingUtil.java +++ b/src/main/java/org/onap/music/main/CachingUtil.java @@ -41,8 +41,6 @@ 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.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; import com.att.eelf.configuration.EELFLogger; import com.datastax.driver.core.DataType; @@ -69,7 +67,6 @@ public class CachingUtil implements Runnable { private static CacheAccess> musicValidateCache = JCS.getInstance("musicValidateCache"); private static Map userAttempts = new HashMap<>(); private static Map lastFailedTime = new HashMap<>(); - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); public boolean isCacheRefreshNeeded() { if (aafCache.get("initBlankMap") == null) @@ -94,7 +91,7 @@ public class CachingUtil implements Runnable { logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),AppMessages.CACHEERROR, ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); e1.printStackTrace(); } - ResultSet rs = musicCore.get(pQuery); + ResultSet rs = MusicCore.get(pQuery); Iterator it = rs.iterator(); Map map = null; while (it.hasNext()) { @@ -248,7 +245,7 @@ public class CachingUtil implements Runnable { + namespace + "' allow filtering"); Row rs = null; try { - rs = musicCore.get(pQuery).one(); + rs = MusicCore.get(pQuery).one(); } catch(InvalidQueryException e) { logger.error(EELFLoggerDelegate.errorLogger,"Exception admin keyspace not configured."+e.getMessage()); throw new MusicServiceException("Please make sure admin.keyspace_master table is configured."); @@ -272,7 +269,7 @@ public class CachingUtil implements Runnable { pQuery.appendQueryString( "SELECT uuid from admin.keyspace_master where keyspace_name = '" + keyspace + "' allow filtering"); - Row rs = musicCore.get(pQuery).one(); + Row rs = MusicCore.get(pQuery).one(); try { uuid = rs.getUUID("uuid").toString(); } catch (Exception e) { @@ -289,7 +286,7 @@ public class CachingUtil implements Runnable { pQuery.appendQueryString( "SELECT application_name from admin.keyspace_master where keyspace_name = '" + keyspace + "' allow filtering"); - Row rs = musicCore.get(pQuery).one(); + Row rs = MusicCore.get(pQuery).one(); try { appName = rs.getString("application_name"); } catch (Exception e) { @@ -338,7 +335,7 @@ public class CachingUtil implements Runnable { } Row rs = null; try { - rs = musicCore.get(queryObject).one(); + rs = MusicCore.get(queryObject).one(); } catch (MusicServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -389,7 +386,7 @@ public class CachingUtil implements Runnable { } Row rs = null; try { - rs = musicCore.get(queryObject).one(); + rs = MusicCore.get(queryObject).one(); } catch (MusicServiceException e) { e.printStackTrace(); resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage()); diff --git a/src/main/java/org/onap/music/main/CronJobManager.java b/src/main/java/org/onap/music/main/CronJobManager.java index 2ed1a97e..5b7a8de4 100644 --- a/src/main/java/org/onap/music/main/CronJobManager.java +++ b/src/main/java/org/onap/music/main/CronJobManager.java @@ -34,8 +34,6 @@ import javax.servlet.annotation.WebListener; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicLockingException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -44,7 +42,6 @@ import com.datastax.driver.core.Row; public class CronJobManager implements ServletContextListener { private ScheduledExecutorService scheduler; - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); @Override public void contextInitialized(ServletContextEvent event) { @@ -54,7 +51,7 @@ public class CronJobManager implements ServletContextListener { String consistency = MusicUtil.EVENTUAL; pQuery.appendQueryString("CREATE TABLE IF NOT EXISTS admin.locks ( lock_id text PRIMARY KEY, ctime text)"); try { - ResultType result = musicCore.nonKeyRelatedPut(pQuery, consistency); + ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency); } catch (MusicServiceException e1) { e1.printStackTrace(); } @@ -63,7 +60,7 @@ public class CronJobManager implements ServletContextListener { pQuery.appendQueryString( "select * from admin.locks"); try { - ResultSet rs = musicCore.get(pQuery); + ResultSet rs = MusicCore.get(pQuery); Iterator it = rs.iterator(); StringBuilder deleteKeys = new StringBuilder(); Boolean expiredKeys = false; @@ -74,7 +71,7 @@ public class CronJobManager implements ServletContextListener { if(System.currentTimeMillis() >= ctime + 24 * 60 * 60 * 1000) { expiredKeys = true; String new_id = id.substring(1); - musicCore.deleteLock(new_id); + MusicCore.deleteLock(new_id); deleteKeys.append(id).append(","); } else { @@ -106,7 +103,7 @@ public class CronJobManager implements ServletContextListener { expiredKeys = true; String id = pair.getKey(); deleteKeys.append("'").append(id).append("'").append(","); - musicCore.deleteLock(id.substring(1)); + MusicCore.deleteLock(id.substring(1)); MusicUtil.zkNodeMap.remove(id); } catch (MusicLockingException e) { @@ -132,7 +129,7 @@ public class CronJobManager implements ServletContextListener { pQuery.appendQueryString( "DELETE FROM admin.locks WHERE lock_id IN ("+deleteKeys+")"); try { - musicCore.nonKeyRelatedPut(pQuery, "eventual"); + MusicCore.nonKeyRelatedPut(pQuery, "eventual"); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/org/onap/music/main/MusicCore.java b/src/main/java/org/onap/music/main/MusicCore.java new file mode 100644 index 00000000..d7c5bcec --- /dev/null +++ b/src/main/java/org/onap/music/main/MusicCore.java @@ -0,0 +1,826 @@ +/* + * ============LICENSE_START========================================== + * org.onap.music + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property + * =================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================= + * ==================================================================== + */ +package org.onap.music.main; + + +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.UUID; + +import org.onap.music.datastore.CassaDataStore; +import org.onap.music.datastore.CassaLockStore; +import org.onap.music.datastore.CassaLockStore.LockObject; +import org.onap.music.datastore.MusicLockState; +import org.onap.music.datastore.PreparedQueryObject; +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.MusicLockingException; +import org.onap.music.exceptions.MusicQueryException; +import org.onap.music.exceptions.MusicServiceException; + +import com.datastax.driver.core.ColumnDefinitions; +import com.datastax.driver.core.ColumnDefinitions.Definition; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.TableMetadata; + + +/** + * This class ..... + * + * + */ +public class MusicCore { + + public static CassaLockStore mLockHandle = null; + public static CassaDataStore mDstoreHandle = null; + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicCore.class); + private static boolean unitTestRun=true; + + public static class Condition { + Map conditions; + PreparedQueryObject selectQueryForTheRow; + + public Condition(Map conditions, PreparedQueryObject selectQueryForTheRow) { + this.conditions = conditions; + this.selectQueryForTheRow = selectQueryForTheRow; + } + + public boolean testCondition() throws Exception { + // first generate the row + ResultSet results = quorumGet(selectQueryForTheRow); + Row row = results.one(); + return getDSHandle().doesRowSatisfyCondition(row, conditions); + } + } + + + public static CassaLockStore getLockingServiceHandle() throws MusicLockingException { + logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring lock store handle"); + long start = System.currentTimeMillis(); + + if (mLockHandle == null) { + try { + mLockHandle = new CassaLockStore(getDSHandle()); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKHANDLE,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); + throw new MusicLockingException("Failed to aquire Locl store handle " + e); + } + } + long end = System.currentTimeMillis(); + logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire lock store handle:" + (end - start) + " ms"); + return mLockHandle; + } + + /** + * + * @param remoteIp + * @return + */ + public static CassaDataStore getDSHandle(String remoteIp) { + 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 CassaDataStore(remoteIp); + } + 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 CassaDataStore getDSHandle() throws MusicServiceException { + + 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."); + } + // Quick Fix - Best to put this into every call to getDSHandle? + if (! MusicUtil.getMyCassaHost().equals("localhost") ) { + mDstoreHandle = new CassaDataStore(MusicUtil.getMyCassaHost()); + } else { + mDstoreHandle = new CassaDataStore(); + } + } + if(mDstoreHandle.getSession() == null) { + String message = "Connection to Cassandra has not been enstablished." + + " Please check connection properites and reboot."; + logger.info(EELFLoggerDelegate.applicationLogger, message); + throw new MusicServiceException(message); + } + long end = System.currentTimeMillis(); + logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire data store handle:" + (end - start) + " ms"); + return mDstoreHandle; + } + + public static String createLockReference(String fullyQualifiedKey) { + String[] splitString = fullyQualifiedKey.split("\\."); + String keyspace = splitString[0]; + String table = splitString[1]; + String lockName = splitString[2]; + + logger.info(EELFLoggerDelegate.applicationLogger,"Creating lock reference for lock name:" + lockName); + long start = System.currentTimeMillis(); + String lockReference = null; + try { + lockReference = "" + getLockingServiceHandle().genLockRefandEnQueue(keyspace, table, lockName); + } catch (MusicLockingException | MusicServiceException | MusicQueryException e) { + e.printStackTrace(); + } + long end = System.currentTimeMillis(); + logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to create lock reference:" + (end - start) + " ms"); + return lockReference; + } + + + public static ReturnType acquireLockWithLease(String fullyQualifiedKey, String lockReference, long leasePeriod) throws MusicLockingException, MusicQueryException, MusicServiceException { + evictExpiredLockHolder(fullyQualifiedKey,leasePeriod); + return acquireLock(fullyQualifiedKey, lockReference); + } + + private static void evictExpiredLockHolder(String fullyQualifiedKey, long leasePeriod) throws MusicLockingException, MusicQueryException, MusicServiceException { + + String[] splitString = fullyQualifiedKey.split("\\."); + String keyspace = splitString[0]; + String table = splitString[1]; + String primaryKeyValue = splitString[2]; + + LockObject currentLockHolderObject = getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue); + + /* Release the lock of the previous holder if it has expired. if the update to the acquire time has not reached due to network delays, simply use the create time as the + * reference*/ + + long referenceTime = Math.max(Long.parseLong(currentLockHolderObject.acquireTime), Long.parseLong(currentLockHolderObject.createTime)); + if((System.currentTimeMillis() - referenceTime) > leasePeriod) { + forciblyReleaseLock(fullyQualifiedKey, currentLockHolderObject.lockRef+""); + logger.info(EELFLoggerDelegate.applicationLogger, currentLockHolderObject.lockRef+" forcibly released"); + } + } + + private static ReturnType isTopOfLockStore(String keyspace, String table, String primaryKeyValue, String lockReference) throws MusicLockingException, MusicQueryException, MusicServiceException { + + //return failure to lock holders too early or already evicted from the lock store + String topOfLockStoreS = getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue).lockRef; + long topOfLockStoreL = Long.parseLong(topOfLockStoreS); + long lockReferenceL = Long.parseLong(lockReference); + + if(lockReferenceL > topOfLockStoreL) { + logger.info(EELFLoggerDelegate.applicationLogger, lockReference+" is not the lock holder yet"); + return new ReturnType(ResultType.FAILURE, lockReference+" is not the lock holder yet"); + } + + + if(lockReferenceL < topOfLockStoreL) { + logger.info(EELFLoggerDelegate.applicationLogger, lockReference+" is no longer/or was never in the lock store queue"); + return new ReturnType(ResultType.FAILURE, lockReference+" is no longer/or was never in the lock store queue"); + } + + return new ReturnType(ResultType.SUCCESS, lockReference+" is top of lock store"); + } + + public static ReturnType acquireLock(String fullyQualifiedKey, String lockReference) throws MusicLockingException, MusicQueryException, MusicServiceException { + String[] splitString = fullyQualifiedKey.split("\\."); + String keyspace = splitString[0]; + String table = splitString[1]; + String primaryKeyValue = splitString[2]; + + ReturnType result = isTopOfLockStore(keyspace, table, primaryKeyValue, lockReference); + + if(result.getResult().equals(ResultType.FAILURE)) + return result;//not top of the lock store q + + //check to see if the value of the key has to be synced in case there was a forceful release + String syncTable = keyspace+".unsyncedKeys_"+table; + String query = "select * from "+syncTable+" where key='"+fullyQualifiedKey+"';"; + PreparedQueryObject readQueryObject = new PreparedQueryObject(); + readQueryObject.appendQueryString(query); + ResultSet results = getDSHandle().executeCriticalGet(readQueryObject); + if (results.all().size() != 0) { + logger.info("In acquire lock: Since there was a forcible release, need to sync quorum!"); + try { + syncQuorum(keyspace, table, primaryKeyValue); + } catch (Exception e) { + StringWriter sw = new StringWriter(); + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR506E] Failed to aquire lock ",ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); + String exceptionAsString = sw.toString(); + return new ReturnType(ResultType.FAILURE, "Exception thrown while syncing key:\n" + exceptionAsString); + } + String cleanQuery = "delete * from music_internal.unsynced_keys where key='"+fullyQualifiedKey+"';"; + PreparedQueryObject deleteQueryObject = new PreparedQueryObject(); + deleteQueryObject.appendQueryString(cleanQuery); + getDSHandle().executePut(deleteQueryObject, "critical"); + } + + getLockingServiceHandle().updateLockAcquireTime(keyspace, table, primaryKeyValue, lockReference); + + return new ReturnType(ResultType.SUCCESS, lockReference+" is the lock holder for the key"); + } + + + + /** + * + * @param tableQueryObject + * @param consistency + * @return Boolean Indicates success or failure + * @throws MusicServiceException + * + * + */ + public static ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject, String consistency) throws MusicServiceException { + boolean result = false; + + try { + //create shadow locking table + result = getLockingServiceHandle().createLockQueue(keyspace, table); + if(result == false) + return ResultType.FAILURE; + + result = false; + + //create table to track unsynced_keys + table = "unsyncedKeys_"+table; + + String tabQuery = "CREATE TABLE IF NOT EXISTS "+keyspace+"."+table + + " ( key text,PRIMARY KEY (key) );"; + System.out.println(tabQuery); + PreparedQueryObject queryObject = new PreparedQueryObject(); + + queryObject.appendQueryString(tabQuery); + result = false; + result = getDSHandle().executePut(queryObject, "eventual"); + + + //create actual table + result = getDSHandle().executePut(tableQueryObject, consistency); + } catch (MusicQueryException | MusicServiceException | MusicLockingException ex) { + logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); + throw new MusicServiceException(ex.getMessage()); + } + return result?ResultType.SUCCESS:ResultType.FAILURE; + } + + private static void syncQuorum(String keyspace, String table, String primaryKeyValue) throws Exception { + logger.info(EELFLoggerDelegate.applicationLogger,"Performing sync operation---"); + PreparedQueryObject selectQuery = new PreparedQueryObject(); + PreparedQueryObject updateQuery = new PreparedQueryObject(); + + // get the primary key d + TableMetadata tableInfo = returnColumnMetadata(keyspace, table); + String primaryKeyName = tableInfo.getPrimaryKey().get(0).getName();// we only support single + // primary key + DataType primaryKeyType = tableInfo.getPrimaryKey().get(0).getType(); + Object cqlFormattedPrimaryKeyValue = + MusicUtil.convertToActualDataType(primaryKeyType, primaryKeyValue); + + // get the row of data from a quorum + selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + table + " WHERE " + + primaryKeyName + "= ?" + ";"); + selectQuery.addValue(cqlFormattedPrimaryKeyValue); + ResultSet results = null; + try { + results = getDSHandle().executeCriticalGet(selectQuery); + // write it back to a quorum + Row row = results.one(); + ColumnDefinitions colInfo = row.getColumnDefinitions(); + int totalColumns = colInfo.size(); + int counter = 1; + StringBuilder fieldValueString = new StringBuilder(""); + for (Definition definition : colInfo) { + String colName = definition.getName(); + if (colName.equals(primaryKeyName)) + continue; + DataType colType = definition.getType(); + Object valueObj = getDSHandle().getColValue(row, colName, colType); + Object valueString = MusicUtil.convertToActualDataType(colType, valueObj); + fieldValueString.append(colName + " = ?"); + updateQuery.addValue(valueString); + if (counter != (totalColumns - 1)) + fieldValueString.append(","); + counter = counter + 1; + } + updateQuery.appendQueryString("UPDATE " + keyspace + "." + table + " SET " + + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";"); + updateQuery.addValue(cqlFormattedPrimaryKeyValue); + + getDSHandle().executePut(updateQuery, "critical"); + } catch (MusicServiceException | MusicQueryException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.QUERYERROR +""+updateQuery ,ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR); + } + } + + + + + /** + * + * @param query + * @return ResultSet + */ + public static ResultSet quorumGet(PreparedQueryObject query) { + ResultSet results = null; + try { + results = getDSHandle().executeCriticalGet(query); + } catch (MusicServiceException | MusicQueryException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.MAJOR, ErrorTypes.GENERALSERVICEERROR); + + } + return results; + + } + + /** + * + * @param results + * @return + * @throws MusicServiceException + */ + public static Map> marshallResults(ResultSet results) throws MusicServiceException { + return getDSHandle().marshalData(results); + } + + /** + * + * @param fullyQualifiedKey lockName + * @return + */ + public static String whoseTurnIsIt(String fullyQualifiedKey) { + String[] splitString = fullyQualifiedKey.split("\\."); + String keyspace = splitString[0]; + String table = splitString[1]; + String primaryKeyValue = splitString[2]; + try { + return getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue)+""; + } catch (MusicLockingException | MusicServiceException | MusicQueryException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+fullyQualifiedKey ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); + } + return null; + } + + /** + * + * @param lockReference + * @return + */ + public static String getLockNameFromId(String lockReference) { + StringTokenizer st = new StringTokenizer(lockReference); + return st.nextToken("$"); + } + + public static MusicLockState destroyLockRef(String fullyQualifiedKey, String lockReference) { + long start = System.currentTimeMillis(); + String[] splitString = fullyQualifiedKey.split("\\."); + String keyspace = splitString[0]; + String table = splitString[1]; + String primaryKeyValue = splitString[2]; + try { + getLockingServiceHandle().deQueueLockRef(keyspace, table, primaryKeyValue, lockReference); + } catch (MusicLockingException | MusicServiceException | MusicQueryException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DESTROYLOCK+lockReference ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); + } + long end = System.currentTimeMillis(); + logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to destroy lock reference:" + (end - start) + " ms"); + return getMusicLockState(fullyQualifiedKey); + } + + public static MusicLockState voluntaryReleaseLock(String fullyQualifiedKey, String lockReference) throws MusicLockingException{ + return destroyLockRef(fullyQualifiedKey, lockReference); + } + + public static MusicLockState forciblyReleaseLock(String fullyQualifiedKey, String lockReference) throws MusicLockingException, MusicServiceException, MusicQueryException{ + String[] splitString = fullyQualifiedKey.split("\\."); + String keyspace = splitString[0]; + String table = splitString[1]; + + //leave a signal that this key could potentially be unsynchronized + String syncTable = keyspace+".unsyncedKeys_"+table; + PreparedQueryObject queryObject = new PreparedQueryObject(); + String values = "(?)"; + queryObject.addValue(fullyQualifiedKey); + String insQuery = "insert into "+syncTable+" (key) values "+values+"';"; + queryObject.appendQueryString(insQuery); + getDSHandle().executePut(queryObject, "critical"); + + //now release the lock + return destroyLockRef(fullyQualifiedKey, lockReference); + } + + /** + * + * @param lockName + * @throws MusicLockingException + */ + public static void deleteLock(String lockName) throws MusicLockingException { + //deprecated + } + + + + /** + * + * @param keyspace + * @param tablename + * @return + * @throws MusicServiceException + */ + public static TableMetadata returnColumnMetadata(String keyspace, String tablename) throws MusicServiceException { + return getDSHandle().returnColumnMetadata(keyspace, tablename); + } + + + + + // Prepared Query Additions. + + /** + * + * @param queryObject + * @return ReturnType + * @throws MusicServiceException + */ + public static ReturnType eventualPut(PreparedQueryObject queryObject) { + boolean result = false; + try { + result = getDSHandle().executePut(queryObject, MusicUtil.EVENTUAL); + } catch (MusicServiceException | MusicQueryException ex) { + logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() + " " + ex.getCause() + " " + ex); + return new ReturnType(ResultType.FAILURE, ex.getMessage()); + } + if (result) { + return new ReturnType(ResultType.SUCCESS, "Success"); + } else { + return new ReturnType(ResultType.FAILURE, "Failure"); + } + } + + /** + * + * @param keyspace + * @param table + * @param primaryKeyValue + * @param queryObject + * @param lockReference + * @return + */ + public static ReturnType criticalPut(String keyspace, String table, String primaryKeyValue, + PreparedQueryObject queryObject, String lockReference, Condition conditionInfo) { + long start = System.currentTimeMillis(); + try { + ReturnType result = isTopOfLockStore(keyspace, table, primaryKeyValue, lockReference); + if(result.getResult().equals(ResultType.FAILURE)) + return result;//not top of the lock store q + + if (conditionInfo != null) + try { + if (conditionInfo.testCondition() == false) + return new ReturnType(ResultType.FAILURE, + "Lock acquired but the condition is not true"); + } catch (Exception e) { + return new ReturnType(ResultType.FAILURE, + "Exception thrown while checking the condition, check its sanctity:\n" + + e.getMessage()); + } + + String query = queryObject.getQuery(); + query = query.replaceFirst("SET", "using TIMESTAMP "+ v2sTimeStampInMicroseconds(lockReference, System.currentTimeMillis())+ " SET"); + queryObject.replaceQueryString(query); + getDSHandle().executePut(queryObject, MusicUtil.CRITICAL); + long end = System.currentTimeMillis(); + logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms"); + }catch (MusicQueryException | MusicServiceException | MusicLockingException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + return new ReturnType(ResultType.FAILURE, + "Exception thrown while doing the critical put\n" + + e.getMessage()); + } + return new ReturnType(ResultType.SUCCESS, "Update performed"); + } + + + /** + * + * @param queryObject + * @param consistency + * @return Boolean Indicates success or failure + * @throws MusicServiceException + * + * + */ + public static ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException { + // this is mainly for some functions like keyspace creation etc which does not + // really need the bells and whistles of Music locking. + boolean result = false; + try { + result = getDSHandle().executePut(queryObject, consistency); + } catch (MusicQueryException | MusicServiceException ex) { + logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(), AppMessages.UNKNOWNERROR, + ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); + throw new MusicServiceException(ex.getMessage()); + } + return result ? ResultType.SUCCESS : ResultType.FAILURE; + } + + /** + * This method performs DDL operation on cassandra. + * + * @param queryObject query object containing prepared query and values + * @return ResultSet + * @throws MusicServiceException + */ + public static ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException { + ResultSet results = null; + try { + results = getDSHandle().executeEventualGet(queryObject); + } catch (MusicQueryException | MusicServiceException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + throw new MusicServiceException(e.getMessage()); + } + return results; + } + + /** + * This method performs DDL operations on cassandra, if the the resource is available. Lock ID + * is used to check if the resource is free. + * + * @param keyspace name of the keyspace + * @param table name of the table + * @param primaryKeyValue primary key value + * @param queryObject query object containing prepared query and values + * @param lockReference lock ID to check if the resource is free to perform the operation. + * @return ResultSet + */ + public static ResultSet criticalGet(String keyspace, String table, String primaryKeyValue, + PreparedQueryObject queryObject, String lockReference) throws MusicServiceException { + ResultSet results = null; + + try { + ReturnType result = isTopOfLockStore(keyspace, table, primaryKeyValue, lockReference); + if(result.getResult().equals(ResultType.FAILURE)) + return null;//not top of the lock store q + results = getDSHandle().executeCriticalGet(queryObject); + } catch (MusicQueryException | MusicServiceException | MusicLockingException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); + } + return results; + } + + /** + * This method performs DML operation on cassandra, when the lock of the dd is acquired. + * + * @param keyspaceName name of the keyspace + * @param tableName name of the table + * @param primaryKey primary key value + * @param queryObject query object containing prepared query and values + * @return ReturnType + * @throws MusicLockingException + * @throws MusicServiceException + * @throws MusicQueryException + */ + public static ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey, + PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException, MusicQueryException, MusicServiceException { + long start = System.currentTimeMillis(); + String fullyQualifiedKey = keyspaceName + "." + tableName + "." + primaryKey; + String lockReference = createLockReference(fullyQualifiedKey); + long lockCreationTime = System.currentTimeMillis(); + ReturnType lockAcqResult = acquireLock(fullyQualifiedKey, lockReference); + long lockAcqTime = System.currentTimeMillis(); + if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { + logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockReference); + ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, + queryObject, lockReference, conditionInfo); + long criticalPutTime = System.currentTimeMillis(); + voluntaryReleaseLock(fullyQualifiedKey,lockReference); + long lockDeleteTime = System.currentTimeMillis(); + String timingInfo = "|lock creation time:" + (lockCreationTime - start) + + "|lock accquire time:" + (lockAcqTime - lockCreationTime) + + "|critical put time:" + (criticalPutTime - lockAcqTime) + + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|"; + criticalPutResult.setTimingInfo(timingInfo); + return criticalPutResult; + } else { + logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockReference); + voluntaryReleaseLock(fullyQualifiedKey,lockReference); + return lockAcqResult; + } + } + + + + + /** + * This method performs DDL operation on cassasndra, when the lock for the resource is acquired. + * + * @param keyspaceName name of the keyspace + * @param tableName name of the table + * @param primaryKey primary key value + * @param queryObject query object containing prepared query and values + * @return ResultSet + * @throws MusicServiceException + * @throws MusicLockingException + * @throws MusicQueryException + */ + public static ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey, + PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException, MusicQueryException { + String fullyQualifiedKey = keyspaceName + "." + tableName + "." + primaryKey; + String lockReference = createLockReference(fullyQualifiedKey); + long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); + ReturnType lockAcqResult = acquireLock(fullyQualifiedKey, lockReference); + if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { + logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockReference); + ResultSet result = + criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockReference); + voluntaryReleaseLock(fullyQualifiedKey,lockReference); + return result; + } else { + voluntaryReleaseLock(fullyQualifiedKey,lockReference); + logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockReference); + return null; + } + } + + + public static MusicLockState getMusicLockState(String fullyQualifiedKey) { + return null; + } + + /** + * authenticate user logic + * + * @param nameSpace + * @param userId + * @param password + * @param keyspace + * @param aid + * @param operation + * @return + * @throws Exception + */ + public static Map authenticate(String nameSpace, String userId, + String password, String keyspace, String aid, String operation) + throws Exception { + Map resultMap = new HashMap<>(); + String uuid = null; + resultMap = CachingUtil.validateRequest(nameSpace, userId, password, keyspace, aid, + operation); + if (!resultMap.isEmpty()) + return resultMap; + String isAAFApp = null; + try { + isAAFApp= CachingUtil.isAAFApplication(nameSpace); + } catch(MusicServiceException e) { + resultMap.put("Exception", e.getMessage()); + return resultMap; + } + if(isAAFApp == null) { + resultMap.put("Exception", "Namespace: "+nameSpace+" doesn't exist. Please make sure ns(appName)" + + " is correct and Application is onboarded."); + return resultMap; + } + boolean isAAF = Boolean.valueOf(isAAFApp); + if (userId == null || password == null) { + logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); + logger.error(EELFLoggerDelegate.errorLogger,"One or more required headers is missing. userId: " + userId + + " :: password: " + password); + resultMap.put("Exception", + "UserId and Password are mandatory for the operation " + operation); + return resultMap; + } + if(!isAAF && !(operation.equals("createKeySpace"))) { + resultMap = CachingUtil.authenticateAIDUser(nameSpace, userId, password, keyspace); + if (!resultMap.isEmpty()) + return resultMap; + + } + if (isAAF && nameSpace != null && userId != null && password != null) { + boolean isValid = true; + try { + isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); + logger.error(EELFLoggerDelegate.errorLogger,"Got exception while AAF authentication for namespace " + nameSpace); + resultMap.put("Exception", e.getMessage()); + } + if (!isValid) { + logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); + resultMap.put("Exception", "User not authenticated..."); + } + if (!resultMap.isEmpty()) + return resultMap; + + } + + if (operation.equals("createKeySpace")) { + logger.info(EELFLoggerDelegate.applicationLogger,"AID is not provided. Creating new UUID for keyspace."); + PreparedQueryObject pQuery = new PreparedQueryObject(); + pQuery.appendQueryString( + "select uuid from admin.keyspace_master where application_name=? and username=? and keyspace_name=? allow filtering"); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace)); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), + MusicUtil.DEFAULTKEYSPACENAME)); + + try { + Row rs = MusicCore.get(pQuery).one(); + uuid = rs.getUUID("uuid").toString(); + resultMap.put("uuid", "existing"); + } catch (Exception e) { + logger.info(EELFLoggerDelegate.applicationLogger,"No UUID found in DB. So creating new UUID."); + uuid = CachingUtil.generateUUID(); + resultMap.put("uuid", "new"); + } + resultMap.put("aid", uuid); + } + + return resultMap; + } + + /** + * @param lockName + * @return + */ + public static Map validateLock(String lockName) { + Map resultMap = new HashMap<>(); + String[] locks = lockName.split("\\."); + if(locks.length < 3) { + resultMap.put("Exception", "Invalid lock. Please make sure lock is of the type keyspaceName.tableName.primaryKey"); + return resultMap; + } + String keyspace= locks[0]; + if(keyspace.startsWith("$")) + keyspace = keyspace.substring(1); + resultMap.put("keyspace",keyspace); + return resultMap; + } + + + /** + * Given the time of write for an update in a critical section, this method provides a transformed timestamp + * that ensures that a previous lock holder who is still alive can never corrupt a later critical section. + * The main idea is to us the lock reference to clearly demarcate the timestamps across critical sections. + * @param the UUID lock reference associated with the write. + * @param the long timeOfWrite which is the actual time at which the write took place + * @throws MusicServiceException + * @throws MusicQueryException + */ + private static long v2sTimeStampInMicroseconds(String lockReference, long timeOfWrite) throws MusicServiceException, MusicQueryException{ + long lockEpochMillis = Long.parseLong(lockReference); + + long lockEternityMillis = lockEpochMillis - MusicUtil.MusicEternityEpochMillis; + + long ts = lockEternityMillis * MusicUtil.MaxCriticalSectionDurationMillis + + (timeOfWrite - lockEpochMillis); + + return ts; + +// long test = (lockReferenceUUID.timestamp()-MusicUtil.MusicEternityEpochMillis); +// long timeStamp = (lockReferenceUUID.timestamp()-MusicUtil.MusicEternityEpochMillis)*MusicUtil.MaxCriticalSectionDurationMillis +// +timeOfWrite; +// return timeStamp; + +// return timeOfWrite*1000; + } + + public static void main(String[] args) { + String x = "axe top"; + x = x.replaceFirst("top", "sword"); + System.out.print(x); //returns sword pickaxe + } +} diff --git a/src/main/java/org/onap/music/main/VotingAppJar.java b/src/main/java/org/onap/music/main/VotingAppJar.java index 68c6923c..1c475639 100644 --- a/src/main/java/org/onap/music/main/VotingAppJar.java +++ b/src/main/java/org/onap/music/main/VotingAppJar.java @@ -7,8 +7,7 @@ import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicLockingException; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; +import org.onap.music.main.MusicCore; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -20,7 +19,6 @@ public class VotingAppJar { String keyspaceName; String tableName; - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); public VotingAppJar() throws MusicServiceException { keyspaceName = "VotingAppForMusic"; @@ -49,7 +47,7 @@ public class VotingAppJar "CREATE KEYSPACE " + keyspaceName + " WITH REPLICATION = " + replicationInfo.toString().replaceAll("=", ":")); try { - musicCore.nonKeyRelatedPut(queryObject, "eventual"); + MusicCore.nonKeyRelatedPut(queryObject, "eventual"); } catch (MusicServiceException e) { if (e.getMessage().equals("Keyspace votingappformusic already exists")) { // ignore @@ -65,7 +63,7 @@ public class VotingAppJar "CREATE TABLE " + keyspaceName + "." + tableName + " (name text PRIMARY KEY, count varint);"); try { - musicCore.createTable(keyspaceName, tableName, queryObject, "eventual"); + MusicCore.createTable(keyspaceName, tableName, queryObject, "eventual"); } catch (MusicServiceException e) { if (e.getMessage().equals("Table votingappformusic.votevount already exists")) { //ignore @@ -81,7 +79,7 @@ public class VotingAppJar "INSERT INTO " + keyspaceName + "." + tableName + " (name, count) " + "VALUES ('"+candidateName+"', 0);"); - musicCore.nonKeyRelatedPut(queryObject, "eventual"); + MusicCore.nonKeyRelatedPut(queryObject, "eventual"); } public void vote() throws MusicLockingException, MusicQueryException, MusicServiceException { @@ -96,13 +94,13 @@ public class VotingAppJar queryObject.appendQueryString( "INSERT INTO " + keyspaceName + "." + tableName + " (name, count) " + "VALUES ('"+candidateName+"', "+numVotes+");"); - musicCore.atomicPut(keyspaceName, tableName, candidateName, queryObject, null); + MusicCore.atomicPut(keyspaceName, tableName, candidateName, queryObject, null); } private void readAllVotes() throws MusicServiceException { PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString("SELECT * FROM " + keyspaceName + "." + tableName); - ResultSet rs = musicCore.get(queryObject); + ResultSet rs = MusicCore.get(queryObject); for(Row candidate : rs.all()) { System.out.println(candidate.getString("name") + " - " + candidate.getVarint("count")); } diff --git a/src/main/java/org/onap/music/response/jsonobjects/JsonResponse.java b/src/main/java/org/onap/music/response/jsonobjects/JsonResponse.java index 958ef6ea..6a843607 100644 --- a/src/main/java/org/onap/music/response/jsonobjects/JsonResponse.java +++ b/src/main/java/org/onap/music/response/jsonobjects/JsonResponse.java @@ -24,7 +24,7 @@ package org.onap.music.response.jsonobjects; import java.util.HashMap; import java.util.Map; -import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus; +import org.onap.music.datastore.MusicLockState.LockStatus; import org.onap.music.main.ResultType; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java index 9ae7bc32..90436499 100755 --- a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java @@ -47,11 +47,9 @@ 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.main.CachingUtil; +import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -65,7 +63,6 @@ import io.swagger.annotations.ApiOperation; public class RestMusicAdminAPI { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicAdminAPI.class); - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); /* * API to onboard an application with MUSIC. This is the mandatory first step. @@ -96,7 +93,7 @@ public class RestMusicAdminAPI { pQuery.appendQueryString( "select uuid from admin.keyspace_master where application_name = ? allow filtering"); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - ResultSet rs = musicCore.get(pQuery); + ResultSet rs = MusicCore.get(pQuery); if (!rs.all().isEmpty()) { resultMap.put("Exception", "Application " + appName + " has already been onboarded. Please contact admin."); @@ -117,7 +114,7 @@ public class RestMusicAdminAPI { pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); - String returnStr = musicCore.eventualPut(pQuery).toString(); + String returnStr = MusicCore.eventualPut(pQuery).toString(); if (returnStr.contains("Failure")) { resultMap.put("Exception", "Oops. Something wrong with onboarding process. Please retry later or contact admin."); @@ -173,7 +170,7 @@ public class RestMusicAdminAPI { if (isAAF != null) pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), Boolean.parseBoolean(isAAF))); - ResultSet rs = musicCore.get(pQuery); + ResultSet rs = MusicCore.get(pQuery); Iterator it = rs.iterator(); while (it.hasNext()) { Row row = (Row) it.next(); @@ -218,20 +215,20 @@ public class RestMusicAdminAPI { "SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?"); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), UUID.fromString(aid))); - Row row = musicCore.get(pQuery).one(); + Row row = MusicCore.get(pQuery).one(); if (row != null) { String ks = row.getString("keyspace_name"); if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) { PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString("DROP KEYSPACE IF EXISTS " + ks + ";"); - musicCore.nonKeyRelatedPut(queryObject, consistency); + MusicCore.nonKeyRelatedPut(queryObject, consistency); } } pQuery = new PreparedQueryObject(); pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ? IF EXISTS"); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), UUID.fromString(aid))); - ResultType result = musicCore.nonKeyRelatedPut(pQuery, consistency); + ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency); if (result == ResultType.SUCCESS) { resultMap.put("Success", "Your application has been deleted successfully"); } else { @@ -248,7 +245,7 @@ public class RestMusicAdminAPI { pQuery.appendQueryString( "select uuid from admin.keyspace_master where application_name = ? allow filtering"); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - ResultSet rs = musicCore.get(pQuery); + ResultSet rs = MusicCore.get(pQuery); List rows = rs.all(); String uuid = null; if (rows.size() == 0) { @@ -264,19 +261,19 @@ public class RestMusicAdminAPI { "SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?"); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), UUID.fromString(uuid))); - Row row = musicCore.get(pQuery).one(); + Row row = MusicCore.get(pQuery).one(); String ks = row.getString("keyspace_name"); if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) { PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString("DROP KEYSPACE " + ks + ";"); - musicCore.nonKeyRelatedPut(queryObject, consistency); + MusicCore.nonKeyRelatedPut(queryObject, consistency); } pQuery = new PreparedQueryObject(); pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?"); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), UUID.fromString(uuid))); - musicCore.eventualPut(pQuery); + MusicCore.eventualPut(pQuery); resultMap.put("Success", "Your application " + appName + " has been deleted."); return Response.status(Status.OK).entity(resultMap).build(); } else { @@ -326,7 +323,7 @@ public class RestMusicAdminAPI { pQuery.appendQueryString( "select uuid from admin.keyspace_master where application_name = ? allow filtering"); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - ResultSet rs = musicCore.get(pQuery); + ResultSet rs = MusicCore.get(pQuery); if (!rs.all().isEmpty()) { resultMap.put("Exception", "Application " + appName + " has already been onboarded. Please contact admin."); @@ -359,7 +356,7 @@ public class RestMusicAdminAPI { pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), UUID.fromString(aid))); - ResultType result = musicCore.nonKeyRelatedPut(pQuery, consistency); + ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency); if (result == ResultType.SUCCESS) { resultMap.put("Success", "Your application has been updated successfully"); diff --git a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java index 986d25ec..99c60b30 100755 --- a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java @@ -48,7 +48,6 @@ import javax.ws.rs.core.UriInfo; import org.apache.commons.lang3.StringUtils; import org.mindrot.jbcrypt.BCrypt; -import org.onap.music.authentication.MusicAuthentication; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonDelete; import org.onap.music.datastore.jsonobjects.JsonInsert; @@ -63,14 +62,12 @@ 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.CachingUtil; -import org.onap.music.datastore.Condition; -import org.onap.music.datastore.MusicDataStoreHandle; +import org.onap.music.main.MusicCore; +import org.onap.music.main.MusicCore.Condition; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; import org.onap.music.response.jsonobjects.JsonResponse; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; @@ -114,7 +111,6 @@ public class RestMusicDataAPI { private static final String XPATCHVERSION = "X-patchVersion"; private static final String NS = "ns"; private static final String VERSION = "v2"; - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); private class RowIdentifier { public String primarKeyValue; @@ -174,7 +170,7 @@ public class RestMusicDataAPI { try { - authMap = MusicAuthentication.authenticate(ns, userId, password, keyspaceName, aid, + authMap = MusicCore.authenticate(ns, userId, password, keyspaceName, aid, "createKeySpace"); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); @@ -219,7 +215,7 @@ public class RestMusicDataAPI { ResultType result = ResultType.FAILURE; try { - result = musicCore.nonKeyRelatedPut(queryObject, consistency); + result = MusicCore.nonKeyRelatedPut(queryObject, consistency); logger.info(EELFLoggerDelegate.applicationLogger, "result = " + result); } catch ( MusicServiceException ex) { logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); @@ -230,12 +226,12 @@ public class RestMusicDataAPI { queryObject = new PreparedQueryObject(); queryObject.appendQueryString("CREATE ROLE IF NOT EXISTS '" + userId + "' WITH PASSWORD = '" + password + "' AND LOGIN = true;"); - musicCore.nonKeyRelatedPut(queryObject, consistency); + MusicCore.nonKeyRelatedPut(queryObject, consistency); queryObject = new PreparedQueryObject(); queryObject.appendQueryString("GRANT ALL PERMISSIONS on KEYSPACE " + keyspaceName + " to '" + userId + "'"); queryObject.appendQueryString(";"); - musicCore.nonKeyRelatedPut(queryObject, consistency); + MusicCore.nonKeyRelatedPut(queryObject, consistency); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); } @@ -256,7 +252,7 @@ public class RestMusicDataAPI { queryObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); CachingUtil.updateMusicCache(keyspaceName, ns); CachingUtil.updateMusicValidateCache(ns, userId, hashedpwd); - musicCore.eventualPut(queryObject); + MusicCore.eventualPut(queryObject); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); return response.status(Response.Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); @@ -290,7 +286,7 @@ public class RestMusicDataAPI { Map userCredentials = MusicUtil.extractBasicAuthentication(authorization); String userId = userCredentials.get(MusicUtil.USERID); String password = userCredentials.get(MusicUtil.PASSWORD); - Map authMap = MusicAuthentication.authenticate(ns, userId, password,keyspaceName, aid, "dropKeySpace"); + Map authMap = MusicCore.authenticate(ns, userId, password,keyspaceName, aid, "dropKeySpace"); if (authMap.containsKey("aid")) authMap.remove("aid"); if (!authMap.isEmpty()) { @@ -306,7 +302,7 @@ public class RestMusicDataAPI { pQuery.appendQueryString( "select count(*) as count from admin.keyspace_master where application_name=? allow filtering;"); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - Row row = musicCore.get(pQuery).one(); + Row row = MusicCore.get(pQuery).one(); long count = row.getLong(0); if (count == 0) { @@ -320,17 +316,17 @@ public class RestMusicDataAPI { pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), MusicUtil.DEFAULTKEYSPACENAME)); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - musicCore.nonKeyRelatedPut(pQuery, consistency); + MusicCore.nonKeyRelatedPut(pQuery, consistency); } else { pQuery = new PreparedQueryObject(); pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?"); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - musicCore.nonKeyRelatedPut(pQuery, consistency); + MusicCore.nonKeyRelatedPut(pQuery, consistency); } PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString("DROP KEYSPACE " + keyspaceName + ";"); - ResultType result = musicCore.nonKeyRelatedPut(queryObject, consistency); + ResultType result = MusicCore.nonKeyRelatedPut(queryObject, consistency); if ( result.equals(ResultType.FAILURE) ) { return response.status(Status.BAD_REQUEST).entity(new JsonResponse(result).setError("Error Deleteing Keyspace " + keyspaceName).toMap()).build(); } @@ -367,7 +363,7 @@ public class RestMusicDataAPI { Map userCredentials = MusicUtil.extractBasicAuthentication(authorization); String userId = userCredentials.get(MusicUtil.USERID); String password = userCredentials.get(MusicUtil.PASSWORD); - Map authMap = MusicAuthentication.authenticate(ns, userId, password, keyspace, + Map authMap = MusicCore.authenticate(ns, userId, password, keyspace, aid, "createTable"); if (authMap.containsKey("aid")) authMap.remove("aid"); @@ -553,7 +549,7 @@ public class RestMusicDataAPI { ResultType result = ResultType.FAILURE; try { //logger.info("cjc query="+queryObject.getQuery()); - result = musicCore.createTable(keyspace, tablename, queryObject, consistency); + result = MusicCore.createTable(keyspace, tablename, queryObject, consistency); } catch (MusicServiceException ex) { logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.MUSICSERVICEERROR); response.status(Status.BAD_REQUEST); @@ -593,7 +589,7 @@ public class RestMusicDataAPI { Map userCredentials = MusicUtil.extractBasicAuthentication(authorization); String userId = userCredentials.get(MusicUtil.USERID); String password = userCredentials.get(MusicUtil.PASSWORD); - Map authMap = MusicAuthentication.authenticate(ns, userId, password, keyspace,aid, "createIndex"); + Map authMap = MusicCore.authenticate(ns, userId, password, keyspace,aid, "createIndex"); if (authMap.containsKey("aid")) authMap.remove("aid"); if (!authMap.isEmpty()) { @@ -611,7 +607,7 @@ public class RestMusicDataAPI { ResultType result = ResultType.FAILURE; try { - result = musicCore.nonKeyRelatedPut(query, "eventual"); + result = MusicCore.nonKeyRelatedPut(query, "eventual"); } catch (MusicServiceException ex) { logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); response.status(Status.BAD_REQUEST); @@ -657,7 +653,7 @@ public class RestMusicDataAPI { Map authMap = null; try { - authMap = MusicAuthentication.authenticate(ns, userId, password, keyspace, + authMap = MusicCore.authenticate(ns, userId, password, keyspace, aid, "insertIntoTable"); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); @@ -674,7 +670,7 @@ public class RestMusicDataAPI { PreparedQueryObject queryObject = new PreparedQueryObject(); TableMetadata tableInfo = null; try { - tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename); + tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); if(tableInfo == null) { return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Table name doesn't exists. Please check the table name.").toMap()).build(); } @@ -799,7 +795,7 @@ public class RestMusicDataAPI { String consistency = insObj.getConsistencyInfo().get("type"); try { if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) { - result = musicCore.eventualPut(queryObject); + result = MusicCore.eventualPut(queryObject); } else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { String lockId = insObj.getConsistencyInfo().get("lockId"); if(lockId == null) { @@ -808,9 +804,9 @@ public class RestMusicDataAPI { return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("LockId cannot be null. Create lock " + "and acquire lock or use ATOMIC instead of CRITICAL").toMap()).build(); } - result = musicCore.criticalPut(keyspace, tablename, primaryKey, queryObject, lockId,null); + result = MusicCore.criticalPut(keyspace, tablename, primaryKey, queryObject, lockId,null); } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { - result = musicCore.atomicPut(keyspace, tablename, primaryKey, queryObject, null); + result = MusicCore.atomicPut(keyspace, tablename, primaryKey, queryObject, null); } } catch (Exception ex) { @@ -863,7 +859,7 @@ public class RestMusicDataAPI { String password = userCredentials.get(MusicUtil.PASSWORD); Map authMap; try { - authMap = MusicAuthentication.authenticate(ns, userId, password, keyspace, + authMap = MusicCore.authenticate(ns, userId, password, keyspace, aid, "updateTable"); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); @@ -888,7 +884,7 @@ public class RestMusicDataAPI { TableMetadata tableInfo; try { - tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename); + tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); } catch (MusicServiceException e) { logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); @@ -974,14 +970,14 @@ public class RestMusicDataAPI { selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " WHERE " + rowId.rowIdString + ";"); selectQuery.addValue(rowId.primarKeyValue); - conditionInfo = new Condition(updateObj.getConditions(), selectQuery); + conditionInfo = new MusicCore.Condition(updateObj.getConditions(), selectQuery); } ReturnType operationResult = null; long jsonParseCompletionTime = System.currentTimeMillis(); if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) - operationResult = musicCore.eventualPut(queryObject); + operationResult = MusicCore.eventualPut(queryObject); else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { String lockId = updateObj.getConsistencyInfo().get("lockId"); if(lockId == null) { @@ -990,11 +986,11 @@ public class RestMusicDataAPI { return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("LockId cannot be null. Create lock " + "and acquire lock or use ATOMIC instead of CRITICAL").toMap()).build(); } - operationResult = musicCore.criticalPut(keyspace, tablename, rowId.primarKeyValue, + operationResult = MusicCore.criticalPut(keyspace, tablename, rowId.primarKeyValue, queryObject, lockId, conditionInfo); } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { try { - operationResult = musicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, + operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, queryObject, conditionInfo); } catch (MusicLockingException | MusicQueryException | MusicServiceException e) { logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); @@ -1067,7 +1063,7 @@ public class RestMusicDataAPI { String password = userCredentials.get(MusicUtil.PASSWORD); Map authMap = null; try { - authMap = MusicAuthentication.authenticate(ns, userId, password, keyspace, + authMap = MusicCore.authenticate(ns, userId, password, keyspace, aid, "deleteFromTable"); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); @@ -1131,7 +1127,7 @@ public class RestMusicDataAPI { selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " WHERE " + rowId.rowIdString + ";"); selectQuery.addValue(rowId.primarKeyValue); - conditionInfo = new Condition(delObj.getConditions(), selectQuery); + conditionInfo = new MusicCore.Condition(delObj.getConditions(), selectQuery); } String consistency = delObj.getConsistencyInfo().get("type"); @@ -1139,7 +1135,7 @@ public class RestMusicDataAPI { ReturnType operationResult = null; try { if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) - operationResult = musicCore.eventualPut(queryObject); + operationResult = MusicCore.eventualPut(queryObject); else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { String lockId = delObj.getConsistencyInfo().get("lockId"); if(lockId == null) { @@ -1148,10 +1144,10 @@ public class RestMusicDataAPI { return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("LockId cannot be null. Create lock " + "and acquire lock or use ATOMIC instead of CRITICAL").toMap()).build(); } - operationResult = musicCore.criticalPut(keyspace, tablename, rowId.primarKeyValue, + operationResult = MusicCore.criticalPut(keyspace, tablename, rowId.primarKeyValue, queryObject, lockId, conditionInfo); } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { - operationResult = musicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, + operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, queryObject, conditionInfo); } } catch (MusicLockingException | MusicQueryException | MusicServiceException e) { @@ -1203,7 +1199,7 @@ public class RestMusicDataAPI { String userId = userCredentials.get(MusicUtil.USERID); String password = userCredentials.get(MusicUtil.PASSWORD); Map authMap = - MusicAuthentication.authenticate(ns, userId, password, keyspace, aid, "dropTable"); + MusicCore.authenticate(ns, userId, password, keyspace, aid, "dropTable"); if (authMap.containsKey("aid")) authMap.remove("aid"); if (!authMap.isEmpty()) { @@ -1215,7 +1211,7 @@ public class RestMusicDataAPI { PreparedQueryObject query = new PreparedQueryObject(); query.appendQueryString("DROP TABLE " + keyspace + "." + tablename + ";"); try { - return response.status(Status.OK).entity(new JsonResponse(musicCore.nonKeyRelatedPut(query, consistency)).toMap()).build(); + return response.status(Status.OK).entity(new JsonResponse(MusicCore.nonKeyRelatedPut(query, consistency)).toMap()).build(); } catch (MusicServiceException ex) { logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); @@ -1258,7 +1254,7 @@ public class RestMusicDataAPI { Map userCredentials = MusicUtil.extractBasicAuthentication(authorization); String userId = userCredentials.get(MusicUtil.USERID); String password = userCredentials.get(MusicUtil.PASSWORD); - Map authMap = MusicAuthentication.authenticate(ns, userId, password, keyspace,aid, "selectCritical"); + Map authMap = MusicCore.authenticate(ns, userId, password, keyspace,aid, "selectCritical"); if (authMap.containsKey("aid")) authMap.remove("aid"); if (!authMap.isEmpty()) { @@ -1290,13 +1286,13 @@ public class RestMusicDataAPI { return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("LockId cannot be null. Create lock " + "and acquire lock or use ATOMIC instead of CRITICAL").toMap()).build(); } - results = musicCore.criticalGet(keyspace, tablename, rowId.primarKeyValue, queryObject, + results = MusicCore.criticalGet(keyspace, tablename, rowId.primarKeyValue, queryObject, lockId); } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { - results = musicCore.atomicGet(keyspace, tablename, rowId.primarKeyValue, queryObject); + results = MusicCore.atomicGet(keyspace, tablename, rowId.primarKeyValue, queryObject); } if(results!=null && results.getAvailableWithoutFetching() >0) { - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicDataStoreHandle.marshallResults(results)).toMap()).build(); + return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicCore.marshallResults(results)).toMap()).build(); } return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setError("No data found").toMap()).build(); @@ -1337,7 +1333,7 @@ public class RestMusicDataAPI { String userId = userCredentials.get(MusicUtil.USERID); String password = userCredentials.get(MusicUtil.PASSWORD); Map authMap = - MusicAuthentication.authenticate(ns, userId, password, keyspace, aid, "select"); + MusicCore.authenticate(ns, userId, password, keyspace, aid, "select"); if (authMap.containsKey("aid")) authMap.remove("aid"); if (!authMap.isEmpty()) { @@ -1360,9 +1356,9 @@ public class RestMusicDataAPI { } try { - ResultSet results = musicCore.get(queryObject); + ResultSet results = MusicCore.get(queryObject); if(results.getAvailableWithoutFetching() >0) { - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicDataStoreHandle.marshallResults(results)).toMap()).build(); + return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicCore.marshallResults(results)).toMap()).build(); } return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setError("No data found").toMap()).build(); } catch (MusicServiceException ex) { @@ -1416,7 +1412,7 @@ public class RestMusicDataAPI { throws MusicServiceException { StringBuilder rowSpec = new StringBuilder(); int counter = 0; - TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename); + TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); if (tableInfo == null) { logger.error(EELFLoggerDelegate.errorLogger, "Table information not found. Please check input for table name= " diff --git a/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java b/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java index 92dd625e..835dda14 100644 --- a/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java @@ -40,14 +40,12 @@ 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.lockingservice.cassandra.MusicLockState; -import org.onap.music.authentication.MusicAuthentication; +import org.onap.music.datastore.MusicLockState; +import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; import org.onap.music.response.jsonobjects.JsonResponse; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -62,7 +60,6 @@ public class RestMusicLocksAPI { private static final String XMINORVERSION = "X-minorVersion"; private static final String XPATCHVERSION = "X-patchVersion"; private static final String VERSION = "v2"; - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); /** * Puts the requesting process in the q for this lock. The corresponding @@ -89,7 +86,7 @@ public class RestMusicLocksAPI { @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns) throws Exception{ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map resultMap = MusicCassaCore.validateLock(lockName); + Map resultMap = MusicCore.validateLock(lockName); if (resultMap.containsKey("Exception")) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(resultMap).build(); @@ -99,7 +96,7 @@ public class RestMusicLocksAPI { String password = userCredentials.get(MusicUtil.PASSWORD); String keyspaceName = (String) resultMap.get("keyspace"); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.authenticate(ns, userId, password, keyspaceName, aid, + resultMap = MusicCore.authenticate(ns, userId, password, keyspaceName, aid, "createLockReference"); if (resultMap.containsKey("aid")) resultMap.remove("aid"); @@ -108,7 +105,7 @@ public class RestMusicLocksAPI { return response.status(Status.UNAUTHORIZED).entity(resultMap).build(); } ResultType status = ResultType.SUCCESS; - String lockId = musicCore.createLockReference(lockName); + String lockId = MusicCore.createLockReference(lockName); if (lockId == null) { status = ResultType.FAILURE; @@ -141,7 +138,7 @@ public class RestMusicLocksAPI { @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns) throws Exception{ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map resultMap = MusicCassaCore.validateLock(lockId); + Map resultMap = MusicCore.validateLock(lockId); if (resultMap.containsKey("Exception")) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(resultMap).build(); @@ -151,7 +148,7 @@ public class RestMusicLocksAPI { String password = userCredentials.get(MusicUtil.PASSWORD); String keyspaceName = (String) resultMap.get("keyspace"); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.authenticate(ns, userId, password, keyspaceName, aid, + resultMap = MusicCore.authenticate(ns, userId, password, keyspaceName, aid, "accquireLock"); if (resultMap.containsKey("aid")) resultMap.remove("aid"); @@ -161,7 +158,7 @@ public class RestMusicLocksAPI { } try { String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$')); - ReturnType lockStatus = musicCore.acquireLock(lockName,lockId); + ReturnType lockStatus = MusicCore.acquireLock(lockName,lockId); if ( lockStatus.getResult().equals(ResultType.SUCCESS)) { response.status(Status.OK); } else { @@ -191,7 +188,7 @@ public class RestMusicLocksAPI { @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns) throws Exception{ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map resultMap = MusicCassaCore.validateLock(lockId); + Map resultMap = MusicCore.validateLock(lockId); if (resultMap.containsKey("Exception")) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(resultMap).build(); @@ -201,7 +198,7 @@ public class RestMusicLocksAPI { String password = userCredentials.get(MusicUtil.PASSWORD); String keyspaceName = (String) resultMap.get("keyspace"); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.authenticate(ns, userId, password, keyspaceName, aid, + resultMap = MusicCore.authenticate(ns, userId, password, keyspaceName, aid, "accquireLockWithLease"); if (resultMap.containsKey("aid")) @@ -211,7 +208,7 @@ public class RestMusicLocksAPI { return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$')); - ReturnType lockLeaseStatus = musicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod()); + ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod()); if ( lockLeaseStatus.getResult().equals(ResultType.SUCCESS)) { response.status(Status.OK); } else { @@ -238,7 +235,7 @@ public class RestMusicLocksAPI { @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns) throws Exception{ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map resultMap = MusicCassaCore.validateLock(lockName); + Map resultMap = MusicCore.validateLock(lockName); if (resultMap.containsKey("Exception")) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(resultMap).build(); @@ -248,7 +245,7 @@ public class RestMusicLocksAPI { String password = userCredentials.get(MusicUtil.PASSWORD); String keyspaceName = (String) resultMap.get("keyspace"); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.authenticate(ns, userId, password, keyspaceName, aid, + resultMap = MusicCore.authenticate(ns, userId, password, keyspaceName, aid, "currentLockHolder"); if (resultMap.containsKey("aid")) resultMap.remove("aid"); @@ -256,7 +253,7 @@ public class RestMusicLocksAPI { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } - String who = musicCore.whoseTurnIsIt(lockName); + String who = MusicCore.whoseTurnIsIt(lockName); ResultType status = ResultType.SUCCESS; String error = ""; if ( who == null ) { @@ -287,14 +284,14 @@ public class RestMusicLocksAPI { @ApiParam(value = "Password", required = true) @HeaderParam("password") String password) throws Exception{ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map resultMap = MusicCassaCore.validateLock(lockName); + Map resultMap = MusicCore.validateLock(lockName); if (resultMap.containsKey("Exception")) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } String keyspaceName = (String) resultMap.get("keyspace"); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.authenticate(ns, userId, password, keyspaceName, aid, + resultMap = MusicCore.authenticate(ns, userId, password, keyspaceName, aid, "currentLockState"); if (resultMap.containsKey("aid")) @@ -304,7 +301,7 @@ public class RestMusicLocksAPI { return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } - org.onap.music.lockingservice.cassandra.MusicLockState mls = MusicCassaCore.getMusicLockState(lockName); + org.onap.music.datastore.MusicLockState mls = MusicCore.getMusicLockState(lockName); Map returnMap = null; JsonResponse jsonResponse = new JsonResponse(ResultType.FAILURE).setLock(lockName); if(mls == null) { @@ -341,7 +338,7 @@ public class RestMusicLocksAPI { @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns) throws Exception{ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map resultMap = MusicCassaCore.validateLock(lockId); + Map resultMap = MusicCore.validateLock(lockId); if (resultMap.containsKey("Exception")) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(resultMap).build(); @@ -351,7 +348,7 @@ public class RestMusicLocksAPI { String password = userCredentials.get(MusicUtil.PASSWORD); String keyspaceName = (String) resultMap.get("keyspace"); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.authenticate(ns, userId, password, keyspaceName, aid, + resultMap = MusicCore.authenticate(ns, userId, password, keyspaceName, aid, "unLock"); if (resultMap.containsKey("aid")) resultMap.remove("aid"); @@ -360,7 +357,7 @@ public class RestMusicLocksAPI { return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } String fullyQualifiedKey = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$')); - MusicLockState mls = musicCore.voluntaryReleaseLock(fullyQualifiedKey, lockId); + MusicLockState mls = MusicCore.voluntaryReleaseLock(fullyQualifiedKey, lockId); if(mls.getErrorMessage() != null) { resultMap.put(ResultType.EXCEPTION.getResult(), mls.getErrorMessage()); @@ -399,7 +396,7 @@ public class RestMusicLocksAPI { @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns) throws Exception{ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map resultMap = MusicCassaCore.validateLock(lockName); + Map resultMap = MusicCore.validateLock(lockName); if (resultMap.containsKey("Exception")) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(resultMap).build(); @@ -409,7 +406,7 @@ public class RestMusicLocksAPI { String password = userCredentials.get(MusicUtil.PASSWORD); String keyspaceName = (String) resultMap.get("keyspace"); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.authenticate(ns, userId, password, keyspaceName, aid, + resultMap = MusicCore.authenticate(ns, userId, password, keyspaceName, aid, "deleteLock"); if (resultMap.containsKey("aid")) resultMap.remove("aid"); @@ -418,7 +415,7 @@ public class RestMusicLocksAPI { return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } try{ - musicCore.deleteLock(lockName); + MusicCore.deleteLock(lockName); }catch (Exception e) { return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); } diff --git a/src/main/java/org/onap/music/rest/RestMusicQAPI.java b/src/main/java/org/onap/music/rest/RestMusicQAPI.java index 9248ee1a..8af334c7 100755 --- a/src/main/java/org/onap/music/rest/RestMusicQAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicQAPI.java @@ -45,17 +45,14 @@ 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.apache.commons.lang3.StringUtils; -import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; import com.datastax.driver.core.ResultSet; import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; // cjc import org.onap.music.main.ReturnType; import org.onap.music.response.jsonobjects.JsonResponse; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; - import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @@ -67,7 +64,6 @@ import io.swagger.annotations.ApiParam; public class RestMusicQAPI { private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicQAPI.class); - private static MusicCoreService musicCore = MusicCassaCore.getInstance(); // private static String xLatestVersion = "X-latestVersion"; /* * private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicDataAPI.class); @@ -416,9 +412,9 @@ public class RestMusicQAPI { } try { - ResultSet results = musicCore.get(queryObject); + ResultSet results = MusicCore.get(queryObject); return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS) - .setDataResult(MusicDataStoreHandle.marshallResults(results)).toMap()).build(); + .setDataResult(MusicCore.marshallResults(results)).toMap()).build(); } catch (MusicServiceException ex) { logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR); diff --git a/src/main/java/org/onap/music/service/MusicCoreService.java b/src/main/java/org/onap/music/service/MusicCoreService.java deleted file mode 100644 index 6b7cc65c..00000000 --- a/src/main/java/org/onap/music/service/MusicCoreService.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.service; - -import org.onap.music.datastore.Condition; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.exceptions.MusicLockingException; -import org.onap.music.exceptions.MusicQueryException; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.MusicLockState; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.lockingservice.cassandra.*; - -import com.datastax.driver.core.ResultSet; - - - -/** - * @author srupane - * - */ -public interface MusicCoreService { - - // Core Music Database Methods - - - public ReturnType eventualPut(PreparedQueryObject queryObject); - - public ReturnType criticalPut(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, String lockId, Condition conditionInfo); - - public ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) - throws MusicServiceException; - - public ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException; - - public ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException, MusicQueryException; - - public ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException; - - public ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, Condition conditionInfo) - throws MusicLockingException, MusicQueryException, MusicServiceException; - - public ResultSet criticalGet(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, String lockId) throws MusicServiceException; - - // Core Music Locking Service Methods - - public String createLockReference(String fullyQualifiedKey); // lock name - - public ReturnType acquireLockWithLease(String key, String lockReference, long leasePeriod) - throws MusicLockingException, MusicQueryException, MusicServiceException; // key,lock id,time - - public ReturnType acquireLock(String key, String lockReference) - throws MusicLockingException, MusicQueryException, MusicServiceException; // key,lock id - - public ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject, - String consistency) throws MusicServiceException; - - public ResultSet quorumGet(PreparedQueryObject query); - - public String whoseTurnIsIt(String fullyQualifiedKey);// lock name - - public MusicLockState destroyLockRef(String fullyQualifiedKey, String lockReference); // lock name, lock id - - public MusicLockState voluntaryReleaseLock(String fullyQualifiedKey, String lockReference) - throws MusicLockingException;// lock name,lock id - - public void deleteLock(String lockName) throws MusicLockingException; - -} diff --git a/src/main/java/org/onap/music/service/impl/MusicCassaCore.java b/src/main/java/org/onap/music/service/impl/MusicCassaCore.java deleted file mode 100644 index b145081a..00000000 --- a/src/main/java/org/onap/music/service/impl/MusicCassaCore.java +++ /dev/null @@ -1,669 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.service.impl; - - -import java.io.StringWriter; -import java.util.HashMap; -import java.util.Map; -import java.util.StringTokenizer; -import java.util.UUID; - -import org.onap.music.datastore.MusicDataStore; -import org.onap.music.datastore.MusicDataStoreHandle; -import org.onap.music.datastore.Condition; -import org.onap.music.datastore.PreparedQueryObject; -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.MusicLockingException; -import org.onap.music.exceptions.MusicQueryException; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.CassaLockStore; -import org.onap.music.lockingservice.cassandra.MusicLockState; -import org.onap.music.lockingservice.cassandra.CassaLockStore.LockObject; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.service.MusicCoreService; - -import com.datastax.driver.core.ColumnDefinitions; -import com.datastax.driver.core.ColumnDefinitions.Definition; -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.core.TableMetadata; - - -/** - * This class ..... - * - * - */ -public class MusicCassaCore implements MusicCoreService { - - public static CassaLockStore mLockHandle = null;; - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicCassaCore.class); - private static boolean unitTestRun=true; - private static MusicCassaCore musicCassaCoreInstance = null; - - private MusicCassaCore() { - - } - public static MusicCassaCore getInstance() { - - if(musicCassaCoreInstance == null) { - musicCassaCoreInstance = new MusicCassaCore(); - } - return musicCassaCoreInstance; - } - - public static CassaLockStore getLockingServiceHandle() throws MusicLockingException { - logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring lock store handle"); - long start = System.currentTimeMillis(); - - if (mLockHandle == null) { - try { - mLockHandle = new CassaLockStore(MusicDataStoreHandle.getDSHandle()); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKHANDLE,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - throw new MusicLockingException("Failed to aquire Locl store handle " + e); - } - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire lock store handle:" + (end - start) + " ms"); - return mLockHandle; - } - - - - public String createLockReference(String fullyQualifiedKey) { - String[] splitString = fullyQualifiedKey.split("\\."); - String keyspace = splitString[0]; - String table = splitString[1]; - String lockName = splitString[2]; - - logger.info(EELFLoggerDelegate.applicationLogger,"Creating lock reference for lock name:" + lockName); - long start = System.currentTimeMillis(); - String lockReference = null; - try { - lockReference = "" + getLockingServiceHandle().genLockRefandEnQueue(keyspace, table, lockName); - } catch (MusicLockingException | MusicServiceException | MusicQueryException e) { - e.printStackTrace(); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to create lock reference:" + (end - start) + " ms"); - return lockReference; - } - - - public ReturnType acquireLockWithLease(String fullyQualifiedKey, String lockReference, long leasePeriod) throws MusicLockingException, MusicQueryException, MusicServiceException { - evictExpiredLockHolder(fullyQualifiedKey,leasePeriod); - return acquireLock(fullyQualifiedKey, lockReference); - } - - private void evictExpiredLockHolder(String fullyQualifiedKey, long leasePeriod) throws MusicLockingException, MusicQueryException, MusicServiceException { - - String[] splitString = fullyQualifiedKey.split("\\."); - String keyspace = splitString[0]; - String table = splitString[1]; - String primaryKeyValue = splitString[2]; - - LockObject currentLockHolderObject = getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue); - - /* Release the lock of the previous holder if it has expired. if the update to the acquire time has not reached due to network delays, simply use the create time as the - * reference*/ - - long referenceTime = Math.max(Long.parseLong(currentLockHolderObject.acquireTime), Long.parseLong(currentLockHolderObject.createTime)); - if((System.currentTimeMillis() - referenceTime) > leasePeriod) { - forciblyReleaseLock(fullyQualifiedKey, currentLockHolderObject.lockRef+""); - logger.info(EELFLoggerDelegate.applicationLogger, currentLockHolderObject.lockRef+" forcibly released"); - } - } - - private static ReturnType isTopOfLockStore(String keyspace, String table, String primaryKeyValue, String lockReference) throws MusicLockingException, MusicQueryException, MusicServiceException { - - //return failure to lock holders too early or already evicted from the lock store - String topOfLockStoreS = getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue).lockRef; - long topOfLockStoreL = Long.parseLong(topOfLockStoreS); - long lockReferenceL = Long.parseLong(lockReference); - - if(lockReferenceL > topOfLockStoreL) { - logger.info(EELFLoggerDelegate.applicationLogger, lockReference+" is not the lock holder yet"); - return new ReturnType(ResultType.FAILURE, lockReference+" is not the lock holder yet"); - } - - - if(lockReferenceL < topOfLockStoreL) { - logger.info(EELFLoggerDelegate.applicationLogger, lockReference+" is no longer/or was never in the lock store queue"); - return new ReturnType(ResultType.FAILURE, lockReference+" is no longer/or was never in the lock store queue"); - } - - return new ReturnType(ResultType.SUCCESS, lockReference+" is top of lock store"); - } - - public ReturnType acquireLock(String fullyQualifiedKey, String lockReference) throws MusicLockingException, MusicQueryException, MusicServiceException { - String[] splitString = fullyQualifiedKey.split("\\."); - String keyspace = splitString[0]; - String table = splitString[1]; - String primaryKeyValue = splitString[2]; - - ReturnType result = isTopOfLockStore(keyspace, table, primaryKeyValue, lockReference); - - if(result.getResult().equals(ResultType.FAILURE)) - return result;//not top of the lock store q - - //check to see if the value of the key has to be synced in case there was a forceful release - String syncTable = keyspace+".unsyncedKeys_"+table; - String query = "select * from "+syncTable+" where key='"+fullyQualifiedKey+"';"; - PreparedQueryObject readQueryObject = new PreparedQueryObject(); - readQueryObject.appendQueryString(query); - ResultSet results = MusicDataStoreHandle.getDSHandle().executeCriticalGet(readQueryObject); - if (results.all().size() != 0) { - logger.info("In acquire lock: Since there was a forcible release, need to sync quorum!"); - try { - syncQuorum(keyspace, table, primaryKeyValue); - } catch (Exception e) { - StringWriter sw = new StringWriter(); - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR506E] Failed to aquire lock ",ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - String exceptionAsString = sw.toString(); - return new ReturnType(ResultType.FAILURE, "Exception thrown while syncing key:\n" + exceptionAsString); - } - String cleanQuery = "delete * from music_internal.unsynced_keys where key='"+fullyQualifiedKey+"';"; - PreparedQueryObject deleteQueryObject = new PreparedQueryObject(); - deleteQueryObject.appendQueryString(cleanQuery); - MusicDataStoreHandle.getDSHandle().executePut(deleteQueryObject, "critical"); - } - - getLockingServiceHandle().updateLockAcquireTime(keyspace, table, primaryKeyValue, lockReference); - - return new ReturnType(ResultType.SUCCESS, lockReference+" is the lock holder for the key"); - } - - - - /** - * - * @param tableQueryObject - * @param consistency - * @return Boolean Indicates success or failure - * @throws MusicServiceException - * - * - */ - public ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject, String consistency) throws MusicServiceException { - boolean result = false; - - try { - //create shadow locking table - result = getLockingServiceHandle().createLockQueue(keyspace, table); - if(result == false) - return ResultType.FAILURE; - - result = false; - - //create table to track unsynced_keys - table = "unsyncedKeys_"+table; - - String tabQuery = "CREATE TABLE IF NOT EXISTS "+keyspace+"."+table - + " ( key text,PRIMARY KEY (key) );"; - System.out.println(tabQuery); - PreparedQueryObject queryObject = new PreparedQueryObject(); - - queryObject.appendQueryString(tabQuery); - result = false; - result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, "eventual"); - - - //create actual table - result = MusicDataStoreHandle.getDSHandle().executePut(tableQueryObject, consistency); - } catch (MusicQueryException | MusicServiceException | MusicLockingException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - throw new MusicServiceException(ex.getMessage()); - } - return result?ResultType.SUCCESS:ResultType.FAILURE; - } - - private static void syncQuorum(String keyspace, String table, String primaryKeyValue) throws Exception { - logger.info(EELFLoggerDelegate.applicationLogger,"Performing sync operation---"); - PreparedQueryObject selectQuery = new PreparedQueryObject(); - PreparedQueryObject updateQuery = new PreparedQueryObject(); - - // get the primary key d - TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, table); - String primaryKeyName = tableInfo.getPrimaryKey().get(0).getName();// we only support single - // primary key - DataType primaryKeyType = tableInfo.getPrimaryKey().get(0).getType(); - Object cqlFormattedPrimaryKeyValue = - MusicUtil.convertToActualDataType(primaryKeyType, primaryKeyValue); - - // get the row of data from a quorum - selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + table + " WHERE " - + primaryKeyName + "= ?" + ";"); - selectQuery.addValue(cqlFormattedPrimaryKeyValue); - ResultSet results = null; - try { - results = MusicDataStoreHandle.getDSHandle().executeCriticalGet(selectQuery); - // write it back to a quorum - Row row = results.one(); - ColumnDefinitions colInfo = row.getColumnDefinitions(); - int totalColumns = colInfo.size(); - int counter = 1; - StringBuilder fieldValueString = new StringBuilder(""); - for (Definition definition : colInfo) { - String colName = definition.getName(); - if (colName.equals(primaryKeyName)) - continue; - DataType colType = definition.getType(); - Object valueObj = MusicDataStoreHandle.getDSHandle().getColValue(row, colName, colType); - Object valueString = MusicUtil.convertToActualDataType(colType, valueObj); - fieldValueString.append(colName + " = ?"); - updateQuery.addValue(valueString); - if (counter != (totalColumns - 1)) - fieldValueString.append(","); - counter = counter + 1; - } - updateQuery.appendQueryString("UPDATE " + keyspace + "." + table + " SET " - + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";"); - updateQuery.addValue(cqlFormattedPrimaryKeyValue); - - MusicDataStoreHandle.getDSHandle().executePut(updateQuery, "critical"); - } catch (MusicServiceException | MusicQueryException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.QUERYERROR +""+updateQuery ,ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR); - } - } - - - - - /** - * - * @param query - * @return ResultSet - */ - public ResultSet quorumGet(PreparedQueryObject query) { - ResultSet results = null; - try { - results = MusicDataStoreHandle.getDSHandle().executeCriticalGet(query); - } catch (MusicServiceException | MusicQueryException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.MAJOR, ErrorTypes.GENERALSERVICEERROR); - - } - return results; - - } - - - - /** - * - * @param fullyQualifiedKey lockName - * @return - */ - public String whoseTurnIsIt(String fullyQualifiedKey) { - String[] splitString = fullyQualifiedKey.split("\\."); - String keyspace = splitString[0]; - String table = splitString[1]; - String primaryKeyValue = splitString[2]; - try { - return getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue)+""; - } catch (MusicLockingException | MusicServiceException | MusicQueryException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+fullyQualifiedKey ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - return null; - } - - /** - * - * @param lockReference - * @return - */ - public static String getLockNameFromId(String lockReference) { - StringTokenizer st = new StringTokenizer(lockReference); - return st.nextToken("$"); - } - - public MusicLockState destroyLockRef(String fullyQualifiedKey, String lockReference) { - long start = System.currentTimeMillis(); - String[] splitString = fullyQualifiedKey.split("\\."); - String keyspace = splitString[0]; - String table = splitString[1]; - String primaryKeyValue = splitString[2]; - try { - getLockingServiceHandle().deQueueLockRef(keyspace, table, primaryKeyValue, lockReference); - } catch (MusicLockingException | MusicServiceException | MusicQueryException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DESTROYLOCK+lockReference ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to destroy lock reference:" + (end - start) + " ms"); - return getMusicLockState(fullyQualifiedKey); - } - - public MusicLockState voluntaryReleaseLock(String fullyQualifiedKey, String lockReference) throws MusicLockingException{ - return destroyLockRef(fullyQualifiedKey, lockReference); - } - - public MusicLockState forciblyReleaseLock(String fullyQualifiedKey, String lockReference) throws MusicLockingException, MusicServiceException, MusicQueryException{ - String[] splitString = fullyQualifiedKey.split("\\."); - String keyspace = splitString[0]; - String table = splitString[1]; - - //leave a signal that this key could potentially be unsynchronized - String syncTable = keyspace+".unsyncedKeys_"+table; - PreparedQueryObject queryObject = new PreparedQueryObject(); - String values = "(?)"; - queryObject.addValue(fullyQualifiedKey); - String insQuery = "insert into "+syncTable+" (key) values "+values+"';"; - queryObject.appendQueryString(insQuery); - MusicDataStoreHandle.getDSHandle().executePut(queryObject, "critical"); - - //now release the lock - return destroyLockRef(fullyQualifiedKey, lockReference); - } - - /** - * - * @param lockName - * @throws MusicLockingException - */ - public void deleteLock(String lockName) throws MusicLockingException { - //deprecated - } - - // Prepared Query Additions. - - /** - * - * @param queryObject - * @return ReturnType - * @throws MusicServiceException - */ - public ReturnType eventualPut(PreparedQueryObject queryObject) { - boolean result = false; - try { - result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, MusicUtil.EVENTUAL); - } catch (MusicServiceException | MusicQueryException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() + " " + ex.getCause() + " " + ex); - return new ReturnType(ResultType.FAILURE, ex.getMessage()); - } - if (result) { - return new ReturnType(ResultType.SUCCESS, "Success"); - } else { - return new ReturnType(ResultType.FAILURE, "Failure"); - } - } - - /** - * - * @param keyspace - * @param table - * @param primaryKeyValue - * @param queryObject - * @param lockReference - * @return - */ - public ReturnType criticalPut(String keyspace, String table, String primaryKeyValue, - PreparedQueryObject queryObject, String lockReference, Condition conditionInfo) { - long start = System.currentTimeMillis(); - try { - ReturnType result = isTopOfLockStore(keyspace, table, primaryKeyValue, lockReference); - if(result.getResult().equals(ResultType.FAILURE)) - return result;//not top of the lock store q - - if (conditionInfo != null) - try { - if (conditionInfo.testCondition() == false) - return new ReturnType(ResultType.FAILURE, - "Lock acquired but the condition is not true"); - } catch (Exception e) { - return new ReturnType(ResultType.FAILURE, - "Exception thrown while checking the condition, check its sanctity:\n" - + e.getMessage()); - } - - String query = queryObject.getQuery(); - query = query.replaceFirst("SET", "using TIMESTAMP "+ v2sTimeStampInMicroseconds(lockReference, System.currentTimeMillis())+ " SET"); - queryObject.replaceQueryString(query); - MusicDataStoreHandle.getDSHandle().executePut(queryObject, MusicUtil.CRITICAL); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms"); - }catch (MusicQueryException | MusicServiceException | MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - return new ReturnType(ResultType.FAILURE, - "Exception thrown while doing the critical put\n" - + e.getMessage()); - } - return new ReturnType(ResultType.SUCCESS, "Update performed"); - } - - - /** - * - * @param queryObject - * @param consistency - * @return Boolean Indicates success or failure - * @throws MusicServiceException - * - * - */ - public ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException { - // this is mainly for some functions like keyspace creation etc which does not - // really need the bells and whistles of Music locking. - boolean result = false; - try { - result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, consistency); - } catch (MusicQueryException | MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(), AppMessages.UNKNOWNERROR, - ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - throw new MusicServiceException(ex.getMessage()); - } - return result ? ResultType.SUCCESS : ResultType.FAILURE; - } - - /** - * This method performs DDL operation on cassandra. - * - * @param queryObject query object containing prepared query and values - * @return ResultSet - * @throws MusicServiceException - */ - public ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException { - ResultSet results = null; - try { - results = MusicDataStoreHandle.getDSHandle().executeEventualGet(queryObject); - } catch (MusicQueryException | MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - throw new MusicServiceException(e.getMessage()); - } - return results; - } - - /** - * This method performs DDL operations on cassandra, if the the resource is available. Lock ID - * is used to check if the resource is free. - * - * @param keyspace name of the keyspace - * @param table name of the table - * @param primaryKeyValue primary key value - * @param queryObject query object containing prepared query and values - * @param lockReference lock ID to check if the resource is free to perform the operation. - * @return ResultSet - */ - public ResultSet criticalGet(String keyspace, String table, String primaryKeyValue, - PreparedQueryObject queryObject, String lockReference) throws MusicServiceException { - ResultSet results = null; - - try { - ReturnType result = isTopOfLockStore(keyspace, table, primaryKeyValue, lockReference); - if(result.getResult().equals(ResultType.FAILURE)) - return null;//not top of the lock store q - results = MusicDataStoreHandle.getDSHandle().executeCriticalGet(queryObject); - } catch (MusicQueryException | MusicServiceException | MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - } - return results; - } - - /** - * This method performs DML operation on cassandra, when the lock of the dd is acquired. - * - * @param keyspaceName name of the keyspace - * @param tableName name of the table - * @param primaryKey primary key value - * @param queryObject query object containing prepared query and values - * @return ReturnType - * @throws MusicLockingException - * @throws MusicServiceException - * @throws MusicQueryException - */ - public ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException, MusicQueryException, MusicServiceException { - long start = System.currentTimeMillis(); - String fullyQualifiedKey = keyspaceName + "." + tableName + "." + primaryKey; - String lockReference = createLockReference(fullyQualifiedKey); - long lockCreationTime = System.currentTimeMillis(); - ReturnType lockAcqResult = acquireLock(fullyQualifiedKey, lockReference); - long lockAcqTime = System.currentTimeMillis(); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockReference); - ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, - queryObject, lockReference, conditionInfo); - long criticalPutTime = System.currentTimeMillis(); - voluntaryReleaseLock(fullyQualifiedKey,lockReference); - long lockDeleteTime = System.currentTimeMillis(); - String timingInfo = "|lock creation time:" + (lockCreationTime - start) - + "|lock accquire time:" + (lockAcqTime - lockCreationTime) - + "|critical put time:" + (criticalPutTime - lockAcqTime) - + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|"; - criticalPutResult.setTimingInfo(timingInfo); - return criticalPutResult; - } else { - logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockReference); - voluntaryReleaseLock(fullyQualifiedKey,lockReference); - return lockAcqResult; - } - } - - - - - /** - * This method performs DDL operation on cassasndra, when the lock for the resource is acquired. - * - * @param keyspaceName name of the keyspace - * @param tableName name of the table - * @param primaryKey primary key value - * @param queryObject query object containing prepared query and values - * @return ResultSet - * @throws MusicServiceException - * @throws MusicLockingException - * @throws MusicQueryException - */ - public ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException, MusicQueryException { - String fullyQualifiedKey = keyspaceName + "." + tableName + "." + primaryKey; - String lockReference = createLockReference(fullyQualifiedKey); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLock(fullyQualifiedKey, lockReference); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockReference); - ResultSet result = - criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockReference); - voluntaryReleaseLock(fullyQualifiedKey,lockReference); - return result; - } else { - voluntaryReleaseLock(fullyQualifiedKey,lockReference); - logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockReference); - return null; - } - } - - - public static MusicLockState getMusicLockState(String fullyQualifiedKey) { - return null; - } - - - - /** - * @param lockName - * @return - */ - public static Map validateLock(String lockName) { - Map resultMap = new HashMap<>(); - String[] locks = lockName.split("\\."); - if(locks.length < 3) { - resultMap.put("Exception", "Invalid lock. Please make sure lock is of the type keyspaceName.tableName.primaryKey"); - return resultMap; - } - String keyspace= locks[0]; - if(keyspace.startsWith("$")) - keyspace = keyspace.substring(1); - resultMap.put("keyspace",keyspace); - return resultMap; - } - - - /** - * Given the time of write for an update in a critical section, this method provides a transformed timestamp - * that ensures that a previous lock holder who is still alive can never corrupt a later critical section. - * The main idea is to us the lock reference to clearly demarcate the timestamps across critical sections. - * @param the UUID lock reference associated with the write. - * @param the long timeOfWrite which is the actual time at which the write took place - * @throws MusicServiceException - * @throws MusicQueryException - */ - private static long v2sTimeStampInMicroseconds(String lockReference, long timeOfWrite) throws MusicServiceException, MusicQueryException{ - long lockEpochMillis = Long.parseLong(lockReference); - - long lockEternityMillis = lockEpochMillis - MusicUtil.MusicEternityEpochMillis; - - long ts = lockEternityMillis * MusicUtil.MaxCriticalSectionDurationMillis - + (timeOfWrite - lockEpochMillis); - - return ts; - -// long test = (lockReferenceUUID.timestamp()-MusicUtil.MusicEternityEpochMillis); -// long timeStamp = (lockReferenceUUID.timestamp()-MusicUtil.MusicEternityEpochMillis)*MusicUtil.MaxCriticalSectionDurationMillis -// +timeOfWrite; -// return timeStamp; - -// return timeOfWrite*1000; - } - - public static void main(String[] args) { - String x = "axe top"; - x = x.replaceFirst("top", "sword"); - System.out.print(x); //returns sword pickaxe - } - - - - @Override - public ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException { - //Deprecated - return null; - } -} diff --git a/src/test/java/org/onap/music/unittests/CassandraCQL.java b/src/test/java/org/onap/music/unittests/CassandraCQL.java new file mode 100644 index 00000000..a9cbe109 --- /dev/null +++ b/src/test/java/org/onap/music/unittests/CassandraCQL.java @@ -0,0 +1,256 @@ +/* + * ============LICENSE_START========================================== + * org.onap.music + * =================================================================== + * Copyright (c) 2017 AT&T Intellectual Property + * =================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================= + * ==================================================================== + */ + +package org.onap.music.unittests; + +/** + * @author srupane + * + */ + +import java.io.IOException; +import java.math.BigInteger; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.exceptions.NoHostAvailableException; +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.thrift.transport.TTransportException; +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; +import org.onap.music.datastore.CassaDataStore; +import org.onap.music.datastore.PreparedQueryObject; + +public class CassandraCQL { + + public static final String createKeySpace = + "CREATE KEYSPACE IF NOT EXISTS testCassa WITH replication = " + +"{'class':'SimpleStrategy','replication_factor':1} AND durable_writes = true;"; + + public static final String dropKeyspace = "DROP KEYSPACE IF EXISTS testCassa"; + + public static final String createTableEmployees = + "CREATE TABLE IF NOT EXISTS testCassa.employees " + + "(vector_ts text,empId uuid,empName text,empSalary varint,address Map,PRIMARY KEY (empName)) " + + "WITH comment='Financial Info of employees' " + + "AND compression={'sstable_compression':'DeflateCompressor','chunk_length_kb':64} " + + "AND compaction={'class':'SizeTieredCompactionStrategy','min_threshold':6};"; + + public static final String insertIntoTablePrepared1 = + "INSERT INTO testCassa.employees (vector_ts,empId,empName,empSalary) VALUES (?,?,?,?); "; + + public static final String insertIntoTablePrepared2 = + "INSERT INTO testCassa.employees (vector_ts,empId,empName,empSalary,address) VALUES (?,?,?,?,?);"; + + public static final String selectALL = "SELECT * FROM testCassa.employees;"; + + public static final String selectSpecific = + "SELECT * FROM testCassa.employees WHERE empName= ?;"; + + public static final String updatePreparedQuery = + "UPDATE testCassa.employees SET vector_ts=?,address= ? WHERE empName= ?;"; + + public static final String deleteFromTable = " "; + + public static final String deleteFromTablePrepared = " "; + + // Set Values for Prepared Query + + public static List setPreparedInsertValues1() { + + List preppreparedInsertValues1 = new ArrayList<>(); + String vectorTs = + String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); + UUID empId = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40cd6"); + BigInteger empSalary = BigInteger.valueOf(23443); + String empName = "Mr Test one"; + preppreparedInsertValues1.add(vectorTs); + preppreparedInsertValues1.add(empId); + preppreparedInsertValues1.add(empName); + preppreparedInsertValues1.add(empSalary); + return preppreparedInsertValues1; + } + + public static List setPreparedInsertValues2() { + + List preparedInsertValues2 = new ArrayList<>(); + String vectorTs = + String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); + UUID empId = UUID.fromString("abc434cc-d657-4e90-b4e5-df4223d40cd6"); + BigInteger empSalary = BigInteger.valueOf(45655); + String empName = "Mr Test two"; + Map address = new HashMap<>(); + preparedInsertValues2.add(vectorTs); + preparedInsertValues2.add(empId); + preparedInsertValues2.add(empName); + preparedInsertValues2.add(empSalary); + address.put("Street", "1 some way"); + address.put("City", "Some town"); + preparedInsertValues2.add(address); + return preparedInsertValues2; + } + + public static List setPreparedUpdateValues() { + + List preparedUpdateValues = new ArrayList<>(); + String vectorTs = + String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); + Map address = new HashMap<>(); + preparedUpdateValues.add(vectorTs); + String empName = "Mr Test one"; + address.put("Street", "101 Some Way"); + address.put("City", "New York"); + preparedUpdateValues.add(address); + preparedUpdateValues.add(empName); + return preparedUpdateValues; + } + + // Generate Different Prepared Query Objects + /** + * Query Object for Get. + * + * @return + */ + public static PreparedQueryObject setPreparedGetQuery() { + + PreparedQueryObject queryObject = new PreparedQueryObject(); + String empName1 = "Mr Test one"; + queryObject.appendQueryString(selectSpecific); + queryObject.addValue(empName1); + return queryObject; + } + + /** + * Query Object 1 for Insert. + * + * @return {@link PreparedQueryObject} + */ + public static PreparedQueryObject setPreparedInsertQueryObject1() { + + PreparedQueryObject queryobject = new PreparedQueryObject(); + queryobject.appendQueryString(insertIntoTablePrepared1); + List values = setPreparedInsertValues1(); + if (!values.isEmpty() || values != null) { + for (Object o : values) { + queryobject.addValue(o); + } + } + return queryobject; + + } + + /** + * Query Object 2 for Insert. + * + * @return {@link PreparedQueryObject} + */ + public static PreparedQueryObject setPreparedInsertQueryObject2() { + + PreparedQueryObject queryobject = new PreparedQueryObject(); + queryobject.appendQueryString(insertIntoTablePrepared2); + List values = setPreparedInsertValues2(); + if (!values.isEmpty() || values != null) { + for (Object o : values) { + queryobject.addValue(o); + } + } + return queryobject; + + } + + /** + * Query Object for Update. + * + * @return {@link PreparedQueryObject} + */ + public static PreparedQueryObject setPreparedUpdateQueryObject() { + + PreparedQueryObject queryobject = new PreparedQueryObject(); + queryobject.appendQueryString(updatePreparedQuery); + List values = setPreparedUpdateValues(); + if (!values.isEmpty() || values != null) { + for (Object o : values) { + queryobject.addValue(o); + } + } + return queryobject; + + } + + private static ArrayList getAllPossibleLocalIps() { + ArrayList allPossibleIps = new ArrayList(); + try { + Enumeration en = NetworkInterface.getNetworkInterfaces(); + while (en.hasMoreElements()) { + NetworkInterface ni = (NetworkInterface) en.nextElement(); + Enumeration ee = ni.getInetAddresses(); + while (ee.hasMoreElements()) { + InetAddress ia = (InetAddress) ee.nextElement(); + allPossibleIps.add(ia.getHostAddress()); + } + } + } catch (SocketException e) { + System.out.println(e.getMessage()); + } + return allPossibleIps; + } + + public static CassaDataStore connectToEmbeddedCassandra() { + Iterator it = getAllPossibleLocalIps().iterator(); + String address = "localhost"; + + Cluster cluster = null; + Session session = null; + while (it.hasNext()) { + try { + + try { + EmbeddedCassandraServerHelper.startEmbeddedCassandra(80000); + } catch (ConfigurationException | TTransportException | IOException e) { + + System.out.println(e.getMessage()); + } + + cluster = new Cluster.Builder().addContactPoint(address).withPort(9142).build(); + cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(20000); + session = cluster.connect(); + + break; + } catch (NoHostAvailableException e) { + address = it.next(); + System.out.println(e.getMessage()); + + } + } + return new CassaDataStore(cluster, session); + + } + +} diff --git a/src/test/java/org/onap/music/unittests/CassandraCQLQueries.java b/src/test/java/org/onap/music/unittests/CassandraCQLQueries.java deleted file mode 100644 index 41c518e9..00000000 --- a/src/test/java/org/onap/music/unittests/CassandraCQLQueries.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ - -package org.onap.music.unittests; - -/** - * @author srupane - * - */ - -import java.io.IOException; -import java.math.BigInteger; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.Session; -import com.datastax.driver.core.exceptions.NoHostAvailableException; -import org.apache.cassandra.exceptions.ConfigurationException; -import org.apache.thrift.transport.TTransportException; -import org.cassandraunit.utils.EmbeddedCassandraServerHelper; -import org.onap.music.datastore.MusicDataStore; -import org.onap.music.datastore.PreparedQueryObject; - -public class CassandraCQLQueries { - - public static final String createKeySpace = - "CREATE KEYSPACE IF NOT EXISTS testCassa WITH replication = " - +"{'class':'SimpleStrategy','replication_factor':1} AND durable_writes = true;"; - - public static final String dropKeyspace = "DROP KEYSPACE IF EXISTS testCassa"; - - public static final String createTableEmployees = - "CREATE TABLE IF NOT EXISTS testCassa.employees " - + "(vector_ts text,empId uuid,empName text,empSalary varint,address Map,PRIMARY KEY (empName)) " - + "WITH comment='Financial Info of employees' " - + "AND compression={'sstable_compression':'DeflateCompressor','chunk_length_kb':64} " - + "AND compaction={'class':'SizeTieredCompactionStrategy','min_threshold':6};"; - - public static final String insertIntoTablePrepared1 = - "INSERT INTO testCassa.employees (vector_ts,empId,empName,empSalary) VALUES (?,?,?,?); "; - - public static final String insertIntoTablePrepared2 = - "INSERT INTO testCassa.employees (vector_ts,empId,empName,empSalary,address) VALUES (?,?,?,?,?);"; - - public static final String selectALL = "SELECT * FROM testCassa.employees;"; - - public static final String selectSpecific = - "SELECT * FROM testCassa.employees WHERE empName= ?;"; - - public static final String updatePreparedQuery = - "UPDATE testCassa.employees SET vector_ts=?,address= ? WHERE empName= ?;"; - - public static final String deleteFromTable = " "; - - public static final String deleteFromTablePrepared = " "; - - // Set Values for Prepared Query - - public static List setPreparedInsertValues1() { - - List preppreparedInsertValues1 = new ArrayList<>(); - String vectorTs = - String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); - UUID empId = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40cd6"); - BigInteger empSalary = BigInteger.valueOf(23443); - String empName = "Mr Test one"; - preppreparedInsertValues1.add(vectorTs); - preppreparedInsertValues1.add(empId); - preppreparedInsertValues1.add(empName); - preppreparedInsertValues1.add(empSalary); - return preppreparedInsertValues1; - } - - public static List setPreparedInsertValues2() { - - List preparedInsertValues2 = new ArrayList<>(); - String vectorTs = - String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); - UUID empId = UUID.fromString("abc434cc-d657-4e90-b4e5-df4223d40cd6"); - BigInteger empSalary = BigInteger.valueOf(45655); - String empName = "Mr Test two"; - Map address = new HashMap<>(); - preparedInsertValues2.add(vectorTs); - preparedInsertValues2.add(empId); - preparedInsertValues2.add(empName); - preparedInsertValues2.add(empSalary); - address.put("Street", "1 some way"); - address.put("City", "Some town"); - preparedInsertValues2.add(address); - return preparedInsertValues2; - } - - public static List setPreparedUpdateValues() { - - List preparedUpdateValues = new ArrayList<>(); - String vectorTs = - String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); - Map address = new HashMap<>(); - preparedUpdateValues.add(vectorTs); - String empName = "Mr Test one"; - address.put("Street", "101 Some Way"); - address.put("City", "New York"); - preparedUpdateValues.add(address); - preparedUpdateValues.add(empName); - return preparedUpdateValues; - } - - // Generate Different Prepared Query Objects - /** - * Query Object for Get. - * - * @return - */ - public static PreparedQueryObject setPreparedGetQuery() { - - PreparedQueryObject queryObject = new PreparedQueryObject(); - String empName1 = "Mr Test one"; - queryObject.appendQueryString(selectSpecific); - queryObject.addValue(empName1); - return queryObject; - } - - /** - * Query Object 1 for Insert. - * - * @return {@link PreparedQueryObject} - */ - public static PreparedQueryObject setPreparedInsertQueryObject1() { - - PreparedQueryObject queryobject = new PreparedQueryObject(); - queryobject.appendQueryString(insertIntoTablePrepared1); - List values = setPreparedInsertValues1(); - if (!values.isEmpty() || values != null) { - for (Object o : values) { - queryobject.addValue(o); - } - } - return queryobject; - - } - - /** - * Query Object 2 for Insert. - * - * @return {@link PreparedQueryObject} - */ - public static PreparedQueryObject setPreparedInsertQueryObject2() { - - PreparedQueryObject queryobject = new PreparedQueryObject(); - queryobject.appendQueryString(insertIntoTablePrepared2); - List values = setPreparedInsertValues2(); - if (!values.isEmpty() || values != null) { - for (Object o : values) { - queryobject.addValue(o); - } - } - return queryobject; - - } - - /** - * Query Object for Update. - * - * @return {@link PreparedQueryObject} - */ - public static PreparedQueryObject setPreparedUpdateQueryObject() { - - PreparedQueryObject queryobject = new PreparedQueryObject(); - queryobject.appendQueryString(updatePreparedQuery); - List values = setPreparedUpdateValues(); - if (!values.isEmpty() || values != null) { - for (Object o : values) { - queryobject.addValue(o); - } - } - return queryobject; - - } - - private static ArrayList getAllPossibleLocalIps() { - ArrayList allPossibleIps = new ArrayList(); - try { - Enumeration en = NetworkInterface.getNetworkInterfaces(); - while (en.hasMoreElements()) { - NetworkInterface ni = (NetworkInterface) en.nextElement(); - Enumeration ee = ni.getInetAddresses(); - while (ee.hasMoreElements()) { - InetAddress ia = (InetAddress) ee.nextElement(); - allPossibleIps.add(ia.getHostAddress()); - } - } - } catch (SocketException e) { - System.out.println(e.getMessage()); - } - return allPossibleIps; - } - - public static MusicDataStore connectToEmbeddedCassandra() { - Iterator it = getAllPossibleLocalIps().iterator(); - String address = "localhost"; - - Cluster cluster = null; - Session session = null; - while (it.hasNext()) { - try { - - try { - EmbeddedCassandraServerHelper.startEmbeddedCassandra(80000); - } catch (ConfigurationException | TTransportException | IOException e) { - - System.out.println(e.getMessage()); - } - - cluster = new Cluster.Builder().addContactPoint(address).withPort(9142).build(); - cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(20000); - session = cluster.connect(); - - break; - } catch (NoHostAvailableException e) { - address = it.next(); - System.out.println(e.getMessage()); - - } - } - return new MusicDataStore(cluster, session); - - } - -} diff --git a/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java b/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java index 62275bb3..3f7fd3b7 100644 --- a/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java +++ b/src/test/java/org/onap/music/unittests/MusicDataStoreTest.java @@ -33,7 +33,7 @@ import org.mockito.Mock; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.datastore.MusicDataStore; +import org.onap.music.datastore.CassaDataStore; import org.onap.music.datastore.PreparedQueryObject; import com.datastax.driver.core.DataType; @@ -44,12 +44,12 @@ import com.datastax.driver.core.TableMetadata; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MusicDataStoreTest { - static MusicDataStore dataStore; + static CassaDataStore dataStore; static PreparedQueryObject testObject; @BeforeClass public static void init() { - dataStore = CassandraCQLQueries.connectToEmbeddedCassandra(); + dataStore = CassandraCQL.connectToEmbeddedCassandra(); } @@ -57,7 +57,7 @@ public class MusicDataStoreTest { public static void close() throws MusicServiceException, MusicQueryException { testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.dropKeyspace); + testObject.appendQueryString(CassandraCQL.dropKeyspace); dataStore.executePut(testObject, "eventual"); dataStore.close(); @@ -67,10 +67,10 @@ public class MusicDataStoreTest { public void Test1_SetUp() throws MusicServiceException, MusicQueryException { boolean result = false; testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.createKeySpace); + testObject.appendQueryString(CassandraCQL.createKeySpace); result = dataStore.executePut(testObject, "eventual");; testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.createTableEmployees); + testObject.appendQueryString(CassandraCQL.createTableEmployees); result = dataStore.executePut(testObject, "eventual"); assertEquals(true, result); @@ -78,21 +78,21 @@ public class MusicDataStoreTest { @Test public void Test2_ExecutePut_eventual_insert() throws MusicServiceException, MusicQueryException { - testObject = CassandraCQLQueries.setPreparedInsertQueryObject1(); + testObject = CassandraCQL.setPreparedInsertQueryObject1(); boolean result = dataStore.executePut(testObject, "eventual"); assertEquals(true, result); } @Test public void Test3_ExecutePut_critical_insert() throws MusicServiceException, MusicQueryException { - testObject = CassandraCQLQueries.setPreparedInsertQueryObject2(); + testObject = CassandraCQL.setPreparedInsertQueryObject2(); boolean result = dataStore.executePut(testObject, "Critical"); assertEquals(true, result); } @Test public void Test4_ExecutePut_eventual_update() throws MusicServiceException, MusicQueryException { - testObject = CassandraCQLQueries.setPreparedUpdateQueryObject(); + testObject = CassandraCQL.setPreparedUpdateQueryObject(); boolean result = false; result = dataStore.executePut(testObject, "eventual"); assertEquals(true, result); @@ -101,7 +101,7 @@ public class MusicDataStoreTest { @Test public void Test5_ExecuteEventualGet() throws MusicServiceException, MusicQueryException { testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.selectALL); + testObject.appendQueryString(CassandraCQL.selectALL); boolean result = false; int count = 0; ResultSet output = null; @@ -120,7 +120,7 @@ public class MusicDataStoreTest { @Test public void Test6_ExecuteCriticalGet() throws MusicServiceException, MusicQueryException { - testObject = CassandraCQLQueries.setPreparedGetQuery(); + testObject = CassandraCQL.setPreparedGetQuery(); boolean result = false; int count = 0; ResultSet output = null; diff --git a/src/test/java/org/onap/music/unittests/MusicLockStoreTest.java b/src/test/java/org/onap/music/unittests/MusicLockStoreTest.java index 45ce9296..86774538 100644 --- a/src/test/java/org/onap/music/unittests/MusicLockStoreTest.java +++ b/src/test/java/org/onap/music/unittests/MusicLockStoreTest.java @@ -32,8 +32,9 @@ import org.junit.runners.MethodSorters; import org.mockito.Mock; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.CassaLockStore; -import org.onap.music.datastore.MusicDataStore; + +import org.onap.music.datastore.CassaDataStore; +import org.onap.music.datastore.CassaLockStore; import org.onap.music.datastore.PreparedQueryObject; import com.datastax.driver.core.DataType; @@ -44,13 +45,13 @@ import com.datastax.driver.core.TableMetadata; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MusicLockStoreTest { - static MusicDataStore dataStore; + static CassaDataStore dataStore; static CassaLockStore lockStore; static PreparedQueryObject testObject; @BeforeClass public static void init() { - dataStore = CassandraCQLQueries.connectToEmbeddedCassandra(); + dataStore = CassandraCQL.connectToEmbeddedCassandra(); lockStore = new CassaLockStore(dataStore); } @@ -58,7 +59,7 @@ public class MusicLockStoreTest { @AfterClass public static void close() throws MusicServiceException, MusicQueryException { testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.dropKeyspace); + testObject.appendQueryString(CassandraCQL.dropKeyspace); dataStore.executePut(testObject, "eventual"); dataStore.close(); @@ -68,10 +69,10 @@ public class MusicLockStoreTest { public void Test1_SetUp() throws MusicServiceException, MusicQueryException { boolean result = false; testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.createKeySpace); + testObject.appendQueryString(CassandraCQL.createKeySpace); result = dataStore.executePut(testObject, "eventual");; testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.createTableEmployees); + testObject.appendQueryString(CassandraCQL.createTableEmployees); result = dataStore.executePut(testObject, "eventual"); assertEquals(true, result); @@ -92,14 +93,14 @@ public class MusicLockStoreTest { @Test public void Test3_ExecutePut_critical_insert() throws MusicServiceException, MusicQueryException { - testObject = CassandraCQLQueries.setPreparedInsertQueryObject2(); + testObject = CassandraCQL.setPreparedInsertQueryObject2(); boolean result = dataStore.executePut(testObject, "Critical"); assertEquals(true, result); } @Test public void Test4_ExecutePut_eventual_update() throws MusicServiceException, MusicQueryException { - testObject = CassandraCQLQueries.setPreparedUpdateQueryObject(); + testObject = CassandraCQL.setPreparedUpdateQueryObject(); boolean result = false; result = dataStore.executePut(testObject, "eventual"); assertEquals(true, result); @@ -108,7 +109,7 @@ public class MusicLockStoreTest { @Test public void Test5_ExecuteEventualGet() throws MusicServiceException, MusicQueryException { testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.selectALL); + testObject.appendQueryString(CassandraCQL.selectALL); boolean result = false; int count = 0; ResultSet output = null; @@ -127,7 +128,7 @@ public class MusicLockStoreTest { @Test public void Test6_ExecuteCriticalGet() throws MusicServiceException, MusicQueryException { - testObject = CassandraCQLQueries.setPreparedGetQuery(); + testObject = CassandraCQL.setPreparedGetQuery(); boolean result = false; int count = 0; ResultSet output = null; diff --git a/src/test/java/org/onap/music/unittests/TestCassaLockStore.java b/src/test/java/org/onap/music/unittests/TestCassaLockStore.java index 2f3750a2..bf058121 100644 --- a/src/test/java/org/onap/music/unittests/TestCassaLockStore.java +++ b/src/test/java/org/onap/music/unittests/TestCassaLockStore.java @@ -2,18 +2,16 @@ package org.onap.music.unittests; import java.util.HashMap; import java.util.Map; +import java.util.UUID; +import org.onap.music.datastore.CassaLockStore; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.CassaLockStore; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; +import org.onap.music.main.MusicCore; public class TestCassaLockStore { - static MusicCoreService musicCore = MusicCassaCore.getInstance(); - public static void main(String[] args) { @@ -29,12 +27,12 @@ public class TestCassaLockStore { PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString("CREATE KEYSPACE " + keyspace + " WITH REPLICATION = " + replicationInfo.toString().replaceAll("=", ":")); - musicCore.nonKeyRelatedPut(queryObject, "eventual"); + MusicCore.nonKeyRelatedPut(queryObject, "eventual"); queryObject = new PreparedQueryObject(); queryObject.appendQueryString("CREATE TABLE " + keyspace + "." + table + " (name text PRIMARY KEY, count varint);"); - musicCore.createTable(keyspace, table, queryObject, "eventual"); + MusicCore.createTable(keyspace, table, queryObject, "eventual"); lockStore.createLockQueue(keyspace, table); diff --git a/src/test/java/org/onap/music/unittests/TestMusicCore.java b/src/test/java/org/onap/music/unittests/TestMusicCore.java index 6f0a93a0..01d2ffb6 100644 --- a/src/test/java/org/onap/music/unittests/TestMusicCore.java +++ b/src/test/java/org/onap/music/unittests/TestMusicCore.java @@ -1,6 +1,10 @@ package org.onap.music.unittests; +import static org.junit.Assert.assertEquals; + import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import org.junit.AfterClass; @@ -9,14 +13,12 @@ import org.junit.FixMethodOrder; import org.junit.Ignore; import org.junit.Test; import org.junit.runners.MethodSorters; -import org.onap.music.datastore.MusicDataStore; -import org.onap.music.datastore.MusicDataStoreHandle; +import org.onap.music.datastore.CassaDataStore; +import org.onap.music.datastore.CassaLockStore; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.CassaLockStore; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.MusicCassaCore; +import org.onap.music.main.MusicCore; import com.datastax.driver.core.ResultSet; @@ -25,17 +27,16 @@ import com.datastax.driver.core.ResultSet; public class TestMusicCore { static PreparedQueryObject testObject; - static MusicDataStore dataStore; + static CassaDataStore dataStore; String keyspace = "MusicCoreUnitTestKp"; String table = "SampleTable"; - static MusicCoreService musicCore = MusicCassaCore.getInstance(); @BeforeClass public static void init() { System.out.println("TestMusicCore Init"); try { - MusicDataStoreHandle.mDstoreHandle = CassandraCQLQueries.connectToEmbeddedCassandra(); - CassaLockStore mLockHandle = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle); + MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); + MusicCore.mLockHandle = new CassaLockStore(MusicCore.mDstoreHandle); } catch (Exception e) { e.printStackTrace(); } @@ -45,9 +46,9 @@ public class TestMusicCore { public static void close() throws MusicServiceException, MusicQueryException { System.out.println("After class TestMusicCore"); testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.dropKeyspace); - musicCore.eventualPut(testObject); - MusicDataStoreHandle.mDstoreHandle.close(); + testObject.appendQueryString(CassandraCQL.dropKeyspace); + MusicCore.eventualPut(testObject); + MusicCore.mDstoreHandle.close(); } @Test @@ -60,7 +61,7 @@ public class TestMusicCore { PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString( "CREATE KEYSPACE " + keyspace + " WITH REPLICATION = " + replicationInfo.toString().replaceAll("=", ":")); - musicCore.nonKeyRelatedPut(queryObject, "eventual"); + MusicCore.nonKeyRelatedPut(queryObject, "eventual"); //check with the system table in cassandra @@ -76,7 +77,7 @@ public class TestMusicCore { PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString( "CREATE TABLE " + keyspace + "." + table + " (name text PRIMARY KEY, count varint);"); - musicCore.createTable(keyspace, table, queryObject, "eventual"); + MusicCore.createTable(keyspace, table, queryObject, "eventual"); //check with the system table in cassandra diff --git a/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java b/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java index 53b6d8bc..9b4fc229 100644 --- a/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java +++ b/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java @@ -19,29 +19,26 @@ package org.onap.music.unittests; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; - +import static org.junit.Assert.assertTrue; +import java.io.File; import java.util.List; - +import org.apache.curator.test.TestingServer; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Ignore; import org.junit.Test; import org.junit.runners.MethodSorters; -import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.CassaLockStore; -import org.onap.music.lockingservice.cassandra.MusicLockState; -import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus; -import org.onap.music.service.MusicCoreService; -import org.onap.music.service.impl.*; +import org.onap.music.datastore.CassaLockStore; +import org.onap.music.datastore.MusicLockState; +import org.onap.music.datastore.MusicLockState.LockStatus; +import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; -import org.onap.music.service.impl.MusicCassaCore; - import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -52,13 +49,12 @@ public class TestMusicCoreIntegration { static PreparedQueryObject testObject; static String lockId = null; static String lockName = "ks1.tb1.pk1"; - static MusicCassaCore musicCore = MusicCassaCore.getInstance(); @BeforeClass public static void init() throws Exception { try { - MusicDataStoreHandle.mDstoreHandle = CassandraCQLQueries.connectToEmbeddedCassandra(); - MusicCassaCore.mLockHandle = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle); + MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); + MusicCore.mLockHandle = new CassaLockStore(MusicCore.mDstoreHandle); } catch (Exception e) { e.printStackTrace(); } @@ -68,31 +64,31 @@ public class TestMusicCoreIntegration { public static void tearDownAfterClass() throws Exception { System.out.println("After class"); testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.dropKeyspace); - musicCore.eventualPut(testObject); - musicCore.deleteLock(lockName); - MusicDataStoreHandle.mDstoreHandle.close(); + testObject.appendQueryString(CassandraCQL.dropKeyspace); + MusicCore.eventualPut(testObject); + MusicCore.deleteLock(lockName); + MusicCore.mDstoreHandle.close(); } @Test public void Test1_SetUp() throws MusicServiceException, MusicQueryException { - MusicDataStoreHandle.mDstoreHandle = CassandraCQLQueries.connectToEmbeddedCassandra(); - MusicCassaCore.mLockHandle = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle); + MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); + MusicCore.mLockHandle = new CassaLockStore(MusicCore.mDstoreHandle); ResultType result = ResultType.FAILURE; testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.createKeySpace); - musicCore.eventualPut(testObject); + testObject.appendQueryString(CassandraCQL.createKeySpace); + MusicCore.eventualPut(testObject); testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQLQueries.createTableEmployees); - result = musicCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL); + testObject.appendQueryString(CassandraCQL.createTableEmployees); + result = MusicCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL); assertEquals(ResultType.SUCCESS, result); } @Test public void Test2_atomicPut() throws Exception { testObject = new PreparedQueryObject(); - testObject = CassandraCQLQueries.setPreparedInsertQueryObject1(); - ReturnType returnType = musicCore.atomicPut("testCassa", "employees", "Mr Test one", + testObject = CassandraCQL.setPreparedInsertQueryObject1(); + ReturnType returnType = MusicCore.atomicPut("testCassa", "employees", "Mr Test one", testObject, null); assertEquals(ResultType.SUCCESS, returnType.getResult()); } @@ -101,28 +97,28 @@ public class TestMusicCoreIntegration { @Test public void Test5_atomicGet() throws Exception { testObject = new PreparedQueryObject(); - testObject = CassandraCQLQueries.setPreparedGetQuery(); + testObject = CassandraCQL.setPreparedGetQuery(); ResultSet resultSet = - musicCore.atomicGet("testCassa", "employees", "Mr Test two", testObject); + MusicCore.atomicGet("testCassa", "employees", "Mr Test two", testObject); List rows = resultSet.all(); assertEquals(1, rows.size()); } @Test public void Test6_createLockReference() throws Exception { - lockId = musicCore.createLockReference(lockName); + lockId = MusicCore.createLockReference(lockName); assertNotNull(lockId); } @Test public void Test7_acquireLockwithLease() throws Exception { - ReturnType lockLeaseStatus = musicCore.acquireLockWithLease(lockName, lockId, 1000); + ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, 1000); assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult()); } @Test public void Test8_acquireLock() throws Exception { - ReturnType lockStatus = musicCore.acquireLock(lockName, lockId); + ReturnType lockStatus = MusicCore.acquireLock(lockName, lockId); assertEquals(ResultType.SUCCESS, lockStatus.getResult()); } @@ -130,9 +126,9 @@ public class TestMusicCoreIntegration { public void Test9_release() throws Exception { MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1"); - musicCore.whoseTurnIsIt(lockName); - MusicLockState mls = musicCore.getMusicLockState(lockName); - MusicLockState mls1 = musicCore.voluntaryReleaseLock(lockName,lockId); + MusicCore.whoseTurnIsIt(lockName); + MusicLockState mls = MusicCore.getMusicLockState(lockName); + MusicLockState mls1 = MusicCore.voluntaryReleaseLock(lockName,lockId); assertEquals(musicLockState.getLockStatus(), mls.getLockStatus()); assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus()); } diff --git a/src/test/java/org/onap/music/unittests/TestRestMusicData.java b/src/test/java/org/onap/music/unittests/TestRestMusicData.java index 01ae861b..f0c52a3b 100644 --- a/src/test/java/org/onap/music/unittests/TestRestMusicData.java +++ b/src/test/java/org/onap/music/unittests/TestRestMusicData.java @@ -21,17 +21,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; - +import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; - import javax.servlet.http.HttpServletResponse; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; - import org.apache.curator.test.TestingServer; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -46,7 +44,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.runners.MockitoJUnitRunner; -import org.onap.music.datastore.MusicDataStoreHandle; +import org.onap.music.datastore.CassaLockStore; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonDelete; import org.onap.music.datastore.jsonobjects.JsonInsert; @@ -55,16 +53,13 @@ import org.onap.music.datastore.jsonobjects.JsonOnboard; import org.onap.music.datastore.jsonobjects.JsonSelect; import org.onap.music.datastore.jsonobjects.JsonTable; import org.onap.music.datastore.jsonobjects.JsonUpdate; -import org.onap.music.lockingservice.cassandra.CassaLockStore; import org.onap.music.main.CachingUtil; -import org.onap.music.service.impl.*; +import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.rest.RestMusicAdminAPI; import org.onap.music.rest.RestMusicDataAPI; import org.onap.music.rest.RestMusicLocksAPI; -import org.onap.music.service.impl.MusicCassaCore; - import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -92,7 +87,7 @@ public class TestRestMusicData { CachingUtil cachUtilMock; @InjectMocks - private static MusicCassaCore MusicCore =MusicCassaCore.getInstance(); + private MusicCore mCore; //*/ static String appName = "TestApp"; @@ -114,8 +109,8 @@ public class TestRestMusicData { @BeforeClass public static void init() throws Exception { try { - MusicDataStoreHandle.mDstoreHandle = CassandraCQLQueries.connectToEmbeddedCassandra(); - MusicCore.mLockHandle = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle); + MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); + MusicCore.mLockHandle = new CassaLockStore(MusicCore.mDstoreHandle); } catch (Exception e) { e.printStackTrace(); } @@ -129,8 +124,8 @@ public class TestRestMusicData { testObject = new PreparedQueryObject(); testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin"); MusicCore.eventualPut(testObject); - if(MusicDataStoreHandle.mDstoreHandle!=null) - MusicDataStoreHandle.mDstoreHandle.close(); + if(MusicCore.mDstoreHandle!=null) + MusicCore.mDstoreHandle.close(); if(zkServer!=null) zkServer.stop(); } diff --git a/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java b/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java index 93591bfd..a3d3d709 100644 --- a/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java +++ b/src/test/java/org/onap/music/unittests/TestRestMusicQAPI.java @@ -47,8 +47,7 @@ import org.mindrot.jbcrypt.BCrypt; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import org.onap.music.lockingservice.cassandra.CassaLockStore; -import org.onap.music.datastore.MusicDataStoreHandle; +import org.onap.music.datastore.CassaLockStore; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonDelete; import org.onap.music.datastore.jsonobjects.JsonInsert; @@ -58,12 +57,12 @@ import org.onap.music.datastore.jsonobjects.JsonKeySpace; import org.onap.music.datastore.jsonobjects.JsonSelect; import org.onap.music.datastore.jsonobjects.JsonTable; import org.onap.music.datastore.jsonobjects.JsonUpdate; +import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; //import org.onap.music.main.ResultType; import org.onap.music.rest.RestMusicAdminAPI; import org.onap.music.rest.RestMusicDataAPI; import org.onap.music.rest.RestMusicQAPI; -import org.onap.music.service.impl.MusicCassaCore; import org.onap.music.rest.RestMusicLocksAPI; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; @@ -120,13 +119,12 @@ public class TestRestMusicQAPI { static JsonKeySpace kspObject=null; static RestMusicDataAPI data = new RestMusicDataAPI(); static Response resp; - static MusicCassaCore MusicCore = MusicCassaCore.getInstance(); @BeforeClass public static void init() throws Exception { try { - MusicDataStoreHandle.mDstoreHandle = CassandraCQLQueries.connectToEmbeddedCassandra(); - MusicCassaCore.mLockHandle = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle); + MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); + MusicCore.mLockHandle = new CassaLockStore(MusicCore.mDstoreHandle); // System.out.println("before class keysp"); //resp=data.createKeySpace(majorV,minorV,patchV,aid,appName,userId,password,kspObject,keyspaceName); @@ -213,8 +211,8 @@ public class TestRestMusicQAPI { testObject = new PreparedQueryObject(); testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin"); MusicCore.eventualPut(testObject); - if (MusicDataStoreHandle.mDstoreHandle!=null) - MusicDataStoreHandle.mDstoreHandle.close(); + if (MusicCore.mDstoreHandle!=null) + MusicCore.mDstoreHandle.close(); if (zkServer!=null) zkServer.stop(); } diff --git a/src/test/java/org/onap/music/unittests/TestVotingApp.java b/src/test/java/org/onap/music/unittests/TestVotingApp.java index 8ac71802..e7a1e252 100644 --- a/src/test/java/org/onap/music/unittests/TestVotingApp.java +++ b/src/test/java/org/onap/music/unittests/TestVotingApp.java @@ -2,12 +2,12 @@ package org.onap.music.unittests; import java.util.HashMap; import java.util.Map; - + import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicLockingException; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.service.impl.MusicCassaCore; +import org.onap.music.main.MusicCore; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -19,7 +19,6 @@ public class TestVotingApp { String keyspaceName; String tableName; - static MusicCassaCore MusicCore = MusicCassaCore.getInstance(); public TestVotingApp() throws MusicServiceException { keyspaceName = "VotingAppForMusic"+System.currentTimeMillis(); -- cgit 1.2.3-korg