diff options
author | Tschaen, Brendan <ctschaen@att.com> | 2019-02-25 17:31:00 -0500 |
---|---|---|
committer | Tschaen, Brendan <ctschaen@att.com> | 2019-02-26 11:51:24 -0500 |
commit | b58e1d3db05aed02b62a557bf14db6ea7b8df5f5 (patch) | |
tree | 52efe61d7a948c98d088b6c09e3a31fdc41f801f /src | |
parent | 7c2316f479f338c4b0fd9a150d8b858cea3b377c (diff) |
Remove zookeeper reference
Test cachingUtil, and other test coverage increase
Remove unused code
Rebased ontop of authentication changes
Change-Id: I795a9a90d0863b03cc2a4917d4ff13a0a6fca1cc
Issue-ID: MUSIC-341
Signed-off-by: Tschaen, Brendan <ctschaen@att.com>
Diffstat (limited to 'src')
24 files changed, 225 insertions, 3113 deletions
diff --git a/src/main/java/org/onap/music/authentication/CachingUtil.java b/src/main/java/org/onap/music/authentication/CachingUtil.java index 534038e3..80eed1e6 100755 --- a/src/main/java/org/onap/music/authentication/CachingUtil.java +++ b/src/main/java/org/onap/music/authentication/CachingUtil.java @@ -64,11 +64,12 @@ public class CachingUtil implements Runnable { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CachingUtil.class); + /** keyspace & ns */ private static CacheAccess<String, String> musicCache = JCS.getInstance("musicCache"); - private static CacheAccess<String, Map<String, String>> aafCache = JCS.getInstance("aafCache"); + /** cache to hold isaaf application */ private static CacheAccess<String, String> appNameCache = JCS.getInstance("appNameCache"); + /** hold user creds for namespace */ private static CacheAccess<String, Map<String, String>> musicValidateCache = JCS.getInstance("musicValidateCache"); - private static CacheAccess<String, List<String>> callbackNotifyList = JCS.getInstance("eternalCache"); private static Map<String, Number> userAttempts = new HashMap<>(); private static Map<String, Calendar> lastFailedTime = new HashMap<>(); private static CacheAccess<String, PreparedStatement> queryBank = JCS.getInstance("statementBank"); @@ -98,26 +99,6 @@ public class CachingUtil implements Runnable { private static final String USERNAME="username"; private static final String PASSWORD="password"; - - public boolean isCacheRefreshNeeded() { - if (aafCache.get("initBlankMap") == null) - return true; - return false; - } - - public static void updateCallbackNotifyList(List<String> notifyList) { - logger.info("callbackNotifyList: updating cache....."); - callbackNotifyList.put("callbackNotify", notifyList); - } - - public static List<String> getCallbackNotifyList() { - return callbackNotifyList.get("callbackNotify"); - } - - public void initializeMusicCache() { - logger.info(EELFLoggerDelegate.applicationLogger,"Initializing Music Cache..."); - musicCache.put("isInitialized", "true"); - } public void initializeAafCache() throws MusicServiceException { logger.info(EELFLoggerDelegate.applicationLogger,"Resetting and initializing AAF Cache..."); @@ -145,7 +126,7 @@ public class CachingUtil implements Runnable { if (responseObj) { map = new HashMap<>(); map.put(userId, password); - aafCache.put(nameSpace, map); + musicValidateCache.put(nameSpace, map); musicCache.put(keySpace, nameSpace); logger.debug("Cronjob: Cache Updated with AAF response for namespace " + nameSpace); @@ -171,10 +152,10 @@ public class CachingUtil implements Runnable { public static boolean authenticateAAFUser(String nameSpace, String userId, String password, String keySpace) throws Exception { - if (aafCache.get(nameSpace) != null && musicCache.get(keySpace)!=null) { + if (musicValidateCache.get(nameSpace) != null && musicCache.get(keySpace)!=null) { if (keySpace != null && !musicCache.get(keySpace).equals(nameSpace)) { logger.info(EELFLoggerDelegate.applicationLogger,"Create new application for the same namespace."); - } else if (aafCache.get(nameSpace).get(userId).equals(password)) { + } else if (musicValidateCache.get(nameSpace).get(userId).equals(password)) { logger.info(EELFLoggerDelegate.applicationLogger,"Authenticated with cache value.."); // reset invalid attempts to 0 userAttempts.put(nameSpace, 0); @@ -214,7 +195,7 @@ public class CachingUtil implements Runnable { logger.info(EELFLoggerDelegate.applicationLogger,"Valid user. Cache is updated for "+nameSpace); Map<String, String> map = new HashMap<>(); map.put(userId, password); - aafCache.put(nameSpace, map); + musicValidateCache.put(nameSpace, map); musicCache.put(keySpace, nameSpace); return true; } @@ -475,16 +456,4 @@ public class CachingUtil implements Runnable { CachingUtil.updateMusicValidateCache(nameSpace, userId, pwd); return resultMap; } - - public static void deleteKeysFromDB(String deleteKeys) { - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "DELETE FROM admin.locks WHERE lock_id IN ("+deleteKeys+")"); - try { - MusicCore.nonKeyRelatedPut(pQuery, "eventual"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), "Deleting keys from " - + "DB failed."); - } - } } diff --git a/src/main/java/org/onap/music/authentication/MusicAuthentication.java b/src/main/java/org/onap/music/authentication/MusicAuthentication.java index 10f1e30c..6c38e6df 100644 --- a/src/main/java/org/onap/music/authentication/MusicAuthentication.java +++ b/src/main/java/org/onap/music/authentication/MusicAuthentication.java @@ -62,8 +62,7 @@ public class MusicAuthentication implements MusicAuthenticator { */ @Deprecated public static Map<String, Object> autheticateUser(String nameSpace, String userId, - String password, String keyspace, String aid, String operation) - throws Exception { + String password, String keyspace, String aid, String operation) { logger.info(EELFLoggerDelegate.applicationLogger,"Inside User Authentication......."); Map<String, Object> resultMap = new HashMap<>(); String uuid = null; @@ -132,10 +131,17 @@ public class MusicAuthentication implements MusicAuthenticator { 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 { + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace)); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), + MusicUtil.DEFAULTKEYSPACENAME)); + } catch (Exception e1) { + logger.error(EELFLoggerDelegate.errorLogger, e1, "Can not authenticate for createkeyspace", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); + resultMap.put("Exception", "Cannot authenticate for createKeyspace"); + return resultMap; + } + try { Row rs = MusicCore.get(pQuery).one(); 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 c523d5f4..6ea05c7b 100644 --- a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java +++ b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java @@ -52,7 +52,6 @@ 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.MusicZKCore; import org.onap.music.authentication.MusicAuthentication; import org.onap.music.conductor.*; 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 1f9fe5ba..079fc579 100644 --- a/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java +++ b/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java @@ -33,10 +33,8 @@ 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.MusicServiceException; -import org.onap.music.lockingservice.zookeeper.MusicLockingService; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; -import org.onap.music.service.impl.MusicZKCore; import org.onap.music.main.MusicCore; import com.datastax.driver.core.ConsistencyLevel; @@ -50,7 +48,6 @@ public class MusicHealthCheck { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class); private String cassandrHost; - private String zookeeperHost; public String getCassandraStatus(String consistency) { logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra"); @@ -67,7 +64,7 @@ public class MusicHealthCheck { try { result = getAdminKeySpace(consistency); } catch (MusicServiceException e1) { - logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); + logger.error(EELFLoggerDelegate.errorLogger, e1, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); } } else { logger.error("Error", e); @@ -98,29 +95,11 @@ public class MusicHealthCheck { try { rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString()); } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); + logger.error(EELFLoggerDelegate.errorLogger, e, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); } return rs != null && rs.getResult().toLowerCase().contains("success"); } - public String getZookeeperStatus() { - - String host = MusicUtil.getMyZkHost(); - logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host); - try { - MusicLockingService lockingService = MusicZKCore.getLockingServiceHandle(); - // additionally need to call the ZK to create,aquire and delete lock - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR, - ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL); - return "INACTIVE"; - } - - logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running"); - return "ACTIVE"; - - } - public String getCassandrHost() { return cassandrHost; } @@ -129,12 +108,4 @@ public class MusicHealthCheck { this.cassandrHost = cassandrHost; } - public String getZookeeperHost() { - return zookeeperHost; - } - - public void setZookeeperHost(String zookeeperHost) { - this.zookeeperHost = zookeeperHost; - } - } diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/LockListener.java b/src/main/java/org/onap/music/lockingservice/zookeeper/LockListener.java deleted file mode 100644 index e8ad3a5f..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/LockListener.java +++ /dev/null @@ -1,40 +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.lockingservice.zookeeper; - -/** - * This class has two methods which are call back methods when a lock is acquired and when the lock - * is released. - * - */ -public interface LockListener { - /** - * call back called when the lock is acquired - */ - public void lockAcquired(); - - /** - * call back called when the lock is released. - */ - public void lockReleased(); -} diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/MusicLockingService.java b/src/main/java/org/onap/music/lockingservice/zookeeper/MusicLockingService.java deleted file mode 100644 index a35e236b..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/MusicLockingService.java +++ /dev/null @@ -1,182 +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.lockingservice.zookeeper; - - -import java.io.IOException; -import java.util.StringTokenizer; -import java.util.concurrent.CountDownLatch; - -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.Watcher.Event.KeeperState; -import org.apache.zookeeper.ZooKeeper; -import org.onap.music.authentication.CachingUtil; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import org.onap.music.exceptions.MusicLockingException; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.MusicLockState; -import org.onap.music.main.MusicUtil; - - -public class MusicLockingService implements Watcher { - - - private static final int SESSION_TIMEOUT = 180000; - ZkStatelessLockService zkLockHandle = null; - private CountDownLatch connectedSignal = new CountDownLatch(1); - private static EELFLoggerDelegate logger = - EELFLoggerDelegate.getLogger(MusicLockingService.class); - - public MusicLockingService() throws MusicServiceException { - try { - ZooKeeper zk = new ZooKeeper(MusicUtil.getMyZkHost(), SESSION_TIMEOUT, this); - connectedSignal.await(); - zkLockHandle = new ZkStatelessLockService(zk); - } catch (IOException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); - throw new MusicServiceException("IO Error has occured" + e.getMessage()); - } catch (InterruptedException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw new MusicServiceException("Exception Occured " + e.getMessage()); - } - } - - public MusicLockingService(String lockServer) { - try { - ZooKeeper zk = new ZooKeeper(lockServer, SESSION_TIMEOUT, this); - connectedSignal.await(); - zkLockHandle = new ZkStatelessLockService(zk); - } catch (IOException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); - }catch( InterruptedException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch(Exception e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - } - - public ZkStatelessLockService getzkLockHandle() { - return zkLockHandle; - } - - public void createLockaIfItDoesNotExist(String lockName) { - if (!zkLockHandle.checkIfLockExists(lockName)) { - String lockHolder = null; - MusicLockState ml = new MusicLockState(MusicLockState.LockStatus.UNLOCKED, lockHolder); - byte[] data = ml.serialize(); - zkLockHandle.createLock(lockName, data); - } - } - - public void setLockState(String lockName, MusicLockState mls) { - byte[] data = mls.serialize(); - zkLockHandle.setNodeData(lockName, data); - } - - public MusicLockState getLockState(String lockName) throws MusicLockingException { - - byte[] data = null; - try{ - data = zkLockHandle.getNodeData(lockName); - }catch (Exception ex){ - logger.error(EELFLoggerDelegate.errorLogger, ex,AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - if(data !=null) - return MusicLockState.deSerialize(data); - else { - logger.error(EELFLoggerDelegate.errorLogger,"Invalid lock or acquire failed",AppMessages.INVALIDLOCK, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw new MusicLockingException("Invalid lock or acquire failed"); - } - } - - public String createLockId(String lockName) { - String lockIdWithSlash = zkLockHandle.createLockId(lockName); - return lockIdWithSlash.replace('/', '$'); - } - - public boolean isMyTurn(String lockIdWithDollar) { - String lockId = lockIdWithDollar.replace('$', '/'); - StringTokenizer st = new StringTokenizer(lockId); - String lockName = "/" + st.nextToken("/"); - try { - return zkLockHandle.lock(lockName, lockId); - } catch (KeeperException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.LOCKINGERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch( InterruptedException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch(Exception e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - return false; - } - - public void unlockAndDeleteId(String lockIdWithDollar) throws KeeperException.NoNodeException { - String lockId = lockIdWithDollar.replace('$', '/'); - zkLockHandle.unlock(lockId); - CachingUtil.deleteKeysFromDB("'"+lockId+"'"); - } - - public void deleteLock(String lockName) throws MusicLockingException { - if(lockIdExists(lockName)) - zkLockHandle.deleteLock(lockName); - else{ - throw new MusicLockingException("Lock does not exist.Please check the lock: " + lockName + " and try again"); - } - } - - public String whoseTurnIsIt(String lockName) { - String lockHolder = zkLockHandle.currentLockHolder(lockName); - return lockHolder.replace('/', '$'); - - } - - public void process(WatchedEvent event) { // Watcher interface - if (event.getState() == KeeperState.SyncConnected) { - connectedSignal.countDown(); - } - } - - - public void close() { - zkLockHandle.close(); - } - - public boolean lockIdExists(String lockIdWithDollar) { - String lockId = lockIdWithDollar.replace('$', '/'); - return zkLockHandle.checkIfLockExists(lockId); - } - -} diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/ProtocolSupport.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ProtocolSupport.java deleted file mode 100644 index 2e88cc7a..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ProtocolSupport.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * Modifications Copyright (C) 2018 IBM. - * =================================================================== - * 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.lockingservice.zookeeper; - -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.ZooKeeper; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Stat; -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.zookeeper.ZooKeeperOperation; - -import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * A base class for protocol implementations which provides a number of higher level helper methods - * for working with ZooKeeper along with retrying synchronous operations if the connection to - * ZooKeeper closes such as {@link #retryOperation(ZooKeeperOperation)} - * - */ -class ProtocolSupport { - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProtocolSupport.class); - - protected ZooKeeper zookeeper; - private AtomicBoolean closed = new AtomicBoolean(false); - private long retryDelay = 500L; - private int retryCount = 10; - private List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE; - - /** - * Closes this strategy and releases any ZooKeeper resources; but keeps the ZooKeeper instance - * open - */ - public void close() { - if (closed.compareAndSet(false, true)) { - doClose(); - } - } - - /** - * return zookeeper client instance - * - * @return zookeeper client instance - */ - public ZooKeeper getZookeeper() { - return zookeeper; - } - - /** - * return the acl its using - * - * @return the acl. - */ - public List<ACL> getAcl() { - return acl; - } - - /** - * set the acl - * - * @param acl the acl to set to - */ - public void setAcl(List<ACL> acl) { - this.acl = acl; - } - - /** - * get the retry delay in milliseconds - * - * @return the retry delay - */ - public long getRetryDelay() { - return retryDelay; - } - - /** - * Sets the time waited between retry delays - * - * @param retryDelay the retry delay - */ - public void setRetryDelay(long retryDelay) { - this.retryDelay = retryDelay; - } - - /** - * Allow derived classes to perform some custom closing operations to release resources - */ - protected void doClose() { - throw new UnsupportedOperationException(); - } - - - /** - * Perform the given operation, retrying if the connection fails - * - * @return object. it needs to be cast to the callee's expected return type. - * @param operation FILL IN - * @throws KeeperException FILL IN - * @throws InterruptedException FILL IN - */ - protected Object retryOperation(ZooKeeperOperation operation) - throws KeeperException, InterruptedException { - KeeperException exception = null; - for (int i = 0; i < retryCount; i++) { - try { - return operation.execute(); - } catch (KeeperException.SessionExpiredException e) { - logger.error(EELFLoggerDelegate.errorLogger, e,AppMessages.SESSIONEXPIRED+" for: " + zookeeper + " so reconnecting due to: " + e, ErrorSeverity.ERROR, ErrorTypes.SESSIONEXPIRED); - throw e; - } catch (KeeperException.ConnectionLossException e) { - if (exception == null) { - exception = e; - } - logger.error(EELFLoggerDelegate.errorLogger, e,AppMessages.CONNCECTIVITYERROR, ErrorSeverity.ERROR, ErrorTypes.SESSIONEXPIRED); - logger.error(EELFLoggerDelegate.errorLogger, e,"Attempt " + i + " failed with connection loss so attempting to reconnect: " + e); - - retryDelay(i); - } - } - if(exception == null) - { - throw new NullPointerException(); - } - else{ - throw exception; - } - } - - /** - * Ensures that the given path exists with no data, the current ACL and no flags - * - * @param path the lock path - */ - protected void ensurePathExists(String path) { - ensureExists(path, null, acl, CreateMode.PERSISTENT); - } - - /** - * Ensures that the given path exists with the given data, ACL and flags - * - * @param path the lock path - * @param data the data - * @param acl list of ACLs applying to the path - * @param flags create mode flags - */ - protected void ensureExists(final String path, final byte[] data, final List<ACL> acl, - final CreateMode flags) { - try { - retryOperation(new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - Stat stat = zookeeper.exists(path, false); - if (stat != null) { - return true; - } - zookeeper.create(path, data, acl, flags); - return true; - } - }); - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - } - - /** - * Returns true if this protocol has been closed - * - * @return true if this protocol is closed - */ - protected boolean isClosed() { - return closed.get(); - } - - /** - * Performs a retry delay if this is not the first attempt - * - * @param attemptCount the number of the attempts performed so far - */ - protected void retryDelay(int attemptCount) { - if (attemptCount > 0) { - try { - Thread.sleep(attemptCount * retryDelay); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e,AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR); - logger.error(EELFLoggerDelegate.errorLogger, e,"Thread failed to sleep: " + e); - Thread.currentThread().interrupt(); - } - } - } -} diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/ZNodeName.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZNodeName.java deleted file mode 100644 index 5bdc5457..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ZNodeName.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Modifications Copyright (c) 2018 IBM - * =================================================================== - * 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.lockingservice.zookeeper; - -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; - -/** - * Represents an ephemeral znode name which has an ordered sequence number and can be sorted in - * order - * - */ -class ZNodeName implements Comparable<ZNodeName> { - private final String name; - private String prefix; - private int sequence = -1; - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ZNodeName.class); - - public ZNodeName(String name) { - if (name == null) { - throw new NullPointerException("id cannot be null"); - } - this.name = name; - this.prefix = name; - int idx = name.lastIndexOf('-'); - if (idx >= 0) { - this.prefix = name.substring(0, idx); - try { - this.sequence = Integer.parseInt(name.substring(idx + 1)); - // If an exception occurred we misdetected a sequence suffix, - // so return -1. - } catch (NumberFormatException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Number format exception "+idx, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR); - } catch (ArrayIndexOutOfBoundsException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Array out of bounds for "+idx, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR); - } - } - } - - @Override - public String toString() { - return name; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - - ZNodeName sequence = (ZNodeName) o; - - if (!name.equals(sequence.name)) - return false; - - return true; - } - - @Override - public int hashCode() { - return name.hashCode() + 37; - } - - public int compareTo(ZNodeName that) { - int answer = this.prefix.compareTo(that.prefix); - if (answer == 0) { - int s1 = this.sequence; - int s2 = that.sequence; - if (s1 == -1 && s2 == -1) { - return this.name.compareTo(that.name); - } - answer = s1 == -1 ? 1 : s2 == -1 ? -1 : s1 - s2; - } - return answer; - } - - /** - * Returns the name of the znode - */ - public String getName() { - return name; - } - - /** - * Returns the sequence number - */ - public int getZNodeName() { - return sequence; - } - - /** - * Returns the text prefix before the sequence number - */ - public String getPrefix() { - return prefix; - } -} diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java deleted file mode 100644 index e8ed257a..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java +++ /dev/null @@ -1,377 +0,0 @@ -/* - * ============LICENSE_START========================================== org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Modifications Copyright (c) 2018 IBM. - * Modifications Copyright (c) 2019 Samsung. - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. 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.lockingservice.zookeeper; - - -import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.KeeperException.NoNodeException; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.ZooKeeper; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Stat; -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 com.datastax.driver.core.DataType; - -/** - * A <a href="package.html">protocol to implement an exclusive write lock or to elect a leader</a>. - * <p/> - * You invoke {@link #lock()} to start the process of grabbing the lock; you may get the lock then - * or it may be some time later. - * <p/> - * You can register a listener so that you are invoked when you get the lock; otherwise you can ask - * if you have the lock by calling {@link #isOwner()} - * - */ -public class ZkStatelessLockService extends ProtocolSupport { - public ZkStatelessLockService(ZooKeeper zk) { - zookeeper = zk; - } - - private static EELFLoggerDelegate logger = - EELFLoggerDelegate.getLogger(ZkStatelessLockService.class); - - protected void createLock(final String path, final byte[] data) { - final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE; - try { - retryOperation(new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - zookeeper.create(path, data, acl, CreateMode.PERSISTENT); - return true; - } - }); - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - } - - @Override - public void close() { - try { - zookeeper.close(); - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - } - - public void setNodeData(final String lockName, final byte[] data) { - try { - retryOperation(new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - zookeeper.getSessionId(); - zookeeper.setData("/" + lockName, data, -1); - return true; - } - }); - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - - } - - public byte[] getNodeData(final String lockName) { - try { - if (zookeeper.exists("/" + lockName, null) != null) - return zookeeper.getData("/" + lockName, false, null); - else - return null; - - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - return null; - } - - public boolean checkIfLockExists(String lockName) { - boolean result = false; - try { - Stat stat = zookeeper.exists(lockName, false); - if (stat != null) { - result = true; - } - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - Thread.currentThread().interrupt(); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - return result; - } - - public void createNode(String nodeName) { - ensurePathExists(nodeName); - } - - public String createLockId(String dir) { - ensurePathExists(dir); - LockZooKeeperOperation zop = new LockZooKeeperOperation(dir); - try { - retryOperation(zop); - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - Thread.currentThread().interrupt(); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - return zop.getId(); - } - - /** - * Attempts to acquire the exclusive write lock returning whether or not it was acquired. Note - * that the exclusive lock may be acquired some time later after this method has been invoked - * due to the current lock owner going away. - */ - public synchronized boolean lock(String dir, String lockId) - throws KeeperException, InterruptedException { - if (isClosed()) { - return false; - } - LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId); - return (Boolean) retryOperation(zop); - } - - /** - * Removes the lock or associated znode if you no longer require the lock. this also removes - * your request in the queue for locking in case you do not already hold the lock. - * - * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper. - * @throws NoNodeException - */ - public synchronized void unlock(String lockId) throws RuntimeException, KeeperException.NoNodeException { - final String id = lockId; - if (!isClosed() && id != null) { - try { - ZooKeeperOperation zopdel = new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - zookeeper.delete(id, -1); - return Boolean.TRUE; - } - }; - zopdel.execute(); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - // set that we have been interrupted. - Thread.currentThread().interrupt(); - } catch (KeeperException.NoNodeException e) { - // do nothing - throw new KeeperException.NoNodeException("Lock doesn't exists. Release lock operation failed."); - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e); - } - } - } - - public synchronized String currentLockHolder(String mainLock) { - final String id = mainLock; - if (!isClosed() && id != null) { - List<String> names; - try { - names = zookeeper.getChildren(id, false); - if (names.isEmpty()) - return ""; - SortedSet<ZNodeName> sortedNames = new TreeSet<>(); - for (String name : names) { - sortedNames.add(new ZNodeName(id + "/" + name)); - } - return sortedNames.first().getName(); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - // set that we have been interrupted. - Thread.currentThread().interrupt(); - } catch (KeeperException.NoNodeException e) { - // do nothing - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e); - } - } - return "No lock holder!"; - } - - public synchronized void deleteLock(String mainLock) { - final String id = mainLock; - if (!isClosed() && id != null) { - try { - ZooKeeperOperation zopdel = new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - List<String> names = zookeeper.getChildren(id, false); - for (String name : names) { - zookeeper.delete(id + "/" + name, -1); - } - zookeeper.delete(id, -1); - return Boolean.TRUE; - } - }; - zopdel.execute(); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - // set that we have been interrupted. - Thread.currentThread().interrupt(); - } catch (KeeperException.NoNodeException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - // do nothing - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e); - } - } - - } - - /** - * a zoookeeper operation that is mainly responsible for all the magic required for locking. - */ - private class LockZooKeeperOperation implements ZooKeeperOperation { - - /** - * find if we have been created earlier if not create our node - * - * @param prefix the prefix node - * @param zookeeper the zookeeper client - * @param dir the dir parent - * @throws KeeperException - * @throws InterruptedException - */ - private String dir; - private String id = null; - - public String getId() { - return id; - } - - public LockZooKeeperOperation(String dir) { - this.dir = dir; - } - - public LockZooKeeperOperation(String dir, String id) { - this.dir = dir; - this.id = id; - } - - /** - * the command that is run and retried for actually obtaining the lock - * - * @return if the command was successful or not - */ - public boolean execute() throws KeeperException, InterruptedException { - do { - if (id == null) { - String prefix = "x-"; - byte[] data = {0x12, 0x34}; - id = zookeeper.create(dir + "/" + prefix, data, getAcl(), - CreateMode.PERSISTENT_SEQUENTIAL); - - if (logger.isDebugEnabled()) { - logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id); - } - if (id != null) { - Stat stat = null; - try { - stat = zookeeper.exists(id, false); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - Thread.currentThread().interrupt(); - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - - if (stat != null){ - Long ctime = stat.getCtime(); - MusicUtil.zkNodeMap.put(id, ctime); - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "INSERT INTO admin.locks(lock_id, ctime) VALUES (?,?)"); - try { - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), id)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), ctime)); - MusicCore.eventualPut(pQuery); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); - } - } - break; - } - } - if (id != null) { - List<String> names = zookeeper.getChildren(dir, false); - if (names.isEmpty()) { - logger.info(EELFLoggerDelegate.applicationLogger, "No children in: " + dir - + " when we've just " + "created one! Lets recreate it..."); - // lets force the recreation of the id - id = null; - return Boolean.FALSE; - - } else { - // lets sort them explicitly (though they do seem to come back in order - // ususally :) - ZNodeName idName = new ZNodeName(id); - SortedSet<ZNodeName> sortedNames = new TreeSet<>(); - for (String name : names) { - sortedNames.add(new ZNodeName(dir + "/" + name)); - } - if (!sortedNames.contains(idName)) - return Boolean.FALSE; - - SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName); - if (!lessThanMe.isEmpty()) { - ZNodeName lastChildName = lessThanMe.last(); - String lastChildId = lastChildName.getName(); - if (logger.isDebugEnabled()) { - logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId); - } - Stat stat = zookeeper.exists(lastChildId, false); - if (stat != null) { - return Boolean.FALSE; - } else { - logger.info(EELFLoggerDelegate.applicationLogger, - "Could not find the" + " stats for less than me: " - + lastChildName.getName()); - } - } else - return Boolean.TRUE; - } - } - } while (id == null); - return Boolean.FALSE; - } - } - -} - diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/ZooKeeperOperation.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZooKeeperOperation.java deleted file mode 100644 index 34efebf2..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ZooKeeperOperation.java +++ /dev/null @@ -1,43 +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.lockingservice.zookeeper; - -import org.apache.zookeeper.KeeperException; - -/** - * A callback object which can be used for implementing retry-able operations in the - * {@link org.onap.music.lockingservice.zookeeper.ProtocolSupport} class - * - */ -public interface ZooKeeperOperation { - - /** - * Performs the operation - which may be involved multiple times if the connection - * to ZooKeeper closes during this operation - * - * @return the result of the operation or null - * @throws KeeperException FILL IN - * @throws InterruptedException FILL IN - */ - public boolean execute() throws KeeperException, InterruptedException; -} diff --git a/src/main/java/org/onap/music/main/CronJobManager.java b/src/main/java/org/onap/music/main/CronJobManager.java deleted file mode 100644 index 0d7d9239..00000000 --- a/src/main/java/org/onap/music/main/CronJobManager.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Modifications Copyright (c) 2019 Samsung - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * 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.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.Iterator; -import org.onap.music.authentication.CachingUtil; -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.MusicServiceException; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; - -@Component -public class CronJobManager { - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CronJobManager.class); - - private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); - - - @Scheduled(cron = "0 0 0 * * ?") - public void scheduleTaskWithFixedRate() { - logger.info("Executing cronjob to cleanup locks..", dateTimeFormatter.format(LocalDateTime.now()) ); - deleteLocksFromDB(); - } - - public void deleteLocksFromDB() { - PreparedQueryObject pQuery = new PreparedQueryObject(); - 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); - if ( result.equals(ResultType.FAILURE)) { - logger.error(EELFLoggerDelegate.errorLogger,"Error creating Admin.locks table.",AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR); - } - } catch (MusicServiceException e1) { - logger.error(EELFLoggerDelegate.errorLogger,e1,AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR); - } - - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "select * from admin.locks"); - try { - ResultSet rs = MusicCore.get(pQuery); - Iterator<Row> it = rs.iterator(); - StringBuilder deleteKeys = new StringBuilder(); - Boolean expiredKeys = false; - while (it.hasNext()) { - Row row = it.next(); - String id = row.getString("lock_id"); - long ctime = Long.parseLong(row.getString("ctime")); - if(System.currentTimeMillis() >= ctime + 24 * 60 * 60 * 1000) { - expiredKeys = true; - String new_id = id.substring(1); - try { - MusicCore.deleteLock(new_id); - } catch (MusicLockingException e) { - logger.info(EELFLoggerDelegate.applicationLogger, - e.getMessage()); - } - deleteKeys.append("'").append(id).append("'").append(","); - } - } - if(expiredKeys) { - deleteKeys.deleteCharAt(deleteKeys.length()-1); - CachingUtil.deleteKeysFromDB(deleteKeys.toString()); - } - } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e,AppMessages.CACHEERROR,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - } - } -} diff --git a/src/main/java/org/onap/music/main/MusicUtil.java b/src/main/java/org/onap/music/main/MusicUtil.java index 06eeb805..aa82ea97 100755 --- a/src/main/java/org/onap/music/main/MusicUtil.java +++ b/src/main/java/org/onap/music/main/MusicUtil.java @@ -58,7 +58,6 @@ 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.service.impl.MusicZKCore; import com.datastax.driver.core.ConsistencyLevel; import com.datastax.driver.core.DataType; @@ -94,7 +93,6 @@ public class MusicUtil { public static final String USERID = "userId"; public static final String PASSWORD = "password"; public static final String CASSANDRA = "cassandra"; - public static final String ZOOKEEPER = "zookeeper"; public static final String AUTHORIZATION = "Authorization"; @@ -788,8 +786,6 @@ public class MusicUtil { public static MusicCoreService getMusicCoreService() { if(getLockUsing().equals(MusicUtil.CASSANDRA)) return MusicCassaCore.getInstance(); - else if (getLockUsing().equals(MusicUtil.ZOOKEEPER)) - return MusicZKCore.getInstance(); else return MusicCassaCore.getInstance(); } diff --git a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java index f72a1ac9..a7522b90 100755 --- a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java @@ -862,10 +862,6 @@ public class RestMusicDataAPI { result = MusicCore.atomicPut(keyspace, tablename, primaryKey, queryObject, null); } - else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { - result = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, primaryKey, queryObject, null); - - } } catch (Exception ex) { logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); @@ -1238,11 +1234,7 @@ public class RestMusicDataAPI { } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, queryObject, conditionInfo); - } - else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { - operationResult = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, rowId.primarKeyValue, - queryObject, conditionInfo); - }else if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL_NB)) { + } else if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL_NB)) { operationResult = MusicCore.eventualPut_nb(queryObject, keyspace, tablename, rowId.primarKeyValue); } @@ -1397,10 +1389,6 @@ public class RestMusicDataAPI { } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { results = MusicCore.atomicGet(keyspace, tablename, rowId.primarKeyValue, queryObject); } - - else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { - results = MusicCore.atomicGetWithDeleteLock(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(); } diff --git a/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java b/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java index a9b4c7af..cb8965ee 100644 --- a/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java @@ -91,24 +91,6 @@ public class RestMusicHealthCheckAPI { } @GET - @Path("/pingZookeeper") - @ApiOperation(value = "Get Health Status", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response ZKStatus(@Context HttpServletResponse response) { - logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Zookeeper"); - Map<String, Object> resultMap = new HashMap<>(); - MusicHealthCheck ZKHealthCheck = new MusicHealthCheck(); - String status = ZKHealthCheck.getZookeeperStatus(); - if(status.equals(ACTIVE_STATUS)) { - resultMap.put(ACTIVE_STATUS, "Zookeeper is Active and Running"); - return Response.status(Status.OK).entity(resultMap).build(); - }else { - resultMap.put("INACTIVE", "Zookeeper is not responding"); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - } - - @GET @Path("/musicHealthCheck") @ApiOperation(value = "Get Health Status", response = Map.class) @Produces(MediaType.APPLICATION_JSON) @@ -116,13 +98,8 @@ public class RestMusicHealthCheckAPI { logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for Health Check status for MUSIC"); Map<String, Object> resultMap = new HashMap<>(); MusicHealthCheck healthCheck = new MusicHealthCheck(); - String status = healthCheck.getZookeeperStatus(); - if(status.equals(ACTIVE_STATUS)) { - resultMap.put("ZooKeeper", "Active"); - }else { - resultMap.put("ZooKeeper", "Inactive"); - } - status = healthCheck.getCassandraStatus(ConsistencyLevel.ANY.toString()); + + String status = healthCheck.getCassandraStatus(ConsistencyLevel.ANY.toString()); if(status.equals(ACTIVE_STATUS)) { resultMap.put("Cassandra", "Active"); } else { diff --git a/src/main/java/org/onap/music/rest/RestMusicQAPI.java b/src/main/java/org/onap/music/rest/RestMusicQAPI.java index 800dad71..0865eafe 100755 --- a/src/main/java/org/onap/music/rest/RestMusicQAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicQAPI.java @@ -57,7 +57,6 @@ import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.response.jsonobjects.JsonResponse; -import org.onap.music.service.impl.MusicZKCore; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; diff --git a/src/main/java/org/onap/music/service/impl/MusicZKCore.java b/src/main/java/org/onap/music/service/impl/MusicZKCore.java deleted file mode 100644 index 93c5abc8..00000000 --- a/src/main/java/org/onap/music/service/impl/MusicZKCore.java +++ /dev/null @@ -1,888 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Modifications Copyright (c) 2019 Samsung - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * 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.List; -import java.util.Map; -import java.util.StringTokenizer; - -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.KeeperException.NoNodeException; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.datastore.jsonobjects.JsonKeySpace; -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.MusicLockState; -import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus; -import org.onap.music.lockingservice.zookeeper.MusicLockingService; -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 org.onap.music.datastore.*; - -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 MusicZKCore implements MusicCoreService { - - public static MusicLockingService mLockHandle = null; - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicZKCore.class); - private static MusicZKCore musicZKCoreInstance = null; - - private MusicZKCore() { - - } - public static MusicZKCore getInstance() { - - if(musicZKCoreInstance == null) { - musicZKCoreInstance = new MusicZKCore(); - } - return musicZKCoreInstance; - } - - - - - - public static MusicLockingService getLockingServiceHandle() throws MusicLockingException { - logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring lock store handle"); - long start = System.currentTimeMillis(); - - if (mLockHandle == null) { - try { - mLockHandle = new MusicLockingService(); - } 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 lockName) { - logger.info(EELFLoggerDelegate.applicationLogger,"Creating lock reference for lock name:" + lockName); - long start = System.currentTimeMillis(); - String lockId = null; - try { - lockId = getLockingServiceHandle().createLockId("/" + lockName); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.CREATELOCK+lockName,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to create lock reference:" + (end - start) + " ms"); - return lockId; - } - - /** - * - * @param key - * @return - */ - public static boolean isTableOrKeySpaceLock(String key) { - String[] splitString = key.split("\\."); - if (splitString.length > 2) - return false; - else - return true; - } - - /** - * - * @param key - * @return - */ - public static MusicLockState getMusicLockState(String key) { - long start = System.currentTimeMillis(); - try { - String[] splitString = key.split("\\."); - String keyspaceName = splitString[0]; - String tableName = splitString[1]; - String primaryKey = splitString[2]; - MusicLockState mls; - String lockName = keyspaceName + "." + tableName + "." + primaryKey; - mls = getLockingServiceHandle().getLockState(lockName); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to get lock state:" + (end - start) + " ms"); - return mls; - } catch (NullPointerException | MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.INVALIDLOCK,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - return null; - } - - public ReturnType acquireLockWithLease(String key, String lockId, long leasePeriod) { - try { - long start = System.currentTimeMillis(); - /* check if the current lock has exceeded its lease and if yes, release that lock */ - MusicLockState mls = getMusicLockState(key); - if (mls != null) { - if (mls.getLockStatus().equals(LockStatus.LOCKED)) { - logger.info(EELFLoggerDelegate.applicationLogger,"The current lock holder for " + key + " is " + mls.getLockHolder() - + ". Checking if it has exceeded lease"); - long currentLockPeriod = System.currentTimeMillis() - mls.getLeaseStartTime(); - long currentLeasePeriod = mls.getLeasePeriod(); - if (currentLockPeriod > currentLeasePeriod) { - logger.info(EELFLoggerDelegate.applicationLogger,"Lock period " + currentLockPeriod - + " has exceeded lease period " + currentLeasePeriod); - boolean voluntaryRelease = false; - String currentLockHolder = mls.getLockHolder(); - mls = releaseLock(currentLockHolder, voluntaryRelease); - } - } - } else - logger.error(EELFLoggerDelegate.errorLogger,key, AppMessages.INVALIDLOCK,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - - /* - * call the traditional acquire lock now and if the result returned is true, set the - * begin time-stamp and lease period - */ - if (acquireLock(key, lockId).getResult() == ResultType.SUCCESS) { - mls = getMusicLockState(key);// get latest state - if ( mls == null ) { - logger.info(EELFLoggerDelegate.applicationLogger,"Music Lock State is null"); - return new ReturnType(ResultType.FAILURE, "Could not acquire lock, Lock State is null"); - } - if (mls.getLeaseStartTime() == -1) {// set it again only if it is not set already - mls.setLeaseStartTime(System.currentTimeMillis()); - mls.setLeasePeriod(leasePeriod); - getLockingServiceHandle().setLockState(key, mls); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire leased lock:" + (end - start) + " ms"); - return new ReturnType(ResultType.SUCCESS, "Accquired lock"); - } else { - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to fail to acquire leased lock:" + (end - start) + " ms"); - return new ReturnType(ResultType.FAILURE, "Could not acquire lock"); - } - } 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 in acquireLockWithLease:\n" + exceptionAsString); - } - } - - public ReturnType acquireLock(String key, String lockId) throws MusicLockingException { - /* - * first check if I am on top. Since ids are not reusable there is no need to check - * lockStatus If the status is unlocked, then the above call will automatically return - * false. - */ - Boolean result = false; - try { - result = getLockingServiceHandle().isMyTurn(lockId); - } catch (MusicLockingException e2) { - logger.error(EELFLoggerDelegate.errorLogger,AppMessages.INVALIDLOCK + lockId + " " + e2); - throw new MusicLockingException(); - } - if (!result) { - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Not your turn, someone else has the lock"); - try { - if (!getLockingServiceHandle().lockIdExists(lockId)) { - logger.info(EELFLoggerDelegate.applicationLogger, "In acquire lock: this lockId doesn't exist"); - return new ReturnType(ResultType.FAILURE, "Lockid doesn't exist"); - } - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.INVALIDLOCK+lockId,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - throw new MusicLockingException(); - } - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: returning failure"); - return new ReturnType(ResultType.FAILURE, "Not your turn, someone else has the lock"); - } - - - // this is for backward compatibility where locks could also be acquired on just - // keyspaces or tables. - if (isTableOrKeySpaceLock(key)) { - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: A table or keyspace lock so no need to perform sync...so returning true"); - return new ReturnType(ResultType.SUCCESS, "A table or keyspace lock so no need to perform sync...so returning true"); - } - - // read the lock name corresponding to the key and if the status is locked or being locked, - // then return false - MusicLockState currentMls = null; - MusicLockState newMls = null; - try { - currentMls = getMusicLockState(key); - String currentLockHolder = null; - if(currentMls != null) { currentLockHolder = currentMls.getLockHolder(); }; - if (lockId.equals(currentLockHolder)) { - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: You already have the lock!"); - return new ReturnType(ResultType.SUCCESS, "You already have the lock!"); - } - } catch (NullPointerException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.INVALIDLOCK+lockId,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - - // change status to "being locked". This state transition is necessary to ensure syncing - // before granting the lock - String lockHolder = null; - boolean needToSyncQuorum = false; - if (currentMls != null) - needToSyncQuorum = currentMls.isNeedToSyncQuorum(); - - - newMls = new MusicLockState(MusicLockState.LockStatus.BEING_LOCKED, lockHolder, - needToSyncQuorum); - try { - getLockingServiceHandle().setLockState(key, newMls); - } catch (MusicLockingException e1) { - logger.error(EELFLoggerDelegate.errorLogger,e1.getMessage(), AppMessages.LOCKSTATE+key,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Set lock state to being_locked"); - - // do syncing if this was a forced lock release - if (needToSyncQuorum) { - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Since there was a forcible release, need to sync quorum!"); - try { - syncQuorum(key); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,"Failed to set Lock state " + e); - } - } - - // change status to locked - lockHolder = lockId; - needToSyncQuorum = false; - newMls = new MusicLockState(MusicLockState.LockStatus.LOCKED, lockHolder, needToSyncQuorum); - try { - getLockingServiceHandle().setLockState(key, newMls); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKSTATE+key,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Set lock state to locked and assigned current lock ref " - + lockId + " as holder"); - - return new ReturnType(result?ResultType.SUCCESS:ResultType.FAILURE, "Set lock state to locked and assigned a lock holder"); - } - - - - /** - * - * @param keyspaceName - * @param kspObject - * @return - * @throws Exception - */ - public boolean createKeyspace(String keyspaceName, JsonKeySpace kspObject) throws Exception { - return true; - } - - - private static void syncQuorum(String key) throws Exception { - logger.info(EELFLoggerDelegate.applicationLogger,"Performing sync operation---"); - String[] splitString = key.split("\\."); - String keyspaceName = splitString[0]; - String tableName = splitString[1]; - String primaryKeyValue = splitString[2]; - PreparedQueryObject selectQuery = new PreparedQueryObject(); - PreparedQueryObject updateQuery = new PreparedQueryObject(); - - // get the primary key d - TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspaceName, tableName); - 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 " + keyspaceName + "." + tableName + " WHERE " - + primaryKeyName + "= ?" + ";"); - selectQuery.addValue(cqlFormattedPrimaryKeyValue); - MusicUtil.writeBackToQuorum(selectQuery, primaryKeyName, updateQuery, keyspaceName, tableName, - cqlFormattedPrimaryKeyValue); - } - - - - - /** - * - * @param query - * @return ResultSet - */ - public ResultSet quorumGet(PreparedQueryObject query) { - ResultSet results = null; - try { - results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(query); - } catch (MusicServiceException | MusicQueryException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.MAJOR, ErrorTypes.GENERALSERVICEERROR); - - } - return results; - - } - - - - /** - * - * @param lockName - * @return - */ - public String whoseTurnIsIt(String lockName) { - - try { - return getLockingServiceHandle().whoseTurnIsIt("/" + lockName) + ""; - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - return null; - - - } - - /** - * - * @param lockId - * @return - */ - public static String getLockNameFromId(String lockId) { - StringTokenizer st = new StringTokenizer(lockId); - return st.nextToken("$"); - } - - public void destroyLockRef(String lockId) { - long start = System.currentTimeMillis(); - try { - getLockingServiceHandle().unlockAndDeleteId(lockId); - } catch (MusicLockingException | NoNodeException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DESTROYLOCK+lockId ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to destroy lock reference:" + (end - start) + " ms"); - } - - public MusicLockState releaseLock(String lockId, boolean voluntaryRelease) { - long start = System.currentTimeMillis(); - try { - getLockingServiceHandle().unlockAndDeleteId(lockId); - } catch (MusicLockingException e1) { - logger.error(EELFLoggerDelegate.errorLogger,e1.getMessage(), AppMessages.RELEASELOCK+lockId ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } catch (KeeperException.NoNodeException nne) { - logger.error(EELFLoggerDelegate.errorLogger,"Failed to release Lock " + lockId + " " + nne); - MusicLockState mls = new MusicLockState("Lock doesn't exists. Release lock operation failed."); - return mls; - } - String lockName = getLockNameFromId(lockId); - MusicLockState mls; - String lockHolder = null; - if (voluntaryRelease) { - mls = new MusicLockState(MusicLockState.LockStatus.UNLOCKED, lockHolder); - logger.info(EELFLoggerDelegate.applicationLogger,"In unlock: lock voluntarily released for " + lockId); - } else { - boolean needToSyncQuorum = true; - mls = new MusicLockState(MusicLockState.LockStatus.UNLOCKED, lockHolder, - needToSyncQuorum); - logger.info(EELFLoggerDelegate.applicationLogger,"In unlock: lock forcibly released for " + lockId); - } - try { - getLockingServiceHandle().setLockState(lockName, mls); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.RELEASELOCK+lockId ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to release lock:" + (end - start) + " ms"); - return mls; - } - - public static void voluntaryReleaseLock(String lockId) throws MusicLockingException{ - try { - getLockingServiceHandle().unlockAndDeleteId(lockId); - } catch (KeeperException.NoNodeException e) { - // ??? No way - } - } - - /** - * - * @param lockName - * @throws MusicLockingException - */ - public void deleteLock(String lockName) throws MusicLockingException { - long start = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Deleting lock for " + lockName); - try { - getLockingServiceHandle().deleteLock("/" + lockName); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DELTELOCK+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - throw new MusicLockingException(e.getMessage()); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to delete lock:" + (end - start) + " ms"); - } - - - /** - * - * @param nodeName - */ - public static void pureZkCreate(String nodeName) { - try { - getLockingServiceHandle().getzkLockHandle().createNode(nodeName); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - } - - /** - * - * @param nodeName - * @param data - */ - public static void pureZkWrite(String nodeName, byte[] data) { - long start = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Performing zookeeper write to " + nodeName); - try { - getLockingServiceHandle().getzkLockHandle().setNodeData(nodeName, data); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - logger.info(EELFLoggerDelegate.applicationLogger,"Performed zookeeper write to " + nodeName); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the actual zk put:" + (end - start) + " ms"); - } - - /** - * - * @param nodeName - * @return - */ - public static byte[] pureZkRead(String nodeName) { - long start = System.currentTimeMillis(); - byte[] data = null; - try { - data = getLockingServiceHandle().getzkLockHandle().getNodeData(nodeName); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the actual zk put:" + (end - start) + " ms"); - return data; - } - - - - // Prepared Query Additions. - - /** - * - * @param keyspaceName - * @param tableName - * @param primaryKey - * @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, "Eventual Operation Successfully performed"); - } else { - return new ReturnType(ResultType.FAILURE, "Eventual Operation failed to perform"); - } - } - - /** - * - * @param keyspaceName - * @param tableName - * @param primaryKey - * @param queryObject - * @param lockId - * @return - */ - public ReturnType criticalPut(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, String lockId, Condition conditionInfo) { - long start = System.currentTimeMillis(); - - try { - MusicLockState mls = getLockingServiceHandle() - .getLockState(keyspaceName + "." + tableName + "." + primaryKey); - if (mls.getLockHolder().equals(lockId) == true) { - 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 doing the critical put, check sanctity of the row/conditions:\n" - + e.getMessage()); - } - boolean result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, MusicUtil.CRITICAL); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms"); - if (result) { - return new ReturnType(ResultType.SUCCESS, "Update performed"); - } else { - return new ReturnType(ResultType.FAILURE, "Update failed to perform"); - } - } else - return new ReturnType(ResultType.FAILURE, - "Cannot perform operation since you are the not the lock holder"); - } catch (MusicQueryException | MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - return new ReturnType(ResultType.FAILURE, - "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n" - + e.getMessage()); - }catch(MusicLockingException ex){ - return new ReturnType(ResultType.FAILURE,ex.getMessage()); - } - - } - - /** - * - * @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().executeOneConsistencyGet(queryObject); - } catch (MusicQueryException | MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - throw new MusicServiceException(e.getMessage()); - } - return results; - } - - public static String getMyHostId() { - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString("SELECT HOST_ID FROM SYSTEM.LOCAL"); - ResultSet rs = null; - try { - rs = MusicDataStoreHandle.getDSHandle().executeOneConsistencyGet(pQuery); - Row row = rs.one(); - return (row == null) ? "UNKNOWN" : row.getUUID("HOST_ID").toString(); - } catch (Exception e) { - e.printStackTrace(); - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - } - logger.error(EELFLoggerDelegate.errorLogger, "Some issue during MusicCore.getMyHostId"); - return "UNKNOW"; - } - - /** - * 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 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 - * @param lockId lock ID to check if the resource is free to perform the operation. - * @return ResultSet - */ - public ResultSet criticalGet(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, String lockId) throws MusicServiceException { - ResultSet results = null; - try { - MusicLockState mls = getLockingServiceHandle() - .getLockState(keyspaceName + "." + tableName + "." + primaryKey); - if (mls.getLockHolder().equals(lockId)) { - results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryObject); - } else - throw new MusicServiceException("YOU DO NOT HAVE THE LOCK"); - } 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 - */ - public ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException { - - long start = System.currentTimeMillis(); - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long lockCreationTime = System.currentTimeMillis(); - ReturnType lockAcqResult = acquireLock(key, lockId); - long lockAcqTime = System.currentTimeMillis(); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, - queryObject, lockId, conditionInfo); - long criticalPutTime = System.currentTimeMillis(); - voluntaryReleaseLock(lockId); - 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 " + lockId); - destroyLockRef(lockId); - return lockAcqResult; - } - } - - /** - * this function is mainly for the benchmarks to see the effect of lock deletion. - * - * @param keyspaceName - * @param tableName - * @param primaryKey - * @param queryObject - * @param conditionInfo - * @return - * @throws MusicLockingException - */ - public ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, - String primaryKey, PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException { - - long start = System.currentTimeMillis(); - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long lockCreationTime = System.currentTimeMillis(); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLock(key, lockId); - long lockAcqTime = System.currentTimeMillis(); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, - queryObject, lockId, conditionInfo); - long criticalPutTime = System.currentTimeMillis(); - deleteLock(key); - 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 " + lockId); - deleteLock(key); - 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 - */ - public ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException { - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLock(key, lockId); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - ResultSet result = - criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId); - voluntaryReleaseLock(lockId); - return result; - } else { - destroyLockRef(lockId); - logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); - return null; - } - } - - public ResultSet atomicGetWithDeleteLock(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException { - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - - ReturnType lockAcqResult = acquireLock(key, lockId); - - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger, "acquired lock with id " + lockId); - ResultSet result = criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId); - deleteLock(key); - return result; - } else { - deleteLock(key); - logger.info(EELFLoggerDelegate.applicationLogger, "unable to acquire lock, id " + lockId); - return null; - } - } - - /** - * @param lockName - * @return - */ - public Map<String, Object> validateLock(String lockName) { - return MusicUtil.validateLock(lockName); - } - - @Override - public ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject, - String consistency) throws MusicServiceException { - boolean result = false; - try { - //create shadow locking table - //result = 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 ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - throw new MusicServiceException(ex.getMessage()); - } - return result?ResultType.SUCCESS:ResultType.FAILURE; - } - - public static 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 = MusicDataStoreHandle.mDstoreHandle.executePut(queryObject, "eventual"); - return result; - } - - - @Override - public List<String> getLockQueue(String fullyQualifiedKey) - throws MusicServiceException, MusicQueryException, MusicLockingException { - // TODO Auto-generated method stub - return null; - } - - - - @Override - public long getLockQueueSize(String fullyQualifiedKey) - throws MusicServiceException, MusicQueryException, MusicLockingException { - // TODO Auto-generated method stub - return 0; - } - @Override - public ReturnType eventualPut_nb(PreparedQueryObject queryObject, String keyspace, String tablename, - String primaryKey) { - return eventualPut(queryObject); - } - - - - -} diff --git a/src/test/java/org/onap/music/unittests/MusicUtilTest.java b/src/test/java/org/onap/music/unittests/MusicUtilTest.java index b117c330..47f387cf 100644 --- a/src/test/java/org/onap/music/unittests/MusicUtilTest.java +++ b/src/test/java/org/onap/music/unittests/MusicUtilTest.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.UUID; import org.apache.cassandra.exceptions.PreparedQueryNotFoundException; import org.junit.Test; +import org.onap.music.authentication.CachingUtil; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.exceptions.MusicServiceException; import org.onap.music.main.MusicUtil; @@ -203,5 +204,15 @@ public class MusicUtilTest { assertTrue(result.contains("name")); assertTrue(result.contains("value")); } + + @Test + public void test_generateUUID() { + //this function shouldn't be in cachingUtil + System.out.println("Testing getUUID"); + String uuid1 = MusicUtil.generateUUID(); + String uuid2 = MusicUtil.generateUUID(); + assertFalse(uuid1==uuid2); + } + } diff --git a/src/test/java/org/onap/music/unittests/TestMusicCore.java b/src/test/java/org/onap/music/unittests/TestMusicCore.java deleted file mode 100644 index 5f9bf8f6..00000000 --- a/src/test/java/org/onap/music/unittests/TestMusicCore.java +++ /dev/null @@ -1,666 +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; - -import static org.junit.Assert.*; -import static org.onap.music.service.impl.MusicZKCore.mLockHandle; - -import java.util.HashMap; -import java.util.Map; -import org.apache.zookeeper.KeeperException.NoNodeException; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -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.lockingservice.cassandra.MusicLockState.LockStatus; -import org.onap.music.lockingservice.zookeeper.MusicLockingService; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.service.impl.MusicZKCore; -import org.onap.music.datastore.Condition; -import org.onap.music.datastore.MusicDataStore; -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 com.att.eelf.exception.EELFException; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Session; - - -public class TestMusicCore { - - - private Condition condition; - - - private ResultSet rs; - - - private PreparedQueryObject preparedQueryObject; - - - private Session session; - - @Mock - MusicZKCore musicZKCore; - - @Before - public void setUp() { - mLockHandle = Mockito.mock(MusicLockingService.class); - musicZKCore = MusicZKCore.getInstance(); - condition=Mockito.mock(Condition.class); - rs =Mockito.mock(ResultSet.class); - preparedQueryObject =Mockito.mock(PreparedQueryObject.class); - session =Mockito.mock(Session.class); - - } - - @Test - public void testCreateLockReferenceforvalidlock() { - Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock"); - String lockId = musicZKCore.createLockReference("test"); - assertEquals("lock", lockId); - Mockito.verify(mLockHandle).createLockId("/" + "test"); - } - - - @Test - public void testCreateLockReferencefornullname() { - //Mockito.when(mLockHandle.createLockId("/" + "test")).thenReturn("lock"); - String lockId = musicZKCore.createLockReference("x"); //test"); - //System.out.println("cjc exception lockhandle=" + mLockHandle+"lockid="+lockId ); - assertNotEquals("lock", lockId); - //Mockito.verify(mLockHandle).createLockId("/" + "test"); - } - - @Test - public void testIsTableOrKeySpaceLock() { - Boolean result = musicZKCore.isTableOrKeySpaceLock("ks1.tn1"); - assertTrue(result); - } - - @Test - public void testIsTableOrKeySpaceLockwithPrimarykey() { - Boolean result = musicZKCore.isTableOrKeySpaceLock("ks1.tn1.pk1"); - assertFalse(result); - } - - @Test - public void testGetMusicLockState() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - Mockito.when(mLockHandle.getLockState("ks1.tb1.pk1")).thenReturn(musicLockState); - MusicLockState mls = MusicZKCore.getMusicLockState("ks1.tb1.pk1"); - assertEquals(musicLockState, mls); - Mockito.verify(mLockHandle).getLockState("ks1.tb1.pk1"); - } - - @Test - public void testAcquireLockifisMyTurnTrue() throws MusicLockingException { - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType lock = musicZKCore.acquireLock("ks1.tn1", "id1"); - assertEquals(lock.getResult(), ResultType.SUCCESS); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testAcquireLockifisMyTurnFalse() throws MusicLockingException { - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ReturnType lock = musicZKCore.acquireLock("ks1.ts1", "id1"); - assertEquals(lock.getResult(), ResultType.FAILURE); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockTrue() throws MusicLockingException { - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType lock = musicZKCore.acquireLock("ks1.tn1", "id1"); - assertEquals(lock.getResult(), ResultType.SUCCESS); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandHaveLock() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - ReturnType lock = musicZKCore.acquireLock("ks1.tn1.pk1", "id1"); - assertEquals(lock.getResult(), ResultType.SUCCESS); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); - } - - @Test - public void testAcquireLockifisMyTurnTrueandIsTableOrKeySpaceLockFalseandDontHaveLock() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id2"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - ReturnType lock = musicZKCore.acquireLock("ks1.tn1.pk1", "id1"); - assertEquals(lock.getResult(), ResultType.SUCCESS); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); - } - - @Test - public void testAcquireLockifLockRefDoesntExist() throws MusicLockingException { - Mockito.when(mLockHandle.lockIdExists("bs1")).thenReturn(false); - ReturnType lock = musicZKCore.acquireLock("ks1.ts1", "bs1"); - assertEquals(lock.getResult(), ResultType.FAILURE); - assertEquals(lock.getMessage(), "Lockid doesn't exist"); - Mockito.verify(mLockHandle).lockIdExists("bs1"); - } - - @Test - public void testAcquireLockWithLeasewithLease() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - musicLockState.setLeasePeriod(0); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - } - - @Test - public void testAcquireLockWithLeasewithException() throws MusicLockingException { - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "failure"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenThrow(new MusicLockingException()); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - } - - @Test - public void testAcquireLockWithLeasewithLockStatusLOCKED() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - } - - @Test - public void testAcquireLockWithLeasewithLockStatusUNLOCKED() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - - } - - @Test - public void testAcquireLockWithLeaseIfNotMyTurn() throws MusicLockingException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ReturnType actualResult = musicZKCore.acquireLockWithLease("ks1.tn1.pk1", "id1", 6000); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle).getLockState("ks1.tn1.pk1"); - } - - @Test - public void testQuorumGet() throws MusicServiceException, MusicQueryException { - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - rs = Mockito.mock(ResultSet.class); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeQuorumConsistencyGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = musicZKCore.quorumGet(preparedQueryObject); - assertNotNull(rs1); - } - - @Test - public void testGetLockNameFromId() { - String lockname = MusicZKCore.getLockNameFromId("lockName$id"); - assertEquals("lockName", lockname); - } - - @Test - public void testDestroyLockRef() throws NoNodeException { - Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); - musicZKCore.destroyLockRef("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); - } - - @Test - public void testreleaseLockwithvoluntaryReleaseTrue() throws NoNodeException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); - Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); - MusicLockState musicLockState1 = musicZKCore.releaseLock("id1", true); - assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus()); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); - } - - @Test - public void testreleaseLockwithvoluntaryReleaseFalse() throws NoNodeException { - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); - Mockito.doNothing().when(mLockHandle).unlockAndDeleteId("id1"); - MusicLockState musicLockState1 = musicZKCore.releaseLock("id1", false); - assertEquals(musicLockState.getLockStatus(), musicLockState1.getLockStatus()); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).unlockAndDeleteId("id1"); - } - - @Test - public void testDeleteLock() throws MusicLockingException { - Mockito.doNothing().when(mLockHandle).deleteLock("/" + "id1"); - musicZKCore.deleteLock("id1"); - Mockito.verify(mLockHandle).deleteLock("/" + "id1"); - } - - /* - * @Test public void testNonKeyRelatedPut() throws Exception { MusicDataStoreHandle.mDstoreHandle = - * Mockito.mock(MusicDataStore.class); Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut("qu1", - * "consistency")).thenReturn(true); Boolean result = MusicCore.nonKeyRelatedPut("qu1", - * "consistency"); assertTrue(result); Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut("qu1", - * "consistency"); } - */ - - @Test - public void testEventualPutPreparedQuery() throws MusicServiceException, MusicQueryException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(true); - ReturnType actualResult = musicZKCore.eventualPut(preparedQueryObject); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "eventual"); - } - - @Test - public void testEventualPutPreparedQuerywithResultFalse() - throws MusicServiceException, MusicQueryException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "eventual")).thenReturn(false); - ReturnType actualResult = musicZKCore.eventualPut(preparedQueryObject); - assertEquals(expectedResult.getResult(), actualResult.getResult()); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "eventual"); - //Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, MusicUtil.EVENTUAL); - } - - @Test - public void testCriticalPutPreparedQuerywithValidLockId() - throws Exception { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - Mockito.when(condition.testCondition()).thenReturn(true); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); - ReturnType returnType = musicZKCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, - "id1", condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(condition).testCondition(); - Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "critical"); - } - - @Test - public void testCriticalPutPreparedQuerywithInvalidLockId() throws MusicLockingException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - ReturnType returnType = musicZKCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, - "id1", condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); - } - - @Test - public void testCriticalPutPreparedQuerywithvalidLockIdandTestConditionFalse() throws Exception { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - Mockito.when(condition.testCondition()).thenReturn(false); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - ReturnType returnType = musicZKCore.criticalPut("ks1", "tn1", "pk1", preparedQueryObject, - "id1", condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(condition).testCondition(); - Mockito.verify(mLockHandle).getLockState("ks1" + "." + "tn1" + "." + "pk1"); - } - - @Test - public void testNonKeyRelatedPutPreparedQuery() throws Exception { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "consistency")).thenReturn(true); - ResultType result = musicZKCore.nonKeyRelatedPut(preparedQueryObject, "consistency"); - assertEquals(ResultType.SUCCESS, result); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "consistency"); - } - - @Test - public void testAtomicPutPreparedQuery() throws Exception { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - ReturnType expectedResult = new ReturnType(ResultType.SUCCESS, "Succes"); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - Mockito.when(condition.testCondition()).thenReturn(true); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executePut(preparedQueryObject, "critical")).thenReturn(true); - ReturnType returnType = - musicZKCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(condition).testCondition(); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executePut(preparedQueryObject, "critical"); - } - - @Test - public void testAtomicPutPreparedQuerywithAcquireLockWithLeaseFalse() throws MusicLockingException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - ReturnType expectedResult = new ReturnType(ResultType.FAILURE, "Failure"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ReturnType returnType = - musicZKCore.atomicPut("ks1", "tn1", "pk1", preparedQueryObject, condition); - assertEquals(expectedResult.getResult(), returnType.getResult()); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - } - - @Test - public void testAtomicGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - rs = Mockito.mock(ResultSet.class); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - Mockito.when(mLockHandle.getLockState("ks1.tn1.pk1")).thenReturn(musicLockState); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(true); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeQuorumConsistencyGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = musicZKCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); - assertNotNull(rs1); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()).getLockState("ks1.tn1.pk1"); - Mockito.verify(mLockHandle).isMyTurn("id1"); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeQuorumConsistencyGet(preparedQueryObject); - } - - @Test - public void testAtomicGetPreparedQuerywithAcquireLockWithLeaseFalse() - throws MusicServiceException, MusicLockingException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - rs = Mockito.mock(ResultSet.class); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ResultSet rs1 = musicZKCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); - assertNull(rs1); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testGetPreparedQuery() throws MusicServiceException, MusicQueryException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - rs = Mockito.mock(ResultSet.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeOneConsistencyGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = musicZKCore.get(preparedQueryObject); - assertNotNull(rs1); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeOneConsistencyGet(preparedQueryObject); - - } - - @Test - public void testcriticalGetPreparedQuery() throws MusicServiceException, MusicQueryException, MusicLockingException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1"); - rs = Mockito.mock(ResultSet.class); - session = Mockito.mock(Session.class); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeQuorumConsistencyGet(preparedQueryObject)).thenReturn(rs); - ResultSet rs1 = musicZKCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); - assertNotNull(rs1); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - Mockito.verify(MusicDataStoreHandle.mDstoreHandle).executeQuorumConsistencyGet(preparedQueryObject); - } - - @Test - public void testcriticalGetPreparedQuerywithInvalidLockId() throws MusicServiceException, MusicLockingException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id2"); - Mockito.when(mLockHandle.getLockState("ks1" + "." + "tn1" + "." + "pk1")) - .thenReturn(musicLockState); - ResultSet rs1 = musicZKCore.criticalGet("ks1", "tn1", "pk1", preparedQueryObject, "id1"); - assertNull(rs1); - Mockito.verify(mLockHandle, Mockito.atLeastOnce()) - .getLockState("ks1" + "." + "tn1" + "." + "pk1"); - } - - @Test - public void testAtomicGetPreparedQuerywithDeleteLockWithLeaseFalse() - throws MusicServiceException, MusicLockingException { - MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - preparedQueryObject = Mockito.mock(PreparedQueryObject.class); - rs = Mockito.mock(ResultSet.class); - Mockito.when(mLockHandle.createLockId("/" + "ks1.tn1.pk1")).thenReturn("id1"); - Mockito.when(mLockHandle.isMyTurn("id1")).thenReturn(false); - ResultSet rs1 = musicZKCore.atomicGet("ks1", "tn1", "pk1", preparedQueryObject); - assertNull(rs1); - Mockito.verify(mLockHandle).createLockId("/" + "ks1.tn1.pk1"); - Mockito.verify(mLockHandle).isMyTurn("id1"); - } - - @Test - public void testCondition() throws Exception { - //Condition conClass = Mockito.mock(Condition.class); - // boolean ret=true; - //Mockito.when(conClass.testCondition().thenReturn(ret); - Map<String, Object> conditionsx=null; - PreparedQueryObject selectQueryForTheRowx=null; - try { - Condition con = new Condition(conditionsx,selectQueryForTheRowx); - assertTrue(con.testCondition()); - } catch (Exception e) { - assertFalse(false); - } - } - //getLockingServiceHandl - - @Ignore - @Test(expected = MusicLockingException.class) //("Failed to aquire Locl store handle " + e)) - public void testgetLockingServiceHandle() throws Exception { - // MusicLockingService mLockHandlex = Mockito.mock(MusicLockingService.class); - //MusicLockingService mLockHandlea = mLockHandle; - //mLockHandle=null; - System.out.println("cjc 0 locking test n"); - // Mockito.when(MusicCore.getLockingServiceHandle()).thenReturn(mLockHandle); - //mLockHandle=null; - //System.out.println("cjc 0-1 locking test n"); - MusicLockingService mLockHandlea = mLockHandle; - mLockHandle=null; - - MusicLockingService mLockHandley=null; //MusicCore.getLockingServiceHandle(); - Mockito.when(MusicZKCore.getLockingServiceHandle()).thenReturn(mLockHandley); - System.out.println("cjc locking test n"); - mLockHandle=mLockHandlea; - assertTrue(true); - - } - //add mocking - @Ignore - @Test - public void testGetDSHandleIp() throws MusicServiceException, MusicQueryException { - // rs = Mockito.mock(ResultSet.class); - // session = Mockito.mock(Session.class); - //Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - //Mockito.when(MusicDataStoreHandle.mDstoreHandle.executeCriticalGet(preparedQueryObject)).thenReturn(rs); - - //MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - //MusicUtil mMusicUtil=Mockito.mock(MusicUtil.class); - System.out.println("cjc 0 getDsHandleIP"); - Mockito.when(MusicDataStoreHandle.getDSHandle("1.127.0.1")).thenReturn(MusicDataStoreHandle.mDstoreHandle); - System.out.println("cjc localhost"); - // Mockito.when(mMusicUtil.getMyCassaHost().equals("localhost")).thenReturn(null); - System.out.println("cjc 1 localhost IP"); - // MusicDataStoreHandle.mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost()); - // } else { - // MusicDataStoreHandle.mDstoreHandle = new MusicDataStore(); - // } - assertTrue(true); - } - - @Ignore - @Test - public void testPureZkCreate() { - try { - MusicZKCore.pureZkCreate("local"); - } catch(NullPointerException e) { - System.out.println("cjc zkcreate null pointwer exception:"+ e); - } - } - - @Ignore - @Test - public void testPureZkRead() { //String nodeName) { - byte[] data = MusicZKCore.pureZkRead("localhost"); - } - - //need fixing - @Ignore - @Test - public void testPureZkWrite() { //String nodeName, byte[] data) { - /* - long start = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Performing zookeeper write to " + nodeName); - try { - getLockingServiceHandle().getzkLockHandle().setNodeData(nodeName, data); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - logger.info(EELFLoggerDelegate.applicationLogger,"Performed zookeeper write to " + nodeName); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the actual zk put:" + (end - start) + " ms"); - */ - - // MusicDataStoreHandle.mDstoreHandle = Mockito.mock(MusicDataStore.class); - // rs = Mockito.mock(ResultSet.class); - // session = Mockito.mock(Session.class); - //Mockito.when(MusicDataStoreHandle.mDstoreHandle.getSession()).thenReturn(session); - - byte[] data= "Testing Zoo Keeper".getBytes(); - MusicZKCore.pureZkWrite("1.127.0.1", data); - // assertNotNull(rs1); - } - - @Test - public void testWhoseTurnIsIt() { //(String lockName) { - - /* - try { - return getLockingServiceHandle().whoseTurnIsIt("/" + lockName) + ""; - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - return null; - */ - - String lockName="xxx"; - if (musicZKCore.whoseTurnIsIt(lockName) != null) assertTrue(true); - - - - } - - @Test - public void testMarshallResults() { - Map<String, HashMap<String, Object>> ret=null; - //ResultSet results =null; - rs = Mockito.mock(ResultSet.class); - try { - ret= MusicDataStoreHandle.marshallResults(rs); - - } catch( Exception e ) { - - } - - if (ret != null) assertTrue(true); - } - -} diff --git a/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java b/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java deleted file mode 100644 index f7a22d64..00000000 --- a/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * ============LICENSE_START========================================== org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * Modifications Copyright (c) 2019 IBM - * =================================================================== - * 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; - -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.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.MusicLockState; -import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus; -import org.onap.music.lockingservice.zookeeper.MusicLockingService; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.service.impl.MusicZKCore; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; - -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class TestMusicCoreIntegration { - - static TestingServer zkServer; - static PreparedQueryObject testObject; - static String lockId = null; - static String lockName = "ks1.tb1.pk1"; - static MusicZKCore musicZkCore ; - - @BeforeClass - public static void init() throws Exception { - try { - MusicDataStoreHandle.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra(); - musicZkCore = MusicZKCore.getInstance(); - zkServer = new TestingServer(2181, new File("/tmp/zk")); - MusicZKCore.mLockHandle = new MusicLockingService(); - } catch (Exception e) { - e.printStackTrace(); - } - System.out.println("####Port:" + zkServer.getPort()); - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - System.out.println("After class"); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.dropKeyspace); - musicZkCore.eventualPut(testObject); - musicZkCore.deleteLock(lockName); - MusicDataStoreHandle.mDstoreHandle.close(); - MusicZKCore.mLockHandle.getzkLockHandle().close(); - MusicZKCore.mLockHandle.close(); - zkServer.stop(); - - } - - @Test - public void Test1_SetUp() throws MusicServiceException, MusicQueryException { - MusicZKCore.mLockHandle = new MusicLockingService(); - ResultType result = ResultType.FAILURE; - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createKeySpace); - musicZkCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createTableEmployees); - result = musicZkCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL); - assertEquals(ResultType.SUCCESS, result); - } - - @Test - public void Test2_atomicPut() throws Exception { - testObject = new PreparedQueryObject(); - testObject = CassandraCQL.setPreparedInsertQueryObject1(); - ReturnType returnType = musicZkCore.atomicPut("testCassa", "employees", "Mr Test one", - testObject, null); - assertEquals(ResultType.SUCCESS, returnType.getResult()); - } - - @Test - public void Test3_atomicPutWithDeleteLock() throws Exception { - testObject = new PreparedQueryObject(); - testObject = CassandraCQL.setPreparedInsertQueryObject2(); - ReturnType returnType = musicZkCore.atomicPutWithDeleteLock("testCassa", "employees", - "Mr Test two", testObject, null); - assertEquals(ResultType.SUCCESS, returnType.getResult()); - } - - @Test - public void Test4_atomicGetWithDeleteLock() throws Exception { - testObject = new PreparedQueryObject(); - testObject = CassandraCQL.setPreparedGetQuery(); - ResultSet resultSet = musicZkCore.atomicGetWithDeleteLock("testCassa", "employees", - "Mr Test one", testObject); - List<Row> rows = resultSet.all(); - assertEquals(1, rows.size()); - } - - @Test - public void Test5_atomicGet() throws Exception { - testObject = new PreparedQueryObject(); - testObject = CassandraCQL.setPreparedGetQuery(); - ResultSet resultSet = - musicZkCore.atomicGet("testCassa", "employees", "Mr Test two", testObject); - List<Row> rows = resultSet.all(); - assertEquals(1, rows.size()); - } - - @Test - public void Test6_createLockReference() throws Exception { - lockId = musicZkCore.createLockReference(lockName); - assertNotNull(lockId); - } - - @Test - public void Test7_acquireLockwithLease() throws Exception { - ReturnType lockLeaseStatus = musicZkCore.acquireLockWithLease(lockName, lockId, 1000); - assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult()); - } - - @Test - public void Test8_acquireLock() throws Exception { - ReturnType lockStatus = musicZkCore.acquireLock(lockName, lockId); - assertEquals(ResultType.SUCCESS, lockStatus.getResult()); - } - - @Test - public void Test9_release() throws Exception { - MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1"); - MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1"); - musicZkCore.whoseTurnIsIt(lockName); - MusicLockState mls = MusicZKCore.getMusicLockState(lockName); - boolean voluntaryRelease = true; - MusicLockState mls1 = musicZkCore.releaseLock(lockId, voluntaryRelease); - assertEquals(musicLockState.getLockStatus(), mls.getLockStatus()); - assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus()); - } - - @Test - public void Test10_create() { - MusicZKCore.pureZkCreate("/nodeName"); - } - - @Test - public void Test11_write() { - MusicZKCore.pureZkWrite("nodeName", "I'm Test".getBytes()); - } - - @Test - public void Test12_read() { - byte[] data = MusicZKCore.pureZkRead("nodeName"); - String data1 = new String(data); - assertEquals("I'm Test", data1); - } - @Test - public void Test13_ParameterizedConstructorCall() throws MusicServiceException, MusicQueryException { - MusicZKCore.mLockHandle = new MusicLockingService("localhost"); - ResultType result = ResultType.FAILURE; - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createKeySpace); - musicZkCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createTableEmployees); - result = musicZkCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL); - assertEquals(ResultType.SUCCESS, result); - } - -} diff --git a/src/test/java/org/onap/music/unittests/TestsUsingCassandra.java b/src/test/java/org/onap/music/unittests/TestsUsingCassandra.java index 35f3b326..d2e898e1 100644 --- a/src/test/java/org/onap/music/unittests/TestsUsingCassandra.java +++ b/src/test/java/org/onap/music/unittests/TestsUsingCassandra.java @@ -45,7 +45,6 @@ import com.sun.jersey.core.util.Base64; @RunWith(Suite.class) @SuiteClasses({ TstRestMusicDataAPI.class, TstRestMusicLockAPI.class, TstRestMusicAdminAPI.class, TstRestMusicConditionalAPI.class, TstCachingUtil.class}) -//@SuiteClasses({TstCachingUtil.class}) public class TestsUsingCassandra { static String appName = "TestApp"; @@ -61,6 +60,7 @@ public class TestsUsingCassandra { static String tableName = "employees"; static String xLatestVersion = "X-latestVersion"; static String onboardUUID = null; + static String aid = "abc66ccc-d857-4e90-b1e5-df98a3d40ce6"; @BeforeClass public static void beforeClass() throws Exception { @@ -92,7 +92,7 @@ public class TestsUsingCassandra { + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)"); testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); + keyspaceName)); testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt()))); diff --git a/src/test/java/org/onap/music/unittests/TstCachingUtil.java b/src/test/java/org/onap/music/unittests/TstCachingUtil.java index be2783b2..ea47b7c1 100644 --- a/src/test/java/org/onap/music/unittests/TstCachingUtil.java +++ b/src/test/java/org/onap/music/unittests/TstCachingUtil.java @@ -1,15 +1,16 @@ /* * ============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 + * =================================================================== 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. + * 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============================================= * ==================================================================== @@ -20,7 +21,15 @@ package org.onap.music.unittests; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.UUID; + +import javax.ws.rs.core.Response; + +import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; @@ -28,104 +37,121 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mindrot.jbcrypt.BCrypt; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.music.authentication.CachingUtil; +import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.main.CachingUtil; +import org.onap.music.datastore.jsonobjects.JsonInsert; +import org.onap.music.datastore.jsonobjects.JsonLeasedLock; +import org.onap.music.datastore.jsonobjects.JsonTable; +import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.lockingservice.cassandra.CassaLockStore; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.rest.RestMusicDataAPI; +import org.onap.music.rest.RestMusicLocksAPI; import com.datastax.driver.core.DataType; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; import com.sun.jersey.core.util.Base64; +@RunWith(MockitoJUnitRunner.class) public class TstCachingUtil { - static PreparedQueryObject testObject; - - static String appName = "TestApp"; - static String userId = "TestUser"; - static String password = "TestPassword"; - static String authData = userId + ":" + password; - static String wrongAuthData = userId + ":" + "pass"; - static String authorization = new String(Base64.encode(authData.getBytes())); - static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); - static boolean isAAF = false; - static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); - static String keyspaceName = "testcassa"; - static String tableName = "employees"; - static String xLatestVersion = "X-latestVersion"; - static String onboardUUID = null; - - @BeforeClass - public static void init() throws Exception { - System.out.println("Testing CachingUtil class"); - try { - createKeyspace(); - } catch (Exception e) { - e.printStackTrace(); - throw new Exception("Unable to initialize before CachingUtil test class. " + e.getMessage()); - } - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); - MusicCore.eventualPut(testObject); + RestMusicLocksAPI lock = new RestMusicLocksAPI(); + RestMusicDataAPI data = new RestMusicDataAPI(); + static PreparedQueryObject testObject; + + static String appName = TestsUsingCassandra.appName; + static String userId = TestsUsingCassandra.userId; + static String password = TestsUsingCassandra.password; + static String authData = TestsUsingCassandra.authData; + static String authorization = TestsUsingCassandra.authorization; + static boolean isAAF = TestsUsingCassandra.isAAF; + static UUID uuid = TestsUsingCassandra.uuid; + static String keyspaceName = TestsUsingCassandra.keyspaceName; + static String tableName = TestsUsingCassandra.tableName; + static String onboardUUID = null; + + @BeforeClass + public static void init() throws Exception { + System.out.println("Testing CachingUtil class"); + try { + + } catch (Exception e) { + e.printStackTrace(); + throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); + } + } + + @After + public void afterEachTest( ) throws MusicServiceException { + + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + testObject = new PreparedQueryObject(); + testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); + MusicCore.eventualPut(testObject); + } + + @Test + public void test_verifyOnboard() throws Exception { + System.out.println("Testing onboarding of app"); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); + Map<String, Object> authMap = CachingUtil.verifyOnboarding(appName, userId, password); + assertEquals(0, authMap.size()); } - - @Test - public void test_isAAF_False() throws Exception { - System.out.println("Testing isAAF, nonaaf app"); - assertEquals("false", CachingUtil.isAAFApplication(appName)); - } - + @Test - public void test_getUUidFromMusicCache() throws Exception { - System.out.println("Testing getUUID from MusicCache"); - assertEquals(uuid.toString(), CachingUtil.getUuidFromMusicCache(keyspaceName)); + public void test_authenticateAIDUser() throws Exception { + System.out.println("Testing authentication of AID user"); + createKeyspace(); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); + Map<String, Object> authMap = CachingUtil.authenticateAIDUser(appName, + userId, password, keyspaceName); + System.out.println("authMap is: " + authMap); + assertEquals(0, authMap.size()); } - + @Test - public void test_getAppName() throws Exception { + public void test_getAppName() throws MusicServiceException { System.out.println("Testing getAppName"); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); assertEquals(appName, CachingUtil.getAppName(keyspaceName)); } @Test - public void test_verifyOnboarding() throws Exception { - System.out.println("Testing verifyOnboarding"); - assertTrue(CachingUtil.verifyOnboarding(appName, userId, password).isEmpty()); - } - - @Test - public void test_verifyOnboardingFailure() throws Exception { - System.out.println("Testing verifyOnboarding with bad password"); - assertFalse(CachingUtil.verifyOnboarding(appName, userId, "pass").isEmpty()); - } - - - @Test - public void test_authenticateAIDUser() throws Exception { - System.out.println("Testing authenticateAIDUser"); - assertTrue(CachingUtil.authenticateAIDUser(appName, userId, password, keyspaceName).isEmpty()); + public void test_getUUIDFromCache() throws MusicServiceException { + System.out.println("Testing getUUID"); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); + assertEquals(uuid.toString(), CachingUtil.getUuidFromMusicCache(keyspaceName)); } - + @Test - public void test_authenticateAIDUserFailure() throws Exception { - System.out.println("Testing authenticateAIDUser bad password"); - assertFalse(CachingUtil.authenticateAIDUser(appName, userId, "pass", keyspaceName).isEmpty()); + public void test_isAAFApplcation() throws MusicServiceException { + System.out.println("Testing to see if cache gets correct isAAF info"); + CachingUtil cache = new CachingUtil(); + cache.initializeAafCache(); + assertEquals(isAAF, Boolean.valueOf(CachingUtil.isAAFApplication(appName))); } private static void createKeyspace() throws Exception { - // shouldn't really be doing this here, but create keyspace is currently turned off + //shouldn't really be doing this here, but create keyspace is currently turned off PreparedQueryObject query = new PreparedQueryObject(); query.appendQueryString(CassandraCQL.createKeySpace); MusicCore.eventualPut(query); - + boolean isAAF = false; String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt()); query = new PreparedQueryObject(); - query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); + query.appendQueryString( + "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); diff --git a/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java b/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java index 07a1b8e1..23b98505 100644 --- a/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java +++ b/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java @@ -1,15 +1,20 @@ /* - * ============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 + * ============LICENSE_START========================================== + * org.onap.music + * =================================================================== + * Copyright (c) 2019 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 + * 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. + * 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============================================= * ==================================================================== @@ -53,6 +58,7 @@ import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.rest.RestMusicDataAPI; +import org.onap.music.rest.RestMusicLocksAPI; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -62,14 +68,15 @@ import com.sun.jersey.core.util.MultivaluedMapImpl; @RunWith(MockitoJUnitRunner.class) public class TstRestMusicDataAPI { - RestMusicDataAPI data = new RestMusicDataAPI(); - static PreparedQueryObject testObject; + RestMusicDataAPI data = new RestMusicDataAPI(); + RestMusicLocksAPI lock = new RestMusicLocksAPI(); + static PreparedQueryObject testObject; - @Mock - HttpServletResponse http; + @Mock + HttpServletResponse http; - @Mock - UriInfo info; + @Mock + UriInfo info; static String appName = "TestApp"; static String userId = "TestUser"; @@ -84,6 +91,7 @@ public class TstRestMusicDataAPI { static String tableName = "employees"; static String xLatestVersion = "X-latestVersion"; static String onboardUUID = null; + static String aid = TestsUsingCassandra.aid; @BeforeClass public static void init() throws Exception { @@ -438,29 +446,6 @@ public class TstRestMusicDataAPI { assertEquals(400, response.getStatus()); } - @Ignore - @Test - public void test3_createTablePartitionKey() throws Exception { - System.out.println("Testing create table with a partitionKey"); - JsonTable jsonTable = new JsonTable(); - Map<String, String> consistencyInfo = new HashMap<>(); - Map<String, String> fields = new HashMap<>(); - fields.put("uuid", "text"); - fields.put("emp_name", "text"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "(emp_name)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableName); - jsonTable.setFields(fields); - jsonTable.setPartitionKey("emp_salary"); - Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonTable, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(200, response.getStatus()); - } // good clustering key, need to pass queryparameter @Test @@ -551,7 +536,7 @@ public class TstRestMusicDataAPI { @Test public void test4_insertIntoTableCriticalNoLockID() throws Exception { - System.out.println("Testing atomic insert into table without lockid"); + System.out.println("Testing critical insert into table without lockid"); createTable(); JsonInsert jsonInsert = new JsonInsert(); Map<String, String> consistencyInfo = new HashMap<>(); @@ -564,13 +549,35 @@ public class TstRestMusicDataAPI { jsonInsert.setKeyspaceName(keyspaceName); jsonInsert.setTableName(tableName); jsonInsert.setValues(values); - Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonInsert, keyspaceName, tableName); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, keyspaceName, tableName); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); assertEquals(400, response.getStatus()); } + + @Test + public void test4_insertIntoTableAtomic() throws Exception { + System.out.println("Testing atomic insert into table without lockid"); + createTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map<String, String> consistencyInfo = new HashMap<>(); + Map<String, Object> values = new HashMap<>(); + values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); + values.put("emp_name", "testname"); + values.put("emp_salary", 500); + consistencyInfo.put("type", "atomic"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } + @Test public void test4_insertIntoTableNoName() throws Exception { System.out.println("Testing insert into table w/o table name"); @@ -586,8 +593,8 @@ public class TstRestMusicDataAPI { jsonInsert.setKeyspaceName(keyspaceName); jsonInsert.setTableName(tableName); jsonInsert.setValues(values); - Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonInsert, "", ""); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, "", ""); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); assertEquals(400, response.getStatus()); @@ -818,6 +825,27 @@ public class TstRestMusicDataAPI { assertEquals(401, response.getStatus()); } + @Test + public void test6_selectAtomic() throws Exception { + System.out.println("Testing select atomic"); + createAndInsertIntoTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map<String, String> consistencyInfo = new HashMap<>(); + MultivaluedMap<String, String> row = new MultivaluedMapImpl(); + row.add("emp_name", "testname"); + consistencyInfo.put("type", "atomic"); + jsonInsert.setConsistencyInfo(consistencyInfo); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, keyspaceName, tableName,info); + HashMap<String,HashMap<String,Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity(); + HashMap<String, Object> result = map.get("result"); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + Map<String, String> row0 = (Map<String, String>) result.get("row 0"); + assertEquals("testname", row0.get("emp_name")); + assertEquals(BigInteger.valueOf(500), row0.get("emp_salary")); + } @Test public void test6_select() throws Exception { @@ -842,28 +870,6 @@ public class TstRestMusicDataAPI { } @Test - public void test6_selectCritical() throws Exception { - System.out.println("Testing select critical"); - createAndInsertIntoTable(); - JsonInsert jsonInsert = new JsonInsert(); - Map<String, String> consistencyInfo = new HashMap<>(); - MultivaluedMap<String, String> row = new MultivaluedMapImpl(); - row.add("emp_name", "testname"); - consistencyInfo.put("type", "atomic"); - jsonInsert.setConsistencyInfo(consistencyInfo); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.selectCritical("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonInsert, keyspaceName, tableName, info); - HashMap<String, HashMap<String, Object>> map = (HashMap<String, HashMap<String, Object>>) response.getEntity(); - HashMap<String, Object> result = map.get("result"); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - Map<String, String> row0 = (Map<String, String>) result.get("row 0"); - assertEquals("testname", row0.get("emp_name")); - assertEquals(BigInteger.valueOf(500), row0.get("emp_salary")); - } - - @Test public void test6_deleteFromTable() throws Exception { System.out.println("Testing delete from table"); createAndInsertIntoTable(); @@ -877,8 +883,6 @@ public class TstRestMusicDataAPI { Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonDelete, keyspaceName, tableName, info); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); } // Values diff --git a/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java b/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java index 3896c52f..a5113ea5 100644 --- a/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java +++ b/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java @@ -1,16 +1,20 @@ /* - * ============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 + * ============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 + * 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. + * 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============================================= * ==================================================================== diff --git a/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java b/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java index f48ecd40..ceda3f3a 100644 --- a/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java +++ b/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java @@ -44,11 +44,5 @@ public class MusicHealthCheckTest { musicHealthCheck.setCassandrHost("9042"); assertEquals("9042", musicHealthCheck.getCassandrHost()); } - - @Test - public void testZookeeperHost() - { - musicHealthCheck.setZookeeperHost("ZookeeperHost"); - assertEquals("ZookeeperHost", musicHealthCheck.getZookeeperHost()); - } + } |