diff options
Diffstat (limited to 'src/main/java/org')
33 files changed, 557 insertions, 3379 deletions
diff --git a/src/main/java/org/onap/music/MusicApplication.java b/src/main/java/org/onap/music/MusicApplication.java index 5251c178..e6fee58f 100755 --- a/src/main/java/org/onap/music/MusicApplication.java +++ b/src/main/java/org/onap/music/MusicApplication.java @@ -30,6 +30,7 @@ import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.onap.aaf.cadi.PropAccess; +import org.onap.music.authentication.CadiAuthFilter; import org.onap.music.main.MusicUtil; import org.onap.music.main.PropertiesLoader; import org.springframework.beans.factory.annotation.Autowired; @@ -97,7 +98,7 @@ public class MusicApplication extends SpringBootServletInitializer { propertyLoader.loadProperties(); if(MusicUtil.getIsCadi()) { PropAccess propAccess = propAccess(); - CadiAuthFilter cadiFilter = new CadiAuthFilter(true, propAccess); + CadiAuthFilter cadiFilter = new CadiAuthFilter(propAccess); return cadiFilter; } else return (ServletRequest request, ServletResponse response, FilterChain chain) -> { diff --git a/src/main/java/org/onap/music/main/CachingUtil.java b/src/main/java/org/onap/music/authentication/CachingUtil.java index db74aceb..5c379c6e 100755 --- a/src/main/java/org/onap/music/main/CachingUtil.java +++ b/src/main/java/org/onap/music/authentication/CachingUtil.java @@ -23,28 +23,28 @@ * ==================================================================== */ -package org.onap.music.main; +package org.onap.music.authentication; import java.util.Calendar; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.UUID; - import javax.ws.rs.core.MediaType; import org.apache.commons.codec.binary.Base64; import org.apache.commons.jcs.JCS; import org.apache.commons.jcs.access.CacheAccess; import org.mindrot.jbcrypt.BCrypt; +import org.onap.music.authentication.MusicAuthenticator.Operation; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.eelf.logging.EELFLoggerDelegate; import org.onap.music.eelf.logging.format.AppMessages; import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; import org.onap.music.exceptions.MusicServiceException; - +import org.onap.music.main.MusicCore; +import org.onap.music.main.MusicUtil; import com.datastax.driver.core.DataType; import com.datastax.driver.core.PreparedStatement; import com.datastax.driver.core.ResultSet; @@ -64,14 +64,14 @@ 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"); private static CacheAccess<String, String> adminUserCache = JCS.getInstance("adminUserCache"); public static CacheAccess<String, String> getAdminUserCache() { @@ -82,42 +82,9 @@ public class CachingUtil implements Runnable { adminUserCache.put(authorization,userId); } - - public static void updateStatementBank(String query,PreparedStatement statement) { - queryBank.put(query, statement); - } - - public static void resetStatementBank() { - queryBank.clear(); - } - - public static CacheAccess<String, PreparedStatement> getStatementBank() { - return queryBank; - } - 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 +112,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 +138,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 +181,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; } @@ -348,12 +315,7 @@ public class CachingUtil implements Runnable { return appName; } - public static String generateUUID() { - String uuid = UUID.randomUUID().toString(); - logger.info(EELFLoggerDelegate.applicationLogger,"New AID generated: "+uuid); - return uuid; - } - + @Deprecated public static Map<String, Object> validateRequest(String nameSpace, String userId, String password, String keyspace, String aid, String operation) { Map<String, Object> resultMap = new HashMap<>(); @@ -363,9 +325,19 @@ public class CachingUtil implements Runnable { } } return resultMap; - } + public static Map<String, Object> validateRequest(String nameSpace, String userId, + String password, String keyspace, String aid, Operation operation) { + Map<String, Object> resultMap = new HashMap<>(); + if (Operation.CREATE_KEYSPACE!=operation) { + if (nameSpace == null) { + resultMap.put("Exception", "Application namespace is mandatory."); + } + } + return resultMap; + } + public static Map<String, Object> verifyOnboarding(String ns, String userId, String password) { Map<String, Object> resultMap = new HashMap<>(); if (ns == null || userId == null || password == null) { @@ -470,16 +442,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/CadiAuthFilter.java b/src/main/java/org/onap/music/authentication/CadiAuthFilter.java index 12114662..56371c7d 100644 --- a/src/main/java/org/onap/music/CadiAuthFilter.java +++ b/src/main/java/org/onap/music/authentication/CadiAuthFilter.java @@ -20,58 +20,36 @@ * ==================================================================== */ -package org.onap.music; -import java.io.IOException; +package org.onap.music.authentication; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.onap.aaf.cadi.Access; +import com.att.eelf.configuration.EELFLogger; import org.onap.aaf.cadi.CadiWrap; import org.onap.aaf.cadi.Permission; import org.onap.aaf.cadi.PropAccess; import org.onap.aaf.cadi.aaf.AAFPermission; import org.onap.aaf.cadi.filter.CadiFilter; +import org.onap.music.eelf.logging.EELFLoggerDelegate; +import org.onap.music.main.MusicCore; public class CadiAuthFilter extends CadiFilter { - private static String include_url_endpoints =""; - private static String exclude_url_endpoints = ""; - public static final String AUTHORIZATION = "Authorization"; - - public CadiAuthFilter(boolean init, PropAccess access) throws ServletException { + private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CadiAuthFilter.class); + + public CadiAuthFilter(PropAccess access) throws ServletException { super(true, access); } public void init(FilterConfig filterConfig) throws ServletException { super.init(filterConfig); - include_url_endpoints = filterConfig.getInitParameter("include_url_endpoints"); - exclude_url_endpoints = filterConfig.getInitParameter("exclude_url_endpoints"); - } - - - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) - throws IOException, ServletException { - - super.doFilter(request, response, chain); - } - - - private String getUrl(ServletRequest request) { - String path = ""; - HttpServletRequest httpRequest = (HttpServletRequest) request; - path = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length() + 1); - return path; } @@ -83,7 +61,7 @@ public class CadiAuthFilter extends CadiFilter { boolean match = true; for (int i = 0; i < roleFunctionArray.length; i++) { if (match) { - if (!roleFunctionArray[i].equals("*")) { + if (!"*".equals(roleFunctionArray[i])) { Pattern p = Pattern.compile(Pattern.quote(path[i]), Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(roleFunctionArray[i]); match = m.matches(); @@ -98,7 +76,7 @@ public class CadiAuthFilter extends CadiFilter { } else { if (requestedPath.matches(includeUrl)) return true; - else if (includeUrl.equals("*")) + else if ("*".equals(includeUrl)) return true; } return false; @@ -112,8 +90,8 @@ public class CadiAuthFilter extends CadiFilter { for (Permission perm : perms) { AAFPermission aafPerm = (AAFPermission) perm; aafPermsList.add(aafPerm); - System.out.println(aafPerm.toString()); - System.out.println(aafPerm.getType()); + logger.info(aafPerm.toString()); + logger.info(aafPerm.getType()); } return aafPermsList; } @@ -127,17 +105,6 @@ public class CadiAuthFilter extends CadiFilter { aafPermsList.add(aafPerm); } return aafPermsList; - } - - /** - * - * @param request - * @return returns list of AAFPermission for the specific namespace - */ - public static List<AAFPermission> getNameSpacesAAFPermissions(String nameSpace, - List<AAFPermission> allPermissionsList) { - String type = nameSpace + ".url"; - allPermissionsList.removeIf(perm -> (!perm.getType().equals(type))); - return allPermissionsList; } + }
\ No newline at end of file diff --git a/src/main/java/org/onap/music/authentication/MusicAuthentication.java b/src/main/java/org/onap/music/authentication/MusicAAFAuthentication.java index 1d845b3c..2d0d4e59 100644 --- a/src/main/java/org/onap/music/authentication/MusicAuthentication.java +++ b/src/main/java/org/onap/music/authentication/MusicAAFAuthentication.java @@ -34,7 +34,7 @@ import org.onap.music.eelf.logging.format.AppMessages; import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.CachingUtil; +import org.onap.music.authentication.MusicAuthenticator.Operation; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; @@ -44,70 +44,99 @@ import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; -public class MusicAuthentication { +public class MusicAAFAuthentication implements MusicAuthenticator { - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicAuthentication.class); + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicAAFAuthentication.class); - /** - * authenticate user logic - * - * @param nameSpace - * @param userId - * @param password - * @param keyspace - * @param aid - * @param operation - * @return - * @throws Exception - */ - public static Map<String, Object> autheticateUser(String nameSpace, String userId, - String password, String keyspace, String aid, String operation) - throws Exception { + @Override + public boolean authenticateAdmin(String authorization) { + logger.info(EELFLoggerDelegate.applicationLogger, "MusicCore.authenticateAdmin: "); + String userId = MusicUtil.extractBasicAuthentication(authorization).get(MusicUtil.USERID); + if(MusicUtil.getIsCadi()) { + CachingUtil.updateAdminUserCache(authorization, userId); + return true; + } + CacheAccess<String, String> adminCache = CachingUtil.getAdminUserCache(); + if (authorization == null) { + logger.error(EELFLoggerDelegate.errorLogger, "Authorization cannot be empty..."); + return false; + } + if (adminCache.get(authorization) != null && adminCache.get(authorization).equals(userId)) { + logger.info(EELFLoggerDelegate.applicationLogger, "MusicCore.authenticateAdmin: Validated against admincache.. "); + return true; + } + else { + Client client = Client.create(); + String aafUrl = MusicUtil.getAafAdminUrl(); + if (aafUrl==null) { + logger.error(EELFLoggerDelegate.errorLogger, "Admin url is not set, please set in properties"); + return false; + } + + WebResource webResource = client.resource( + MusicUtil.getAafAdminUrl().concat(userId).concat("/").concat(MusicUtil.getAdminAafRole())); + + ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON) + .header("Authorization", authorization).get(ClientResponse.class); + if (response.getStatus() == 200) { + CachingUtil.updateAdminUserCache(authorization, userId); + return true; + } + } + return false; + } + + @Override + public boolean authenticateUser(String namespace, String authorization, String keyspace, + String aid, Operation operation) { logger.info(EELFLoggerDelegate.applicationLogger,"Inside User Authentication......."); + Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); + String userId = userCredentials.get(MusicUtil.USERID); + String password = userCredentials.get(MusicUtil.PASSWORD); + Map<String, Object> resultMap = new HashMap<>(); String uuid = null; if(! MusicUtil.getIsCadi()) { - resultMap = CachingUtil.validateRequest(nameSpace, userId, password, keyspace, aid, + resultMap = CachingUtil.validateRequest(namespace, userId, password, keyspace, aid, operation); if (!resultMap.isEmpty()) - return resultMap; + return false; String isAAFApp = null; try { - isAAFApp= CachingUtil.isAAFApplication(nameSpace); + isAAFApp= CachingUtil.isAAFApplication(namespace); } catch(MusicServiceException e) { logger.error(e.getErrorMessage(), e); resultMap.put("Exception", e.getMessage()); - return resultMap; + return false; } if(isAAFApp == null) { - resultMap.put("Exception", "Namespace: "+nameSpace+" doesn't exist. Please make sure ns(appName)" + resultMap.put("Exception", "Namespace: "+namespace+" doesn't exist. Please make sure ns(appName)" + " is correct and Application is onboarded."); - return resultMap; + return false; } boolean isAAF = Boolean.parseBoolean(isAAFApp); if (userId == null || password == null) { logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - logger.error(EELFLoggerDelegate.errorLogger,"One or more required headers is missing. userId: " + userId - + " :: password: " + password); + logger.error(EELFLoggerDelegate.errorLogger,"UserId/Password or more required headers is missing."); resultMap.put("Exception", "UserId and Password are mandatory for the operation " + operation); - return resultMap; + return false; } - if(!isAAF && !(operation.equals("createKeySpace"))) { - resultMap = CachingUtil.authenticateAIDUser(nameSpace, userId, password, keyspace); + if(!isAAF && !(operation==Operation.CREATE_KEYSPACE)) { + resultMap = CachingUtil.authenticateAIDUser(namespace, userId, password, keyspace); if (!resultMap.isEmpty()) - return resultMap; + return false; } - if (isAAF && nameSpace != null && userId != null && password != null) { + if (isAAF && namespace != null && userId != null && password != null) { boolean isValid = true; try { - isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace); + isValid = CachingUtil.authenticateAAFUser(namespace, userId, password, keyspace); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,"Error while aaf authentication for user:" + userId); logger.error(EELFLoggerDelegate.errorLogger,"Error: "+ e.getMessage()); logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - logger.error(EELFLoggerDelegate.errorLogger,"Got exception while AAF authentication for namespace " + nameSpace); + logger.error(EELFLoggerDelegate.errorLogger,"Got exception while AAF authentication for namespace " + namespace); resultMap.put("Exception", e.getMessage()); } if (!isValid) { @@ -115,7 +144,7 @@ public class MusicAuthentication { resultMap.put("Exception", "User not authenticated..."); } if (!resultMap.isEmpty()) - return resultMap; + return false; } } else { @@ -123,79 +152,32 @@ public class MusicAuthentication { String cachedKS = CachingUtil.getKSFromCadiCache(userId); if(cachedKS != null && !cachedKS.equals(keyspace)) { resultMap.put("Exception", "User not authenticated to access this keyspace..."); + return false; } } - if (operation.equals("createKeySpace")) { - logger.info(EELFLoggerDelegate.applicationLogger,"AID is not provided. Creating new UUID for keyspace."); - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "select uuid from admin.keyspace_master where application_name=? and username=? and keyspace_name=? allow filtering"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); - + if (operation==Operation.CREATE_KEYSPACE) { try { + logger.info(EELFLoggerDelegate.applicationLogger,"AID is not provided. Creating new UUID for keyspace."); + PreparedQueryObject pQuery = new PreparedQueryObject(); + pQuery.appendQueryString( + "select uuid from admin.keyspace_master where application_name=? and username=? and keyspace_name=? allow filtering"); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), namespace)); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), + MusicUtil.DEFAULTKEYSPACENAME)); Row rs = MusicCore.get(pQuery).one(); uuid = rs.getUUID("uuid").toString(); resultMap.put("uuid", "existing"); } catch (Exception e) { logger.error(EELFLoggerDelegate.applicationLogger,"No UUID found in DB. So creating new UUID."); - uuid = CachingUtil.generateUUID(); + uuid = MusicUtil.generateUUID(); resultMap.put("uuid", "new"); } resultMap.put("aid", uuid); CachingUtil.updateCadiCache(userId, keyspace); } - - return resultMap; - } - - - public static boolean authenticateAdmin(String id,String password) { - return (id.equals(MusicUtil.getAdminId()) && password.equals(MusicUtil.getAdminPass())); - } - - public static boolean authenticateAdmin(Map<String,String> adminCredentials) { - if(adminCredentials.containsKey("ERROR")) - return false; - String admin_id = adminCredentials.get(MusicUtil.USERID); - String admin_password = adminCredentials.get(MusicUtil.PASSWORD); - return (admin_id.equals(MusicUtil.getAdminId()) && admin_password.equals(MusicUtil.getAdminPass())); - } - - public static boolean authenticateAdmin(String authorization) throws Exception { - logger.info(EELFLoggerDelegate.applicationLogger, "MusicCore.authenticateAdmin: "+authorization); - String userId = MusicUtil.extractBasicAuthentication(authorization).get(MusicUtil.USERID); - if(MusicUtil.getIsCadi()) { - CachingUtil.updateAdminUserCache(authorization, userId); - return true; - } - CacheAccess<String, String> adminCache = CachingUtil.getAdminUserCache(); - if (authorization == null) { - logger.error(EELFLoggerDelegate.errorLogger, "Authorization cannot be empty..."+authorization); - throw new Exception("Authorization cannot be empty"); - } - if (adminCache.get(authorization) != null && adminCache.get(authorization).equals(userId)) { - logger.info(EELFLoggerDelegate.applicationLogger, "MusicCore.authenticateAdmin: Validated against admincache.. "+authorization); - return true; - } - else { - Client client = Client.create(); - WebResource webResource = client.resource( - MusicUtil.getAafAdminUrl().concat(userId).concat("/").concat(MusicUtil.getAdminAafRole())); - ; - - ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON) - .header("Authorization", authorization).get(ClientResponse.class); - if (response.getStatus() == 200) { - CachingUtil.updateAdminUserCache(authorization, userId); - return true; - } - } - return false; - + return true; } } diff --git a/src/main/java/org/onap/music/authentication/MusicAuthenticator.java b/src/main/java/org/onap/music/authentication/MusicAuthenticator.java new file mode 100644 index 00000000..78f76ab1 --- /dev/null +++ b/src/main/java/org/onap/music/authentication/MusicAuthenticator.java @@ -0,0 +1,62 @@ +/* + * ============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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================= + * ==================================================================== + */ + +package org.onap.music.authentication; + +public interface MusicAuthenticator { + public enum Operation{ + CREATE_KEYSPACE, + DROP_KEYSPACE, + CREATE_TABLE, + CREATE_INDEX, + INSERT_INTO_TABLE, + UPDATE_TABLE, + DELETE_FROM_TABLE, + DROP_TABLE, + SELECT_CRITICAL, + SELECT, + CREATE_LOCKREF, + ACQUIRE_LOCK, + CURRENT_LOCK, + DELETE_LOCK + } + + /** + * Authenticate a user account + * @param namespace - user's namespace + * @param authorization - basicAuth representation of username/password + * @param keyspace - keyspace user is trying to access + * @param aid - aid that identifies the user + * @param operation - operation that user is trying to do + * @return true if user has access + */ + public boolean authenticateUser(String namespace, String authorization, + String keyspace, String aid, Operation operation); + + /** + * Authenticate an administrative account + * @param authorization - basicAuth representation of username/password + * @return true if user has admin privileges + */ + public boolean authenticateAdmin(String authorization); + +} diff --git a/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java b/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java index ff77bfde..045abd98 100644 --- a/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java +++ b/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java @@ -254,6 +254,7 @@ public class MusicConditional { StringBuilder fieldsString = new StringBuilder("(vector_ts"+","); StringBuilder valueString = new StringBuilder("(" + "?" + ","); String vector = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); + String localPrimaryKey; queryObject.addValue(vector); if(casscadeColumn!=null && casscadeColumnValues!=null) { fieldsString.append(casscadeColumn).append(" ,"); @@ -267,8 +268,8 @@ public class MusicConditional { fieldsString.append(entry.getKey()); Object valueObj = entry.getValue(); if (primaryKeyName.equals(entry.getKey())) { - primaryKey = entry.getValue() + ""; - primaryKey = primaryKey.replace("'", "''"); + localPrimaryKey = entry.getValue() + ""; + localPrimaryKey = localPrimaryKey.replace("'", "''"); } DataType colType = null; try { 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 2a267387..20fd3150 100644 --- a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java +++ b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java @@ -52,8 +52,9 @@ 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.authentication.MusicAAFAuthentication; +import org.onap.music.authentication.MusicAuthenticator; +import org.onap.music.authentication.MusicAuthenticator.Operation; import org.onap.music.conductor.*; import com.datastax.driver.core.DataType; @@ -70,6 +71,8 @@ public class RestMusicConditionalAPI { private static final String XPATCHVERSION = "X-patchVersion"; private static final String NS = "ns"; private static final String VERSION = "v2"; + + private MusicAuthenticator authenticator = new MusicAAFAuthentication(); @POST @Path("/insert/keyspaces/{keyspace}/tables/{tablename}") @@ -82,10 +85,18 @@ public class RestMusicConditionalAPI { @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, @ApiParam(value = "Application namespace", required = true) @HeaderParam(NS) String ns, @ApiParam(value = "Authorization", required = true) @HeaderParam("Authorization") String authorization, - @ApiParam(value = "Major Version", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Major Version", required = true) @PathParam("tablename") String tablename, + @ApiParam(value = "Keyspace Name", required = true) @PathParam("keyspace") String keyspace, + @ApiParam(value = "Table Name", required = true) @PathParam("tablename") String tablename, JsonConditional jsonObj) throws Exception { ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); + + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.INSERT_INTO_TABLE)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); + } + String primaryKey = jsonObj.getPrimaryKey(); String primaryKeyValue = jsonObj.getPrimaryKeyValue(); String casscadeColumnName = jsonObj.getCasscadeColumnName(); @@ -101,28 +112,6 @@ public class RestMusicConditionalAPI { .setError(String.valueOf("One or more input values missing")).toMap()).build(); } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - - Map<String, Object> authMap = null; - try { - authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace, aid, "insertIntoTable"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, - ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED) - .entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, - ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity( - new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()) - .build(); - } Map<String, Object> valuesMap = new LinkedHashMap<>(); for (Map.Entry<String, Object> entry : tableValues.entrySet()) { @@ -157,7 +146,14 @@ public class RestMusicConditionalAPI { @ApiParam(value = "Major Version", required = true) @PathParam("tablename") String tablename, JsonConditional upObj) throws Exception { ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - + + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.INSERT_INTO_TABLE)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); + } + String primaryKey = upObj.getPrimaryKey(); String primaryKeyValue = upObj.getPrimaryKeyValue(); String casscadeColumnName = upObj.getCasscadeColumnName(); @@ -172,28 +168,6 @@ public class RestMusicConditionalAPI { .setError(String.valueOf("One or more input values missing")).toMap()).build(); } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - - Map<String, Object> authMap = null; - try { - authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace, aid, "updateTable"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, - ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED) - .entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, - ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity( - new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()) - .build(); - } String planId = casscadeColumnData.get("key").toString(); Map<String,String> casscadeColumnValueMap = (Map<String, String>) casscadeColumnData.get("value"); diff --git a/src/main/java/org/onap/music/datastore/MusicDataStore.java b/src/main/java/org/onap/music/datastore/MusicDataStore.java index bb8e537e..f58bd831 100755 --- a/src/main/java/org/onap/music/datastore/MusicDataStore.java +++ b/src/main/java/org/onap/music/datastore/MusicDataStore.java @@ -4,7 +4,7 @@ * =================================================================== * Copyright (c) 2017 AT&T Intellectual Property * =================================================================== - * Modifications Copyright (c) 2018 IBM + * Modifications Copyright (c) 2018-2019 IBM * =================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,13 +35,13 @@ import java.util.Iterator; import java.util.Map; import org.apache.commons.jcs.access.CacheAccess; +import org.onap.music.authentication.CachingUtil; import org.onap.music.eelf.logging.EELFLoggerDelegate; import org.onap.music.eelf.logging.format.AppMessages; import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicUtil; import com.codahale.metrics.JmxReporter; @@ -127,7 +127,6 @@ public class MusicDataStore { try { connectToCassaCluster(remoteIp); } catch (MusicServiceException e) { - logger.error("Exception", e); logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); } } @@ -149,10 +148,8 @@ public class MusicDataStore { } } } catch (SocketException e) { - logger.error("Exception", e); logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.CONNCECTIVITYERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); }catch(Exception e) { - logger.error("Exception", e); logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR); } return allPossibleIps; @@ -200,7 +197,6 @@ public class MusicDataStore { break; } catch (NoHostAvailableException e) { - logger.error("Exception", e); address = it.next(); logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.HOSTUNAVAILABLE, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); } @@ -257,7 +253,6 @@ public class MusicDataStore { try { session = cluster.connect(); } catch (Exception ex) { - logger.error("Exception", ex); logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.CASSANDRACONNECTIVITY, ErrorSeverity.ERROR, ErrorTypes.SERVICEUNAVAILABLE); throw new MusicServiceException( "Error while connecting to Cassandra cluster.. " + ex.getMessage()); @@ -451,12 +446,10 @@ public class MusicDataStore { } catch (AlreadyExistsException ae) { - logger.error("Exception", ae); logger.error(EELFLoggerDelegate.errorLogger, ae.getMessage(),AppMessages.SESSIONFAILED+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); throw new MusicServiceException(ae.getMessage()); } catch (Exception e) { - logger.error("Exception", e); logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.SESSIONFAILED+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR); throw new MusicQueryException("Executing Session Failure for Request = " + "[" + queryObject.getQuery() + "]" + " Reason = " + e.getMessage()); diff --git a/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java b/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java index bb8f3cbd..dc1c43a8 100644 --- a/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java +++ b/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java @@ -3,6 +3,9 @@ * org.onap.music * =================================================================== * Copyright (c) 2017 AT&T Intellectual Property + * Modifications Copyright (C) 2019 IBM. + * =================================================================== + * Modifications Copyright (c) 2019 Samsung * =================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,15 +31,16 @@ import java.util.Map; import org.onap.music.eelf.logging.EELFLoggerDelegate; import org.onap.music.exceptions.MusicServiceException; import org.onap.music.main.MusicUtil; -import org.onap.music.service.impl.MusicZKCore; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.TableMetadata; public class MusicDataStoreHandle { - - - + + private MusicDataStoreHandle(){ + throw new IllegalStateException("Utility class"); + } + public static MusicDataStore mDstoreHandle = null; private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStoreHandle.class); @@ -66,7 +70,7 @@ public class MusicDataStoreHandle { long start = System.currentTimeMillis(); if (mDstoreHandle == null) { // Quick Fix - Best to put this into every call to getDSHandle? - if (! MusicUtil.getMyCassaHost().equals("localhost") ) { + if (!"localhost".equals(MusicUtil.getMyCassaHost())) { mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost()); } else { mDstoreHandle = new MusicDataStore(); diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonDelete.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonDelete.java index c90dd005..b98a391d 100644 --- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonDelete.java +++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonDelete.java @@ -4,6 +4,8 @@ * =================================================================== * 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 @@ -22,7 +24,7 @@ package org.onap.music.datastore.jsonobjects; -import java.util.ArrayList; +import java.util.List; import java.util.Map; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -34,7 +36,7 @@ import io.swagger.annotations.ApiModelProperty; @JsonIgnoreProperties(ignoreUnknown = true) public class JsonDelete { - private ArrayList<String> columns = null; + private List<String> columns = null; private Map<String, String> consistencyInfo; private Map<String, Object> conditions; String ttl, timestamp; @@ -59,11 +61,11 @@ public class JsonDelete { } @ApiModelProperty(value = "Column values") - public ArrayList<String> getColumns() { + public List<String> getColumns() { return columns; } - public void setColumns(ArrayList<String> columns) { + public void setColumns(List<String> columns) { this.columns = columns; } diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java index cf691590..badcaebe 100644 --- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java +++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java @@ -38,7 +38,6 @@ public class JsonTable { private Map<String, String> fields; private Map<String, Object> properties; private String primaryKey; - private String sortingKey; private String partitionKey; private String clusteringKey; private String filteringKey; @@ -90,15 +89,6 @@ public class JsonTable { this.tableName = tableName; } - @ApiModelProperty(value = "Sorting Key") - public String getSortingKey() { - return sortingKey; - } - - public void setSortingKey(String sortingKey) { - this.sortingKey = sortingKey; - } - @ApiModelProperty(value = "Clustering Order", notes = "") public String getClusteringOrder() { return clusteringOrder; 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 3e962a02..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); @@ -83,20 +80,12 @@ public class MusicHealthCheck { } private Boolean getAdminKeySpace(String consistency) throws MusicServiceException { - - PreparedQueryObject pQuery = new PreparedQueryObject(); pQuery.appendQueryString("insert into admin.healthcheck (id) values (?)"); pQuery.addValue(UUID.randomUUID()); - ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency); - logger.info(rs.toString()); - if (rs != null) { - return Boolean.TRUE; - } else { - return Boolean.FALSE; - } - - + ResultType rs = MusicCore.nonKeyRelatedPut(pQuery, consistency); + logger.info(rs.toString()); + return null != rs; } private boolean createKeyspace() { @@ -106,30 +95,9 @@ 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); - } - if(rs != null && rs.getResult().toLowerCase().contains("success")) - return true; - else - return false; - } - - 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.error(EELFLoggerDelegate.errorLogger, e, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); } - - logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running"); - return "ACTIVE"; - + return rs != null && rs.getResult().toLowerCase().contains("success"); } public String getCassandrHost() { @@ -140,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/eelf/logging/format/AppMessages.java b/src/main/java/org/onap/music/eelf/logging/format/AppMessages.java index 40f69e36..5af3661c 100644 --- a/src/main/java/org/onap/music/eelf/logging/format/AppMessages.java +++ b/src/main/java/org/onap/music/eelf/logging/format/AppMessages.java @@ -55,10 +55,10 @@ public enum AppMessages { * [ERR402E] Ill formed queryObject for the request * [ERR403E] Error processing Prepared Query Object * - * 500-599 - Zookeepr/Locking Related + * 500-599 - Locking Related * [ERR500E] Invalid lock * [ERR501E] Locking Error has occured - * [ERR502E] Zookeeper error has occured + * [ERR502E] Deprecated * [ERR503E] Failed to aquire lock store handle * [ERR504E] Failed to create Lock Reference * [ERR505E] Lock does not exist @@ -68,7 +68,7 @@ public enum AppMessages { * [ERR509E] Lock not destroyed * [ERR510E] Lock not released * [ERR511E] Lock not deleted - * [ERR512E] Failed to get ZK Lock Handle + * [ERR512E] Deprecated * * * 600 - 699 - Music Service Errors @@ -113,7 +113,6 @@ public enum AppMessages { INVALIDLOCK("[ERR500E]"," Invalid lock or acquire failed",""," Lock is not valid to aquire"), LOCKINGERROR("[ERR501E]"," Locking Error has occured",""," Locking Error has occured"), - KEEPERERROR("[ERR502E]"," Zookeeper error has occured","","Please check zookeeper details"), LOCKHANDLE("[ERR503E]","Failed to aquire lock store handle",""," Failed to aquire lock store handle"), CREATELOCK("[ERR504E]","Failed to aquire lock store handle ","","Failed to aquire lock store handle "), LOCKSTATE("[ERR508E]"," Lock state not set",""," Lock state not set"), diff --git a/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java b/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java index 237b9417..4ed63575 100644 --- a/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java +++ b/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java @@ -3,6 +3,7 @@ * 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. @@ -115,14 +116,13 @@ public class CassaLockStore { long prevGuard = 0; long lockRef = 1; - if (latestGuardRow.size() > 0) { + if (!latestGuardRow.isEmpty()) { prevGuard = latestGuardRow.get(0).getLong(0); lockRef = prevGuard + 1; } long lockEpochMillis = System.currentTimeMillis(); -// System.out.println("guard(" + lockName + "): " + prevGuard + "->" + lockRef); logger.info(EELFLoggerDelegate.applicationLogger, "Created lock reference for " + keyspace + "." + lockTable + "." + lockName + ":" + lockRef); @@ -143,8 +143,8 @@ public class CassaLockStore { queryObject.addValue(String.valueOf(lockEpochMillis)); queryObject.addValue("0"); queryObject.appendQueryString(insQuery); - boolean pResult = dsHandle.executePut(queryObject, "critical"); - return "$"+keyspace+"."+table+"."+lockName+"$"+String.valueOf(lockRef); + dsHandle.executePut(queryObject, "critical"); + return "$"+keyspace+"."+table+"."+lockName+"$"+ lockRef; } /** @@ -233,7 +233,7 @@ public class CassaLockStore { public void deQueueLockRef(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{ table = table_prepend_name+table; PreparedQueryObject queryObject = new PreparedQueryObject(); - Long lockReferenceL = Long.parseLong(lockReference.substring(lockReference.lastIndexOf("$")+1)); + Long lockReferenceL = Long.parseLong(lockReference.substring(lockReference.lastIndexOf('$')+1)); String deleteQuery = "delete from "+keyspace+"."+table+" where key='"+key+"' AND lockReference ="+lockReferenceL+" IF EXISTS;"; queryObject.appendQueryString(deleteQuery); dsHandle.executePut(queryObject, "critical"); diff --git a/src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java b/src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java index a8e5ac48..5128e2cd 100644 --- a/src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java +++ b/src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java @@ -35,7 +35,7 @@ import org.onap.music.eelf.logging.format.AppMessages; import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; -// the state variable that will be stored in zookeeper, capturing the transitions of +// the state variable that will be stored in the locking service, capturing the transitions of public class MusicLockState implements Serializable { public enum LockStatus { UNLOCKED, BEING_LOCKED, LOCKED 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 e7cad139..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.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.CachingUtil; -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 38c4726f..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java +++ /dev/null @@ -1,369 +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 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 org.onap.music.service.impl.MusicZKCore; - -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); - }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); - }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 (KeeperException | InterruptedException e1) { - e1.printStackTrace(); - } - 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) { - e.printStackTrace(); - } - 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 ea2fa685..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.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 dd089683..b737377d 100755 --- a/src/main/java/org/onap/music/main/MusicUtil.java +++ b/src/main/java/org/onap/music/main/MusicUtil.java @@ -48,7 +48,6 @@ import java.util.concurrent.ConcurrentMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; - import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.eelf.logging.EELFLoggerDelegate; @@ -59,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; @@ -95,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"; @@ -121,7 +118,7 @@ public class MusicUtil { private static String musicRestIp = LOCALHOST; private static String musicPropertiesFilePath = PROPERTIES_FILE; private static long defaultLockLeasePeriod = 6000; - private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug", + private static final String[] propKeys = new String[] { "cassandra.host", "music.ip", "debug", "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip", "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url","admin.username","admin.password","aaf.admin.url", "music.namespace","admin.aaf.role","cassandra.port","lock.using"}; @@ -431,25 +428,6 @@ public class MusicUtil { } /** - * Get MyZkHost - Zookeeper Hostname - Default = localhost property file - * value - zookeeper.host - * - * @return - */ - public static String getMyZkHost() { - return myZkHost; - } - - /** - * Set MyZkHost - Zookeeper Hostname - * - * @param myZkHost - */ - public static void setMyZkHost(String myZkHost) { - MusicUtil.myZkHost = myZkHost; - } - - /** * Get MyCassHost - Cassandra Hostname - Default = localhost property file * value - cassandra.host * @@ -737,8 +715,6 @@ public class MusicUtil { } // get the property value and return it MusicUtil.setMyCassaHost(prop.getProperty("cassandra.host")); - String zkHosts = prop.getProperty("zookeeper.host"); - MusicUtil.setMyZkHost(zkHosts); MusicUtil.setCassName(prop.getProperty("cassandra.user")); MusicUtil.setCassPwd(prop.getProperty("cassandra.password")); MusicUtil.setCassandraPort(Integer.parseInt(prop.getProperty("cassandra.port"))); @@ -789,8 +765,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(); } @@ -815,7 +789,6 @@ public class MusicUtil { public static void setIsCadi(boolean isCadi) { - // TODO Auto-generated method stub MusicUtil.isCadi = isCadi; } @@ -859,5 +832,15 @@ public class MusicUtil { return MusicUtil.isCadi; } + + /** + * @return a random uuid + */ + public static String generateUUID() { + String uuid = UUID.randomUUID().toString(); + logger.info(EELFLoggerDelegate.applicationLogger,"New AID generated: "+uuid); + return uuid; + } + } diff --git a/src/main/java/org/onap/music/main/PropertiesListener.java b/src/main/java/org/onap/music/main/PropertiesListener.java deleted file mode 100755 index c5e8c37c..00000000 --- a/src/main/java/org/onap/music/main/PropertiesListener.java +++ /dev/null @@ -1,257 +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.main; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.Properties; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; - -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 com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; - -public class PropertiesListener { // implements ServletContextListener { - private Properties prop; - private static final String MUSIC_PROPERTIES="music.properties"; -/* private Properties prop; - ->>>>>>> c8db07f77a945bc22046ef50d773c3c3608b014a - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesListener.class); - - @Override - public void contextInitialized(ServletContextEvent servletContextEvent) { - prop = new Properties(); - Properties projectProp = new Properties(); - URL resource = getClass().getResource("/"); - String musicPropertiesFilePath = resource.getPath().replace("WEB-INF/classes/","WEB-INF/classes/project.properties"); - - // Open the file - try { - InputStream musicProps = null; - projectProp.load(new FileInputStream(musicPropertiesFilePath)); - if (projectProp.containsKey(MUSIC_PROPERTIES)) { - musicProps = new FileInputStream(projectProp.getProperty(MUSIC_PROPERTIES)); - } else { - musicProps = new FileInputStream(MusicUtil.getMusicPropertiesFilePath()); - } - prop.load(musicProps); - musicProps.close(); - prop.putAll(projectProp); - String[] propKeys = MusicUtil.getPropkeys(); - for (int k = 0; k < propKeys.length; k++) { - String key = propKeys[k]; - if (prop.containsKey(key) && prop.get(key) != null) { - logger.info(key + " : " + prop.getProperty(key)); - switch (key) { - case "zookeeper.host": - MusicUtil.setMyZkHost(prop.getProperty(key)); - break; - case "cassandra.host": - MusicUtil.setMyCassaHost(prop.getProperty(key)); - break; - case "music.ip": - MusicUtil.setDefaultMusicIp(prop.getProperty(key)); - break; - case "debug": - MusicUtil.setDebug(Boolean - .getBoolean(prop.getProperty(key).toLowerCase())); - break; - case "version": - MusicUtil.setVersion(prop.getProperty(key)); - break; - case "music.rest.ip": - MusicUtil.setMusicRestIp(prop.getProperty(key)); - break; - case MUSIC_PROPERTIES: - MusicUtil.setMusicPropertiesFilePath(prop.getProperty(key)); - break; - case "lock.lease.period": - MusicUtil.setDefaultLockLeasePeriod( - Long.parseLong(prop.getProperty(key))); - break; - case "my.id": - MusicUtil.setMyId(Integer.parseInt(prop.getProperty(key))); - break; - case "all.ids": - String[] ids = prop.getProperty(key).split(":"); - MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids))); - break; - case "public.ip": - MusicUtil.setPublicIp(prop.getProperty(key)); - break; - case "all.public.ips": - String[] ips = prop.getProperty(key).split(":"); - if (ips.length == 1) { - // Future use - } else if (ips.length > 1) { - MusicUtil.setAllPublicIps( - new ArrayList<String>(Arrays.asList(ips))); - } - break; - case "cassandra.user": - MusicUtil.setCassName(prop.getProperty(key)); - break; - case "cassandra.password": - MusicUtil.setCassPwd(prop.getProperty(key)); - break; - case "aaf.endpoint.url": - MusicUtil.setAafEndpointUrl(prop.getProperty(key)); - break; - case "admin.username": - MusicUtil.setAdminId(prop.getProperty(key)); - break; - case "admin.password": - MusicUtil.setAdminPass(prop.getProperty(key)); - break; - case "cassandra.port": - MusicUtil.setCassandraPort(Integer.parseInt(prop.getProperty(key))); - break; - case "aaf.admin.url": - MusicUtil.setAafAdminUrl(prop.getProperty(key)); - break; - case "music.namespace": - MusicUtil.setMusicNamespace(prop.getProperty(key)); - break; - case "admin.aaf.role": - MusicUtil.setAdminAafRole(prop.getProperty(key)); - break; - case "notify.interval": - MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty(key))); - break; - case "notify.timeout": - MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty(key))); - break; - case "lock.using": - MusicUtil.setLockUsing(prop.getProperty(key)); - break; - case "cacheobject.maxlife": - MusicUtil.setCacheObjectMaxLife(Integer.parseInt(prop.getProperty(key))); - CachingUtil.setCacheEternalProps(); - break; - default: - logger.error(EELFLoggerDelegate.errorLogger, - "No case found for " + key); - } - } - } - } catch (IOException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); - } - - logger.info(EELFLoggerDelegate.applicationLogger, - "Starting MUSIC " + MusicUtil.getVersion() + " on node with id " - + MusicUtil.getMyId() + " and public ip " - + MusicUtil.getPublicIp() + "..."); - logger.info(EELFLoggerDelegate.applicationLogger, - "List of all MUSIC ids:" + MusicUtil.getAllIds().toString()); - logger.info(EELFLoggerDelegate.applicationLogger, - "List of all MUSIC public ips:" + MusicUtil.getAllPublicIps().toString()); - - scheduleCronJobForZKCleanup(); - } - - @Override - public void contextDestroyed(ServletContextEvent servletContextEvent) { - prop = null; - } - - - private ScheduledExecutorService scheduler; - public void scheduleCronJobForZKCleanup() { - scheduler = Executors.newSingleThreadScheduledExecutor(); - scheduler.scheduleAtFixedRate(new CachingUtil(), 0, 24, TimeUnit.HOURS); - 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); - } catch (MusicServiceException e1) { - logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),ErrorSeverity.ERROR); - } - - //Zookeeper cleanup - scheduler.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - deleteLocksFromDB(); - } - } , 0, 24, TimeUnit.HOURS); - } - - - public void deleteLocksFromDB() { - PreparedQueryObject 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 = (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.getMessage(),ErrorSeverity.ERROR); - } - } -*/ -} diff --git a/src/main/java/org/onap/music/main/PropertiesLoader.java b/src/main/java/org/onap/music/main/PropertiesLoader.java index ee10db42..db04ff4b 100644 --- a/src/main/java/org/onap/music/main/PropertiesLoader.java +++ b/src/main/java/org/onap/music/main/PropertiesLoader.java @@ -37,9 +37,6 @@ import org.springframework.stereotype.Component; @Component public class PropertiesLoader implements InitializingBean { - @Value("${zookeeper.host}") - private String zookeeperHost; - @Value("${cassandra.host}") public String cassandraHost; @@ -168,8 +165,6 @@ public class PropertiesLoader implements InitializingBean { logger.info("#### Cassandra Host: " + MusicUtil.getMyCassaHost()); if(myId != null && !myId.equals("${my.id}")) MusicUtil.setMyId(Integer.parseInt(myId)); - if(zookeeperHost != null && !zookeeperHost.equals("${zookeeper.host}")) - MusicUtil.setMyZkHost(zookeeperHost); if(notifyInterval != null && !notifyInterval.equals("${notify.interval}")) MusicUtil.setNotifyInterval(Integer.parseInt(notifyInterval)); if(notifyTimeout != null && !notifyTimeout.equals("${notify.timeout}")) diff --git a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java index 289cab06..adcb6584 100755 --- a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java @@ -46,7 +46,9 @@ import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import org.mindrot.jbcrypt.BCrypt; -import org.onap.music.authentication.MusicAuthentication; +import org.onap.music.authentication.CachingUtil; +import org.onap.music.authentication.MusicAAFAuthentication; +import org.onap.music.authentication.MusicAuthenticator; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonOnboard; import org.onap.music.eelf.logging.EELFLoggerDelegate; @@ -54,8 +56,6 @@ import org.onap.music.eelf.logging.format.AppMessages; import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; import org.onap.music.exceptions.MusicServiceException; -//import org.onap.music.main.CacheAccess; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; @@ -80,6 +80,8 @@ public class RestMusicAdminAPI { EELFLoggerDelegate.getLogger(RestMusicAdminAPI.class); // Set to true in env like ONAP. Where access to creating and dropping keyspaces exist. private static final boolean KEYSPACE_ACTIVE = false; + + private MusicAuthenticator authenticator = new MusicAAFAuthentication(); /* * API to onboard an application with MUSIC. This is the mandatory first step. @@ -95,31 +97,28 @@ public class RestMusicAdminAPI { logger.info(EELFLoggerDelegate.errorLogger, "oboarding app"); ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); + if (!authenticator.authenticateAdmin(authorization)) { + logger.error(EELFLoggerDelegate.errorLogger, "Unauthorized: Please check admin username,password and try again", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.CRITICAL, + ErrorTypes.AUTHENTICATIONERROR); + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check admin username,password and try again").toMap()) + .build(); + } + Map<String, Object> resultMap = new HashMap<>(); String appName = jsonObj.getAppname(); String userId = jsonObj.getUserId(); String isAAF = jsonObj.getIsAAF(); String password = jsonObj.getPassword(); String keyspace_name = jsonObj.getKeyspace(); - try { - if (!MusicAuthentication.authenticateAdmin(authorization)) { - logger.error(EELFLoggerDelegate.errorLogger, "Unauthorized: Please check admin username,password and try again", AppMessages.MISSINGDATA, ErrorSeverity.CRITICAL, - ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response - .entity(new JsonResponse(ResultType.FAILURE) - .setError("Unauthorized: Please check admin username,password and try again").toMap()) - .build(); - } - } catch (Exception e) { - return response.entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } + if (appName == null || userId == null || isAAF == null || password == null) { logger.error(EELFLoggerDelegate.errorLogger, "Unauthorized: Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing.", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); resultMap.put("Exception", "Unauthorized: Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing."); - return response.status(Status.UNAUTHORIZED).entity(resultMap).build(); + return response.status(Status.BAD_REQUEST).entity(resultMap).build(); } PreparedQueryObject pQuery = new PreparedQueryObject(); @@ -135,7 +134,7 @@ public class RestMusicAdminAPI { * " has already been onboarded. Please contact admin.").toMap()).build(); } */ //pQuery = new PreparedQueryObject(); - String uuid = CachingUtil.generateUUID(); + String uuid = MusicUtil.generateUUID(); pQuery.appendQueryString( "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)"); @@ -169,24 +168,20 @@ public class RestMusicAdminAPI { public Response getOnboardedInfoSearch(JsonOnboard jsonObj, @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization) throws Exception { ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); + + if (!authenticator.authenticateAdmin(authorization)) { + logger.error(EELFLoggerDelegate.errorLogger, "Unauthorized: Please check admin username,password and try again", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.CRITICAL, + ErrorTypes.AUTHENTICATIONERROR); + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check admin username,password and try again").toMap()) + .build(); + } + Map<String, Object> resultMap = new HashMap<>(); String appName = jsonObj.getAppname(); String uuid = jsonObj.getAid(); String isAAF = jsonObj.getIsAAF(); - - try { - if (!MusicAuthentication.authenticateAdmin(authorization)) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, ErrorSeverity.CRITICAL, - ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response - .entity(new JsonResponse(ResultType.FAILURE) - .setError("Unauthorized: Please check admin username,password and try again").toMap()) - .build(); - } - } catch (Exception e) { - return response.entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } if (appName == null && uuid == null && isAAF == null) { logger.error(EELFLoggerDelegate.errorLogger, "Unauthorized: Please check the request parameters. Enter atleast one of the following parameters: appName(ns), aid, isAAF.", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); @@ -246,24 +241,21 @@ public class RestMusicAdminAPI { public Response deleteOnboardApp(JsonOnboard jsonObj, @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization) throws Exception { ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); + + if (!authenticator.authenticateAdmin(authorization)) { + logger.error(EELFLoggerDelegate.errorLogger, "Unauthorized: Please check admin username,password and try again", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.CRITICAL, + ErrorTypes.AUTHENTICATIONERROR); + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check admin username,password and try again").toMap()) + .build(); + } + Map<String, Object> resultMap = new HashMap<>(); String appName = jsonObj.getAppname(); String aid = jsonObj.getAid(); PreparedQueryObject pQuery = new PreparedQueryObject(); - String consistency = MusicUtil.EVENTUAL;; - try { - if (!MusicAuthentication.authenticateAdmin(authorization)) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, ErrorSeverity.CRITICAL, - ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response - .entity(new JsonResponse(ResultType.FAILURE) - .setError("Unauthorized: Please check admin username,password and try again").toMap()) - .build(); - } - } catch (Exception e) { - return response.entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } + String consistency = MusicUtil.EVENTUAL; if (appName == null && aid == null) { logger.error(EELFLoggerDelegate.errorLogger, "Please make sure either appName(ns) or Aid is present", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); @@ -356,6 +348,15 @@ public class RestMusicAdminAPI { public Response updateOnboardApp(JsonOnboard jsonObj, @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization) throws Exception { ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); + if (!authenticator.authenticateAdmin(authorization)) { + logger.error(EELFLoggerDelegate.errorLogger, "Unauthorized: Please check admin username,password and try again", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.CRITICAL, + ErrorTypes.AUTHENTICATIONERROR); + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check admin username,password and try again").toMap()) + .build(); + } + Map<String, Object> resultMap = new HashMap<>(); String aid = jsonObj.getAid(); String appName = jsonObj.getAppname(); @@ -364,19 +365,7 @@ public class RestMusicAdminAPI { String password = jsonObj.getPassword(); String consistency = "eventual"; PreparedQueryObject pQuery; - try { - if (!MusicAuthentication.authenticateAdmin(authorization)) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, ErrorSeverity.CRITICAL, - ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response - .entity(new JsonResponse(ResultType.FAILURE) - .setError("Unauthorized: Please check admin username,password and try again").toMap()) - .build(); - } - } catch (Exception e) { - return response.entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } + if (aid == null) { resultMap.put("Exception", "Please make sure Aid is present"); logger.error(EELFLoggerDelegate.errorLogger, "Please make sure Aid is present", AppMessages.MISSINGDATA, @@ -456,6 +445,12 @@ public class RestMusicAdminAPI { List<Application> appList = new ArrayList<>(); ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); + if (!authenticator.authenticateAdmin(authorization)) { + logger.error(EELFLoggerDelegate.errorLogger, "Unauthorized: Please check admin username,password and try again", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.CRITICAL, + ErrorTypes.AUTHENTICATIONERROR); + return appList; + } + PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString("SELECT * FROM " + "admin" + "." + "keyspace_master" + ";"); ResultSet results = MusicCore.get(queryObject); @@ -482,6 +477,11 @@ public class RestMusicAdminAPI { @ApiParam(value = "uuid", required = true) @HeaderParam("uuid") String uuid) throws Exception { ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); + if (!authenticator.authenticateAdmin(authorization)) { + logger.error(EELFLoggerDelegate.errorLogger, "Unauthorized: Please check admin username,password and try again", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.CRITICAL, + ErrorTypes.AUTHENTICATIONERROR); + return false; + } PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString("delete from admin.keyspace_master where uuid=?"); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),uuid)); @@ -493,15 +493,4 @@ public class RestMusicAdminAPI { } return true; } - - - @GET - @Path("/login") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - public boolean login(@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization) throws Exception { - - boolean result = MusicAuthentication.authenticateAdmin(authorization); - return result; - } } diff --git a/src/main/java/org/onap/music/rest/RestMusicBmAPI.java b/src/main/java/org/onap/music/rest/RestMusicBmAPI.java deleted file mode 100644 index f2946c14..00000000 --- a/src/main/java/org/onap/music/rest/RestMusicBmAPI.java +++ /dev/null @@ -1,319 +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.rest; - -import java.util.List; -import java.util.Map; -import java.util.UUID; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.UriInfo; -import org.onap.music.datastore.jsonobjects.JsonInsert; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.service.impl.MusicZKCore; -import org.onap.music.datastore.MusicDataStoreHandle; -import org.onap.music.datastore.PreparedQueryObject; -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.TableMetadata; -import io.swagger.annotations.Api; - -/* - * These are functions created purely for benchmarking purposes. Commented out Swagger - This should - * be undocumented API - * - */ -@Path("/v{version: [0-9]+}/benchmarks/") -@Api(value = "Benchmark API", hidden = true) -public class RestMusicBmAPI { - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicBmAPI.class); - public static final String UPDATE_CONST=" update-"; - // pure zk calls... - - /** - * - * @param nodeName - * @throws Exception - */ - @POST - @Path("/purezk/{name}") - @Consumes(MediaType.APPLICATION_JSON) - public void pureZkCreate(@PathParam("name") String nodeName) throws Exception { - MusicZKCore.pureZkCreate("/" + nodeName); - } - - - /** - * - * @param insObj - * @param nodeName - * @throws Exception - */ - @PUT - @Path("/purezk/{name}") - @Consumes(MediaType.APPLICATION_JSON) - public void pureZkUpdate(JsonInsert insObj, @PathParam("name") String nodeName) - throws Exception { - logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zk normal update-------------------------"); - long start = System.currentTimeMillis(); - MusicZKCore.pureZkWrite(nodeName, insObj.serialize()); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk normal update:" + (end - start) + " ms"); - } - - /** - * - * @param nodeName - * @return - * @throws Exception - */ - @GET - @Path("/purezk/{name}") - @Consumes(MediaType.TEXT_PLAIN) - public byte[] pureZkGet(@PathParam("name") String nodeName) throws Exception { - return MusicZKCore.pureZkRead(nodeName); - } - - /** - * - * @param insObj - * @param lockName - * @param nodeName - * @throws Exception - */ - @PUT - @Path("/purezk/atomic/{lockname}/{name}") - @Consumes(MediaType.APPLICATION_JSON) - public void pureZkAtomicPut(JsonInsert updateObj, @PathParam("lockname") String lockname, - @PathParam("name") String nodeName) throws Exception { - long startTime = System.currentTimeMillis(); - String operationId = UUID.randomUUID().toString();// just for debugging purposes. - String consistency = updateObj.getConsistencyInfo().get("type"); - - logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zookeeper " + consistency + UPDATE_CONST + operationId - + "-------------------------"); - - byte[] data = updateObj.serialize(); - long jsonParseCompletionTime = System.currentTimeMillis(); - - String lockId = MusicCore.createLockReference(lockname); - - long lockCreationTime = System.currentTimeMillis(); - - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockname, lockId, leasePeriod); - long lockAcqTime = System.currentTimeMillis(); - long zkPutTime = 0; - long lockReleaseTime = 0; - - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - MusicZKCore.pureZkWrite(lockname, data); - zkPutTime = System.currentTimeMillis(); - boolean voluntaryRelease = true; -/*<<<<<<< HEAD - if (("atomic").equals(consistency)) - MusicCore.releaseLock(lockId, voluntaryRelease); - else if (("atomic_delete_lock").equals(consistency)) - MusicCore.deleteLock(lockname); -=======*/ - if (consistency.equals("atomic")) - MusicCore.releaseLock(lockId, voluntaryRelease); - else if (consistency.equals("atomic_delete_lock")) - MusicCore.deleteLock(lockname); - lockReleaseTime = System.currentTimeMillis(); - } else { - MusicCore.destroyLockRef(lockId); - } - - long actualUpdateCompletionTime = System.currentTimeMillis(); - - - long endTime = System.currentTimeMillis(); - - String lockingInfo = "|lock creation time:" + (lockCreationTime - jsonParseCompletionTime) - + "|lock accquire time:" + (lockAcqTime - lockCreationTime) - + "|zk put time:" + (zkPutTime - lockAcqTime); - - if ("atomic".equals(consistency)) - lockingInfo = lockingInfo + "|lock release time:" + (lockReleaseTime - zkPutTime) + "|"; - else if ("atomic_delete_lock".equals(consistency)) - lockingInfo = lockingInfo + "|lock delete time:" + (lockReleaseTime - zkPutTime) + "|"; - - String timingString = "Time taken in ms for Zookeeper " + consistency + UPDATE_CONST - + operationId + ":" + "|total operation time:" + (endTime - startTime) - + "|json parsing time:" + (jsonParseCompletionTime - startTime) - + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime) - + lockingInfo; - - logger.info(EELFLoggerDelegate.applicationLogger,timingString); - } - - /** - * - * @param insObj - * @param lockName - * @param nodeName - * @throws Exception - */ - @GET - @Path("/purezk/atomic/{lockname}/{name}") - @Consumes(MediaType.APPLICATION_JSON) - public void pureZkAtomicGet(JsonInsert insObj, @PathParam("lockname") String lockName, - @PathParam("name") String nodeName) throws Exception { - logger.info("--------------Zk atomic read-------------------------"); - long start = System.currentTimeMillis(); - String lockId = MusicCore.createLockReference(lockName); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockName, lockId, leasePeriod); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info("acquired lock with id " + lockId); - MusicZKCore.pureZkRead(nodeName); - boolean voluntaryRelease = true; - MusicCore.releaseLock(lockId, voluntaryRelease); - } else { - MusicCore.destroyLockRef(lockId); - } - - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk atomic read:" + (end - start) + " ms"); - } - - /** - * - * doing an update directly to cassa but through the rest api - * - * @param insObj - * @param keyspace - * @param tablename - * @param info - * @return - * @throws Exception - */ - @PUT - @Path("/cassa/keyspaces/{keyspace}/tables/{tablename}/rows") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public boolean updateTableCassa(JsonInsert insObj, @PathParam("keyspace") String keyspace, - @PathParam("tablename") String tablename, @Context UriInfo info) - throws Exception { - long startTime = System.currentTimeMillis(); - String operationId = UUID.randomUUID().toString();// just for debugging purposes. - String consistency = insObj.getConsistencyInfo().get("type"); - logger.info(EELFLoggerDelegate.applicationLogger,"--------------Cassandra " + consistency + UPDATE_CONST + operationId - + "-------------------------"); - PreparedQueryObject queryObject = new PreparedQueryObject(); - Map<String, Object> valuesMap = insObj.getValues(); - TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename); - String vectorTs = "'" + Thread.currentThread().getId() + System.currentTimeMillis() + "'"; - String fieldValueString = "vector_ts= ? ,"; - queryObject.addValue(vectorTs); - - int counter = 0; - for (Map.Entry<String, Object> entry : valuesMap.entrySet()) { - Object valueObj = entry.getValue(); - DataType colType = tableInfo.getColumn(entry.getKey()).getType(); - Object valueString = MusicUtil.convertToActualDataType(colType, valueObj); - fieldValueString = fieldValueString + entry.getKey() + "= ?"; - queryObject.addValue(valueString); - if (counter != valuesMap.size() - 1) - fieldValueString = fieldValueString + ","; - counter = counter + 1; - } - - // get the row specifier - String rowSpec = ""; - counter = 0; - queryObject.appendQueryString("UPDATE " + keyspace + "." + tablename + " "); - MultivaluedMap<String, String> rowParams = info.getQueryParameters(); - String primaryKey = ""; - for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()) { - String keyName = entry.getKey(); - List<String> valueList = entry.getValue(); - String indValue = valueList.get(0); - DataType colType = tableInfo.getColumn(entry.getKey()).getType(); - Object formattedValue = MusicUtil.convertToActualDataType(colType, indValue); - primaryKey = primaryKey + indValue; - rowSpec = rowSpec + keyName + "= ? "; - queryObject.addValue(formattedValue); - if (counter != rowParams.size() - 1) - rowSpec = rowSpec + " AND "; - counter = counter + 1; - } - - - String ttl = insObj.getTtl(); - String timestamp = insObj.getTimestamp(); - - if ((ttl != null) && (timestamp != null)) { - - logger.info(EELFLoggerDelegate.applicationLogger,"both there"); - queryObject.appendQueryString(" USING TTL ? AND TIMESTAMP ?"); - queryObject.addValue(Integer.parseInt(ttl)); - queryObject.addValue(Long.parseLong(timestamp)); - } - - if ((ttl != null) && (timestamp == null)) { - logger.info(EELFLoggerDelegate.applicationLogger,"ONLY TTL there"); - queryObject.appendQueryString(" USING TTL ?"); - queryObject.addValue(Integer.parseInt(ttl)); - } - - if ((ttl == null) && (timestamp != null)) { - logger.info(EELFLoggerDelegate.applicationLogger,"ONLY timestamp there"); - queryObject.appendQueryString(" USING TIMESTAMP ?"); - queryObject.addValue(Long.parseLong(timestamp)); - } - queryObject.appendQueryString(" SET " + fieldValueString + " WHERE " + rowSpec + ";"); - - long jsonParseCompletionTime = System.currentTimeMillis(); - - boolean operationResult = true; - MusicDataStoreHandle.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type")); - - long actualUpdateCompletionTime = System.currentTimeMillis(); - - long endTime = System.currentTimeMillis(); - - String timingString = "Time taken in ms for Cassandra " + consistency + UPDATE_CONST - + operationId + ":" + "|total operation time:" + (endTime - startTime) - + "|json parsing time:" + (jsonParseCompletionTime - startTime) - + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime) - + "|"; - logger.info(EELFLoggerDelegate.applicationLogger,timingString); - - return operationResult; - } -} diff --git a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java index 0010ed1a..dfcf0bdb 100755 --- a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java @@ -25,12 +25,10 @@ package org.onap.music.rest; import java.nio.ByteBuffer; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; -import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -41,7 +39,6 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; -import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; @@ -51,7 +48,10 @@ import javax.ws.rs.core.UriInfo; import org.apache.commons.lang3.StringUtils; import org.mindrot.jbcrypt.BCrypt; -import org.onap.music.authentication.MusicAuthentication; +import org.onap.music.authentication.CachingUtil; +import org.onap.music.authentication.MusicAAFAuthentication; +import org.onap.music.authentication.MusicAuthenticator; +import org.onap.music.authentication.MusicAuthenticator.Operation; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonDelete; import org.onap.music.datastore.jsonobjects.JsonInsert; @@ -65,7 +65,6 @@ import org.onap.music.eelf.logging.format.AppMessages; import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.datastore.Condition; import org.onap.music.datastore.MusicDataStoreHandle; @@ -73,7 +72,6 @@ import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; import org.onap.music.response.jsonobjects.JsonResponse; -import org.onap.music.service.impl.MusicZKCore; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; @@ -119,6 +117,7 @@ public class RestMusicDataAPI { private static final String XPATCHVERSION = "X-patchVersion"; private static final String NS = "ns"; private static final String VERSION = "v2"; + private MusicAuthenticator authenticator = new MusicAAFAuthentication(); // Set to true in env like ONAP. Where access to creating and dropping keyspaces exist. private static final boolean KEYSPACE_ACTIVE = false; @@ -151,7 +150,6 @@ public class RestMusicDataAPI { @ApiOperation(value = "Create Keyspace", response = String.class,hidden = true) @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - //public Map<String, Object> createKeySpace( public Response createKeySpace( @ApiParam(value = "Major Version",required = true) @PathParam("version") String version, @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, @@ -176,34 +174,21 @@ public class RestMusicDataAPI { response.status(Status.UNAUTHORIZED); return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); } - if(kspObject == null || kspObject.getReplicationInfo() == null) { - response.status(Status.BAD_REQUEST); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(ResultType.BODYMISSING.getResult()).toMap()).build(); - } - - try { - authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid, - "createKeySpace"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - response.status(Status.BAD_REQUEST); - return response.entity(new JsonResponse(ResultType.FAILURE).setError("Unable to authenticate.").toMap()).build(); - } - String newAid = null; - if (!authMap.isEmpty()) { - if (authMap.containsKey("aid")) { - newAid = (String) authMap.get("aid"); - } else { - logger.error(EELFLoggerDelegate.errorLogger,String.valueOf(authMap.get("Exception")), AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - } + if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.CREATE_KEYSPACE)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); + } String consistency = MusicUtil.EVENTUAL;// for now this needs only // eventual consistency + if(kspObject == null || kspObject.getReplicationInfo() == null) { + response.status(Status.BAD_REQUEST); + return response.entity(new JsonResponse(ResultType.FAILURE).setError(ResultType.BODYMISSING.getResult()).toMap()).build(); + } PreparedQueryObject queryObject = new PreparedQueryObject(); if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL) && kspObject.getConsistencyInfo().get("consistency") != null) { if(MusicUtil.isValidConsistency(kspObject.getConsistencyInfo().get("consistency"))) @@ -262,7 +247,7 @@ public class RestMusicDataAPI { queryObject.appendQueryString( "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), newAid)); + queryObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), aid)); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName)); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns)); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); @@ -314,17 +299,12 @@ public class RestMusicDataAPI { EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) "); logger.info(EELFLoggerDelegate.applicationLogger,"In Drop Keyspace " + keyspaceName); if ( KEYSPACE_ACTIVE ) { - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = MusicAuthentication.autheticateUser(ns, userId, password,keyspaceName, aid, "dropKeySpace"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,authMap.get("Exception").toString(), AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } + if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.DROP_KEYSPACE)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); + } String consistency = MusicUtil.EVENTUAL;// for now this needs only // eventual @@ -413,17 +393,13 @@ public class RestMusicDataAPI { .toMap()).build(); } EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspace+" ) "); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace, - aid, "createTable"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,authMap.get("Exception").toString(), AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.CREATE_TABLE)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); + } + String consistency = MusicUtil.EVENTUAL; // for now this needs only eventual consistency @@ -450,90 +426,90 @@ public class RestMusicDataAPI { else fieldsString.append("," + entry.getKey() + " " + entry.getValue() + ""); } - if (counter != (fields.size() - 1) ) { - - counter = counter + 1; - } else { - - if((primaryKey != null) && (partitionKey == null)) { - primaryKey = primaryKey.trim(); - int count1 = StringUtils.countMatches(primaryKey, ')'); - int count2 = StringUtils.countMatches(primaryKey, '('); - if (count1 != count2) { + if (counter != (fields.size() - 1) ) { + + counter = counter + 1; + } else { + + if((primaryKey != null) && (partitionKey == null)) { + primaryKey = primaryKey.trim(); + int count1 = StringUtils.countMatches(primaryKey, ')'); + int count2 = StringUtils.countMatches(primaryKey, '('); + if (count1 != count2) { return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) - .setError("Create Table Error: primary key '(' and ')' do not match, primary key=" + primaryKey) - .toMap()).build(); - } + .setError("Create Table Error: primary key '(' and ')' do not match, primary key=" + primaryKey) + .toMap()).build(); + } - if ( primaryKey.indexOf('(') == -1 || ( count2 == 1 && (primaryKey.lastIndexOf(')') +1) == primaryKey.length() ) ) - { - if (primaryKey.contains(",") ) { + if ( primaryKey.indexOf('(') == -1 || ( count2 == 1 && (primaryKey.lastIndexOf(')') +1) == primaryKey.length() ) ) + { + if (primaryKey.contains(",") ) { partitionKey= primaryKey.substring(0,primaryKey.indexOf(',')); - partitionKey=partitionKey.replaceAll("[\\(]+",""); - clusterKey=primaryKey.substring(primaryKey.indexOf(',')+1); // make sure index - clusterKey=clusterKey.replaceAll("[)]+", ""); - } else { - partitionKey=primaryKey; - partitionKey=partitionKey.replaceAll("[\\)]+",""); - partitionKey=partitionKey.replaceAll("[\\(]+",""); - clusterKey=""; + partitionKey=partitionKey.replaceAll("[\\(]+",""); + clusterKey=primaryKey.substring(primaryKey.indexOf(',')+1); // make sure index + clusterKey=clusterKey.replaceAll("[)]+", ""); + } else { + partitionKey=primaryKey; + partitionKey=partitionKey.replaceAll("[\\)]+",""); + partitionKey=partitionKey.replaceAll("[\\(]+",""); + clusterKey=""; + } + } else { // not null and has ) before the last char + partitionKey= primaryKey.substring(0,primaryKey.indexOf(')')); + partitionKey=partitionKey.replaceAll("[\\(]+",""); + partitionKey = partitionKey.trim(); + clusterKey= primaryKey.substring(primaryKey.indexOf(')')); + clusterKey=clusterKey.replaceAll("[\\(]+",""); + clusterKey=clusterKey.replaceAll("[\\)]+",""); + clusterKey = clusterKey.trim(); + if (clusterKey.indexOf(',') == 0) clusterKey=clusterKey.substring(1); + clusterKey = clusterKey.trim(); + if (clusterKey.equals(",") ) clusterKey=""; // print error if needed ( ... ),) } - } else { // not null and has ) before the last char - partitionKey= primaryKey.substring(0,primaryKey.indexOf(')')); - partitionKey=partitionKey.replaceAll("[\\(]+",""); - partitionKey = partitionKey.trim(); - clusterKey= primaryKey.substring(primaryKey.indexOf(')')); - clusterKey=clusterKey.replaceAll("[\\(]+",""); - clusterKey=clusterKey.replaceAll("[\\)]+",""); - clusterKey = clusterKey.trim(); - if (clusterKey.indexOf(',') == 0) clusterKey=clusterKey.substring(1); - clusterKey = clusterKey.trim(); - if (clusterKey.equals(",") ) clusterKey=""; // print error if needed ( ... ),) - } - - if (!(partitionKey.isEmpty() || clusterKey.isEmpty()) - && (partitionKey.equalsIgnoreCase(clusterKey) || - clusterKey.contains(partitionKey) || partitionKey.contains(clusterKey)) ) - { - logger.error("DataAPI createTable partition/cluster key ERROR: partitionKey="+partitionKey+", clusterKey=" + clusterKey + " and primary key=" + primaryKey ); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError( - "Create Table primary key error: clusterKey(" + clusterKey + ") equals/contains/overlaps partitionKey(" +partitionKey+ ") of" - + " primary key=" + primaryKey) - .toMap()).build(); - } + if (!(partitionKey.isEmpty() || clusterKey.isEmpty()) + && (partitionKey.equalsIgnoreCase(clusterKey) || + clusterKey.contains(partitionKey) || partitionKey.contains(clusterKey)) ) + { + logger.error("DataAPI createTable partition/cluster key ERROR: partitionKey="+partitionKey+", clusterKey=" + clusterKey + " and primary key=" + primaryKey ); + return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError( + "Create Table primary key error: clusterKey(" + clusterKey + ") equals/contains/overlaps partitionKey(" +partitionKey+ ") of" + + " primary key=" + primaryKey) + .toMap()).build(); + + } + + if (partitionKey.isEmpty() ) primaryKey=""; + else if (clusterKey.isEmpty() ) primaryKey=" (" + partitionKey + ")"; + else primaryKey=" (" + partitionKey + ")," + clusterKey; + - if (partitionKey.isEmpty() ) primaryKey=""; - else if (clusterKey.isEmpty() ) primaryKey=" (" + partitionKey + ")"; - else primaryKey=" (" + partitionKey + ")," + clusterKey; - - - if (primaryKey != null) fieldsString.append(", PRIMARY KEY (" + primaryKey + " )"); - - } // end of length > 0 - else { - if (!(partitionKey.isEmpty() || clusterKey.isEmpty()) - && (partitionKey.equalsIgnoreCase(clusterKey) || - clusterKey.contains(partitionKey) || partitionKey.contains(clusterKey)) ) - { - logger.error("DataAPI createTable partition/cluster key ERROR: partitionKey="+partitionKey+", clusterKey=" + clusterKey); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError( + if (primaryKey != null) fieldsString.append(", PRIMARY KEY (" + primaryKey + " )"); + + } // end of length > 0 + else { + if (!(partitionKey.isEmpty() || clusterKey.isEmpty()) + && (partitionKey.equalsIgnoreCase(clusterKey) || + clusterKey.contains(partitionKey) || partitionKey.contains(clusterKey)) ) + { + logger.error("DataAPI createTable partition/cluster key ERROR: partitionKey="+partitionKey+", clusterKey=" + clusterKey); + return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError( "Create Table primary key error: clusterKey(" + clusterKey + ") equals/contains/overlaps partitionKey(" +partitionKey+ ")") .toMap()).build(); - } + } - if (partitionKey.isEmpty() ) primaryKey=""; - else if (clusterKey.isEmpty() ) primaryKey=" (" + partitionKey + ")"; - else primaryKey=" (" + partitionKey + ")," + clusterKey; + if (partitionKey.isEmpty() ) primaryKey=""; + else if (clusterKey.isEmpty() ) primaryKey=" (" + partitionKey + ")"; + else primaryKey=" (" + partitionKey + ")," + clusterKey; - - if (primaryKey != null) fieldsString.append(", PRIMARY KEY (" + primaryKey + " )"); - } - fieldsString.append(")"); - } // end of last field check + if (primaryKey != null) fieldsString.append(", PRIMARY KEY (" + primaryKey + " )"); + } + fieldsString.append(")"); + + } // end of last field check - } // end of for each + } // end of for each // information about the name-value style properties Map<String, Object> propertiesMap = tableObj.getProperties(); StringBuilder propertiesString = new StringBuilder(); @@ -561,40 +537,40 @@ public class RestMusicDataAPI { String clusteringOrder = tableObj.getClusteringOrder(); if (clusteringOrder != null && !(clusteringOrder.isEmpty())) { - String[] arrayClusterOrder = clusteringOrder.split("[,]+"); + String[] arrayClusterOrder = clusteringOrder.split("[,]+"); - for (int i = 0; i < arrayClusterOrder.length; i++) { - String[] clusterS = arrayClusterOrder[i].trim().split("[ ]+"); - if ( (clusterS.length ==2) && (clusterS[1].equalsIgnoreCase("ASC") || clusterS[1].equalsIgnoreCase("DESC"))) { - continue; - } else { - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE) - .setError("createTable/Clustering Order vlaue ERROR: valid clustering order is ASC or DESC or expecting colname order; please correct clusteringOrder:"+ clusteringOrder+".") - .toMap()).build(); - } + for (int i = 0; i < arrayClusterOrder.length; i++) { + String[] clusterS = arrayClusterOrder[i].trim().split("[ ]+"); + if ( (clusterS.length ==2) && (clusterS[1].equalsIgnoreCase("ASC") || clusterS[1].equalsIgnoreCase("DESC"))) { + continue; + } else { + return response.status(Status.BAD_REQUEST) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("createTable/Clustering Order vlaue ERROR: valid clustering order is ASC or DESC or expecting colname order; please correct clusteringOrder:"+ clusteringOrder+".") + .toMap()).build(); + } // add validation for column names in cluster key - } + } - if (!(clusterKey.isEmpty())) { - clusteringOrder = "CLUSTERING ORDER BY (" +clusteringOrder +")"; - //cjc check if propertiesString.length() >0 instead propertiesMap - if (propertiesMap != null) { - propertiesString.append(" AND "+ clusteringOrder); + if (!(clusterKey.isEmpty())) { + clusteringOrder = "CLUSTERING ORDER BY (" +clusteringOrder +")"; + //cjc check if propertiesString.length() >0 instead propertiesMap + if (propertiesMap != null) { + propertiesString.append(" AND "+ clusteringOrder); + } else { + propertiesString.append(clusteringOrder); + } } else { - propertiesString.append(clusteringOrder); - } - } else { logger.warn("Skipping clustering order=("+clusteringOrder+ ") since clustering key is empty "); - } - } //if non empty + } + } //if non empty - queryObject.appendQueryString( - "CREATE TABLE " + keyspace + "." + tablename + " " + fieldsString); + queryObject.appendQueryString( + "CREATE TABLE " + keyspace + "." + tablename + " " + fieldsString); - if (propertiesString != null && propertiesString.length()>0 ) - queryObject.appendQueryString(" WITH " + propertiesString); + if (propertiesString != null && propertiesString.length()>0 ) + queryObject.appendQueryString(" WITH " + propertiesString); queryObject.appendQueryString(";"); ResultType result = ResultType.FAILURE; try { @@ -644,17 +620,13 @@ public class RestMusicDataAPI { .toMap()).build(); } EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspace+" ) "); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace,aid, "createIndex"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,authMap.get("Exception").toString(), AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.CREATE_INDEX)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); + } + MultivaluedMap<String, String> rowParams = info.getQueryParameters(); String indexName = ""; if (rowParams.getFirst("index_name") != null) @@ -714,23 +686,11 @@ public class RestMusicDataAPI { .toMap()).build(); } EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspace+" ) "); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = null; - - try { - authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace, - aid, "insertIntoTable"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,authMap.get("Exception").toString(), AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.INSERT_INTO_TABLE)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } Map<String, Object> valuesMap = insObj.getValues(); @@ -742,7 +702,7 @@ public class RestMusicDataAPI { return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Table name doesn't exists. Please check the table name.").toMap()).build(); } } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger, e, AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); } String primaryKeyName = tableInfo.getPrimaryKey().get(0).getName(); @@ -773,7 +733,7 @@ public class RestMusicDataAPI { try { formattedValue = MusicUtil.convertToActualDataType(colType, valueObj); } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + logger.error(EELFLoggerDelegate.errorLogger,e); } valueString.append("?"); @@ -884,10 +844,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(); @@ -946,23 +902,13 @@ public class RestMusicDataAPI { .toMap()).build(); } EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspace+" ) "); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap; - try { - authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace, - aid, "updateTable"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,authMap.get("Exception").toString(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.UPDATE_TABLE)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + long startTime = System.currentTimeMillis(); String operationId = UUID.randomUUID().toString();// just for infoging // purposes. @@ -979,7 +925,7 @@ public class RestMusicDataAPI { try { tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename); } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger,e, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); } if (tableInfo == null) { @@ -999,14 +945,14 @@ public class RestMusicDataAPI { try { colType = tableInfo.getColumn(entry.getKey()).getType(); } catch(NullPointerException ex) { - logger.error(EELFLoggerDelegate.errorLogger, "Invalid column name : "+entry.getKey()); + logger.error(EELFLoggerDelegate.errorLogger, ex, "Invalid column name : "+entry.getKey()); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Invalid column name : "+entry.getKey()).toMap()).build(); } Object valueString = null; try { valueString = MusicUtil.convertToActualDataType(colType, valueObj); } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + logger.error(EELFLoggerDelegate.errorLogger,e); } fieldValueString.append(entry.getKey() + "= ?"); queryObject.addValue(valueString); @@ -1046,7 +992,7 @@ public class RestMusicDataAPI { .setError("Mandatory WHERE clause is missing. Please check the input request.").toMap()).build(); } } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger,ex, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); } @@ -1094,7 +1040,7 @@ public class RestMusicDataAPI { operationResult = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, rowId.primarKeyValue, queryObject, conditionInfo); } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger,e, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); } } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { @@ -1102,7 +1048,7 @@ public class RestMusicDataAPI { operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, queryObject, conditionInfo); } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger,e, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); } }else if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL_NB)) { @@ -1179,23 +1125,13 @@ public class RestMusicDataAPI { .toMap()).build(); } EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspace+" ) "); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = null; - try { - authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace, - aid, "deleteFromTable"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,authMap.get("Exception").toString(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.DELETE_FROM_TABLE)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + if(delObj == null) { logger.error(EELFLoggerDelegate.errorLogger,"Required HTTP Request body is missing.", AppMessages.MISSINGDATA ,ErrorSeverity.WARN, ErrorTypes.DATAERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Required HTTP Request body is missing.").toMap()).build(); @@ -1204,7 +1140,7 @@ public class RestMusicDataAPI { StringBuilder columnString = new StringBuilder(); int counter = 0; - ArrayList<String> columnList = delObj.getColumns(); + List<String> columnList = delObj.getColumns(); if (columnList != null) { for (String column : columnList) { columnString.append(column); @@ -1219,7 +1155,7 @@ public class RestMusicDataAPI { try { rowId = getRowIdentifier(keyspace, tablename, info.getQueryParameters(), queryObject); } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger,ex, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); } String rowSpec = rowId.rowIdString.toString(); @@ -1280,16 +1216,12 @@ 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); } } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger,e, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) .setError("Unable to perform Delete operation. Exception from music").toMap()).build(); } @@ -1342,17 +1274,13 @@ public class RestMusicDataAPI { .toMap()).build(); } EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspace+" ) "); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = - MusicAuthentication.autheticateUser(ns, userId, password, keyspace, aid, "dropTable"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,authMap.get("Exception").toString(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.DROP_TABLE)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + String consistency = "eventual";// for now this needs only eventual // consistency PreparedQueryObject query = new PreparedQueryObject(); @@ -1360,7 +1288,7 @@ public class RestMusicDataAPI { try { return response.status(Status.OK).entity(new JsonResponse(MusicCore.nonKeyRelatedPut(query, consistency)).toMap()).build(); } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger,ex, AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); } } finally { @@ -1406,16 +1334,13 @@ public class RestMusicDataAPI { .toMap()).build(); } EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspace+" ) "); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace,aid, "selectCritical"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,authMap.get("Exception").toString(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.SELECT_CRITICAL)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + String lockId = selObj.getConsistencyInfo().get("lockId"); PreparedQueryObject queryObject = new PreparedQueryObject(); @@ -1424,7 +1349,7 @@ public class RestMusicDataAPI { try { rowId = getRowIdentifier(keyspace, tablename, info.getQueryParameters(), queryObject); } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger,ex, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); } queryObject.appendQueryString( @@ -1446,10 +1371,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(); } @@ -1496,17 +1417,13 @@ public class RestMusicDataAPI { .toMap()).build(); } EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspace+" ) "); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = - MusicAuthentication.autheticateUser(ns, userId, password, keyspace, aid, "select"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,authMap.get("Exception").toString(), AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspace, aid, Operation.SELECT)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + PreparedQueryObject queryObject = new PreparedQueryObject(); if (info.getQueryParameters().isEmpty())// select all @@ -1514,10 +1431,9 @@ public class RestMusicDataAPI { else { int limit = -1; // do not limit the number of results try { - queryObject = selectSpecificQuery(VERSION, minorVersion, patchVersion, aid, ns, - userId, password, keyspace, tablename, info, limit); + queryObject = selectSpecificQuery(keyspace, tablename, info, limit); } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger, ex, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); } } @@ -1529,7 +1445,7 @@ public class RestMusicDataAPI { } return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicDataStoreHandle.marshallResults(results)).setError("No data found").toMap()).build(); } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR); + logger.error(EELFLoggerDelegate.errorLogger, ex, AppMessages.UNKNOWNERROR ,ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); } } finally { @@ -1546,9 +1462,8 @@ public class RestMusicDataAPI { * @return * @throws MusicServiceException */ - public PreparedQueryObject selectSpecificQuery(String version, String minorVersion, - String patchVersion, String aid, String ns, String userId, String password, - String keyspace, String tablename, UriInfo info, int limit) + public PreparedQueryObject selectSpecificQuery(String keyspace, + String tablename, UriInfo info, int limit) throws MusicServiceException { PreparedQueryObject queryObject = new PreparedQueryObject(); @@ -1601,7 +1516,7 @@ public class RestMusicDataAPI { colType = tableInfo.getColumn(entry.getKey()).getType(); formattedValue = MusicUtil.convertToActualDataType(colType, indValue); } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + logger.error(EELFLoggerDelegate.errorLogger,e); } if(tableInfo.getPrimaryKey().get(0).getName().equals(entry.getKey())) primaryKey.append(indValue); 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/RestMusicLocksAPI.java b/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java index 0bb2368a..943f4ca8 100644 --- a/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java @@ -37,7 +37,9 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; -import org.onap.music.authentication.MusicAuthentication; +import org.onap.music.authentication.MusicAAFAuthentication; +import org.onap.music.authentication.MusicAuthenticator; +import org.onap.music.authentication.MusicAuthenticator.Operation; import org.onap.music.datastore.jsonobjects.JsonLeasedLock; import org.onap.music.eelf.logging.EELFLoggerDelegate; import org.onap.music.eelf.logging.format.AppMessages; @@ -63,10 +65,12 @@ public class RestMusicLocksAPI { private static final String XMINORVERSION = "X-minorVersion"; private static final String XPATCHVERSION = "X-patchVersion"; private static final String VERSION = "v2"; + + private MusicAuthenticator authenticator = new MusicAAFAuthentication(); /** * Puts the requesting process in the q for this lock. The corresponding - * node will be created in zookeeper if it did not already exist + * node will be created if it did not already exist * * @param lockName * @return @@ -76,7 +80,7 @@ public class RestMusicLocksAPI { @Path("/create/{lockname}") @ApiOperation(value = "Create Lock", notes = "Puts the requesting process in the q for this lock." + - " The corresponding node will be created in zookeeper if it did not already exist." + + " The corresponding lock will be created if it did not already exist." + " Lock Name is the \"key\" of the form keyspaceName.tableName.rowId", response = Map.class) @Produces(MediaType.APPLICATION_JSON) @@ -96,21 +100,16 @@ public class RestMusicLocksAPI { response.status(Status.BAD_REQUEST); return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); String keyspaceName = (String) resultMap.get("keyspace"); EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) "); - resultMap.remove("keyspace"); - resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid, - "createLockReference"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); + + if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.CREATE_LOCKREF)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + ResultType status = ResultType.SUCCESS; String lockId = MusicCore.createLockReference(lockName); @@ -155,21 +154,17 @@ public class RestMusicLocksAPI { response.status(Status.BAD_REQUEST); return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); + String keyspaceName = (String) resultMap.get("keyspace"); EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) "); - resultMap.remove("keyspace"); - resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid, - "accquireLock"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); + + if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.ACQUIRE_LOCK)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + try { String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$')); ReturnType lockStatus = MusicCore.acquireLock(lockName,lockId); @@ -212,22 +207,16 @@ public class RestMusicLocksAPI { response.status(Status.BAD_REQUEST); return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); String keyspaceName = (String) resultMap.get("keyspace"); EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) "); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid, - "accquireLockWithLease"); - - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.ACQUIRE_LOCK)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$')); ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod()); if ( lockLeaseStatus.getResult().equals(ResultType.SUCCESS)) { @@ -266,21 +255,17 @@ public class RestMusicLocksAPI { response.status(Status.BAD_REQUEST); return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); + String keyspaceName = (String) resultMap.get("keyspace"); EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) "); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid, - "currentLockHolder"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.CURRENT_LOCK)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + String who = MusicCore.whoseTurnIsIt(lockName); ResultType status = ResultType.SUCCESS; String error = ""; @@ -321,18 +306,11 @@ public class RestMusicLocksAPI { String keyspaceName = (String) resultMap.get("keyspace"); EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) "); resultMap.remove("keyspace"); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - resultMap.remove("keyspace"); - resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid, - "currentLockHolder"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.CURRENT_LOCK)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } String who = MusicCore.whoseTurnIsIt(lockName); @@ -394,21 +372,17 @@ public class RestMusicLocksAPI { response.status(Status.BAD_REQUEST); return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); + String keyspaceName = (String) resultMap.get("keyspace"); EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) "); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid, - "unLock"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.DELETE_LOCK)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + boolean voluntaryRelease = true; MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease); if(mls.getErrorMessage() != null) { @@ -458,21 +432,17 @@ public class RestMusicLocksAPI { response.status(Status.BAD_REQUEST); return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); + String keyspaceName = (String) resultMap.get("keyspace"); EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) "); resultMap.remove("keyspace"); - resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid, - "deleteLock"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build(); + if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.DELETE_LOCK)) { + return response.status(Status.UNAUTHORIZED) + .entity(new JsonResponse(ResultType.FAILURE) + .setError("Unauthorized: Please check username, password and make sure your app is onboarded") + .toMap()).build(); } + try{ MusicCore.deleteLock(lockName); }catch (Exception e) { diff --git a/src/main/java/org/onap/music/rest/RestMusicQAPI.java b/src/main/java/org/onap/music/rest/RestMusicQAPI.java index 4164f27f..676730e0 100755 --- a/src/main/java/org/onap/music/rest/RestMusicQAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicQAPI.java @@ -3,6 +3,7 @@ * 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. @@ -52,19 +53,17 @@ import org.apache.commons.lang3.StringUtils; import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; import com.datastax.driver.core.ResultSet; +import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.response.jsonobjects.JsonResponse; -import org.onap.music.service.impl.MusicZKCore; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; -// import io.swagger.models.Response; -// @Path("/v{version: [0-9]+}/priorityq/") @Path("/v2/priorityq/") @Api(value = "Q Api") public class RestMusicQAPI { @@ -96,14 +95,11 @@ public class RestMusicQAPI { @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, JsonTable tableObj, @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename) - throws Exception { - //logger.info(logger, "cjc before start in q 1** major version=" + version); - + @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename) throws Exception { ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion); Map<String, String> fields = tableObj.getFields(); - if (fields == null) { // || (!fields.containsKey("order")) ){ + if (fields == null) { logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); return response.status(Status.BAD_REQUEST) @@ -241,7 +237,7 @@ public class RestMusicQAPI { JsonInsert insObj, @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename) - throws Exception { + { ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion); if (insObj.getValues().isEmpty()) { logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, @@ -279,7 +275,7 @@ public class RestMusicQAPI { JsonUpdate updateObj, @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename, - @Context UriInfo info) throws Exception { + @Context UriInfo info) throws MusicServiceException, MusicQueryException { ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion); if (updateObj.getValues().isEmpty()) { @@ -325,7 +321,7 @@ public class RestMusicQAPI { JsonDelete delObj, @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename, - @Context UriInfo info) throws Exception { + @Context UriInfo info) throws MusicServiceException, MusicQueryException { // added checking as per RestMusicDataAPI ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion); if (delObj == null) { @@ -363,7 +359,7 @@ public class RestMusicQAPI { @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename, - @Context UriInfo info) throws Exception { + @Context UriInfo info) { int limit =1; //peek must return just the top row Map<String ,String> auth = new HashMap<>(); String userId =auth.get(MusicUtil.USERID); @@ -377,8 +373,7 @@ public class RestMusicQAPI { else { try { - queryObject = new RestMusicDataAPI().selectSpecificQuery(version, minorVersion, - patchVersion, aid, ns, userId, password, keyspace, tablename, info, limit); + queryObject = new RestMusicDataAPI().selectSpecificQuery(keyspace, tablename, info, limit); } catch (MusicServiceException ex) { logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.UNKNOWNERROR, ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); @@ -450,8 +445,7 @@ public class RestMusicQAPI { @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns, @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename) - throws Exception { + @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename) throws Exception { return new RestMusicDataAPI().dropTable(version, minorVersion, patchVersion, aid, ns, authorization, keyspace, tablename); } diff --git a/src/main/java/org/onap/music/service/impl/MusicCassaCore.java b/src/main/java/org/onap/music/service/impl/MusicCassaCore.java index 21b4874b..cb6e1ce4 100644 --- a/src/main/java/org/onap/music/service/impl/MusicCassaCore.java +++ b/src/main/java/org/onap/music/service/impl/MusicCassaCore.java @@ -226,7 +226,6 @@ public class MusicCassaCore implements MusicCoreService { 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); 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); - } - - - - -} |