summaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/onap/music/CadiAuthFilter.java143
-rwxr-xr-xsrc/main/java/org/onap/music/JerseyConfig.java77
-rwxr-xr-xsrc/main/java/org/onap/music/MusicApplication.java137
-rw-r--r--src/main/java/org/onap/music/authentication/MusicAuthentication.java203
-rw-r--r--src/main/java/org/onap/music/conductor/conditionals/JsonConditional.java101
-rw-r--r--src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java554
-rw-r--r--src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java317
-rw-r--r--src/main/java/org/onap/music/datastore/Condition.java52
-rwxr-xr-x[-rw-r--r--]src/main/java/org/onap/music/datastore/MusicDataStore.java321
-rw-r--r--src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java108
-rw-r--r--src/main/java/org/onap/music/datastore/PreparedQueryObject.java49
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/AAFResponse.java1
-rwxr-xr-xsrc/main/java/org/onap/music/datastore/jsonobjects/JSONCallbackResponse.java89
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/JSONObject.java18
-rwxr-xr-xsrc/main/java/org/onap/music/datastore/jsonobjects/JsonCallback.java135
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/JsonDelete.java5
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/JsonInsert.java9
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/JsonKeySpace.java1
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/JsonLeasedLock.java1
-rwxr-xr-xsrc/main/java/org/onap/music/datastore/jsonobjects/JsonNotification.java127
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/JsonNotifyClientResponse.java59
-rwxr-xr-xsrc/main/java/org/onap/music/datastore/jsonobjects/JsonOnboard.java11
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/JsonSelect.java7
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java37
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/JsonUpdate.java1
-rw-r--r--src/main/java/org/onap/music/datastore/jsonobjects/NameSpace.java1
-rw-r--r--src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java214
-rw-r--r--src/main/java/org/onap/music/eelf/logging/EELFLoggerDelegate.java1
-rw-r--r--src/main/java/org/onap/music/eelf/logging/format/AppMessages.java254
-rw-r--r--src/main/java/org/onap/music/eelf/logging/format/ErrorCodes.java127
-rw-r--r--src/main/java/org/onap/music/eelf/logging/format/ErrorSeverity.java1
-rw-r--r--src/main/java/org/onap/music/eelf/logging/format/ErrorTypes.java28
-rw-r--r--src/main/java/org/onap/music/exceptions/MusicExceptionMapper.java1
-rw-r--r--src/main/java/org/onap/music/exceptions/MusicLockingException.java1
-rw-r--r--src/main/java/org/onap/music/exceptions/MusicPolicyVoilationException.java1
-rw-r--r--src/main/java/org/onap/music/exceptions/MusicQueryException.java1
-rw-r--r--src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java254
-rw-r--r--src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java (renamed from src/main/java/org/onap/music/lockingservice/MusicLockState.java)16
-rw-r--r--src/main/java/org/onap/music/lockingservice/zookeeper/LockListener.java (renamed from src/main/java/org/onap/music/lockingservice/LockListener.java)3
-rw-r--r--src/main/java/org/onap/music/lockingservice/zookeeper/MusicLockingService.java (renamed from src/main/java/org/onap/music/lockingservice/MusicLockingService.java)68
-rw-r--r--src/main/java/org/onap/music/lockingservice/zookeeper/ProtocolSupport.java (renamed from src/main/java/org/onap/music/lockingservice/ProtocolSupport.java)17
-rw-r--r--src/main/java/org/onap/music/lockingservice/zookeeper/ZNodeName.java (renamed from src/main/java/org/onap/music/lockingservice/ZNodeName.java)7
-rw-r--r--src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java (renamed from src/main/java/org/onap/music/lockingservice/ZkStatelessLockService.java)50
-rw-r--r--src/main/java/org/onap/music/lockingservice/zookeeper/ZooKeeperOperation.java (renamed from src/main/java/org/onap/music/lockingservice/ZooKeeperOperation.java)5
-rwxr-xr-xsrc/main/java/org/onap/music/main/CachingUtil.java140
-rw-r--r--src/main/java/org/onap/music/main/CronJobManager.java55
-rw-r--r--src/main/java/org/onap/music/main/MusicCore.java1028
-rw-r--r--src/main/java/org/onap/music/main/MusicDigest.java1
-rwxr-xr-xsrc/main/java/org/onap/music/main/MusicUtil.java409
-rwxr-xr-xsrc/main/java/org/onap/music/main/PropertiesListener.java112
-rw-r--r--src/main/java/org/onap/music/main/PropertiesLoader.java192
-rw-r--r--src/main/java/org/onap/music/main/ResultType.java7
-rw-r--r--src/main/java/org/onap/music/main/ReturnType.java1
-rw-r--r--src/main/java/org/onap/music/response/jsonobjects/JsonResponse.java55
-rw-r--r--src/main/java/org/onap/music/rest/Application.java79
-rwxr-xr-xsrc/main/java/org/onap/music/rest/RestMusicAdminAPI.java720
-rw-r--r--src/main/java/org/onap/music/rest/RestMusicBmAPI.java47
-rwxr-xr-xsrc/main/java/org/onap/music/rest/RestMusicDataAPI.java842
-rw-r--r--src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java147
-rw-r--r--src/main/java/org/onap/music/rest/RestMusicLocksAPI.java215
-rwxr-xr-xsrc/main/java/org/onap/music/rest/RestMusicQAPI.java123
-rw-r--r--src/main/java/org/onap/music/rest/RestMusicTestAPI.java43
-rw-r--r--src/main/java/org/onap/music/rest/RestMusicVersionAPI.java32
-rw-r--r--src/main/java/org/onap/music/service/MusicCoreService.java110
-rw-r--r--src/main/java/org/onap/music/service/impl/MusicCassaCore.java762
-rw-r--r--src/main/java/org/onap/music/service/impl/MusicZKCore.java937
66 files changed, 5921 insertions, 3749 deletions
diff --git a/src/main/java/org/onap/music/CadiAuthFilter.java b/src/main/java/org/onap/music/CadiAuthFilter.java
new file mode 100644
index 00000000..12114662
--- /dev/null
+++ b/src/main/java/org/onap/music/CadiAuthFilter.java
@@ -0,0 +1,143 @@
+/*
+ * ============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;
+import java.io.IOException;
+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 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;
+
+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 {
+ 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;
+ }
+
+
+ private boolean matchPattern(String requestedPath, String includeUrl) {
+ includeUrl = includeUrl.substring(1);
+ String[] path = requestedPath.split("/");
+ if (path.length > 1) {
+ String[] roleFunctionArray = includeUrl.split("/");
+ boolean match = true;
+ for (int i = 0; i < roleFunctionArray.length; i++) {
+ if (match) {
+ if (!roleFunctionArray[i].equals("*")) {
+ Pattern p = Pattern.compile(Pattern.quote(path[i]), Pattern.CASE_INSENSITIVE);
+ Matcher m = p.matcher(roleFunctionArray[i]);
+ match = m.matches();
+ } else if (roleFunctionArray[i].equals("*")) {
+ match = true;
+ }
+
+ }
+ }
+ if (match)
+ return match;
+ } else {
+ if (requestedPath.matches(includeUrl))
+ return true;
+ else if (includeUrl.equals("*"))
+ return true;
+ }
+ return false;
+ }
+
+
+ public static List<AAFPermission> getAAFPermissions(HttpServletRequest request) {
+ CadiWrap wrapReq = (CadiWrap) request;
+ List<Permission> perms = wrapReq.getPermissions(wrapReq.getUserPrincipal());
+ List<AAFPermission> aafPermsList = new ArrayList<>();
+ for (Permission perm : perms) {
+ AAFPermission aafPerm = (AAFPermission) perm;
+ aafPermsList.add(aafPerm);
+ System.out.println(aafPerm.toString());
+ System.out.println(aafPerm.getType());
+ }
+ return aafPermsList;
+ }
+
+ public static List<AAFPermission> getAAFPermissions(ServletRequest request) {
+ CadiWrap wrapReq = (CadiWrap) request;
+ List<Permission> perms = wrapReq.getPermissions(wrapReq.getUserPrincipal());
+ List<AAFPermission> aafPermsList = new ArrayList<>();
+ for (Permission perm : perms) {
+ AAFPermission aafPerm = (AAFPermission) perm;
+ 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/JerseyConfig.java b/src/main/java/org/onap/music/JerseyConfig.java
new file mode 100755
index 00000000..74958363
--- /dev/null
+++ b/src/main/java/org/onap/music/JerseyConfig.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2012-2015 the original author or authors.
+ *
+ * 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.
+ */
+
+package org.onap.music;
+
+import javax.annotation.PostConstruct;
+import javax.ws.rs.ApplicationPath;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.onap.music.rest.RestMusicAdminAPI;
+import org.onap.music.rest.RestMusicDataAPI;
+import org.onap.music.rest.RestMusicHealthCheckAPI;
+import org.onap.music.rest.RestMusicLocksAPI;
+import org.onap.music.rest.RestMusicQAPI;
+import org.onap.music.rest.RestMusicTestAPI;
+import org.onap.music.rest.RestMusicVersionAPI;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import io.swagger.jaxrs.config.BeanConfig;
+import io.swagger.jaxrs.listing.ApiListingResource;
+import io.swagger.jaxrs.listing.SwaggerSerializers;
+
+@Component
+public class JerseyConfig extends ResourceConfig {
+
+ @Value("${spring.jersey.application-path:/}")
+ private String apiPath;
+
+ public JerseyConfig() {
+ this.registerEndpoints();
+ }
+
+ @PostConstruct
+ public void init() {
+ this.configureSwagger();
+ }
+
+ private void registerEndpoints() {
+ register(RestMusicAdminAPI.class);
+ register(RestMusicDataAPI.class);
+ register(RestMusicLocksAPI.class);
+ register(RestMusicQAPI.class);
+ register(RestMusicTestAPI.class);
+ register(RestMusicVersionAPI.class);
+ register(RestMusicHealthCheckAPI.class);
+ }
+
+ private void configureSwagger() {
+ // Available at localhost:port/swagger.json
+ this.register(ApiListingResource.class);
+ this.register(SwaggerSerializers.class);
+
+ BeanConfig config = new BeanConfig();
+ config.setConfigId("MUSIC");
+ config.setTitle("MUSIC");
+ config.setVersion("v2");
+ config.setContact("Thomas Nelson");
+ config.setSchemes(new String[] { "http", "https" });
+ config.setBasePath("/MUSIC/rest");
+ config.setResourcePackage("org.onap.music");
+ config.setPrettyPrint(true);
+ config.setScan(true);
+ }
+
+}
diff --git a/src/main/java/org/onap/music/MusicApplication.java b/src/main/java/org/onap/music/MusicApplication.java
new file mode 100755
index 00000000..5251c178
--- /dev/null
+++ b/src/main/java/org/onap/music/MusicApplication.java
@@ -0,0 +1,137 @@
+/*
+ * ============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;
+
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.onap.aaf.cadi.PropAccess;
+import org.onap.music.main.MusicUtil;
+import org.onap.music.main.PropertiesLoader;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+//import org.springframework.boot.web.support.SpringBootServletInitializer;
+import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.DependsOn;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.web.context.request.RequestContextListener;
+
+
+@SpringBootApplication(scanBasePackages = { "org.onap.music.rest"})
+@EnableAutoConfiguration(exclude={CassandraDataAutoConfiguration.class})
+@ComponentScan(value = {"org.onap.music"})
+@EnableScheduling
+public class MusicApplication extends SpringBootServletInitializer {
+
+ @Autowired
+ PropertiesLoader propertyLoader;
+
+
+ public static void main(String[] args) {
+ System.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG","false");
+ System.setProperty("AFT_DME2_CLIENT_KEYSTORE","/opt/app/music/etc/truststore2018.jks");
+ System.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD","changeit");
+ System.setProperty("AFT_DME2_CLIENT_SSL_INCLUDE_PROTOCOLS","TLSv1.1,TLSv1.2");
+ new MusicApplication().configure(new SpringApplicationBuilder(MusicApplication.class)).run(args);
+ }
+
+ @Override
+ protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
+
+ return application.sources(MusicApplication.class);
+ }
+
+
+ @Bean("loadProperties")
+ public void loadProperties() {
+ propertyLoader.loadProperties();
+ }
+
+ @Autowired
+ private ApplicationContext appContext;
+
+ @Bean
+ @DependsOn("loadProperties")
+ public PropAccess propAccess() {
+ if(MusicUtil.getIsCadi())
+ return new PropAccess(new String[] { "cadi_prop_files=/opt/app/music/etc/cadi.properties" });
+ else
+ return null;
+ }
+
+ @Bean(name = "cadiFilter")
+ @DependsOn("loadProperties")
+ public Filter cadiFilter() throws ServletException {
+ propertyLoader.loadProperties();
+ if(MusicUtil.getIsCadi()) {
+ PropAccess propAccess = propAccess();
+ CadiAuthFilter cadiFilter = new CadiAuthFilter(true, propAccess);
+ return cadiFilter;
+ } else
+ return (ServletRequest request, ServletResponse response, FilterChain chain) -> {
+ //do nothing for now.
+ };
+
+ }
+
+ @Bean
+ @DependsOn("loadProperties")
+ public FilterRegistrationBean<Filter> cadiFilterRegistration() throws ServletException {
+ FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
+ frb.setFilter(cadiFilter());
+ // The Following Patterns are used to control what APIs will be secure
+ // TODO: Make this a configurable item. Build this from an array?
+ if(MusicUtil.getIsCadi()) {
+ frb.addUrlPatterns(
+ "/v2/keyspaces/*",
+ "/v2/locks/*",
+ "/v3/locks/*",
+ "/v2/priorityq/*",
+ "/v2/admin/*"
+ );
+ } else {
+ frb.addUrlPatterns("/v0/test");
+ }
+ frb.setName("cadiFilter");
+ frb.setOrder(0);
+ return frb;
+ }
+
+ @Bean
+ @ConditionalOnMissingBean(RequestContextListener.class)
+ public RequestContextListener requestContextListener() {
+ return new RequestContextListener();
+ }
+}
diff --git a/src/main/java/org/onap/music/authentication/MusicAuthentication.java b/src/main/java/org/onap/music/authentication/MusicAuthentication.java
new file mode 100644
index 00000000..0471e309
--- /dev/null
+++ b/src/main/java/org/onap/music/authentication/MusicAuthentication.java
@@ -0,0 +1,203 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.authentication;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.commons.jcs.access.CacheAccess;
+import org.onap.music.datastore.PreparedQueryObject;
+import org.onap.music.eelf.logging.EELFLoggerDelegate;
+import org.onap.music.eelf.logging.format.AppMessages;
+import org.onap.music.eelf.logging.format.ErrorSeverity;
+import org.onap.music.eelf.logging.format.ErrorTypes;
+import org.onap.music.exceptions.MusicServiceException;
+import org.onap.music.main.CachingUtil;
+import org.onap.music.main.MusicCore;
+import org.onap.music.main.MusicUtil;
+import org.onap.music.service.impl.MusicZKCore;
+
+import com.datastax.driver.core.DataType;
+import com.datastax.driver.core.Row;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+
+public class MusicAuthentication {
+
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicAuthentication.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 {
+ logger.info(EELFLoggerDelegate.applicationLogger,"Inside User Authentication.......");
+ Map<String, Object> resultMap = new HashMap<>();
+ String uuid = null;
+ if(! MusicUtil.getIsCadi()) {
+ resultMap = CachingUtil.validateRequest(nameSpace, userId, password, keyspace, aid,
+ operation);
+ if (!resultMap.isEmpty())
+ return resultMap;
+ String isAAFApp = null;
+ try {
+ isAAFApp= CachingUtil.isAAFApplication(nameSpace);
+ } catch(MusicServiceException e) {
+ resultMap.put("Exception", e.getMessage());
+ return resultMap;
+ }
+ if(isAAFApp == null) {
+ resultMap.put("Exception", "Namespace: "+nameSpace+" doesn't exist. Please make sure ns(appName)"
+ + " is correct and Application is onboarded.");
+ return resultMap;
+ }
+ boolean isAAF = Boolean.valueOf(isAAFApp);
+ if (userId == null || password == null) {
+ logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,"One or more required headers is missing. userId: " + userId
+ + " :: password: " + password);
+ resultMap.put("Exception",
+ "UserId and Password are mandatory for the operation " + operation);
+ return resultMap;
+ }
+ if(!isAAF && !(operation.equals("createKeySpace"))) {
+ resultMap = CachingUtil.authenticateAIDUser(nameSpace, userId, password, keyspace);
+ if (!resultMap.isEmpty())
+ return resultMap;
+
+ }
+ if (isAAF && nameSpace != null && userId != null && password != null) {
+ boolean isValid = true;
+ try {
+ isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace);
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger,"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);
+ resultMap.put("Exception", e.getMessage());
+ }
+ if (!isValid) {
+ logger.error(EELFLoggerDelegate.errorLogger,"User not authenticated...", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ resultMap.put("Exception", "User not authenticated...");
+ }
+ if (!resultMap.isEmpty())
+ return resultMap;
+
+ }
+ } else {
+
+ String cachedKS = CachingUtil.getKSFromCadiCache(userId);
+ if(cachedKS != null && !cachedKS.equals(keyspace)) {
+ resultMap.put("Exception", "User not authenticated to access this keyspace...");
+ }
+ }
+
+ if (operation.equals("createKeySpace")) {
+ logger.info(EELFLoggerDelegate.applicationLogger,"AID is not provided. Creating new UUID for keyspace.");
+ PreparedQueryObject pQuery = new PreparedQueryObject();
+ pQuery.appendQueryString(
+ "select uuid from admin.keyspace_master where application_name=? and username=? and keyspace_name=? allow filtering");
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace));
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
+ MusicUtil.DEFAULTKEYSPACENAME));
+
+ try {
+ Row rs = MusicCore.get(pQuery).one();
+ uuid = rs.getUUID("uuid").toString();
+ resultMap.put("uuid", "existing");
+ } catch (Exception e) {
+ logger.info(EELFLoggerDelegate.applicationLogger,"No UUID found in DB. So creating new UUID.");
+ uuid = CachingUtil.generateUUID();
+ resultMap.put("uuid", "new");
+ }
+ resultMap.put("aid", uuid);
+ CachingUtil.updateCadiCache(userId, keyspace);
+ }
+
+ return resultMap;
+ }
+
+
+ public static boolean authenticateAdmin(String id,String password) {
+ String a = MusicUtil.getAdminId();
+ String b = MusicUtil.getAdminPass();
+ 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;
+
+ }
+
+}
diff --git a/src/main/java/org/onap/music/conductor/conditionals/JsonConditional.java b/src/main/java/org/onap/music/conductor/conditionals/JsonConditional.java
index 8cacc369..4efcabea 100644
--- a/src/main/java/org/onap/music/conductor/conditionals/JsonConditional.java
+++ b/src/main/java/org/onap/music/conductor/conditionals/JsonConditional.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.conductor.conditionals;
import java.io.Serializable;
@@ -32,58 +33,58 @@ import io.swagger.annotations.ApiModel;
@JsonIgnoreProperties(ignoreUnknown = true)
public class JsonConditional implements Serializable {
- private String primaryKey;
- private String primaryKeyValue;
- private String casscadeColumnName;
- private transient Map<String,Object> tableValues;
- private transient Map<String,Object> casscadeColumnData;
- private Map<String,Map<String,String>> conditions;
-
- public Map<String, Object> getTableValues() {
- return tableValues;
- }
- public void setTableValues(Map<String, Object> tableValues) {
- this.tableValues = tableValues;
- }
-
- public String getPrimaryKey() {
- return primaryKey;
- }
- public String getPrimaryKeyValue() {
- return primaryKeyValue;
- }
- public String getCasscadeColumnName() {
- return casscadeColumnName;
- }
+ private String primaryKey;
+ private String primaryKeyValue;
+ private String casscadeColumnName;
+ private Map<String,Object> tableValues;
+ private Map<String,Object> casscadeColumnData;
+ private Map<String,Map<String,String>> conditions;
+
+ public Map<String, Object> getTableValues() {
+ return tableValues;
+ }
+ public void setTableValues(Map<String, Object> tableValues) {
+ this.tableValues = tableValues;
+ }
+
+ public String getPrimaryKey() {
+ return primaryKey;
+ }
+ public String getPrimaryKeyValue() {
+ return primaryKeyValue;
+ }
+ public String getCasscadeColumnName() {
+ return casscadeColumnName;
+ }
- public Map<String, Object> getCasscadeColumnData() {
- return casscadeColumnData;
- }
+ public Map<String, Object> getCasscadeColumnData() {
+ return casscadeColumnData;
+ }
-
-
- public void setPrimaryKey(String primaryKey) {
- this.primaryKey = primaryKey;
- }
- public void setPrimaryKeyValue(String primaryKeyValue) {
- this.primaryKeyValue = primaryKeyValue;
- }
- public Map<String, Map<String, String>> getConditions() {
- return conditions;
- }
- public void setConditions(Map<String, Map<String, String>> conditions) {
- this.conditions = conditions;
- }
- public void setCasscadeColumnName(String casscadeColumnName) {
- this.casscadeColumnName = casscadeColumnName;
- }
+
+
+ public void setPrimaryKey(String primaryKey) {
+ this.primaryKey = primaryKey;
+ }
+ public void setPrimaryKeyValue(String primaryKeyValue) {
+ this.primaryKeyValue = primaryKeyValue;
+ }
+ public Map<String, Map<String, String>> getConditions() {
+ return conditions;
+ }
+ public void setConditions(Map<String, Map<String, String>> conditions) {
+ this.conditions = conditions;
+ }
+ public void setCasscadeColumnName(String casscadeColumnName) {
+ this.casscadeColumnName = casscadeColumnName;
+ }
- public void setCasscadeColumnData(Map<String, Object> casscadeColumnData) {
- this.casscadeColumnData = casscadeColumnData;
- }
+ public void setCasscadeColumnData(Map<String, Object> casscadeColumnData) {
+ this.casscadeColumnData = casscadeColumnData;
+ }
-
-
-
-
+
+
+
+
} \ No newline at end of file
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 d5e9e4d5..58409b71 100644
--- a/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java
+++ b/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.conductor.conditionals;
import java.io.PrintWriter;
@@ -27,17 +28,22 @@ import java.util.HashMap;
import java.util.Map;
import org.codehaus.jettison.json.JSONObject;
+import org.onap.music.datastore.MusicDataStoreHandle;
import org.onap.music.datastore.PreparedQueryObject;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.eelf.logging.format.AppMessages;
import org.onap.music.eelf.logging.format.ErrorSeverity;
import org.onap.music.eelf.logging.format.ErrorTypes;
-import org.onap.music.lockingservice.MusicLockState;
+import org.onap.music.exceptions.MusicLockingException;
+import org.onap.music.exceptions.MusicQueryException;
+import org.onap.music.exceptions.MusicServiceException;
+import org.onap.music.lockingservice.cassandra.MusicLockState;
import org.onap.music.main.MusicCore;
import org.onap.music.main.MusicUtil;
import org.onap.music.main.ResultType;
import org.onap.music.main.ReturnType;
import org.onap.music.rest.RestMusicDataAPI;
+import org.onap.music.service.impl.MusicZKCore;
import com.datastax.driver.core.ColumnDefinitions;
import com.datastax.driver.core.DataType;
@@ -46,226 +52,224 @@ import com.datastax.driver.core.Row;
import com.datastax.driver.core.TableMetadata;
public class MusicConditional {
- private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicDataAPI.class);
- private static final String CRITICAL = "critical";
-
- public static ReturnType conditionalInsert(String keyspace, String tablename, String casscadeColumnName,
- Map<String, Object> casscadeColumnData, String primaryKey, Map<String, Object> valuesMap,
- Map<String, String> status) throws Exception {
-
- Map<String, PreparedQueryObject> queryBank = new HashMap<>();
- TableMetadata tableInfo = null;
- tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
- DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType();
- String primaryId = tableInfo.getPrimaryKey().get(0).getName();
- DataType casscadeColumnType = tableInfo.getColumn(casscadeColumnName).getType();
- String vector = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
-
- PreparedQueryObject select = new PreparedQueryObject();
- select.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " where " + primaryId + " = ?");
- select.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKey));
- queryBank.put(MusicUtil.SELECT, select);
-
- PreparedQueryObject update = new PreparedQueryObject();
- Map<String, String> updateColumnvalues = new HashMap<>(); //casscade column values
- updateColumnvalues = getValues(true, casscadeColumnData, status);
- Object formatedValues = MusicUtil.convertToActualDataType(casscadeColumnType, updateColumnvalues);
- update.appendQueryString("UPDATE " + keyspace + "." + tablename + " SET " + casscadeColumnName + " ="
- + casscadeColumnName + " + ? , vector_ts = ?" + " WHERE " + primaryId + " = ? ");
- update.addValue(formatedValues);
- update.addValue(MusicUtil.convertToActualDataType(DataType.text(), vector));
- update.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKey));
- queryBank.put(MusicUtil.UPDATE, update);
-
-
- Map<String, String> insertColumnvalues = new HashMap<>();//casscade column values
- insertColumnvalues = getValues(false, casscadeColumnData, status);
- formatedValues = MusicUtil.convertToActualDataType(casscadeColumnType, insertColumnvalues);
- PreparedQueryObject insert = extractQuery(valuesMap, tableInfo, tablename, keyspace, primaryId, primaryKey,casscadeColumnName,formatedValues);
- queryBank.put(MusicUtil.INSERT, insert);
-
-
- String key = keyspace + "." + tablename + "." + primaryKey;
- String lockId = MusicCore.createLockReference(key);
- long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
- ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod);
-
- try {
- if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
- ReturnType criticalPutResult = conditionalInsertAtomic(lockId, keyspace, tablename, primaryKey,
- queryBank);
- MusicCore.destroyLockRef(lockId);
- if (criticalPutResult.getMessage().contains("insert"))
- criticalPutResult
- .setMessage("Insert values: ");
- else if (criticalPutResult.getMessage().contains("update"))
- criticalPutResult
- .setMessage("Update values: " + updateColumnvalues);
- return criticalPutResult;
-
- } else {
- MusicCore.destroyLockRef(lockId);
- return lockAcqResult;
- }
- } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED);
- MusicCore.destroyLockRef(lockId);
- return new ReturnType(ResultType.FAILURE, e.getMessage());
- }
-
- }
-
- public static ReturnType conditionalInsertAtomic(String lockId, String keyspace, String tableName,
- String primaryKey, Map<String, PreparedQueryObject> queryBank) {
-
- ResultSet results = null;
-
- try {
-
- MusicLockState mls = MusicCore.getLockingServiceHandle()
- .getLockState(keyspace + "." + tableName + "." + primaryKey);
- if (mls.getLockHolder().equals(lockId)) {
- try {
- results = MusicCore.getDSHandle().executeCriticalGet(queryBank.get(MusicUtil.SELECT));
- } catch (Exception e) {
- return new ReturnType(ResultType.FAILURE, e.getMessage());
- }
- if (results.all().isEmpty()) {
- MusicCore.getDSHandle().executePut(queryBank.get(MusicUtil.INSERT), CRITICAL);
- return new ReturnType(ResultType.SUCCESS, "insert");
- } else {
- MusicCore.getDSHandle().executePut(queryBank.get(MusicUtil.UPDATE), CRITICAL);
- return new ReturnType(ResultType.SUCCESS, "update");
- }
- } else {
- return new ReturnType(ResultType.FAILURE,
- "Cannot perform operation since you are the not the lock holder");
- }
-
- } catch (Exception e) {
- StringWriter sw = new StringWriter();
- e.printStackTrace(new PrintWriter(sw));
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
- String exceptionAsString = sw.toString();
- return new ReturnType(ResultType.FAILURE,
- "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
- + exceptionAsString);
- }
-
- }
-
- public static ReturnType update(Map<String,PreparedQueryObject> queryBank, String keyspace, String tableName, String primaryKey,String primaryKeyValue,String planId,String cascadeColumnName,Map<String,String> cascadeColumnValues) {
-
- String key = keyspace + "." + tableName + "." + primaryKeyValue;
- String lockId = MusicCore.createLockReference(key);
- long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
- ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod);
-
- try {
-
- if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
- return updateAtomic(lockId, keyspace, tableName, primaryKey,primaryKeyValue, queryBank,planId,cascadeColumnValues,cascadeColumnName);
-
- } else {
- MusicCore.destroyLockRef(lockId);
- return lockAcqResult;
- }
-
- } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
- MusicCore.destroyLockRef(lockId);
- return new ReturnType(ResultType.FAILURE, e.getMessage());
-
- }
- }
-
- public static ReturnType updateAtomic(String lockId, String keyspace, String tableName, String primaryKey,String primaryKeyValue,
- Map<String,PreparedQueryObject> queryBank,String planId,Map<String,String> cascadeColumnValues,String casscadeColumnName) {
- try {
-
- MusicLockState mls = MusicCore.getLockingServiceHandle()
- .getLockState(keyspace + "." + tableName + "." + primaryKeyValue);
- if (mls.getLockHolder().equals(lockId)) {
- Row row = MusicCore.getDSHandle().executeCriticalGet(queryBank.get(MusicUtil.SELECT)).one();
-
- if(row != null) {
- Map<String, String> updatedValues = cascadeColumnUpdateSpecific(row, cascadeColumnValues, casscadeColumnName, planId);
- JSONObject json = new JSONObject(updatedValues);
- PreparedQueryObject update = new PreparedQueryObject();
- String vector_ts = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
- update.appendQueryString("UPDATE " + keyspace + "." + tableName + " SET " + casscadeColumnName + "['" + planId
- + "'] = ?, vector_ts = ? WHERE " + primaryKey + " = ?");
- update.addValue(MusicUtil.convertToActualDataType(DataType.text(), json.toString()));
- update.addValue(MusicUtil.convertToActualDataType(DataType.text(), vector_ts));
- update.addValue(MusicUtil.convertToActualDataType(DataType.text(), primaryKeyValue));
- try {
- MusicCore.getDSHandle().executePut(update, CRITICAL);
- } catch (Exception ex) {
- return new ReturnType(ResultType.FAILURE, ex.getMessage());
- }
- }else {
- return new ReturnType(ResultType.FAILURE,"Cannot find data related to key: "+primaryKey);
- }
- MusicCore.getDSHandle().executePut(queryBank.get(MusicUtil.UPSERT), CRITICAL);
- return new ReturnType(ResultType.SUCCESS, "update success");
-
- } else {
- return new ReturnType(ResultType.FAILURE,
- "Cannot perform operation since you are the not the lock holder");
- }
-
- } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
- StringWriter sw = new StringWriter();
- e.printStackTrace(new PrintWriter(sw));
- String exceptionAsString = sw.toString();
- return new ReturnType(ResultType.FAILURE,
- "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
- + exceptionAsString);
- }
-
- }
-
- @SuppressWarnings("unchecked")
- public static Map<String, String> getValues(boolean isExists, Map<String, Object> casscadeColumnData,
- Map<String, String> status) {
-
- Map<String, String> value = new HashMap<>();
- Map<String, String> returnMap = new HashMap<>();
- Object key = casscadeColumnData.get("key");
- String setStatus = "";
- value = (Map<String, String>) casscadeColumnData.get("value");
-
- if (isExists)
- setStatus = status.get("exists");
- else
- setStatus = status.get("nonexists");
-
- value.put("status", setStatus);
- JSONObject valueJson = new JSONObject(value);
- returnMap.put(key.toString(), valueJson.toString());
- return returnMap;
-
- }
-
- public static PreparedQueryObject extractQuery(Map<String, Object> valuesMap, TableMetadata tableInfo, String tableName,
- String keySpaceName,String primaryKeyName,String primaryKey,String casscadeColumn,Object casscadeColumnValues) throws Exception {
-
- PreparedQueryObject queryObject = new PreparedQueryObject();
- StringBuilder fieldsString = new StringBuilder("(vector_ts"+",");
- StringBuilder valueString = new StringBuilder("(" + "?" + ",");
- String vector = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
- queryObject.addValue(vector);
- if(casscadeColumn!=null && casscadeColumnValues!=null) {
- fieldsString.append("" +casscadeColumn+" ," );
- valueString.append("?,");
- queryObject.addValue(casscadeColumnValues);
- }
-
- int counter = 0;
- for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicDataAPI.class);
+
+ public static ReturnType conditionalInsert(String keyspace, String tablename, String casscadeColumnName,
+ Map<String, Object> casscadeColumnData, String primaryKey, Map<String, Object> valuesMap,
+ Map<String, String> status) throws Exception {
+
+ Map<String, PreparedQueryObject> queryBank = new HashMap<>();
+ TableMetadata tableInfo = null;
+ tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
+ DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType();
+ String primaryId = tableInfo.getPrimaryKey().get(0).getName();
+ DataType casscadeColumnType = tableInfo.getColumn(casscadeColumnName).getType();
+ String vector = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
+
+ PreparedQueryObject select = new PreparedQueryObject();
+ select.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " where " + primaryId + " = ?");
+ select.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKey));
+ queryBank.put(MusicUtil.SELECT, select);
+
+ PreparedQueryObject update = new PreparedQueryObject();
+ Map<String, String> updateColumnvalues = new HashMap<>(); //casscade column values
+ updateColumnvalues = getValues(true, casscadeColumnData, status);
+ Object formatedValues = MusicUtil.convertToActualDataType(casscadeColumnType, updateColumnvalues);
+ update.appendQueryString("UPDATE " + keyspace + "." + tablename + " SET " + casscadeColumnName + " ="
+ + casscadeColumnName + " + ? , vector_ts = ?" + " WHERE " + primaryId + " = ? ");
+ update.addValue(formatedValues);
+ update.addValue(MusicUtil.convertToActualDataType(DataType.text(), vector));
+ update.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKey));
+ queryBank.put(MusicUtil.UPDATE, update);
+
+
+ Map<String, String> insertColumnvalues = new HashMap<>();//casscade column values
+ insertColumnvalues = getValues(false, casscadeColumnData, status);
+ formatedValues = MusicUtil.convertToActualDataType(casscadeColumnType, insertColumnvalues);
+ PreparedQueryObject insert = extractQuery(valuesMap, tableInfo, tablename, keyspace, primaryId, primaryKey,casscadeColumnName,formatedValues);
+ queryBank.put(MusicUtil.INSERT, insert);
+
+
+ String key = keyspace + "." + tablename + "." + primaryKey;
+ String lockId = MusicCore.createLockReference(key);
+ long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
+ ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod);
+
+ try {
+ if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
+ ReturnType criticalPutResult = conditionalInsertAtomic(lockId, keyspace, tablename, primaryKey,
+ queryBank);
+ MusicCore.destroyLockRef(lockId);
+ if (criticalPutResult.getMessage().contains("insert"))
+ criticalPutResult
+ .setMessage("Insert values: ");
+ else if (criticalPutResult.getMessage().contains("update"))
+ criticalPutResult
+ .setMessage("Update values: " + updateColumnvalues);
+ return criticalPutResult;
+
+ } else {
+ MusicCore.destroyLockRef(lockId);
+ return lockAcqResult;
+ }
+ } catch (Exception e) {
+ MusicCore.destroyLockRef(lockId);
+ return new ReturnType(ResultType.FAILURE, e.getMessage());
+ }
+
+ }
+
+ public static ReturnType conditionalInsertAtomic(String lockId, String keyspace, String tableName,
+ String primaryKey, Map<String, PreparedQueryObject> queryBank) {
+
+ ResultSet results = null;
+
+ try {
+ String fullyQualifiedKey = keyspace + "." + tableName + "." + primaryKey;
+ ReturnType lockAcqResult = MusicCore.acquireLock(fullyQualifiedKey, lockId);
+ //MusicLockState mls = MusicZKCore.getLockingServiceHandle()
+ //.getLockState(keyspace + "." + tableName + "." + primaryKey);
+ if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
+ try {
+ results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryBank.get(MusicUtil.SELECT));
+ } catch (Exception e) {
+ return new ReturnType(ResultType.FAILURE, e.getMessage());
+ }
+ if (results.all().isEmpty()) {
+ MusicDataStoreHandle.getDSHandle().executePut(queryBank.get(MusicUtil.INSERT), "critical");
+ return new ReturnType(ResultType.SUCCESS, "insert");
+ } else {
+ MusicDataStoreHandle.getDSHandle().executePut(queryBank.get(MusicUtil.UPDATE), "critical");
+ return new ReturnType(ResultType.SUCCESS, "update");
+ }
+ } else {
+ return new ReturnType(ResultType.FAILURE,
+ "Cannot perform operation since you are the not the lock holder");
+ }
+
+ } catch (Exception e) {
+ StringWriter sw = new StringWriter();
+ e.printStackTrace(new PrintWriter(sw));
+ String exceptionAsString = sw.toString();
+ return new ReturnType(ResultType.FAILURE,
+ "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
+ + exceptionAsString);
+ }
+
+ }
+
+ public static ReturnType update(Map<String,PreparedQueryObject> queryBank, String keyspace, String tableName, String primaryKey,String primaryKeyValue,String planId,String cascadeColumnName,Map<String,String> cascadeColumnValues) throws MusicLockingException, MusicQueryException, MusicServiceException {
+
+ String key = keyspace + "." + tableName + "." + primaryKeyValue;
+ String lockId = MusicCore.createLockReference(key);
+ long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
+ ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod);
+
+ try {
+
+ if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
+ ReturnType criticalPutResult = updateAtomic(lockId, keyspace, tableName, primaryKey,primaryKeyValue, queryBank,planId,cascadeColumnValues,cascadeColumnName);
+ MusicCore.destroyLockRef(lockId);
+ return criticalPutResult;
+ } else {
+ MusicCore.destroyLockRef(lockId);
+ return lockAcqResult;
+ }
+
+ } catch (Exception e) {
+ MusicCore.destroyLockRef(lockId);
+ return new ReturnType(ResultType.FAILURE, e.getMessage());
+
+ }
+ }
+
+ public static ReturnType updateAtomic(String lockId, String keyspace, String tableName, String primaryKey,String primaryKeyValue,
+ Map<String,PreparedQueryObject> queryBank,String planId,Map<String,String> cascadeColumnValues,String casscadeColumnName) {
+ try {
+ String fullyQualifiedKey = keyspace + "." + tableName + "." + primaryKeyValue;
+ ReturnType lockAcqResult = MusicCore.acquireLock(fullyQualifiedKey, lockId);
+ //MusicLockState mls = MusicZKCore.getLockingServiceHandle()
+ //.getLockState(keyspace + "." + tableName + "." + primaryKeyValue);
+ if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
+ Row row = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryBank.get(MusicUtil.SELECT)).one();
+
+ if(row != null) {
+ Map<String, String> updatedValues = cascadeColumnUpdateSpecific(row, cascadeColumnValues, casscadeColumnName, planId);
+ JSONObject json = new JSONObject(updatedValues);
+ PreparedQueryObject update = new PreparedQueryObject();
+ String vector_ts = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
+ update.appendQueryString("UPDATE " + keyspace + "." + tableName + " SET " + casscadeColumnName + "['" + planId
+ + "'] = ?, vector_ts = ? WHERE " + primaryKey + " = ?");
+ update.addValue(MusicUtil.convertToActualDataType(DataType.text(), json.toString()));
+ update.addValue(MusicUtil.convertToActualDataType(DataType.text(), vector_ts));
+ update.addValue(MusicUtil.convertToActualDataType(DataType.text(), primaryKeyValue));
+ try {
+ MusicDataStoreHandle.getDSHandle().executePut(update, "critical");
+ } catch (Exception ex) {
+ return new ReturnType(ResultType.FAILURE, ex.getMessage());
+ }
+ }else {
+ return new ReturnType(ResultType.FAILURE,"Cannot find data related to key: "+primaryKey);
+ }
+ MusicDataStoreHandle.getDSHandle().executePut(queryBank.get(MusicUtil.UPSERT), "critical");
+ return new ReturnType(ResultType.SUCCESS, "update success");
+
+ } else {
+ return new ReturnType(ResultType.FAILURE,
+ "Cannot perform operation since you are the not the lock holder");
+ }
+
+ } catch (Exception e) {
+ StringWriter sw = new StringWriter();
+ e.printStackTrace(new PrintWriter(sw));
+ String exceptionAsString = sw.toString();
+ return new ReturnType(ResultType.FAILURE,
+ "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
+ + exceptionAsString);
+ }
+
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Map<String, String> getValues(boolean isExists, Map<String, Object> casscadeColumnData,
+ Map<String, String> status) {
+
+ Map<String, String> value = new HashMap<>();
+ Map<String, String> returnMap = new HashMap<>();
+ Object key = casscadeColumnData.get("key");
+ String setStatus = "";
+ value = (Map<String, String>) casscadeColumnData.get("value");
+
+ if (isExists)
+ setStatus = status.get("exists");
+ else
+ setStatus = status.get("nonexists");
+
+ value.put("status", setStatus);
+ JSONObject valueJson = new JSONObject(value);
+ returnMap.put(key.toString(), valueJson.toString());
+ return returnMap;
+
+ }
+
+ public static PreparedQueryObject extractQuery(Map<String, Object> valuesMap, TableMetadata tableInfo, String tableName,
+ String keySpaceName,String primaryKeyName,String primaryKey,String casscadeColumn,Object casscadeColumnValues) throws Exception {
+
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ StringBuilder fieldsString = new StringBuilder("(vector_ts"+",");
+ StringBuilder valueString = new StringBuilder("(" + "?" + ",");
+ String vector = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
+ queryObject.addValue(vector);
+ if(casscadeColumn!=null && casscadeColumnValues!=null) {
+ fieldsString.append("" +casscadeColumn+" ," );
+ valueString.append("?,");
+ queryObject.addValue(casscadeColumnValues);
+ }
+
+ int counter = 0;
+ for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
- fieldsString.append("" + entry.getKey());
+ fieldsString.append("" + entry.getKey());
Object valueObj = entry.getValue();
if (primaryKeyName.equals(entry.getKey())) {
primaryKey = entry.getValue() + "";
@@ -286,11 +290,11 @@ public class MusicConditional {
logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
}
- valueString.append("?");
+ valueString.append("?");
queryObject.addValue(formattedValue);
- if (counter == valuesMap.size() - 1) {
+ if (counter == valuesMap.size() - 1) {
fieldsString.append(")");
valueString.append(")");
} else {
@@ -301,58 +305,58 @@ public class MusicConditional {
}
queryObject.appendQueryString("INSERT INTO " + keySpaceName + "." + tableName + " "
+ fieldsString + " VALUES " + valueString);
- return queryObject;
- }
-
- public static Object getColValue(Row row, String colName, DataType colType) {
- switch (colType.getName()) {
- case VARCHAR:
- return row.getString(colName);
- case UUID:
- return row.getUUID(colName);
- case VARINT:
- return row.getVarint(colName);
- case BIGINT:
- return row.getLong(colName);
- case INT:
- return row.getInt(colName);
- case FLOAT:
- return row.getFloat(colName);
- case DOUBLE:
- return row.getDouble(colName);
- case BOOLEAN:
- return row.getBool(colName);
- case MAP:
- return row.getMap(colName, String.class, String.class);
- default:
- return null;
- }
- }
-
- @SuppressWarnings("unchecked")
- public static Map<String, String> cascadeColumnUpdateSpecific(Row row, Map<String, String> changeOfStatus,
- String cascadeColumnName, String planId) {
-
- ColumnDefinitions colInfo = row.getColumnDefinitions();
- DataType colType = colInfo.getType(cascadeColumnName);
- Map<String, String> values = new HashMap<>();
- Object columnValue = getColValue(row, cascadeColumnName, colType);
-
- Map<String, String> finalValues = new HashMap<>();
- values = (Map<String, String>) columnValue;
- if (values != null && values.keySet().contains(planId)) {
- String valueString = values.get(planId);
- String tempValueString = valueString.replaceAll("\\{", "").replaceAll("\"", "").replaceAll("\\}", "");
- String[] elements = tempValueString.split(",");
- for (String str : elements) {
- String[] keyValue = str.split(":");
- if ((changeOfStatus.keySet().contains(keyValue[0].replaceAll("\\s", ""))))
- keyValue[1] = changeOfStatus.get(keyValue[0].replaceAll("\\s", ""));
- finalValues.put(keyValue[0], keyValue[1]);
- }
- }
- return finalValues;
-
- }
+ return queryObject;
+ }
+
+ public static Object getColValue(Row row, String colName, DataType colType) {
+ switch (colType.getName()) {
+ case VARCHAR:
+ return row.getString(colName);
+ case UUID:
+ return row.getUUID(colName);
+ case VARINT:
+ return row.getVarint(colName);
+ case BIGINT:
+ return row.getLong(colName);
+ case INT:
+ return row.getInt(colName);
+ case FLOAT:
+ return row.getFloat(colName);
+ case DOUBLE:
+ return row.getDouble(colName);
+ case BOOLEAN:
+ return row.getBool(colName);
+ case MAP:
+ return row.getMap(colName, String.class, String.class);
+ default:
+ return null;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Map<String, String> cascadeColumnUpdateSpecific(Row row, Map<String, String> changeOfStatus,
+ String cascadeColumnName, String planId) {
+
+ ColumnDefinitions colInfo = row.getColumnDefinitions();
+ DataType colType = colInfo.getType(cascadeColumnName);
+ Map<String, String> values = new HashMap<>();
+ Object columnValue = getColValue(row, cascadeColumnName, colType);
+
+ Map<String, String> finalValues = new HashMap<>();
+ values = (Map<String, String>) columnValue;
+ if (values != null && values.keySet().contains(planId)) {
+ String valueString = values.get(planId);
+ String tempValueString = valueString.replaceAll("\\{", "").replaceAll("\"", "").replaceAll("\\}", "");
+ String[] elements = tempValueString.split(",");
+ for (String str : elements) {
+ String[] keyValue = str.split(":");
+ if ((changeOfStatus.keySet().contains(keyValue[0].replaceAll("\\s", ""))))
+ keyValue[1] = changeOfStatus.get(keyValue[0].replaceAll("\\s", ""));
+ finalValues.put(keyValue[0], keyValue[1]);
+ }
+ }
+ return finalValues;
+
+ }
}
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 1343628c..2a267387 100644
--- a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java
+++ b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java
@@ -21,6 +21,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.conductor.conditionals;
import java.util.HashMap;
@@ -39,17 +40,21 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
+import org.codehaus.jettison.json.JSONObject;
+import org.onap.music.datastore.MusicDataStoreHandle;
import org.onap.music.datastore.PreparedQueryObject;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.eelf.logging.format.AppMessages;
import org.onap.music.eelf.logging.format.ErrorSeverity;
import org.onap.music.eelf.logging.format.ErrorTypes;
-import org.onap.music.main.MusicCore;
import org.onap.music.main.MusicUtil;
import org.onap.music.main.ResultType;
import org.onap.music.main.ReturnType;
import org.onap.music.response.jsonobjects.JsonResponse;
import org.onap.music.rest.RestMusicAdminAPI;
+import org.onap.music.service.impl.MusicZKCore;
+import org.onap.music.authentication.MusicAuthentication;
+import org.onap.music.conductor.*;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.TableMetadata;
@@ -60,160 +65,160 @@ import io.swagger.annotations.ApiParam;
@Path("/v2/conditional")
@Api(value = "Conditional Api", hidden = true)
public class RestMusicConditionalAPI {
- private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicAdminAPI.class);
- private static final String XMINORVERSION = "X-minorVersion";
- private static final String XPATCHVERSION = "X-patchVersion";
- private static final String NS = "ns";
- private static final String VERSION = "v2";
-
- @POST
- @Path("/insert/keyspaces/{keyspace}/tables/{tablename}")
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response insertConditional(
- @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
- @ApiParam(value = "Minor Version", required = false) @HeaderParam(XMINORVERSION) String minorVersion,
- @ApiParam(value = "Patch Version", required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @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,
- JsonConditional jsonObj) throws Exception {
- ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
- String primaryKey = jsonObj.getPrimaryKey();
- String primaryKeyValue = jsonObj.getPrimaryKeyValue();
- String casscadeColumnName = jsonObj.getCasscadeColumnName();
- Map<String, Object> tableValues = jsonObj.getTableValues();
- Map<String, Object> casscadeColumnData = jsonObj.getCasscadeColumnData();
- Map<String, Map<String, String>> conditions = jsonObj.getConditions();
-
- if (primaryKey == null || primaryKeyValue == null || casscadeColumnName == null || tableValues.isEmpty()
- || casscadeColumnData.isEmpty() || conditions.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL,
- ErrorTypes.AUTHENTICATIONERROR);
- return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE)
- .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 = MusicCore.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()) {
- valuesMap.put(entry.getKey(), entry.getValue());
- }
-
- Map<String, String> status = new HashMap<>();
- status.put("exists", conditions.get("exists").get("status"));
- status.put("nonexists", conditions.get("nonexists").get("status"));
- ReturnType out = null;
-
- out = MusicConditional.conditionalInsert(keyspace, tablename, casscadeColumnName, casscadeColumnData,
- primaryKeyValue, valuesMap, status);
- return response.status(Status.OK).entity(new JsonResponse(out.getResult()).setMessage(out.getMessage()).toMap())
- .build();
-
- }
-
- @SuppressWarnings("unchecked")
- @PUT
- @Path("/update/keyspaces/{keyspace}/tables/{tablename}")
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response updateConditional(
- @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
- @ApiParam(value = "Minor Version", required = false) @HeaderParam(XMINORVERSION) String minorVersion,
- @ApiParam(value = "Patch Version", required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @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,
- JsonConditional upObj) throws Exception {
- ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- String primaryKey = upObj.getPrimaryKey();
- String primaryKeyValue = upObj.getPrimaryKeyValue();
- String casscadeColumnName = upObj.getCasscadeColumnName();
- Map<String, Object> casscadeColumnData = upObj.getCasscadeColumnData();
- Map<String, Object> tableValues = upObj.getTableValues();
-
- if (primaryKey == null || primaryKeyValue == null || casscadeColumnName == null
- || casscadeColumnData.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL,
- ErrorTypes.AUTHENTICATIONERROR);
- return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE)
- .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 = MusicCore.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");
- TableMetadata tableInfo = null;
- tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
- DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType();
- String primaryId = tableInfo.getPrimaryKey().get(0).getName();
-
- PreparedQueryObject select = new PreparedQueryObject();
- select.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " where " + primaryId + " = ?");
- select.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKeyValue));
-
- PreparedQueryObject upsert = MusicConditional.extractQuery(tableValues, tableInfo, tablename, keyspace, primaryKey, primaryKeyValue, null, null);
- Map<String,PreparedQueryObject> queryBank = new HashMap<>();
- queryBank.put(MusicUtil.SELECT, select);
- queryBank.put(MusicUtil.UPSERT, upsert);
- ReturnType result = MusicConditional.update(queryBank, keyspace, tablename, primaryKey,primaryKeyValue,planId,casscadeColumnName,casscadeColumnValueMap);
- if (result.getResult() == ResultType.SUCCESS) {
- return response.status(Status.OK)
- .entity(new JsonResponse(result.getResult()).setMessage(result.getMessage()).toMap()).build();
-
- }
- return response.status(Status.BAD_REQUEST)
- .entity(new JsonResponse(result.getResult()).setMessage(result.getMessage()).toMap()).build();
-
- }
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicAdminAPI.class);
+ private static final String XMINORVERSION = "X-minorVersion";
+ private static final String XPATCHVERSION = "X-patchVersion";
+ private static final String NS = "ns";
+ private static final String VERSION = "v2";
+
+ @POST
+ @Path("/insert/keyspaces/{keyspace}/tables/{tablename}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response insertConditional(
+ @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
+ @ApiParam(value = "Minor Version", required = false) @HeaderParam(XMINORVERSION) String minorVersion,
+ @ApiParam(value = "Patch Version", required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
+ @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,
+ JsonConditional jsonObj) throws Exception {
+ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
+ String primaryKey = jsonObj.getPrimaryKey();
+ String primaryKeyValue = jsonObj.getPrimaryKeyValue();
+ String casscadeColumnName = jsonObj.getCasscadeColumnName();
+ Map<String, Object> tableValues = jsonObj.getTableValues();
+ Map<String, Object> casscadeColumnData = jsonObj.getCasscadeColumnData();
+ Map<String, Map<String, String>> conditions = jsonObj.getConditions();
+
+ if (primaryKey == null || primaryKeyValue == null || casscadeColumnName == null || tableValues.isEmpty()
+ || casscadeColumnData.isEmpty() || conditions.isEmpty()) {
+ logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL,
+ ErrorTypes.AUTHENTICATIONERROR);
+ return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE)
+ .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()) {
+ valuesMap.put(entry.getKey(), entry.getValue());
+ }
+
+ Map<String, String> status = new HashMap<>();
+ status.put("exists", conditions.get("exists").get("status"));
+ status.put("nonexists", conditions.get("nonexists").get("status"));
+ ReturnType out = null;
+
+ out = MusicConditional.conditionalInsert(keyspace, tablename, casscadeColumnName, casscadeColumnData,
+ primaryKeyValue, valuesMap, status);
+ return response.status(Status.OK).entity(new JsonResponse(out.getResult()).setMessage(out.getMessage()).toMap())
+ .build();
+
+ }
+
+ @SuppressWarnings("unchecked")
+ @PUT
+ @Path("/update/keyspaces/{keyspace}/tables/{tablename}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updateConditional(
+ @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
+ @ApiParam(value = "Minor Version", required = false) @HeaderParam(XMINORVERSION) String minorVersion,
+ @ApiParam(value = "Patch Version", required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
+ @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,
+ JsonConditional upObj) throws Exception {
+ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
+
+ String primaryKey = upObj.getPrimaryKey();
+ String primaryKeyValue = upObj.getPrimaryKeyValue();
+ String casscadeColumnName = upObj.getCasscadeColumnName();
+ Map<String, Object> casscadeColumnData = upObj.getCasscadeColumnData();
+ Map<String, Object> tableValues = upObj.getTableValues();
+
+ if (primaryKey == null || primaryKeyValue == null || casscadeColumnName == null
+ || casscadeColumnData.isEmpty()) {
+ logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL,
+ ErrorTypes.AUTHENTICATIONERROR);
+ return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE)
+ .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");
+ TableMetadata tableInfo = null;
+ tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
+ DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType();
+ String primaryId = tableInfo.getPrimaryKey().get(0).getName();
+
+ PreparedQueryObject select = new PreparedQueryObject();
+ select.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " where " + primaryId + " = ?");
+ select.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKeyValue));
+
+ PreparedQueryObject upsert = MusicConditional.extractQuery(tableValues, tableInfo, tablename, keyspace, primaryKey, primaryKeyValue, null, null);
+ Map<String,PreparedQueryObject> queryBank = new HashMap<>();
+ queryBank.put(MusicUtil.SELECT, select);
+ queryBank.put(MusicUtil.UPSERT, upsert);
+ ReturnType result = MusicConditional.update(queryBank, keyspace, tablename, primaryKey,primaryKeyValue,planId,casscadeColumnName,casscadeColumnValueMap);
+ if (result.getResult() == ResultType.SUCCESS) {
+ return response.status(Status.OK)
+ .entity(new JsonResponse(result.getResult()).setMessage(result.getMessage()).toMap()).build();
+
+ }
+ return response.status(Status.BAD_REQUEST)
+ .entity(new JsonResponse(result.getResult()).setMessage(result.getMessage()).toMap()).build();
+
+ }
} \ No newline at end of file
diff --git a/src/main/java/org/onap/music/datastore/Condition.java b/src/main/java/org/onap/music/datastore/Condition.java
new file mode 100644
index 00000000..2fd4596a
--- /dev/null
+++ b/src/main/java/org/onap/music/datastore/Condition.java
@@ -0,0 +1,52 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.datastore;
+import java.util.Map;
+
+import org.onap.music.main.MusicCore;
+
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+
+public class Condition {
+ Map<String, Object> conditions;
+ PreparedQueryObject selectQueryForTheRow;
+
+ public Condition(Map<String, Object> conditions, PreparedQueryObject selectQueryForTheRow) {
+ this.conditions = conditions;
+ this.selectQueryForTheRow = selectQueryForTheRow;
+ }
+
+ public boolean testCondition() throws Exception {
+ // first generate the row
+ ResultSet results = MusicCore.quorumGet(selectQueryForTheRow);
+ Row row = null;
+ if(results != null) {
+ row = results.one();
+ }
+ if(row == null) {
+ throw new Exception(" No data found to update");
+ }
+ return MusicDataStoreHandle.getDSHandle().doesRowSatisfyCondition(row, conditions);
+ }
+ } \ No newline at end of file
diff --git a/src/main/java/org/onap/music/datastore/MusicDataStore.java b/src/main/java/org/onap/music/datastore/MusicDataStore.java
index c6b022cc..bb8e537e 100644..100755
--- a/src/main/java/org/onap/music/datastore/MusicDataStore.java
+++ b/src/main/java/org/onap/music/datastore/MusicDataStore.java
@@ -9,18 +9,19 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- *
+ *
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore;
import java.net.InetAddress;
@@ -33,14 +34,17 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
+import org.apache.commons.jcs.access.CacheAccess;
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;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ColumnDefinitions;
import com.datastax.driver.core.ColumnDefinitions.Definition;
@@ -54,10 +58,12 @@ import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
+import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.TableMetadata;
import com.datastax.driver.core.exceptions.AlreadyExistsException;
import com.datastax.driver.core.exceptions.InvalidQueryException;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
+import com.sun.jersey.core.util.Base64;
/**
* @author nelson24
@@ -65,10 +71,33 @@ import com.datastax.driver.core.exceptions.NoHostAvailableException;
*/
public class MusicDataStore {
+ public static final String CONSISTENCY_LEVEL_ONE = "ONE";
+ public static final String CONSISTENCY_LEVEL_QUORUM = "QUORUM";
private Session session;
private Cluster cluster;
+ /**
+ * @param session
+ */
+ public void setSession(Session session) {
+ this.session = session;
+ }
+
+ /**
+ * @param session
+ */
+ public Session getSession() {
+ return session;
+ }
+
+ /**
+ * @param cluster
+ */
+ public void setCluster(Cluster cluster) {
+ this.cluster = cluster;
+ }
+
private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStore.class);
@@ -103,31 +132,8 @@ public class MusicDataStore {
}
}
-
-
/**
- * @param session
- */
- public void setSession(Session session) {
- this.session = session;
- }
-
- /**
- * @param session
- */
- public Session getSession() {
- return session;
- }
-
- /**
- * @param cluster
- */
- public void setCluster(Cluster cluster) {
- this.cluster = cluster;
- }
-
- /**
- *
+ *
* @return
*/
private ArrayList<String> getAllPossibleLocalIps() {
@@ -147,7 +153,7 @@ public class MusicDataStore {
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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR);
}
return allPossibleIps;
}
@@ -157,12 +163,12 @@ public class MusicDataStore {
* clusters.
*/
private void connectToCassaCluster() {
- Iterator<String> it = getAllPossibleLocalIps().iterator();
+ Iterator<String> it = getAllPossibleLocalIps().iterator();
String address = "localhost";
String[] addresses = null;
address = MusicUtil.getMyCassaHost();
- addresses = address.split(",");
-
+ addresses = address.split(",");
+
logger.info(EELFLoggerDelegate.applicationLogger,
"Connecting to cassa cluster: Iterating through possible ips:"
+ getAllPossibleLocalIps());
@@ -172,20 +178,21 @@ public class MusicDataStore {
.setConnectionsPerHost(HostDistance.REMOTE, 2, 4);
while (it.hasNext()) {
try {
- if(MusicUtil.getCassName() != null && MusicUtil.getCassPwd() != null) {
- logger.info(EELFLoggerDelegate.applicationLogger,
- "Building with credentials "+MusicUtil.getCassName()+" & "+MusicUtil.getCassPwd());
- cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
- .withCredentials(MusicUtil.getCassName(), MusicUtil.getCassPwd())
- //.withLoadBalancingPolicy(new RoundRobinPolicy())
- .withPoolingOptions(poolingOptions)
- .addContactPoints(addresses).build();
- }
- else
- cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
- //.withLoadBalancingPolicy(new RoundRobinPolicy())
- .addContactPoints(addresses).build();
-
+ if(MusicUtil.getCassName() != null && MusicUtil.getCassPwd() != null) {
+ logger.info(EELFLoggerDelegate.applicationLogger,
+ "Building with credentials "+MusicUtil.getCassName()+" & "+MusicUtil.getCassPwd());
+ cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
+ .withCredentials(MusicUtil.getCassName(), MusicUtil.getCassPwd())
+ //.withLoadBalancingPolicy(new RoundRobinPolicy())
+ .withoutJMXReporting()
+ .withPoolingOptions(poolingOptions)
+ .addContactPoints(addresses).build();
+ }
+ else
+ cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
+ //.withLoadBalancingPolicy(new RoundRobinPolicy())
+ .addContactPoints(addresses).build();
+
Metadata metadata = cluster.getMetadata();
logger.info(EELFLoggerDelegate.applicationLogger, "Connected to cassa cluster "
+ metadata.getClusterName() + " at " + address);
@@ -201,7 +208,7 @@ public class MusicDataStore {
}
/**
- *
+ *
*/
public void close() {
session.close();
@@ -209,31 +216,41 @@ public class MusicDataStore {
/**
* This method connects to cassandra cluster on specific address.
- *
+ *
* @param address
*/
private void connectToCassaCluster(String address) throws MusicServiceException {
- String[] addresses = null;
- addresses = address.split(",");
- PoolingOptions poolingOptions = new PoolingOptions();
+ String[] addresses = null;
+ addresses = address.split(",");
+ PoolingOptions poolingOptions = new PoolingOptions();
poolingOptions
.setConnectionsPerHost(HostDistance.LOCAL, 4, 10)
.setConnectionsPerHost(HostDistance.REMOTE, 2, 4);
if(MusicUtil.getCassName() != null && MusicUtil.getCassPwd() != null) {
- logger.info(EELFLoggerDelegate.applicationLogger,
- "Building with credentials "+MusicUtil.getCassName()+" & "+MusicUtil.getCassPwd());
- cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
- .withCredentials(MusicUtil.getCassName(), MusicUtil.getCassPwd())
- //.withLoadBalancingPolicy(new RoundRobinPolicy())
- .withPoolingOptions(poolingOptions)
- .addContactPoints(addresses).build();
+ logger.info(EELFLoggerDelegate.applicationLogger,
+ "Building with credentials "+MusicUtil.getCassName()+" & "+MusicUtil.getCassPwd());
+ cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
+ .withCredentials(MusicUtil.getCassName(), MusicUtil.getCassPwd())
+ //.withLoadBalancingPolicy(new RoundRobinPolicy())
+ .withoutJMXReporting()
+ .withPoolingOptions(poolingOptions)
+ .addContactPoints(addresses).build();
}
else {
- cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
- //.withLoadBalancingPolicy(new RoundRobinPolicy())
- .withPoolingOptions(poolingOptions)
- .addContactPoints(addresses).build();
+ cluster = Cluster.builder().withPort(MusicUtil.getCassandraPort())
+ //.withLoadBalancingPolicy(new RoundRobinPolicy())
+ .withoutJMXReporting()
+ .withPoolingOptions(poolingOptions)
+ .addContactPoints(addresses).build();
}
+
+ // JmxReporter reporter =
+ // JmxReporter.forRegistry(cluster.getMetrics().getRegistry())
+ // .inDomain(cluster.getClusterName() + "-metrics")
+ // .build();
+
+ // reporter.start();
+
Metadata metadata = cluster.getMetadata();
logger.info(EELFLoggerDelegate.applicationLogger, "Connected to cassa cluster "
+ metadata.getClusterName() + " at " + address);
@@ -248,7 +265,7 @@ public class MusicDataStore {
}
/**
- *
+ *
* @param keyspace
* @param tableName
* @param columnName
@@ -262,7 +279,7 @@ public class MusicDataStore {
}
/**
- *
+ *
* @param keyspace
* @param tableName
* @return TableMetadata
@@ -275,7 +292,7 @@ public class MusicDataStore {
/**
* Utility function to return the Java specific object type.
- *
+ *
* @param row
* @param colName
* @param colType
@@ -303,15 +320,15 @@ public class MusicDataStore {
case MAP:
return row.getMap(colName, String.class, String.class);
case LIST:
- return row.getList(colName, String.class);
+ return row.getList(colName, String.class);
default:
return null;
}
}
-
+
public byte[] getBlobValue(Row row, String colName, DataType colType) {
- ByteBuffer bb = row.getBytes(colName);
- return bb.array();
+ ByteBuffer bb = row.getBytes(colName);
+ return bb.array();
}
public boolean doesRowSatisfyCondition(Row row, Map<String, Object> condition) throws Exception {
@@ -322,7 +339,7 @@ public class MusicDataStore {
DataType colType = colInfo.getType(colName);
Object columnValue = getColValue(row, colName, colType);
Object conditionValue = MusicUtil.convertToActualDataType(colType, entry.getValue());
- if (!columnValue.equals(conditionValue))
+ if (columnValue.equals(conditionValue) == false)
return false;
}
return true;
@@ -330,7 +347,7 @@ public class MusicDataStore {
/**
* Utility function to store ResultSet values in to a MAP for output.
- *
+ *
* @param results
* @return MAP
*/
@@ -343,12 +360,12 @@ public class MusicDataStore {
HashMap<String, Object> resultOutput = new HashMap<>();
for (Definition definition : colInfo) {
if (!(("vector_ts").equals(definition.getName()))) {
- if(definition.getType().toString().toLowerCase().contains("blob")) {
- resultOutput.put(definition.getName(),
+ if(definition.getType().toString().toLowerCase().contains("blob")) {
+ resultOutput.put(definition.getName(),
getBlobValue(row, definition.getName(), definition.getType()));
- }
- else
- resultOutput.put(definition.getName(),
+ }
+ else
+ resultOutput.put(definition.getName(),
getColValue(row, definition.getName(), definition.getType()));
}
}
@@ -360,9 +377,14 @@ public class MusicDataStore {
// Prepared Statements 1802 additions
+
+ public boolean executePut(PreparedQueryObject queryObject, String consistency)
+ throws MusicServiceException, MusicQueryException {
+ return executePut(queryObject, consistency, 0);
+ }
/**
* This Method performs DDL and DML operations on Cassandra using specified consistency level
- *
+ *
* @param queryObject Object containing cassandra prepared query and values.
* @param consistency Specify consistency level for data synchronization across cassandra
* replicas
@@ -370,13 +392,13 @@ public class MusicDataStore {
* @throws MusicServiceException
* @throws MusicQueryException
*/
- public boolean executePut(PreparedQueryObject queryObject, String consistency)
+ public boolean executePut(PreparedQueryObject queryObject, String consistency,long timeSlot)
throws MusicServiceException, MusicQueryException {
boolean result = false;
-
+ long timeOfWrite = System.currentTimeMillis();
if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) {
- logger.error(EELFLoggerDelegate.errorLogger, queryObject.getQuery(),AppMessages.QUERYERROR, ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ logger.error(EELFLoggerDelegate.errorLogger, queryObject.getQuery(),AppMessages.QUERYERROR, ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
throw new MusicQueryException("Ill formed queryObject for the request = " + "["
+ queryObject.getQuery() + "]");
}
@@ -384,43 +406,59 @@ public class MusicDataStore {
"In preprared Execute Put: the actual insert query:"
+ queryObject.getQuery() + "; the values"
+ queryObject.getValues());
+/*<<<<<<< HEAD
PreparedStatement preparedInsert = null;
try {
-
- preparedInsert = session.prepare(queryObject.getQuery());
-
+
+ preparedInsert = session.prepare(queryObject.getQuery());
+
} catch(InvalidQueryException iqe) {
logger.error("Exception", iqe);
- logger.error(EELFLoggerDelegate.errorLogger, iqe.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
- throw new MusicQueryException(iqe.getMessage());
+ logger.error(EELFLoggerDelegate.errorLogger, iqe.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
+ throw new MusicQueryException(iqe.getMessage());
}catch(Exception e) {
logger.error("Exception", e);
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
- throw new MusicQueryException(e.getMessage());
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.QUERYERROR, ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
+ throw new MusicQueryException(e.getMessage());
}
+=======*/
+ SimpleStatement preparedInsert = null;
+
try {
+ preparedInsert = new SimpleStatement(queryObject.getQuery(), queryObject.getValues().toArray());
if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) {
logger.info(EELFLoggerDelegate.applicationLogger, "Executing critical put query");
preparedInsert.setConsistencyLevel(ConsistencyLevel.QUORUM);
} else if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) {
logger.info(EELFLoggerDelegate.applicationLogger, "Executing simple put query");
+ if(queryObject.getConsistency() == null)
+ preparedInsert.setConsistencyLevel(ConsistencyLevel.ONE);
+ else
+ preparedInsert.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency()));
+ } else if (consistency.equalsIgnoreCase(MusicUtil.ONE)) {
preparedInsert.setConsistencyLevel(ConsistencyLevel.ONE);
+ } else if (consistency.equalsIgnoreCase(MusicUtil.QUORUM)) {
+ preparedInsert.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
+ } else if (consistency.equalsIgnoreCase(MusicUtil.ALL)) {
+ preparedInsert.setConsistencyLevel(ConsistencyLevel.ALL);
}
+ long timestamp = MusicUtil.v2sTimeStampInMicroseconds(timeSlot, timeOfWrite);
+ preparedInsert.setDefaultTimestamp(timestamp);
- ResultSet rs = session.execute(preparedInsert.bind(queryObject.getValues().toArray()));
+ ResultSet rs = session.execute(preparedInsert);
result = rs.wasApplied();
}
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());
+ 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 = " + "["
+ 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());
}
@@ -428,52 +466,63 @@ public class MusicDataStore {
return result;
}
- /**
+ /* *//**
* This method performs DDL operations on Cassandra using consistency level ONE.
- *
+ *
* @param queryObject Object containing cassandra prepared query and values.
* @return ResultSet
* @throws MusicServiceException
* @throws MusicQueryException
- */
+ *//*
public ResultSet executeEventualGet(PreparedQueryObject queryObject)
throws MusicServiceException, MusicQueryException {
-
+ CacheAccess<String, PreparedStatement> queryBank = CachingUtil.getStatementBank();
+ PreparedStatement preparedEventualGet = null;
if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) {
- logger.error(EELFLoggerDelegate.errorLogger, "",AppMessages.QUERYERROR+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
- throw new MusicQueryException("Ill formed queryObject for the request = " + "["
+ logger.error(EELFLoggerDelegate.errorLogger, "",AppMessages.QUERYERROR+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ throw new MusicQueryException("Ill formed queryObject for the request = " + "["
+ queryObject.getQuery() + "]");
}
logger.info(EELFLoggerDelegate.applicationLogger,
"Executing Eventual get query:" + queryObject.getQuery());
-
+
ResultSet results = null;
try {
- PreparedStatement preparedEventualGet = session.prepare(queryObject.getQuery());
- preparedEventualGet.setConsistencyLevel(ConsistencyLevel.ONE);
- results = session.execute(preparedEventualGet.bind(queryObject.getValues().toArray()));
+ if(queryBank.get(queryObject.getQuery()) != null )
+ preparedEventualGet=queryBank.get(queryObject.getQuery());
+ else {
+ preparedEventualGet = session.prepare(queryObject.getQuery());
+ CachingUtil.updateStatementBank(queryObject.getQuery(), preparedEventualGet);
+ }
+ if(queryObject.getConsistency() == null) {
+ preparedEventualGet.setConsistencyLevel(ConsistencyLevel.ONE);
+ }
+ else {
+ preparedEventualGet.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency()));
+ }
+ results = session.execute(preparedEventualGet.bind(queryObject.getValues().toArray()));
} catch (Exception ex) {
logger.error("Exception", ex);
- logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
- throw new MusicServiceException(ex.getMessage());
+ logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ throw new MusicServiceException(ex.getMessage());
}
return results;
}
- /**
- *
+ *//**
+ *
* This method performs DDL operation on Cassandra using consistency level QUORUM.
- *
+ *
* @param queryObject Object containing cassandra prepared query and values.
* @return ResultSet
* @throws MusicServiceException
* @throws MusicQueryException
- */
+ *//*
public ResultSet executeCriticalGet(PreparedQueryObject queryObject)
throws MusicServiceException, MusicQueryException {
if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) {
- logger.error(EELFLoggerDelegate.errorLogger, "",AppMessages.QUERYERROR+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ logger.error(EELFLoggerDelegate.errorLogger, "",AppMessages.QUERYERROR+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
throw new MusicQueryException("Error processing Prepared Query Object for the request = " + "["
+ queryObject.getQuery() + "]");
}
@@ -486,11 +535,65 @@ public class MusicDataStore {
results = session.execute(preparedEventualGet.bind(queryObject.getValues().toArray()));
} catch (Exception ex) {
logger.error("Exception", ex);
- logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
- throw new MusicServiceException(ex.getMessage());
+ logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ throw new MusicServiceException(ex.getMessage());
+ }
+ return results;
+
+ }
+ */
+ public ResultSet executeGet(PreparedQueryObject queryObject,String consistencyLevel) throws MusicQueryException, MusicServiceException {
+ if (!MusicUtil.isValidQueryObject(!queryObject.getValues().isEmpty(), queryObject)) {
+ logger.error(EELFLoggerDelegate.errorLogger, "",AppMessages.QUERYERROR+ " [" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ throw new MusicQueryException("Error processing Prepared Query Object for the request = " + "["
+ + queryObject.getQuery() + "]");
+ }
+ ResultSet results = null;
+ try {
+ SimpleStatement statement = new SimpleStatement(queryObject.getQuery(), queryObject.getValues().toArray());
+
+ if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_ONE)) {
+ if(queryObject.getConsistency() == null) {
+ statement.setConsistencyLevel(ConsistencyLevel.ONE);
+ }
+ else {
+ statement.setConsistencyLevel(MusicUtil.getConsistencyLevel(queryObject.getConsistency()));
+ }
+ }
+ else if (consistencyLevel.equalsIgnoreCase(CONSISTENCY_LEVEL_QUORUM)) {
+ statement.setConsistencyLevel(ConsistencyLevel.QUORUM);
+ }
+
+ results = session.execute(statement);
+
+ } catch (Exception ex) {
+ logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR+ "[" + queryObject.getQuery() + "]", ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ throw new MusicServiceException(ex.getMessage());
}
+
return results;
+
+ }
+
+ /**
+ * This method performs DDL operations on Cassandra using consistency level ONE.
+ *
+ * @param queryObject Object containing cassandra prepared query and values.
+ */
+ public ResultSet executeOneConsistencyGet(PreparedQueryObject queryObject)
+ throws MusicServiceException, MusicQueryException {
+ return executeGet(queryObject, CONSISTENCY_LEVEL_ONE);
+ }
+ /**
+ *
+ * This method performs DDL operation on Cassandra using consistency level QUORUM.
+ *
+ * @param queryObject Object containing cassandra prepared query and values.
+ */
+ public ResultSet executeQuorumConsistencyGet(PreparedQueryObject queryObject)
+ throws MusicServiceException, MusicQueryException {
+ return executeGet(queryObject, CONSISTENCY_LEVEL_QUORUM);
}
}
diff --git a/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java b/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java
new file mode 100644
index 00000000..bb8f3cbd
--- /dev/null
+++ b/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java
@@ -0,0 +1,108 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.datastore;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.onap.music.eelf.logging.EELFLoggerDelegate;
+import org.onap.music.exceptions.MusicServiceException;
+import org.onap.music.main.MusicUtil;
+import org.onap.music.service.impl.MusicZKCore;
+
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.TableMetadata;
+
+public class MusicDataStoreHandle {
+
+
+
+ public static MusicDataStore mDstoreHandle = null;
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStoreHandle.class);
+
+ /**
+ *
+ * @param remoteIp
+ * @return
+ */
+ public static MusicDataStore getDSHandle(String remoteIp) {
+ logger.info(EELFLoggerDelegate.metricsLogger,"Acquiring data store handle");
+ long start = System.currentTimeMillis();
+ if (mDstoreHandle == null) {
+ mDstoreHandle = new MusicDataStore(remoteIp);
+ }
+ long end = System.currentTimeMillis();
+ logger.info(EELFLoggerDelegate.metricsLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
+ return mDstoreHandle;
+ }
+
+ /**
+ *
+ * @return
+ * @throws MusicServiceException
+ */
+ public static MusicDataStore getDSHandle() throws MusicServiceException {
+ logger.info(EELFLoggerDelegate.metricsLogger,"Acquiring data store handle");
+ long start = System.currentTimeMillis();
+ if (mDstoreHandle == null) {
+ // Quick Fix - Best to put this into every call to getDSHandle?
+ if (! MusicUtil.getMyCassaHost().equals("localhost") ) {
+ mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost());
+ } else {
+ mDstoreHandle = new MusicDataStore();
+ }
+ }
+ if(mDstoreHandle.getSession() == null) {
+ String message = "Connection to Cassandra has not been enstablished."
+ + " Please check connection properites and reboot.";
+ logger.info(EELFLoggerDelegate.applicationLogger, message);
+ throw new MusicServiceException(message);
+ }
+ long end = System.currentTimeMillis();
+ logger.info(EELFLoggerDelegate.metricsLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
+ return mDstoreHandle;
+ }
+
+
+ /**
+ *
+ * @param keyspace
+ * @param tablename
+ * @return
+ * @throws MusicServiceException
+ */
+ public static TableMetadata returnColumnMetadata(String keyspace, String tablename) throws MusicServiceException {
+ return getDSHandle().returnColumnMetadata(keyspace, tablename);
+ }
+
+ /**
+ *
+ * @param results
+ * @return
+ * @throws MusicServiceException
+ */
+ public static Map<String, HashMap<String, Object>> marshallResults(ResultSet results) throws MusicServiceException {
+ return getDSHandle().marshalData(results);
+ }
+
+}
diff --git a/src/main/java/org/onap/music/datastore/PreparedQueryObject.java b/src/main/java/org/onap/music/datastore/PreparedQueryObject.java
index 694d9acd..9b109630 100644
--- a/src/main/java/org/onap/music/datastore/PreparedQueryObject.java
+++ b/src/main/java/org/onap/music/datastore/PreparedQueryObject.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore;
import java.util.ArrayList;
@@ -34,8 +35,53 @@ public class PreparedQueryObject {
private List<Object> values;
private StringBuilder query;
+ private String consistency;
+ private String keyspaceName;
+ private String tableName;
+ private String operation;
+ private String primaryKeyValue;
+
+
+
+ public String getKeyspaceName() {
+ return keyspaceName;
+ }
+
+ public void setKeyspaceName(String keyspaceName) {
+ this.keyspaceName = keyspaceName;
+ }
+
+ public String getTableName() {
+ return tableName;
+ }
+
+ public void setTableName(String tableName) {
+ this.tableName = tableName;
+ }
+ public String getOperation() {
+ return operation;
+ }
+
+ public void setOperation(String operation) {
+ this.operation = operation;
+ }
+ public String getPrimaryKeyValue() {
+ return primaryKeyValue;
+ }
+
+ public void setPrimaryKeyValue(String primaryKeyValue) {
+ this.primaryKeyValue = primaryKeyValue;
+ }
+
+ public String getConsistency() {
+ return consistency;
+ }
+
+ public void setConsistency(String consistency) {
+ this.consistency = consistency;
+ }
/**
*
@@ -66,6 +112,9 @@ public class PreparedQueryObject {
public void appendQueryString(String s) {
this.query.append(s);
}
+ public void replaceQueryString(String s) {
+ this.query.replace(0, query.length(), s);
+ }
/**
* @return
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/AAFResponse.java b/src/main/java/org/onap/music/datastore/jsonobjects/AAFResponse.java
index b0f41311..3544e068 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/AAFResponse.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/AAFResponse.java
@@ -21,6 +21,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import java.util.List;
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JSONCallbackResponse.java b/src/main/java/org/onap/music/datastore/jsonobjects/JSONCallbackResponse.java
deleted file mode 100755
index c5a56a71..00000000
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JSONCallbackResponse.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * ============LICENSE_START==========================================
- * org.onap.music
- * ===================================================================
- * Copyright (c) 2017 AT&T Intellectual Property
- * ===================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * ============LICENSE_END=============================================
- * ====================================================================
- */
-
-package org.onap.music.datastore.jsonobjects;
-
-import java.util.List;
-import java.util.Map;
-
-public class JSONCallbackResponse {
-
- private String fullTable;
- private String keyspace;
- private Map<String, String> changeValue;
- private String operation;
- private String tableName;
- private String primaryKey;
- private Object miscObjects;
- private List<String> updateList;
-
- public String getFull_table() {
- return fullTable;
- }
- public void setFull_table(String fullTable) {
- this.fullTable = fullTable;
- }
- public String getKeyspace() {
- return keyspace;
- }
- public void setKeyspace(String keyspace) {
- this.keyspace = keyspace;
- }
- public String getOperation() {
- return operation;
- }
- public void setOperation(String operation) {
- this.operation = operation;
- }
- public String getTable_name() {
- return tableName;
- }
- public void setTable_name(String tableName) {
- this.tableName = tableName;
- }
- public String getPrimary_key() {
- return primaryKey;
- }
- public void setPrimary_key(String primaryKey) {
- this.primaryKey = primaryKey;
- }
- public Object getMiscObjects() {
- return miscObjects;
- }
- public void setMiscObjects(Object miscObjects) {
- this.miscObjects = miscObjects;
- }
- public void setChangeValue(Map<String, String> changeValue) {
- this.changeValue = changeValue;
- }
- public Map<String, String> getChangeValue() {
- return changeValue;
- }
- public List<String> getUpdateList() {
- return updateList;
- }
- public void setUpdateList(List<String> updateList) {
- this.updateList = updateList;
- }
-
-
-}
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JSONObject.java b/src/main/java/org/onap/music/datastore/jsonobjects/JSONObject.java
index 8de0a2cd..2fc4215b 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JSONObject.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/JSONObject.java
@@ -24,14 +24,14 @@ package org.onap.music.datastore.jsonobjects;
public class JSONObject {
- private String data;
+ private String data;
- public String getData() {
- return data;
- }
-
- public void setData(String data) {
- this.data = data;
- }
-
+ public String getData() {
+ return data;
+ }
+
+ public void setData(String data) {
+ this.data = data;
+ }
+
}
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonCallback.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonCallback.java
deleted file mode 100755
index 4a865320..00000000
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonCallback.java
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * ============LICENSE_START==========================================
- * org.onap.music
- * ===================================================================
- * Copyright (c) 2017 AT&T Intellectual Property
- * ===================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * ============LICENSE_END=============================================
- * ====================================================================
- */
-package org.onap.music.datastore.jsonobjects;
-
-import java.io.Serializable;
-import java.util.Map;
-
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-
-import io.swagger.annotations.ApiModel;
-import io.swagger.annotations.ApiModelProperty;
-
-@ApiModel(value = "JsonCallback", description = "Json model for callback")
-@JsonIgnoreProperties(ignoreUnknown = true)
-public class JsonCallback implements Serializable {
- private String applicationName;
- private String applicationUsername;
- private String applicationPassword;
- private String applicationNotificationEndpoint;
- private String notifyOn;
- private String notifyWhenChangeIn;
- private String notifyWhenInsertsIn;
- private String notifyWhenDeletesIn;
- private Map<String, String> responseBody;
-
- private String uuid;
-
- public String getUuid() {
- return uuid;
- }
-
- public void setUuid(String uuid) {
- this.uuid = uuid;
- }
-
- @ApiModelProperty(value = "application name")
- public String getApplicationName() {
- return applicationName;
- }
-
- public void setApplicationName(String applicationName) {
- this.applicationName = applicationName;
- }
-
- @ApiModelProperty(value = "notify On")
- public String getNotifyOn() {
- return notifyOn;
- }
-
- public void setNotifyOn(String notifyOn) {
- this.notifyOn = notifyOn;
- }
-
- @ApiModelProperty(value = "application User name")
- public String getApplicationUsername() {
- return applicationUsername;
- }
-
- public void setApplicationUsername(String applicationUsername) {
- this.applicationUsername = applicationUsername;
- }
-
- @ApiModelProperty(value = "application password")
- public String getApplicationPassword() {
- return applicationPassword;
- }
-
- public void setApplicationPassword(String applicationPassword) {
- this.applicationPassword = applicationPassword;
- }
-
- @ApiModelProperty(value = "application notification endpoint")
- public String getApplicationNotificationEndpoint() {
- return applicationNotificationEndpoint;
- }
-
- public void setApplicationNotificationEndpoint(String applicationNotificationEndpoint) {
- this.applicationNotificationEndpoint = applicationNotificationEndpoint;
- }
-
- @ApiModelProperty(value = "notify when updates")
- public String getNotifyWhenChangeIn() {
- return notifyWhenChangeIn;
- }
-
- public void setNotifyWhenChangeIn(String notifyWhenChangeIn) {
- this.notifyWhenChangeIn = notifyWhenChangeIn;
- }
-
- @ApiModelProperty(value = "notify when inserts")
- public String getNotifyWhenInsertsIn() {
- return notifyWhenInsertsIn;
- }
-
- public void setNotifyWhenInsertsIn(String notifyWhenInsertsIn) {
- this.notifyWhenInsertsIn = notifyWhenInsertsIn;
- }
-
- @ApiModelProperty(value = "notify when deletes")
- public String getNotifyWhenDeletesIn() {
- return notifyWhenDeletesIn;
- }
-
- public void setNotifyWhenDeletesIn(String notifyWhenDeletesIn) {
- this.notifyWhenDeletesIn = notifyWhenDeletesIn;
- }
-
- public Map<String, String> getResponseBody() {
- return responseBody;
- }
-
- public void setResponseBody(Map<String, String> responseBody) {
- this.responseBody = responseBody;
- }
-
-}
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 ce7f509b..c90dd005 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonDelete.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonDelete.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import java.util.ArrayList;
@@ -36,8 +37,7 @@ public class JsonDelete {
private ArrayList<String> columns = null;
private Map<String, String> consistencyInfo;
private Map<String, Object> conditions;
- String ttl;
- String timestamp;
+ String ttl, timestamp;
@ApiModelProperty(value = "Conditions")
@@ -86,4 +86,3 @@ public class JsonDelete {
this.timestamp = timestamp;
}
}
-
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonInsert.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonInsert.java
index 456fb951..4533f947 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonInsert.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonInsert.java
@@ -20,6 +20,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import java.io.ByteArrayOutputStream;
@@ -54,12 +55,12 @@ public class JsonInsert implements Serializable {
@ApiModelProperty(value = "objectMap")
public Map<String, byte[]> getObjectMap() {
- return objectMap;
- }
+ return objectMap;
+ }
public void setObjectMap(Map<String, byte[]> objectMap) {
- this.objectMap = objectMap;
- }
+ this.objectMap = objectMap;
+ }
@ApiModelProperty(value = "keyspace")
public String getKeyspaceName() {
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonKeySpace.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonKeySpace.java
index 54de02fd..f2232ffd 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonKeySpace.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonKeySpace.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import java.util.Map;
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonLeasedLock.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonLeasedLock.java
index 497e17d1..06a76f57 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonLeasedLock.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonLeasedLock.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonNotification.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonNotification.java
deleted file mode 100755
index 5190de58..00000000
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonNotification.java
+++ /dev/null
@@ -1,127 +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.datastore.jsonobjects;
-
-import java.io.Serializable;
-import java.util.Map;
-
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.annotation.JsonInclude.Include;
-
-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 io.swagger.annotations.ApiModel;
-
-@ApiModel(value = "JsonNotification", description = "Json model for callback")
-@JsonIgnoreProperties(ignoreUnknown = true)
-@JsonInclude(Include.NON_NULL)
-public class JsonNotification implements Serializable {
-
- private String notify_field;
- private String endpoint;
- private String username;
- private String password;
- private String notify_change;
- private String notify_insert;
- private String notify_delete;
- private String operation_type;
- private String triggerName;
- private Map<String, String> response_body;
- private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JsonNotification.class);
-
- public String getNotify_field() {
- return notify_field;
- }
- public void setNotify_field(String notify_field) {
- this.notify_field = notify_field;
- }
- public String getEndpoint() {
- return endpoint;
- }
- public void setEndpoint(String endpoint) {
- this.endpoint = endpoint;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Map<String, String> getResponse_body() {
- return response_body;
- }
- public void setResponse_body(Map<String, String> response_body) {
- this.response_body = response_body;
- }
- public String getNotify_change() {
- return notify_change;
- }
- public void setNotify_change(String notify_change) {
- this.notify_change = notify_change;
- }
- public String getNotify_insert() {
- return notify_insert;
- }
- public void setNotify_insert(String notify_insert) {
- this.notify_insert = notify_insert;
- }
- public String getNotify_delete() {
- return notify_delete;
- }
- public void setNotify_delete(String notify_delete) {
- this.notify_delete = notify_delete;
- }
- public String getOperation_type() {
- return operation_type;
- }
- public void setOperation_type(String operation_type) {
- this.operation_type = operation_type;
- }
- public String getTriggerName() {
- return triggerName;
- }
- public void setTriggerName(String triggerName) {
- this.triggerName = triggerName;
- }
- @Override
- public String toString() {
- try {
- return new com.fasterxml.jackson.databind.ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
- } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e, AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR);
- return notify_field+ " : "+endpoint+ " : "+username+ " : "+password+ " : "+response_body;
- }
-
- }
-
-}
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonNotifyClientResponse.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonNotifyClientResponse.java
deleted file mode 100644
index 963352d0..00000000
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonNotifyClientResponse.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * ============LICENSE_START==========================================
- * org.onap.music
- * ===================================================================
- * Copyright (c) 2017 AT&T Intellectual Property
- * ===================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * ============LICENSE_END=============================================
- * ====================================================================
- */
-package org.onap.music.datastore.jsonobjects;
-
-import java.io.Serializable;
-
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
-
-import io.swagger.annotations.ApiModel;
-
-@ApiModel(value = "JsonNotifyClientResponse", description = "Json model for callback")
-@JsonIgnoreProperties(ignoreUnknown = true)
-public class JsonNotifyClientResponse implements Serializable {
- private String message;
- private String status;
-
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- public String getStatus() {
- return status;
- }
- public void setStatus(String status) {
- this.status = status;
- }
-
- @Override
- public String toString() {
- try {
- return new com.fasterxml.jackson.databind.ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
- } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
- return message+ " : "+status;
- }
-
- }
-
-}
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonOnboard.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonOnboard.java
index 0bac1e31..82993ebc 100755
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonOnboard.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonOnboard.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@@ -34,6 +35,16 @@ public class JsonOnboard {
private String password;
private String isAAF;
private String aid;
+ private String keyspace;
+
+ @ApiModelProperty(value = "Application Keyspace")
+ public String getKeyspace() {
+ return keyspace;
+ }
+
+ public void setKeyspace_name(String keyspace) {
+ this.keyspace = keyspace;
+ }
@ApiModelProperty(value = "Application Password")
public String getPassword() {
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonSelect.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonSelect.java
index 73237cd2..faef9b8f 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonSelect.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonSelect.java
@@ -20,6 +20,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import java.io.ByteArrayOutputStream;
@@ -28,13 +29,13 @@ import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;
-import org.apache.log4j.Logger;
+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class JsonSelect implements Serializable {
private Map<String, String> consistencyInfo;
- static Logger logger = Logger.getLogger(JsonSelect.class.getName());
+
public Map<String, String> getConsistencyInfo() {
return consistencyInfo;
@@ -52,7 +53,7 @@ public class JsonSelect implements Serializable {
out.writeObject(this);
} catch (IOException e) {
// TODO Auto-generated catch block
- logger.error("Error", e);
+ e.printStackTrace();
}
return bos.toByteArray();
}
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 2e0a5ded..cf691590 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonTable.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import java.util.Map;
@@ -116,29 +117,29 @@ public class JsonTable {
this.primaryKey = primaryKey;
}
- public String getClusteringKey() {
- return clusteringKey;
- }
+ public String getClusteringKey() {
+ return clusteringKey;
+ }
- public void setClusteringKey(String clusteringKey) {
- this.clusteringKey = clusteringKey;
- }
+ public void setClusteringKey(String clusteringKey) {
+ this.clusteringKey = clusteringKey;
+ }
- public String getFilteringKey() {
- return filteringKey;
- }
+ public String getFilteringKey() {
+ return filteringKey;
+ }
- public void setFilteringKey(String filteringKey) {
- this.filteringKey = filteringKey;
- }
+ public void setFilteringKey(String filteringKey) {
+ this.filteringKey = filteringKey;
+ }
- public String getPartitionKey() {
- return partitionKey;
- }
+ public String getPartitionKey() {
+ return partitionKey;
+ }
- public void setPartitionKey(String partitionKey) {
- this.partitionKey = partitionKey;
- }
+ public void setPartitionKey(String partitionKey) {
+ this.partitionKey = partitionKey;
+ }
}
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/JsonUpdate.java b/src/main/java/org/onap/music/datastore/jsonobjects/JsonUpdate.java
index 514f34ab..e31c6ccf 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/JsonUpdate.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/JsonUpdate.java
@@ -20,6 +20,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import java.io.ByteArrayOutputStream;
diff --git a/src/main/java/org/onap/music/datastore/jsonobjects/NameSpace.java b/src/main/java/org/onap/music/datastore/jsonobjects/NameSpace.java
index 232353c1..3c18c791 100644
--- a/src/main/java/org/onap/music/datastore/jsonobjects/NameSpace.java
+++ b/src/main/java/org/onap/music/datastore/jsonobjects/NameSpace.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.datastore.jsonobjects;
import java.util.List;
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 837e37c2..93c44468 100644
--- a/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java
+++ b/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.eelf.healthcheck;
import java.util.UUID;
@@ -30,10 +31,11 @@ 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.MusicLockingService;
-import org.onap.music.main.MusicCore;
+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;
@@ -43,109 +45,109 @@ import com.datastax.driver.core.ConsistencyLevel;
*/
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");
-
- boolean result = false;
- try {
- result = getAdminKeySpace(consistency);
- } catch(Exception e) {
- if(e.getMessage().toLowerCase().contains("unconfigured table healthcheck")) {
- logger.error("Error", e);
- logger.debug("Creating table....");
- boolean ksresult = createKeyspace();
- if(ksresult)
- try {
- result = getAdminKeySpace(consistency);
- } catch (MusicServiceException e1) {
- // TODO Auto-generated catch block
- logger.error("Error", e);
- e1.printStackTrace();
- }
- } else {
- logger.error("Error", e);
- return "One or more nodes are down or not responding.";
- }
- }
- if (result) {
- return "ACTIVE";
- } else {
- logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
- return "INACTIVE";
- }
- }
-
- 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;
- }
-
-
- }
-
- private boolean createKeyspace() {
- PreparedQueryObject pQuery = new PreparedQueryObject();
- pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
- ResultType rs = null ;
- try {
- rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
- } catch (MusicServiceException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- logger.error("Error", e);
- }
- 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 {
- MusicCore.getLockingServiceHandle();
- // additionally need to call the ZK to create,aquire and delete lock
- } catch (MusicLockingException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR,
- ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL);
- return "INACTIVE";
- }
-
- logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
- return "ACTIVE";
-
- }
-
- public String getCassandrHost() {
- return cassandrHost;
- }
-
- public void setCassandrHost(String cassandrHost) {
- this.cassandrHost = cassandrHost;
- }
-
- public String getZookeeperHost() {
- return zookeeperHost;
- }
-
- public void setZookeeperHost(String zookeeperHost) {
- this.zookeeperHost = zookeeperHost;
- }
+ 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");
+
+ boolean result = false;
+ try {
+ result = getAdminKeySpace(consistency);
+ } catch(Exception e) {
+ if(e.getMessage().toLowerCase().contains("unconfigured table healthcheck")) {
+ logger.error("Error", e);
+ logger.debug("Creating table....");
+ boolean ksresult = createKeyspace();
+ if(ksresult)
+ try {
+ result = getAdminKeySpace(consistency);
+ } catch (MusicServiceException e1) {
+ // TODO Auto-generated catch block
+ logger.error("Error", e);
+ e1.printStackTrace();
+ }
+ } else {
+ logger.error("Error", e);
+ return "One or more nodes are down or not responding.";
+ }
+ }
+ if (result) {
+ return "ACTIVE";
+ } else {
+ logger.info(EELFLoggerDelegate.applicationLogger, "Cassandra Service is not responding");
+ return "INACTIVE";
+ }
+ }
+
+ 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;
+ }
+
+
+ }
+
+ private boolean createKeyspace() {
+ PreparedQueryObject pQuery = new PreparedQueryObject();
+ pQuery.appendQueryString("CREATE TABLE admin.healthcheck (id uuid PRIMARY KEY)");
+ ResultType rs = null ;
+ try {
+ rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString());
+ } catch (MusicServiceException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ logger.error("Error", e);
+ }
+ 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.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running");
+ return "ACTIVE";
+
+ }
+
+ public String getCassandrHost() {
+ return cassandrHost;
+ }
+
+ public void setCassandrHost(String cassandrHost) {
+ 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/EELFLoggerDelegate.java b/src/main/java/org/onap/music/eelf/logging/EELFLoggerDelegate.java
index bb28d172..64960b1e 100644
--- a/src/main/java/org/onap/music/eelf/logging/EELFLoggerDelegate.java
+++ b/src/main/java/org/onap/music/eelf/logging/EELFLoggerDelegate.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.eelf.logging;
import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
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 c406c694..40f69e36 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
@@ -28,101 +28,101 @@ package org.onap.music.eelf.logging.format;
*
*/
public enum AppMessages {
-
-
-
- /*
- * 100-199 Security/Permission Related - Authentication problems
- * [ERR100E] Missing Information
- * [ERR101E] Authentication error occured
- *
- * 200-299 Availability/Timeout Related/IO - connectivity error - connection timeout
- * [ERR200E] Connectivity
- * [ERR201E] Host not available
- * [ERR202E] Error while connecting to Cassandra cluster
- * [ERR203E] IO Error has occured
- * [ERR204E] Execution Interrupted
- * [ERR205E] Session Expired
- * [ERR206E] Cache not authenticated
- *
- *
- * 300-399 Data Access/Integrity Related
- * [ERR300E] Incorrect data
- *
- * 400-499 - Cassandra Query Related
- * [ERR400E] Error while processing prepared query object
- * [ERR401E] Executing Session Failure for Request
- * [ERR402E] Ill formed queryObject for the request
- * [ERR403E] Error processing Prepared Query Object
- *
- * 500-599 - Zookeepr/Locking Related
- * [ERR500E] Invalid lock
- * [ERR501E] Locking Error has occured
- * [ERR502E] Zookeeper error has occured
- * [ERR503E] Failed to aquire lock store handle
- * [ERR504E] Failed to create Lock Reference
- * [ERR505E] Lock does not exist
- * [ERR506E] Failed to aquire lock
- * [ERR507E] Lock not aquired
- * [ERR508E] Lock state not set
- * [ERR509E] Lock not destroyed
- * [ERR510E] Lock not released
- * [ERR511E] Lock not deleted
- * [ERR512E] Failed to get ZK Lock Handle
- *
- *
- * 600 - 699 - Music Service Errors
- * [ERR600E] Error initializing the cache
- *
- * 700-799 Schema Interface Type/Validation - received Pay-load checksum is
- * invalid - received JSON is not valid
- *
- * 800-899 Business/Flow Processing Related - check out to service is not
- * allowed - Roll-back is done - failed to generate heat file
- *
- *
- * 900-999 Unknown Errors - Unexpected exception
- * [ERR900E] Unexpected error occured
- * [ERR901E] Number format exception
- *
- *
- * 1000-1099 Reserved - do not use
- *
- */
-
-
-
-
- MISSINGINFO("[ERR100E]", "Missing Information ","Details: NA", "Please check application credentials and/or headers"),
- AUTHENTICATIONERROR("[ERR101E]", "Authentication error occured ","Details: NA", "Please verify application credentials"),
- CONNCECTIVITYERROR("[ERR200E]"," Connectivity error","Details: NA ","Please check connectivity to external resources"),
- HOSTUNAVAILABLE("[ERR201E]","Host not available","Details: NA","Please verify the host details"),
- CASSANDRACONNECTIVITY("[ERR202E]","Error while connecting to Cassandra cluster",""," Please check cassandra cluster details"),
- IOERROR("[ERR203E]","IO Error has occured","","Please check IO"),
- EXECUTIONINTERRUPTED("[ERR204E]"," Execution Interrupted","",""),
- SESSIONEXPIRED("[ERR205E]"," Session Expired","","Session has expired."),
- CACHEAUTHENTICATION("[ERR206E]","Cache not authenticated",""," Cache not authenticated"),
-
- INCORRECTDATA("[ERR300E]"," Incorrect data",""," Please verify the request payload and try again"),
- MULTIPLERECORDS("[ERR301E]"," Multiple records found",""," Please verify the request payload and try again"),
- ALREADYEXIST("[ERR302E]"," Record already exist",""," Please verify the request payload and try again"),
- MISSINGDATA("[ERR300E]"," Incorrect data",""," Please verify the request payload and try again"),
-
- QUERYERROR("[ERR400E]","Error while processing prepared query object",""," Please verify the query"),
- SESSIONFAILED("[ERR401E]","Executing Session Failure for Request","","Please verify the session and request"),
-
- 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"),
- DESTROYLOCK("[ERR509E]"," Lock not destroyed",""," Lock not destroyed"),
- RELEASELOCK("[ERR510E]"," Lock not released",""," Lock not released"),
- DELTELOCK("[ERR511E]",""," Lock not deleted "," Lock not deleted "),
- CACHEERROR("[ERR600E]"," Error initializing the cache",""," Error initializing the cache"),
-
- UNKNOWNERROR("[ERR900E]"," Unexpected error occured",""," Please check logs for details");
+
+
+
+ /*
+ * 100-199 Security/Permission Related - Authentication problems
+ * [ERR100E] Missing Information
+ * [ERR101E] Authentication error occured
+ *
+ * 200-299 Availability/Timeout Related/IO - connectivity error - connection timeout
+ * [ERR200E] Connectivity
+ * [ERR201E] Host not available
+ * [ERR202E] Error while connecting to Cassandra cluster
+ * [ERR203E] IO Error has occured
+ * [ERR204E] Execution Interrupted
+ * [ERR205E] Session Expired
+ * [ERR206E] Cache not authenticated
+ *
+ *
+ * 300-399 Data Access/Integrity Related
+ * [ERR300E] Incorrect data
+ *
+ * 400-499 - Cassandra Query Related
+ * [ERR400E] Error while processing prepared query object
+ * [ERR401E] Executing Session Failure for Request
+ * [ERR402E] Ill formed queryObject for the request
+ * [ERR403E] Error processing Prepared Query Object
+ *
+ * 500-599 - Zookeepr/Locking Related
+ * [ERR500E] Invalid lock
+ * [ERR501E] Locking Error has occured
+ * [ERR502E] Zookeeper error has occured
+ * [ERR503E] Failed to aquire lock store handle
+ * [ERR504E] Failed to create Lock Reference
+ * [ERR505E] Lock does not exist
+ * [ERR506E] Failed to aquire lock
+ * [ERR507E] Lock not aquired
+ * [ERR508E] Lock state not set
+ * [ERR509E] Lock not destroyed
+ * [ERR510E] Lock not released
+ * [ERR511E] Lock not deleted
+ * [ERR512E] Failed to get ZK Lock Handle
+ *
+ *
+ * 600 - 699 - Music Service Errors
+ * [ERR600E] Error initializing the cache
+ *
+ * 700-799 Schema Interface Type/Validation - received Pay-load checksum is
+ * invalid - received JSON is not valid
+ *
+ * 800-899 Business/Flow Processing Related - check out to service is not
+ * allowed - Roll-back is done - failed to generate heat file
+ *
+ *
+ * 900-999 Unknown Errors - Unexpected exception
+ * [ERR900E] Unexpected error occured
+ * [ERR901E] Number format exception
+ *
+ *
+ * 1000-1099 Reserved - do not use
+ *
+ */
+
+
+
+
+ MISSINGINFO("[ERR100E]", "Missing Information ","Details: NA", "Please check application credentials and/or headers"),
+ AUTHENTICATIONERROR("[ERR101E]", "Authentication error occured ","Details: NA", "Please verify application credentials"),
+ CONNCECTIVITYERROR("[ERR200E]"," Connectivity error","Details: NA ","Please check connectivity to external resources"),
+ HOSTUNAVAILABLE("[ERR201E]","Host not available","Details: NA","Please verify the host details"),
+ CASSANDRACONNECTIVITY("[ERR202E]","Error while connecting to Cassandra cluster",""," Please check cassandra cluster details"),
+ IOERROR("[ERR203E]","IO Error has occured","","Please check IO"),
+ EXECUTIONINTERRUPTED("[ERR204E]"," Execution Interrupted","",""),
+ SESSIONEXPIRED("[ERR205E]"," Session Expired","","Session has expired."),
+ CACHEAUTHENTICATION("[ERR206E]","Cache not authenticated",""," Cache not authenticated"),
+
+ INCORRECTDATA("[ERR300E]"," Incorrect data",""," Please verify the request payload and try again"),
+ MULTIPLERECORDS("[ERR301E]"," Multiple records found",""," Please verify the request payload and try again"),
+ ALREADYEXIST("[ERR302E]"," Record already exist",""," Please verify the request payload and try again"),
+ MISSINGDATA("[ERR300E]"," Incorrect data",""," Please verify the request payload and try again"),
+
+ QUERYERROR("[ERR400E]","Error while processing prepared query object",""," Please verify the query"),
+ SESSIONFAILED("[ERR401E]","Executing Session Failure for Request","","Please verify the session and request"),
+
+ 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"),
+ DESTROYLOCK("[ERR509E]"," Lock not destroyed",""," Lock not destroyed"),
+ RELEASELOCK("[ERR510E]"," Lock not released",""," Lock not released"),
+ DELTELOCK("[ERR511E]",""," Lock not deleted "," Lock not deleted "),
+ CACHEERROR("[ERR600E]"," Error initializing the cache",""," Error initializing the cache"),
+
+ UNKNOWNERROR("[ERR900E]"," Unexpected error occured",""," Please check logs for details");
@@ -130,22 +130,22 @@ public enum AppMessages {
private ErrorTypes eType;
private ErrorSeverity alarmSeverity;
private ErrorSeverity errorSeverity;
- private String errorCode;
- private String errorDescription;
- private String details;
- private String resolution;
+ private String errorCode;
+ private String errorDescription;
+ private String details;
+ private String resolution;
- AppMessages(String errorCode, String errorDescription, String details,String resolution) {
-
- this.errorCode = errorCode;
- this.errorDescription = errorDescription;
- this.details = details;
- this.resolution = resolution;
- }
+ AppMessages(String errorCode, String errorDescription, String details,String resolution) {
+
+ this.errorCode = errorCode;
+ this.errorDescription = errorDescription;
+ this.details = details;
+ this.resolution = resolution;
+ }
- public ErrorTypes getEType() {
+ public ErrorTypes getEType() {
return eType;
}
@@ -156,34 +156,34 @@ public enum AppMessages {
return errorSeverity;
}
- public void setDetails(String details){ this.details=details; }
+ public void setDetails(String details){ this.details=details; }
- public String getDetails() {
- return this.details;
- }
+ public String getDetails() {
+ return this.details;
+ }
- public void setResolution(String resolution){ this.resolution=resolution; }
+ public void setResolution(String resolution){ this.resolution=resolution; }
- public String getResolution() {
- return this.resolution;
- }
+ public String getResolution() {
+ return this.resolution;
+ }
- public void setErrorCode(String errorCode){ this.errorCode=errorCode; }
+ public void setErrorCode(String errorCode){ this.errorCode=errorCode; }
- public String getErrorCode() {
- return this.errorCode;
- }
+ public String getErrorCode() {
+ return this.errorCode;
+ }
- public void setErrorDescription(String errorDescription){ this.errorDescription=errorDescription; }
+ public void setErrorDescription(String errorDescription){ this.errorDescription=errorDescription; }
- public String getErrorDescription() {
- return this.errorDescription;
- }
+ public String getErrorDescription() {
+ return this.errorDescription;
+ }
-
+
-
-
-
+
+
+
}
diff --git a/src/main/java/org/onap/music/eelf/logging/format/ErrorCodes.java b/src/main/java/org/onap/music/eelf/logging/format/ErrorCodes.java
index b18c1771..0afd9eea 100644
--- a/src/main/java/org/onap/music/eelf/logging/format/ErrorCodes.java
+++ b/src/main/java/org/onap/music/eelf/logging/format/ErrorCodes.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.eelf.logging.format;
@@ -28,67 +29,67 @@ package org.onap.music.eelf.logging.format;
*
*/
public enum ErrorCodes {
-
-
- /*
- * 100-199 Security/Permission Related - Authentication problems
- * [ERR100E] Missing Information
- * [ERR101E] Authentication error occured
- *
- * 200-299 Availability/Timeout Related/IO - connectivity error - connection timeout
- * [ERR200E] Connectivity
- * [ERR201E] Host not available
- * [ERR202E] Error while connecting to Cassandra cluster
- * [ERR203E] IO Error has occured
- * [ERR204E] Execution Interrupted
- * [ERR205E] Session Expired
- * [ERR206E] Cache not authenticated
- *
- *
- * 300-399 Data Access/Integrity Related
- *
- * 400-499 - Cassandra Query Related
- * [ERR400E] Error while processing prepared query object
- * [ERR401E] Executing Session Failure for Request
- * [ERR402E] Ill formed queryObject for the request
- * [ERR403E] Error processing Prepared Query Object
- *
- * 500-599 - Zookeepr/Locking Related
- * [ERR500E] Invalid lock
- * [ERR501E] Locking Error has occured
- * [ERR502E] Zookeeper error has occured
- * [ERR503E] Failed to aquire lock store handle
- * [ERR504E] Failed to create Lock Reference
- * [ERR505E] Lock does not exist
- * [ERR506E] Failed to aquire lock
- * [ERR507E] Lock not aquired
- * [ERR508E] Lock state not set
- * [ERR509E] Lock not destroyed
- * [ERR510E] Lock not released
- * [ERR511E] Lock not deleted
- * [ERR512E] Failed to get ZK Lock Handle
- *
- *
- * 600 - 699 - Music Service Errors
- * [ERR600E] Error initializing the cache
- *
- * 700-799 Schema Interface Type/Validation - received Pay-load checksum is
- * invalid - received JSON is not valid
- *
- * 800-899 Business/Flow Processing Related - check out to service is not
- * allowed - Roll-back is done - failed to generate heat file
- *
- *
- * 900-999 Unknown Errors - Unexpected exception
- * [ERR900E] Unexpected error occured
- * [ERR901E] Number format exception
- *
- *
- * 1000-1099 Reserved - do not use
- *
- */
-
- /*SUCCESS("Success"), FAILURE("Failure");
+
+
+ /*
+ * 100-199 Security/Permission Related - Authentication problems
+ * [ERR100E] Missing Information
+ * [ERR101E] Authentication error occured
+ *
+ * 200-299 Availability/Timeout Related/IO - connectivity error - connection timeout
+ * [ERR200E] Connectivity
+ * [ERR201E] Host not available
+ * [ERR202E] Error while connecting to Cassandra cluster
+ * [ERR203E] IO Error has occured
+ * [ERR204E] Execution Interrupted
+ * [ERR205E] Session Expired
+ * [ERR206E] Cache not authenticated
+ *
+ *
+ * 300-399 Data Access/Integrity Related
+ *
+ * 400-499 - Cassandra Query Related
+ * [ERR400E] Error while processing prepared query object
+ * [ERR401E] Executing Session Failure for Request
+ * [ERR402E] Ill formed queryObject for the request
+ * [ERR403E] Error processing Prepared Query Object
+ *
+ * 500-599 - Zookeepr/Locking Related
+ * [ERR500E] Invalid lock
+ * [ERR501E] Locking Error has occured
+ * [ERR502E] Zookeeper error has occured
+ * [ERR503E] Failed to aquire lock store handle
+ * [ERR504E] Failed to create Lock Reference
+ * [ERR505E] Lock does not exist
+ * [ERR506E] Failed to aquire lock
+ * [ERR507E] Lock not aquired
+ * [ERR508E] Lock state not set
+ * [ERR509E] Lock not destroyed
+ * [ERR510E] Lock not released
+ * [ERR511E] Lock not deleted
+ * [ERR512E] Failed to get ZK Lock Handle
+ *
+ *
+ * 600 - 699 - Music Service Errors
+ * [ERR600E] Error initializing the cache
+ *
+ * 700-799 Schema Interface Type/Validation - received Pay-load checksum is
+ * invalid - received JSON is not valid
+ *
+ * 800-899 Business/Flow Processing Related - check out to service is not
+ * allowed - Roll-back is done - failed to generate heat file
+ *
+ *
+ * 900-999 Unknown Errors - Unexpected exception
+ * [ERR900E] Unexpected error occured
+ * [ERR901E] Number format exception
+ *
+ *
+ * 1000-1099 Reserved - do not use
+ *
+ */
+
+ /*SUCCESS("Success"), FAILURE("Failure");
private String result;
@@ -100,7 +101,7 @@ public enum ErrorCodes {
return result;
}
*/
-
-
+
+
}
diff --git a/src/main/java/org/onap/music/eelf/logging/format/ErrorSeverity.java b/src/main/java/org/onap/music/eelf/logging/format/ErrorSeverity.java
index cda5dd31..4e798239 100644
--- a/src/main/java/org/onap/music/eelf/logging/format/ErrorSeverity.java
+++ b/src/main/java/org/onap/music/eelf/logging/format/ErrorSeverity.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.eelf.logging.format;
/**
diff --git a/src/main/java/org/onap/music/eelf/logging/format/ErrorTypes.java b/src/main/java/org/onap/music/eelf/logging/format/ErrorTypes.java
index 2536f4a4..9bdbf20f 100644
--- a/src/main/java/org/onap/music/eelf/logging/format/ErrorTypes.java
+++ b/src/main/java/org/onap/music/eelf/logging/format/ErrorTypes.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.eelf.logging.format;
import com.att.eelf.i18n.EELFResolvableErrorEnum;
@@ -28,17 +29,18 @@ import com.att.eelf.i18n.EELFResolvableErrorEnum;
*
*/
public enum ErrorTypes implements EELFResolvableErrorEnum {
-
-
- CONNECTIONERROR,
- SESSIONEXPIRED,
- AUTHENTICATIONERROR,
- SERVICEUNAVAILABLE,
- QUERYERROR,
- DATAERROR,
- GENERALSERVICEERROR,
- MUSICSERVICEERROR,
- LOCKINGERROR,
- UNKNOWN,
-
+
+
+ CONNECTIONERROR,
+ SESSIONEXPIRED,
+ AUTHENTICATIONERROR,
+ CACHEERROR,
+ SERVICEUNAVAILABLE,
+ QUERYERROR,
+ DATAERROR,
+ GENERALSERVICEERROR,
+ MUSICSERVICEERROR,
+ LOCKINGERROR,
+ UNKNOWN,
+
}
diff --git a/src/main/java/org/onap/music/exceptions/MusicExceptionMapper.java b/src/main/java/org/onap/music/exceptions/MusicExceptionMapper.java
index 14a23d1f..83bb1b8a 100644
--- a/src/main/java/org/onap/music/exceptions/MusicExceptionMapper.java
+++ b/src/main/java/org/onap/music/exceptions/MusicExceptionMapper.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.exceptions;
import java.io.EOFException;
diff --git a/src/main/java/org/onap/music/exceptions/MusicLockingException.java b/src/main/java/org/onap/music/exceptions/MusicLockingException.java
index 1a9e45d9..ab70c54c 100644
--- a/src/main/java/org/onap/music/exceptions/MusicLockingException.java
+++ b/src/main/java/org/onap/music/exceptions/MusicLockingException.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.exceptions;
/**
diff --git a/src/main/java/org/onap/music/exceptions/MusicPolicyVoilationException.java b/src/main/java/org/onap/music/exceptions/MusicPolicyVoilationException.java
index 7de53c59..f489cd45 100644
--- a/src/main/java/org/onap/music/exceptions/MusicPolicyVoilationException.java
+++ b/src/main/java/org/onap/music/exceptions/MusicPolicyVoilationException.java
@@ -21,6 +21,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.exceptions;
/**
diff --git a/src/main/java/org/onap/music/exceptions/MusicQueryException.java b/src/main/java/org/onap/music/exceptions/MusicQueryException.java
index 24b8568b..803f993f 100644
--- a/src/main/java/org/onap/music/exceptions/MusicQueryException.java
+++ b/src/main/java/org/onap/music/exceptions/MusicQueryException.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.exceptions;
diff --git a/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java b/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java
new file mode 100644
index 00000000..237b9417
--- /dev/null
+++ b/src/main/java/org/onap/music/lockingservice/cassandra/CassaLockStore.java
@@ -0,0 +1,254 @@
+/*
+ * ============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.cassandra;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.onap.music.datastore.MusicDataStore;
+import org.onap.music.datastore.PreparedQueryObject;
+import org.onap.music.eelf.logging.EELFLoggerDelegate;
+import org.onap.music.exceptions.MusicQueryException;
+import org.onap.music.exceptions.MusicServiceException;
+
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+
+/*
+ * This is the lock store that is built on top of Cassandra that is used by MUSIC to maintain lock state.
+ */
+
+public class CassaLockStore {
+
+ private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CassaLockStore.class);
+ private static String table_prepend_name = "lockQ_";
+
+ public class LockObject{
+ public String lockRef;
+ public String createTime;
+ public String acquireTime;
+ public LockObject(String lockRef, String createTime, String acquireTime) {
+ this.lockRef = lockRef;
+ this.acquireTime = acquireTime;
+ this.createTime = createTime;
+
+ }
+ }
+ MusicDataStore dsHandle;
+ public CassaLockStore() {
+ dsHandle = new MusicDataStore();
+ }
+
+ public CassaLockStore(MusicDataStore dsHandle) {
+ this.dsHandle=dsHandle;
+ }
+
+
+ /**
+ *
+ * This method creates a shadow locking table for every main table in Cassandra. This table tracks all information regarding locks.
+ * @param keyspace of the application.
+ * @param table of the application.
+ * @return true if the operation was successful.
+ * @throws MusicServiceException
+ * @throws MusicQueryException
+ */
+ public boolean createLockQueue(String keyspace, String table) throws MusicServiceException, MusicQueryException {
+ logger.info(EELFLoggerDelegate.applicationLogger,
+ "Create lock queue/table for " + keyspace+"."+table);
+ table = table_prepend_name+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);";
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+
+ queryObject.appendQueryString(tabQuery);
+ boolean result;
+ result = dsHandle.executePut(queryObject, "eventual");
+ return result;
+ }
+
+ /**
+ * This method creates a lock reference for each invocation. The lock references are monotonically increasing timestamps.
+ * @param keyspace of the locks.
+ * @param table of the locks.
+ * @param lockName is the primary key of the lock table
+ * @return the UUID lock reference.
+ * @throws MusicServiceException
+ * @throws MusicQueryException
+ */
+ public String genLockRefandEnQueue(String keyspace, String table, String lockName) throws MusicServiceException, MusicQueryException {
+ logger.info(EELFLoggerDelegate.applicationLogger,
+ "Create lock reference for " + keyspace + "." + table + "." + lockName);
+ String lockTable ="";
+ lockTable = table_prepend_name+table;
+
+
+
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ String selectQuery = "SELECT guard FROM " + keyspace + "." + lockTable + " WHERE key=?;";
+
+ queryObject.addValue(lockName);
+ queryObject.appendQueryString(selectQuery);
+ ResultSet gqResult = dsHandle.executeOneConsistencyGet(queryObject);
+ List<Row> latestGuardRow = gqResult.all();
+
+ long prevGuard = 0;
+ long lockRef = 1;
+ if (latestGuardRow.size() > 0) {
+ 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);
+
+ queryObject = new PreparedQueryObject();
+ String insQuery = "BEGIN BATCH" +
+ " UPDATE " + keyspace + "." + lockTable +
+ " SET guard=? WHERE key=? IF guard = " + (prevGuard == 0 ? "NULL" : "?") +";" +
+ " INSERT INTO " + keyspace + "." + lockTable +
+ "(key, lockReference, createTime, acquireTime) VALUES (?,?,?,?) IF NOT EXISTS; APPLY BATCH;";
+
+ queryObject.addValue(lockRef);
+ queryObject.addValue(lockName);
+ if (prevGuard != 0)
+ queryObject.addValue(prevGuard);
+
+ queryObject.addValue(lockName);
+ queryObject.addValue(lockRef);
+ queryObject.addValue(String.valueOf(lockEpochMillis));
+ queryObject.addValue("0");
+ queryObject.appendQueryString(insQuery);
+ boolean pResult = dsHandle.executePut(queryObject, "critical");
+ return "$"+keyspace+"."+table+"."+lockName+"$"+String.valueOf(lockRef);
+ }
+
+ /**
+ * Returns a result set containing the list of clients waiting for a particular lock
+ * @param keyspace
+ * @param table
+ * @param key
+ * @return list of lockrefs in the queue
+ * @throws MusicServiceException
+ * @throws MusicQueryException
+ */
+ public List<String> getLockQueue(String keyspace, String table, String key)
+ throws MusicServiceException, MusicQueryException {
+ logger.info(EELFLoggerDelegate.applicationLogger,
+ "Getting the queue for " + keyspace+"."+table+"."+key);
+ table = table_prepend_name+table;
+ String selectQuery = "select * from " + keyspace + "." + table + " where key='" + key + "';";
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ queryObject.appendQueryString(selectQuery);
+ ResultSet rs = dsHandle.executeOneConsistencyGet(queryObject);
+ ArrayList<String> lockQueue = new ArrayList<>();
+ for (Row row: rs) {
+ lockQueue.add(Long.toString(row.getLong("lockReference")));
+ }
+ return lockQueue;
+ }
+
+
+ /**
+ * Returns a result set containing the list of clients waiting for a particular lock
+ * @param keyspace
+ * @param table
+ * @param key
+ * @return size of lockrefs queue
+ * @throws MusicServiceException
+ * @throws MusicQueryException
+ */
+ public long getLockQueueSize(String keyspace, String table, String key)
+ throws MusicServiceException, MusicQueryException {
+ logger.info(EELFLoggerDelegate.applicationLogger,
+ "Getting the queue size for " + keyspace+"."+table+"."+key);
+ table = table_prepend_name+table;
+ String selectQuery = "select count(*) from " + keyspace + "." + table + " where key='" + key + "';";
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ queryObject.appendQueryString(selectQuery);
+ ResultSet rs = dsHandle.executeOneConsistencyGet(queryObject);
+ return rs.one().getLong("count");
+ }
+
+
+ /**
+ * This method returns the top of lock table/queue for the key.
+ * @param keyspace of the application.
+ * @param table of the application.
+ * @param key is the primary key of the application table
+ * @return the UUID lock reference.
+ * @throws MusicServiceException
+ * @throws MusicQueryException
+ */
+ public LockObject peekLockQueue(String keyspace, String table, String key) throws MusicServiceException, MusicQueryException{
+ logger.info(EELFLoggerDelegate.applicationLogger,
+ "Peek in lock table for " + keyspace+"."+table+"."+key);
+ table = table_prepend_name+table;
+ String selectQuery = "select * from "+keyspace+"."+table+" where key='"+key+"' LIMIT 1;";
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ queryObject.appendQueryString(selectQuery);
+ ResultSet results = dsHandle.executeOneConsistencyGet(queryObject);
+ Row row = results.one();
+ String lockReference = "" + row.getLong("lockReference");
+ String createTime = row.getString("createTime");
+ String acquireTime = row.getString("acquireTime");
+
+ return new LockObject(lockReference, createTime,acquireTime);
+ }
+
+
+ /**
+ * This method removes the lock ref from the lock table/queue for the key.
+ * @param keyspace of the application.
+ * @param table of the application.
+ * @param key is the primary key of the application table
+ * @param lockReference the lock reference that needs to be dequeued.
+ * @throws MusicServiceException
+ * @throws MusicQueryException
+ */
+ public void deQueueLockRef(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{
+ table = table_prepend_name+table;
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ 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");
+ }
+
+
+ public void updateLockAcquireTime(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);
+ String updateQuery = "update "+keyspace+"."+table+" set acquireTime='"+ System.currentTimeMillis()+"' where key='"+key+"' AND lockReference = "+lockReferenceL+" IF EXISTS;";
+ queryObject.appendQueryString(updateQuery);
+ dsHandle.executePut(queryObject, "eventual");
+
+ }
+
+
+}
diff --git a/src/main/java/org/onap/music/lockingservice/MusicLockState.java b/src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java
index 04a1e6b2..a8e5ac48 100644
--- a/src/main/java/org/onap/music/lockingservice/MusicLockState.java
+++ b/src/main/java/org/onap/music/lockingservice/cassandra/MusicLockState.java
@@ -19,7 +19,8 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
-package org.onap.music.lockingservice;
+
+package org.onap.music.lockingservice.cassandra;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -44,8 +45,7 @@ public class MusicLockState implements Serializable {
LockStatus lockStatus;
boolean needToSyncQuorum = false;
String lockHolder;
- long leasePeriod = Long.MAX_VALUE;
- long leaseStartTime = -1;
+ long leasePeriod = Long.MAX_VALUE, leaseStartTime = -1;
private String errorMessage = null;
@@ -108,8 +108,8 @@ public class MusicLockState implements Serializable {
}
public String getErrorMessage() {
- return errorMessage;
- }
+ return errorMessage;
+ }
public byte[] serialize() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -118,8 +118,7 @@ public class MusicLockState implements Serializable {
out = new ObjectOutputStream(bos);
out.writeObject(this);
} catch (IOException e) {
- logger.error("Error", e);
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR);
}
return bos.toByteArray();
}
@@ -132,8 +131,7 @@ public class MusicLockState implements Serializable {
in = new ObjectInputStream(bis);
o = in.readObject();
} catch (ClassNotFoundException | IOException e) {
- logger.error("Error", e);
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
}
return (MusicLockState) o;
}
diff --git a/src/main/java/org/onap/music/lockingservice/LockListener.java b/src/main/java/org/onap/music/lockingservice/zookeeper/LockListener.java
index 33188e60..e8ad3a5f 100644
--- a/src/main/java/org/onap/music/lockingservice/LockListener.java
+++ b/src/main/java/org/onap/music/lockingservice/zookeeper/LockListener.java
@@ -19,7 +19,8 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
-package org.onap.music.lockingservice;
+
+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
diff --git a/src/main/java/org/onap/music/lockingservice/MusicLockingService.java b/src/main/java/org/onap/music/lockingservice/zookeeper/MusicLockingService.java
index a75480ae..e7cad139 100644
--- a/src/main/java/org/onap/music/lockingservice/MusicLockingService.java
+++ b/src/main/java/org/onap/music/lockingservice/zookeeper/MusicLockingService.java
@@ -1,21 +1,26 @@
/*
- * ============LICENSE_START========================================== org.onap.music
- * =================================================================== Copyright (c) 2017 AT&T
- * Intellectual Property ===================================================================
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*
* ============LICENSE_END=============================================
* ====================================================================
*/
-package org.onap.music.lockingservice;
+
+package org.onap.music.lockingservice.zookeeper;
import java.io.IOException;
@@ -33,6 +38,7 @@ 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;
@@ -57,7 +63,7 @@ public class MusicLockingService implements Watcher {
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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
throw new MusicServiceException("Exception Occured " + e.getMessage());
}
}
@@ -99,17 +105,17 @@ public class MusicLockingService implements Watcher {
public MusicLockState getLockState(String lockName) throws MusicLockingException {
- byte[] data = null;
+ byte[] data = null;
try{
- data = zkLockHandle.getNodeData(lockName);
+ data = zkLockHandle.getNodeData(lockName);
}catch (Exception ex){
- logger.error(EELFLoggerDelegate.errorLogger, ex,AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ logger.error(EELFLoggerDelegate.errorLogger, ex,AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
}
if(data !=null)
return MusicLockState.deSerialize(data);
else {
- logger.error(EELFLoggerDelegate.errorLogger,"",AppMessages.INVALIDLOCK, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
- throw new MusicLockingException("Invalid lock or acquire failed");
+ logger.error(EELFLoggerDelegate.errorLogger,"Invalid lock or acquire failed",AppMessages.INVALIDLOCK, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ throw new MusicLockingException("Invalid lock or acquire failed");
}
}
@@ -126,14 +132,14 @@ public class MusicLockingService implements Watcher {
return zkLockHandle.lock(lockName, lockId);
} catch (KeeperException e) {
logger.error("Error", e);
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.LOCKINGERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ 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(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);
- }
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ }
return false;
}
@@ -144,11 +150,11 @@ public class MusicLockingService implements Watcher {
}
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");
- }
+ 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) {
@@ -168,9 +174,9 @@ public class MusicLockingService implements Watcher {
zkLockHandle.close();
}
- public boolean lockIdExists(String lockIdWithDollar) {
- String lockId = lockIdWithDollar.replace('$', '/');
- return zkLockHandle.checkIfLockExists(lockId);
- }
+ public boolean lockIdExists(String lockIdWithDollar) {
+ String lockId = lockIdWithDollar.replace('$', '/');
+ return zkLockHandle.checkIfLockExists(lockId);
+ }
}
diff --git a/src/main/java/org/onap/music/lockingservice/ProtocolSupport.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ProtocolSupport.java
index 589e4e57..2e88cc7a 100644
--- a/src/main/java/org/onap/music/lockingservice/ProtocolSupport.java
+++ b/src/main/java/org/onap/music/lockingservice/zookeeper/ProtocolSupport.java
@@ -20,7 +20,8 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
-package org.onap.music.lockingservice;
+
+package org.onap.music.lockingservice.zookeeper;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
@@ -32,7 +33,8 @@ 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.ZooKeeperOperation;
+import org.onap.music.lockingservice.zookeeper.ZooKeeperOperation;
+
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -129,7 +131,7 @@ class ProtocolSupport {
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);
+ 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) {
@@ -181,10 +183,9 @@ class ProtocolSupport {
}
});
} catch (KeeperException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e,AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
} catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e,AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
-
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
}
}
@@ -207,8 +208,8 @@ class ProtocolSupport {
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);
+ 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/ZNodeName.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZNodeName.java
index f4971e11..5bdc5457 100644
--- a/src/main/java/org/onap/music/lockingservice/ZNodeName.java
+++ b/src/main/java/org/onap/music/lockingservice/zookeeper/ZNodeName.java
@@ -21,7 +21,8 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
-package org.onap.music.lockingservice;
+
+package org.onap.music.lockingservice.zookeeper;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.eelf.logging.format.ErrorSeverity;
@@ -52,9 +53,9 @@ class ZNodeName implements Comparable<ZNodeName> {
// 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);
+ 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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Array out of bounds for "+idx, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR);
}
}
}
diff --git a/src/main/java/org/onap/music/lockingservice/ZkStatelessLockService.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java
index 2aae4969..38c4726f 100644
--- a/src/main/java/org/onap/music/lockingservice/ZkStatelessLockService.java
+++ b/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java
@@ -18,7 +18,8 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
-package org.onap.music.lockingservice;
+
+package org.onap.music.lockingservice.zookeeper;
import java.util.List;
@@ -38,6 +39,7 @@ 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;
@@ -69,9 +71,9 @@ public class ZkStatelessLockService extends ProtocolSupport {
}
});
}catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ 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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
}
}
@@ -80,7 +82,7 @@ public class ZkStatelessLockService extends ProtocolSupport {
try {
zookeeper.close();
}catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
}
}
@@ -94,9 +96,9 @@ public class ZkStatelessLockService extends ProtocolSupport {
}
});
}catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ 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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
}
}
@@ -109,9 +111,9 @@ public class ZkStatelessLockService extends ProtocolSupport {
return null;
}catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ 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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
}
return null;
}
@@ -124,9 +126,9 @@ public class ZkStatelessLockService extends ProtocolSupport {
result = true;
}
}catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ 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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
}
return result;
}
@@ -141,9 +143,9 @@ public class ZkStatelessLockService extends ProtocolSupport {
try {
retryOperation(zop);
}catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ 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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
}
return zop.getId();
}
@@ -181,14 +183,14 @@ public class ZkStatelessLockService extends ProtocolSupport {
};
zopdel.execute();
} catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ 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.");
+ 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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
}
}
@@ -208,13 +210,13 @@ public class ZkStatelessLockService extends ProtocolSupport {
}
return sortedNames.first().getName();
} catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ 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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
}
}
@@ -237,14 +239,14 @@ public class ZkStatelessLockService extends ProtocolSupport {
};
zopdel.execute();
} catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
+ 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);
+ 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);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
}
}
@@ -295,14 +297,14 @@ public class ZkStatelessLockService extends ProtocolSupport {
CreateMode.PERSISTENT_SEQUENTIAL);
if (logger.isDebugEnabled()) {
- logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
+ logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
}
if (id != null) {
Stat stat = null;
try {
stat = zookeeper.exists(id, false);
} catch (KeeperException | InterruptedException e1) {
- logger.error(EELFLoggerDelegate.errorLogger, "Error in execute: " + e1);
+ e1.printStackTrace();
}
Long ctime = stat.getCtime();
MusicUtil.zkNodeMap.put(id, ctime);
@@ -314,7 +316,7 @@ public class ZkStatelessLockService extends ProtocolSupport {
pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), ctime));
MusicCore.eventualPut(pQuery);
} catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, "Error in execute: " + e);
+ e.printStackTrace();
}
break;
}
@@ -344,7 +346,7 @@ public class ZkStatelessLockService extends ProtocolSupport {
ZNodeName lastChildName = lessThanMe.last();
String lastChildId = lastChildName.getName();
if (logger.isDebugEnabled()) {
- logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId);
+ logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId);
}
Stat stat = zookeeper.exists(lastChildId, false);
if (stat != null) {
diff --git a/src/main/java/org/onap/music/lockingservice/ZooKeeperOperation.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZooKeeperOperation.java
index 7020d14d..34efebf2 100644
--- a/src/main/java/org/onap/music/lockingservice/ZooKeeperOperation.java
+++ b/src/main/java/org/onap/music/lockingservice/zookeeper/ZooKeeperOperation.java
@@ -19,13 +19,14 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
-package org.onap.music.lockingservice;
+
+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.ProtocolSupport} class
+ * {@link org.onap.music.lockingservice.zookeeper.ProtocolSupport} class
*
*/
public interface ZooKeeperOperation {
diff --git a/src/main/java/org/onap/music/main/CachingUtil.java b/src/main/java/org/onap/music/main/CachingUtil.java
index 18cf90d9..9c975191 100755
--- a/src/main/java/org/onap/music/main/CachingUtil.java
+++ b/src/main/java/org/onap/music/main/CachingUtil.java
@@ -21,6 +21,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.main;
import java.util.Calendar;
@@ -29,10 +30,16 @@ 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.apache.commons.jcs.engine.CompositeCacheAttributes;
+import org.apache.commons.jcs.engine.ElementAttributes;
+import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
+import org.apache.commons.jcs.engine.behavior.IElementAttributes;
import org.mindrot.jbcrypt.BCrypt;
import org.onap.music.datastore.PreparedQueryObject;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
@@ -40,8 +47,10 @@ 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.datastore.jsonobjects.JsonCallback;
+import org.onap.music.service.impl.MusicZKCore;
+
import com.datastax.driver.core.DataType;
+import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.InvalidQueryException;
@@ -63,36 +72,50 @@ public class CachingUtil implements Runnable {
private static CacheAccess<String, Map<String, String>> aafCache = JCS.getInstance("aafCache");
private static CacheAccess<String, String> appNameCache = JCS.getInstance("appNameCache");
private static CacheAccess<String, Map<String, String>> musicValidateCache = JCS.getInstance("musicValidateCache");
- private static CacheAccess<String, JsonCallback> callBackCache = JCS.getInstance("callBackCache");
- private static CacheAccess<String, List<String>> callbackNotifyList = JCS.getInstance("callbackNotifyList");
+ 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() {
+ return adminUserCache;
+ }
+
+ public static void updateAdminUserCache(String authorization,String userId) {
+ 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 updateCallBackCache(String appName, JsonCallback jsonCallBack) {
- logger.info("updateCallBackCache: updating cache.....");
- callBackCache.put(appName, jsonCallBack);
- }
-
- public static JsonCallback getCallBackCache(String appName) {
- return callBackCache.get(appName);
- }
-
public static void updateCallbackNotifyList(List<String> notifyList) {
- logger.info("callbackNotifyList: updating cache.....");
- callbackNotifyList.put("callbackNotify", notifyList);
+ logger.info("callbackNotifyList: updating cache.....");
+ callbackNotifyList.put("callbackNotify", notifyList);
}
public static List<String> getCallbackNotifyList() {
- return callbackNotifyList.get("callbackNotify");
+ return callbackNotifyList.get("callbackNotify");
}
public void initializeMusicCache() {
@@ -110,6 +133,7 @@ public class CachingUtil implements Runnable {
pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), false));
} catch (Exception e1) {
logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),AppMessages.CACHEERROR, ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
+ e1.printStackTrace();
}
ResultSet rs = MusicCore.get(pQuery);
Iterator<Row> it = rs.iterator();
@@ -134,6 +158,7 @@ public class CachingUtil implements Runnable {
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.INFO, ErrorTypes.GENERALSERVICEERROR);
logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Something at AAF was changed for ns: " + nameSpace+" So not updating Cache for the namespace. ");
+ e.printStackTrace();
}
}
@@ -141,12 +166,12 @@ public class CachingUtil implements Runnable {
@Override
public void run() {
- logger.info(EELFLoggerDelegate.applicationLogger,"Scheduled task invoked. Refreshing Cache...");
+ logger.info(EELFLoggerDelegate.applicationLogger,"Scheduled task invoked. Refreshing Cache...");
try {
- initializeAafCache();
- } catch (MusicServiceException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.INFO, ErrorTypes.GENERALSERVICEERROR);
- }
+ initializeAafCache();
+ } catch (MusicServiceException e) {
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.INFO, ErrorTypes.GENERALSERVICEERROR);
+ }
}
public static boolean authenticateAAFUser(String nameSpace, String userId, String password,
@@ -154,9 +179,9 @@ public class CachingUtil implements Runnable {
if (aafCache.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.");
+ logger.info(EELFLoggerDelegate.applicationLogger,"Create new application for the same namespace.");
} else if (aafCache.get(nameSpace).get(userId).equals(password)) {
- logger.info(EELFLoggerDelegate.applicationLogger,"Authenticated with cache value..");
+ logger.info(EELFLoggerDelegate.applicationLogger,"Authenticated with cache value..");
// reset invalid attempts to 0
userAttempts.put(nameSpace, 0);
return true;
@@ -174,7 +199,7 @@ public class CachingUtil implements Runnable {
logger.info(EELFLoggerDelegate.applicationLogger,"Resetting failed attempt.");
userAttempts.put(nameSpace, 0);
} else {
- logger.info(EELFLoggerDelegate.applicationLogger,"No more attempts allowed. Please wait for atleast 2 min.");
+ logger.info(EELFLoggerDelegate.applicationLogger,"No more attempts allowed. Please wait for atleast 2 min.");
throw new Exception("No more attempts allowed. Please wait for atleast 2 min.");
}
}
@@ -183,15 +208,21 @@ public class CachingUtil implements Runnable {
}
}
- boolean responseObj = triggerAAF(nameSpace, userId, password);
+ boolean responseObj = false;
+ try {
+ responseObj = triggerAAF(nameSpace, userId, password);
+ }catch (Exception ex) {
+ logger.info("Exception while trigger aaf");
+ logger.info("Exception: " + ex.getMessage());
+ throw new Exception("Exception raised while triggering AAF authentication" +ex.getMessage());
+ }
if (responseObj) {
- //if (responseObj.getNs().get(0).getAdmin().contains(userId)) {
- Map<String, String> map = new HashMap<>();
+ 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);
- CachingUtil.updateMusicCache(keySpace, nameSpace);
- return true;
- //}
+ musicCache.put(keySpace, nameSpace);
+ return true;
}
logger.info(EELFLoggerDelegate.applicationLogger,"Invalid user. Cache not updated");
return false;
@@ -199,8 +230,9 @@ public class CachingUtil implements Runnable {
private static boolean triggerAAF(String nameSpace, String userId, String password)
throws Exception {
+ logger.info(EELFLoggerDelegate.applicationLogger,"Inside AAF authorization");
if (MusicUtil.getAafEndpointUrl() == null) {
- logger.error(EELFLoggerDelegate.errorLogger,"",AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,"AAF endpoint is not set. Please specify in the properties file.",AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
throw new Exception("AAF endpoint is not set. Please specify in the properties file.");
}
Client client = Client.create();
@@ -215,6 +247,7 @@ public class CachingUtil implements Runnable {
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
.header("Authorization", "Basic " + base64Creds)
.header("content-type", "application/json").get(ClientResponse.class);
+ logger.info(EELFLoggerDelegate.applicationLogger, "aaf response: "+response.toString());
if (response.getStatus() != 200) {
if (userAttempts.get(nameSpace) == null)
userAttempts.put(nameSpace, 0);
@@ -245,6 +278,14 @@ public class CachingUtil implements Runnable {
musicCache.put(keyspace, nameSpace);
}
+ public static void updateCadiCache(String user, String keyspace) {
+ musicCache.put(user, keyspace);
+ }
+
+ public static String getKSFromCadiCache(String user) {
+ return musicCache.get(user);
+ }
+
public static void updateMusicValidateCache(String nameSpace, String userId, String password) {
logger.info(EELFLoggerDelegate.applicationLogger,"Updating musicCache for nameSpacce " + nameSpace + " with userId " + userId);
Map<String, String> map = new HashMap<>();
@@ -275,7 +316,8 @@ public class CachingUtil implements Runnable {
if(isAAF != null)
appNameCache.put(namespace, isAAF);
} catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR,ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR,ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ e.printStackTrace();
}
}
return isAAF;
@@ -293,6 +335,7 @@ public class CachingUtil implements Runnable {
uuid = rs.getUUID("uuid").toString();
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger,"Exception occured during uuid retrieval from DB."+e.getMessage());
+ e.printStackTrace();
}
}
return uuid;
@@ -308,7 +351,8 @@ public class CachingUtil implements Runnable {
try {
appName = rs.getString("application_name");
} catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR, ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR, ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+ e.printStackTrace();
}
return appName;
}
@@ -334,8 +378,8 @@ public class CachingUtil implements Runnable {
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) {
- 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,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,"One or more required headers is missing. userId: "+userId+" :: password: "+password);
resultMap.put("Exception",
"One or more required headers appName(ns), userId, password is missing. Please check.");
return resultMap;
@@ -344,19 +388,20 @@ public class CachingUtil implements Runnable {
queryObject.appendQueryString(
"select * from admin.keyspace_master where application_name = ? allow filtering");
try {
- queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns));
+ queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns));
} catch(Exception e) {
- resultMap.put("Exception",
+ resultMap.put("Exception",
"Unable to process input data. Invalid input data type. Please check ns, userId and password values. "+e.getMessage());
- return resultMap;
+ return resultMap;
}
Row rs = null;
- try {
- rs = MusicCore.get(queryObject).one();
- } catch (MusicServiceException e) {
- // TODO Auto-generated catch block
- resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage());
- return resultMap;
+ try {
+ rs = MusicCore.get(queryObject).one();
+ } catch (MusicServiceException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage());
+ return resultMap;
} catch (InvalidQueryException e) {
logger.error(EELFLoggerDelegate.errorLogger,"Exception admin keyspace not configured."+e.getMessage());
resultMap.put("Exception", "Please make sure admin.keyspace_master table is configured.");
@@ -366,7 +411,7 @@ public class CachingUtil implements Runnable {
logger.error(EELFLoggerDelegate.errorLogger,"Application is not onboarded. Please contact admin.");
resultMap.put("Exception", "Application is not onboarded. Please contact admin.");
} else {
- if(!(rs.getString(USERNAME).equals(userId)) || !(BCrypt.checkpw(password, rs.getString(PASSWORD)))) {
+ if(!(rs.getString(USERNAME).equals(userId)) || !(BCrypt.checkpw(password, rs.getString(PASSWORD)))) {
logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
logger.error(EELFLoggerDelegate.errorLogger,"Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId);
resultMap.put("Exception", "Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId);
@@ -398,13 +443,14 @@ public class CachingUtil implements Runnable {
try {
queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspace));
} catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR, ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ e.printStackTrace();
}
Row rs = null;
try {
rs = MusicCore.get(queryObject).one();
} catch (MusicServiceException e) {
- resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage());
+ e.printStackTrace();
+ resultMap.put("Exception", "Unable to process operation. Error is "+e.getMessage());
return resultMap;
}
if(rs == null) {
@@ -440,7 +486,7 @@ public class CachingUtil implements Runnable {
try {
MusicCore.nonKeyRelatedPut(pQuery, "eventual");
} catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR, "Error in deleteKeysFromDB");
+ e.printStackTrace();
}
}
}
diff --git a/src/main/java/org/onap/music/main/CronJobManager.java b/src/main/java/org/onap/music/main/CronJobManager.java
index 504ff20e..9cd9f33f 100644
--- a/src/main/java/org/onap/music/main/CronJobManager.java
+++ b/src/main/java/org/onap/music/main/CronJobManager.java
@@ -19,8 +19,11 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.main;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -31,48 +34,46 @@ import javax.servlet.annotation.WebListener;
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;
-//@WebListener
-public class CronJobManager implements ServletContextListener {
+@Component
+public class CronJobManager {
- private ScheduledExecutorService scheduler;
private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CronJobManager.class);
- @Override
- public void contextInitialized(ServletContextEvent event) {
- scheduler = Executors.newSingleThreadScheduledExecutor();
- scheduler.scheduleAtFixedRate(new CachingUtil(), 0, 24, TimeUnit.HOURS);
+ 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.getMessage(),ErrorSeverity.ERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,e1.getMessage(),AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
+ e1.printStackTrace();
}
-
- //Zookeeper cleanup
- scheduler.scheduleAtFixedRate(new Runnable() {
- @Override
- public void run() {
- deleteLocksFromDB();
- }
- } , 0, 24, TimeUnit.HOURS);
- }
-
- @Override
- public void contextDestroyed(ServletContextEvent event) {
- scheduler.shutdownNow();
- }
-
- public void deleteLocksFromDB() {
- PreparedQueryObject pQuery = new PreparedQueryObject();
+
+ pQuery = new PreparedQueryObject();
pQuery.appendQueryString(
"select * from admin.locks");
try {
@@ -101,8 +102,8 @@ public class CronJobManager implements ServletContextListener {
CachingUtil.deleteKeysFromDB(deleteKeys.toString());
}
} catch (MusicServiceException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),ErrorSeverity.ERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(),AppMessages.CACHEERROR,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
+ e.printStackTrace();
}
}
-
}
diff --git a/src/main/java/org/onap/music/main/MusicCore.java b/src/main/java/org/onap/music/main/MusicCore.java
index 98696bab..221e680b 100644
--- a/src/main/java/org/onap/music/main/MusicCore.java
+++ b/src/main/java/org/onap/music/main/MusicCore.java
@@ -7,996 +7,156 @@
* 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;
+package org.onap.music.main;
-import java.io.StringWriter;
-import java.util.HashMap;
+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.MusicDataStore;
+import org.onap.music.datastore.Condition;
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.MusicLockState;
-import org.onap.music.lockingservice.MusicLockState.LockStatus;
-import org.onap.music.lockingservice.MusicLockingService;
+import org.onap.music.lockingservice.cassandra.CassaLockStore;
+import org.onap.music.lockingservice.cassandra.MusicLockState;
+import org.onap.music.service.MusicCoreService;
+import org.onap.music.service.impl.MusicCassaCore;
-import com.datastax.driver.core.ColumnDefinitions;
-import com.datastax.driver.core.ColumnDefinitions.Definition;
-import com.datastax.driver.core.DataType;
import com.datastax.driver.core.ResultSet;
-import com.datastax.driver.core.Row;
-import com.datastax.driver.core.TableMetadata;
-/**
- * This class .....
- *
- *
- */
public class MusicCore {
- public static MusicLockingService mLockHandle = null;
- public static MusicDataStore mDstoreHandle = null;
private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicCore.class);
-
- public static class Condition {
- Map<String, Object> conditions;
- PreparedQueryObject selectQueryForTheRow;
-
- public Condition(Map<String, Object> conditions, PreparedQueryObject selectQueryForTheRow) {
- this.conditions = conditions;
- this.selectQueryForTheRow = selectQueryForTheRow;
- }
-
- public boolean testCondition() throws Exception {
- // first generate the row
- ResultSet results = quorumGet(selectQueryForTheRow);
- Row row = null;
- if(results != null) {
- row = results.one();
- }
- return getDSHandle().doesRowSatisfyCondition(row, conditions);
- }
- }
-
-
- 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;
- }
-
- /**
- *
- * @param remoteIp
- * @return
- */
- public static MusicDataStore getDSHandle(String remoteIp) {
- logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle");
- long start = System.currentTimeMillis();
- if (mDstoreHandle == null) {
- mDstoreHandle = new MusicDataStore(remoteIp);
- }
- long end = System.currentTimeMillis();
- logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
- return mDstoreHandle;
- }
-
- /**
- *
- * @return
- * @throws MusicServiceException
- */
- public static MusicDataStore getDSHandle() throws MusicServiceException {
- logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle");
- long start = System.currentTimeMillis();
- if (mDstoreHandle == null) {
- // Quick Fix - Best to put this into every call to getDSHandle?
- if (! "localhost".equals(MusicUtil.getMyCassaHost())) {
- mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost());
- } else {
- mDstoreHandle = new MusicDataStore();
- }
- }
- if(mDstoreHandle.getSession() == null) {
- String message = "Connection to Cassandra has not been enstablished."
- + " Please check connection properites and reboot.";
- logger.info(EELFLoggerDelegate.applicationLogger, message);
- throw new MusicServiceException(message);
- }
- long end = System.currentTimeMillis();
- logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
- return mDstoreHandle;
- }
-
- public static 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;
- }
-
+ private static boolean unitTestRun=true;
+
+ private static MusicCoreService musicCore = MusicUtil.getMusicCoreService();
+ public static CassaLockStore mLockHandle;
+
+
/**
- *
- * @param key
+ * Acquire lock
+ * @param fullyQualifiedKey DO NOT RELY ON THIS KEY WORKING. INCLUDE THE KEY IN THE LOCKID.
+ * @param lockId - the full lock id (key + lockRef)
* @return
+ * @throws MusicLockingException
+ * @throws MusicQueryException
+ * @throws MusicServiceException
*/
- 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, AppMessages.INVALIDLOCK,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
- }
- return null;
- }
-
- public static 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 static ReturnType acquireLock(String fullyQualifiedKey, String lockId)
+ throws MusicLockingException, MusicQueryException, MusicServiceException {
+ return musicCore.acquireLock(fullyQualifiedKey, lockId);
}
-
- public static 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");
+
+ public static ReturnType acquireLockWithLease(String key, String lockId, long leasePeriod)
+ throws MusicLockingException, MusicQueryException, MusicServiceException {
+ return musicCore.acquireLockWithLease(key, lockId, leasePeriod);
}
-
-
-
- /**
- *
- * @param keyspaceName
- * @param kspObject
- * @return
- * @throws Exception
- */
- public boolean createKeyspace(String keyspaceName, JsonKeySpace kspObject) throws Exception {
- return true;
+
+ public static String createLockReference(String fullyQualifiedKey) {
+ return musicCore.createLockReference(fullyQualifiedKey);
}
-
-
- 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 = 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);
- ResultSet results = null;
- try {
- results = getDSHandle().executeCriticalGet(selectQuery);
- // write it back to a quorum
- Row row = results.one();
- ColumnDefinitions colInfo = row.getColumnDefinitions();
- int totalColumns = colInfo.size();
- int counter = 1;
- StringBuilder fieldValueString = new StringBuilder("");
- for (Definition definition : colInfo) {
- String colName = definition.getName();
- if (colName.equals(primaryKeyName))
- continue;
- DataType colType = definition.getType();
- Object valueObj = getDSHandle().getColValue(row, colName, colType);
- Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
- fieldValueString.append(colName + " = ?");
- updateQuery.addValue(valueString);
- if (counter != (totalColumns - 1))
- fieldValueString.append(",");
- counter = counter + 1;
- }
- updateQuery.appendQueryString("UPDATE " + keyspaceName + "." + tableName + " SET "
- + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";");
- updateQuery.addValue(cqlFormattedPrimaryKeyValue);
-
- getDSHandle().executePut(updateQuery, "critical");
- } catch (MusicServiceException | MusicQueryException e) {
- logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.QUERYERROR +""+updateQuery ,ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR);
- }
+
+ public static ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject,
+ String consistency) throws MusicServiceException {
+ return musicCore.createTable(keyspace, table, tableQueryObject, consistency);
}
-
-
-
-
- /**
- *
- * @param query
- * @return ResultSet
- */
+
public static ResultSet quorumGet(PreparedQueryObject query) {
- ResultSet results = null;
- try {
- results = getDSHandle().executeCriticalGet(query);
- } catch (MusicServiceException | MusicQueryException e) {
- logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.MAJOR, ErrorTypes.GENERALSERVICEERROR);
-
- }
- return results;
-
+ return musicCore.quorumGet(query);
}
-
- /**
- *
- * @param results
- * @return
- * @throws MusicServiceException
- */
- public static Map<String, HashMap<String, Object>> marshallResults(ResultSet results) throws MusicServiceException {
- return getDSHandle().marshalData(results);
- }
-
- /**
- *
- * @param lockName
- * @return
- */
- public static 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 static String whoseTurnIsIt(String fullyQualifiedKey) {
+ return musicCore.whoseTurnIsIt(fullyQualifiedKey);
}
-
+
public static 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 static 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;
+ musicCore.destroyLockRef(lockId);
}
- public static void voluntaryReleaseLock(String lockId) throws MusicLockingException{
- try {
- getLockingServiceHandle().unlockAndDeleteId(lockId);
- } catch (KeeperException.NoNodeException e) {
- // ??? No way
- }
- }
-
- /**
- *
- * @param lockName
- * @throws MusicLockingException
- */
- public static 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");
+ public static ReturnType eventualPut(PreparedQueryObject queryObject) {
+ return musicCore.eventualPut(queryObject);
}
-
-
-
- /**
- *
- * @param keyspace
- * @param tablename
- * @return
- * @throws MusicServiceException
- */
- public static TableMetadata returnColumnMetadata(String keyspace, String tablename) throws MusicServiceException {
- return getDSHandle().returnColumnMetadata(keyspace, tablename);
+
+ public static ReturnType eventualPut_nb(PreparedQueryObject queryObject,String keyspace,
+ String tablename,String primaryKey) {
+ return musicCore.eventualPut_nb(queryObject, keyspace, tablename, primaryKey);
}
-
-
- /**
- *
- * @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);
- }
+
+ public static ReturnType criticalPut(String keyspace, String table, String primaryKeyValue,
+ PreparedQueryObject queryObject, String lockReference, Condition conditionInfo) {
+ return musicCore.criticalPut(keyspace, table, primaryKeyValue, queryObject, lockReference, conditionInfo);
}
-
- /**
- *
- * @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");
+
+ public static ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency)
+ throws MusicServiceException {
+ return musicCore.nonKeyRelatedPut(queryObject, consistency);
}
-
- /**
- *
- * @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;
+
+ public static ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException{
+ return musicCore.get(queryObject);
}
-
-
-
- // Prepared Query Additions.
-
- /**
- *
- * @param keyspaceName
- * @param tableName
- * @param primaryKey
- * @param queryObject
- * @return ReturnType
- * @throws MusicServiceException
- */
- public static ReturnType eventualPut(PreparedQueryObject queryObject) {
- boolean result = false;
- try {
- result = getDSHandle().executePut(queryObject, MusicUtil.EVENTUAL);
- } catch (MusicServiceException | MusicQueryException ex) {
- logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
- logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() + " " + ex.getCause() + " " + ex);
- return new ReturnType(ResultType.FAILURE, ex.getMessage());
- }
- if (result) {
- return new ReturnType(ResultType.SUCCESS, "Success");
- } else {
- return new ReturnType(ResultType.FAILURE, "Failure");
- }
+
+ public static ResultSet criticalGet(String keyspace, String table, String primaryKeyValue,
+ PreparedQueryObject queryObject, String lockReference) throws MusicServiceException {
+ return musicCore.criticalGet(keyspace, table, primaryKeyValue, queryObject,lockReference);
}
-
- /**
- *
- * @param keyspaceName
- * @param tableName
- * @param primaryKey
- * @param queryObject
- * @param lockId
- * @return
- */
- public static 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());
- }
- getDSHandle().executePut(queryObject, MusicUtil.CRITICAL);
- long end = System.currentTimeMillis();
- logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms");
- return new ReturnType(ResultType.SUCCESS, "Update performed");
- } 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());
- }
-
+
+ public static ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException,
+ MusicQueryException,MusicServiceException {
+ return musicCore.atomicPut(keyspaceName, tableName, primaryKey, queryObject, conditionInfo);
}
-
- /**
- *
- * @param queryObject
- * @param consistency
- * @return Boolean Indicates success or failure
- * @throws MusicServiceException
- *
- *
- */
- public static ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException {
- // this is mainly for some functions like keyspace creation etc which does not
- // really need the bells and whistles of Music locking.
- boolean result = false;
- try {
- result = getDSHandle().executePut(queryObject, consistency);
- } catch (MusicQueryException | MusicServiceException ex) {
- logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
- throw new MusicServiceException(ex.getMessage());
- }
- return result?ResultType.SUCCESS:ResultType.FAILURE;
+
+ public static ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException, MusicQueryException {
+ return musicCore.atomicGet(keyspaceName, tableName, primaryKey, queryObject);
}
-
- /**
- * This method performs DDL operation on cassandra.
- *
- * @param queryObject query object containing prepared query and values
- * @return ResultSet
- * @throws MusicServiceException
- */
- public static ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException {
- ResultSet results = null;
- try {
- results = getDSHandle().executeEventualGet(queryObject);
- } catch (MusicQueryException | MusicServiceException e) {
- logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
- throw new MusicServiceException(e.getMessage());
- }
- return results;
+
+ public static List<String> getLockQueue(String fullyQualifiedKey)
+ throws MusicServiceException, MusicQueryException, MusicLockingException {
+ return musicCore.getLockQueue(fullyQualifiedKey);
}
- public static String getMyHostId() {
- PreparedQueryObject pQuery = new PreparedQueryObject();
- pQuery.appendQueryString("SELECT HOST_ID FROM SYSTEM.LOCAL");
- ResultSet rs = null;
- try {
- rs = getDSHandle().executeEventualGet(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 static 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 = getDSHandle().executeCriticalGet(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;
+ public static long getLockQueueSize(String fullyQualifiedKey)
+ throws MusicServiceException, MusicQueryException, MusicLockingException {
+ return musicCore.getLockQueueSize(fullyQualifiedKey);
}
- /**
- * 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 static 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;
- }
+ public static void deleteLock(String lockName) throws MusicLockingException {
+ musicCore.deleteLock(lockName);
}
- /**
- * 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 static 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;
- }
+ String primaryKey, PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException {
+ return musicCore.atomicPutWithDeleteLock(keyspaceName, tableName, primaryKey, queryObject, conditionInfo);
}
-
-
-
- /**
- * 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 static 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 static ResultSet atomicGetWithDeleteLock(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException {
+ return musicCore.atomicGetWithDeleteLock(keyspaceName, tableName, primaryKey, queryObject);
}
-
- public static 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;
- }
- }
-
-
-
- /**
- * 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 {
- Map<String, Object> resultMap = new HashMap<>();
- String uuid = null;
- resultMap = CachingUtil.validateRequest(nameSpace, userId, password, keyspace, aid,
- operation);
- if (!resultMap.isEmpty())
- return resultMap;
- String isAAFApp = null;
- try {
- isAAFApp= CachingUtil.isAAFApplication(nameSpace);
- } catch(MusicServiceException e) {
- resultMap.put("Exception", e.getMessage());
- return resultMap;
- }
- if(isAAFApp == null) {
- resultMap.put("Exception", "Namespace: "+nameSpace+" doesn't exist. Please make sure ns(appName)"
- + " is correct and Application is onboarded.");
- return resultMap;
- }
- boolean isAAF = Boolean.valueOf(isAAFApp);
- if (userId == null || password == null) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
- logger.error(EELFLoggerDelegate.errorLogger,"One or more required headers is missing. userId: " + userId
- + " :: password: " + password);
- resultMap.put("Exception",
- "UserId and Password are mandatory for the operation " + operation);
- return resultMap;
- }
- if(!isAAF && !(operation.equals("createKeySpace"))) {
- resultMap = CachingUtil.authenticateAIDUser(nameSpace, userId, password, keyspace);
- if (!resultMap.isEmpty())
- return resultMap;
-
- }
- if (isAAF && nameSpace != null && userId != null && password != null) {
- boolean isValid = true;
- try {
- isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace);
- } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
- logger.error(EELFLoggerDelegate.errorLogger,"Got exception while AAF authentication for namespace " + nameSpace);
- resultMap.put("Exception", e.getMessage());
- }
- if (!isValid) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
- resultMap.put("Exception", "User not authenticated...");
- }
- if (!resultMap.isEmpty())
- return resultMap;
- }
-
- if (operation.equals("createKeySpace")) {
- logger.info(EELFLoggerDelegate.applicationLogger,"AID is not provided. Creating new UUID for keyspace.");
- PreparedQueryObject pQuery = new PreparedQueryObject();
- pQuery.appendQueryString(
- "select uuid from admin.keyspace_master where application_name=? and username=? and keyspace_name=? allow filtering");
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
- MusicUtil.DEFAULTKEYSPACENAME));
-
- try {
- Row rs = MusicCore.get(pQuery).one();
- uuid = rs.getUUID("uuid").toString();
- resultMap.put("uuid", "existing");
- } catch (Exception e) {
- logger.info(EELFLoggerDelegate.applicationLogger,"No UUID found in DB. So creating new UUID.");
- uuid = CachingUtil.generateUUID();
- resultMap.put("uuid", "new");
- }
- resultMap.put("aid", uuid);
- }
+ public static Map<String, Object> validateLock(String lockName) {
+ return musicCore.validateLock(lockName);
+ }
- return resultMap;
+ public static MusicLockState releaseLock(String lockId, boolean voluntaryRelease) {
+ return musicCore.releaseLock(lockId, voluntaryRelease);
}
- /**
- * @param lockName
- * @return
- */
- public static Map<String, Object> validateLock(String lockName) {
- Map<String, Object> resultMap = new HashMap<>();
- String[] locks = lockName.split("\\.");
- if(locks.length < 3) {
- resultMap.put("Exception", "Invalid lock. Please make sure lock is of the type keyspaceName.tableName.primaryKey");
- return resultMap;
- }
- String keyspace= locks[0];
- if(keyspace.startsWith("$"))
- keyspace = keyspace.substring(1);
- resultMap.put("keyspace",keyspace);
- return resultMap;
- }
-}
+
+} \ No newline at end of file
diff --git a/src/main/java/org/onap/music/main/MusicDigest.java b/src/main/java/org/onap/music/main/MusicDigest.java
index 893cb51f..d05969e3 100644
--- a/src/main/java/org/onap/music/main/MusicDigest.java
+++ b/src/main/java/org/onap/music/main/MusicDigest.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.main;
/**
diff --git a/src/main/java/org/onap/music/main/MusicUtil.java b/src/main/java/org/onap/music/main/MusicUtil.java
index d2e8591c..805f459f 100755
--- a/src/main/java/org/onap/music/main/MusicUtil.java
+++ b/src/main/java/org/onap/music/main/MusicUtil.java
@@ -9,18 +9,19 @@
* 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.File;
@@ -45,24 +46,34 @@ import javax.ws.rs.core.Response.ResponseBuilder;
import org.onap.music.datastore.PreparedQueryObject;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
+import org.onap.music.exceptions.MusicQueryException;
+import org.onap.music.exceptions.MusicServiceException;
+import org.onap.music.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;
import com.sun.jersey.core.util.Base64;
/**
* @author nelson24
- *
+ *
* Properties This will take Properties and load them into MusicUtil.
* This is a hack for now. Eventually it would bebest to do this in
* another way.
- *
+ *
*/
public class MusicUtil {
private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
-
+
public static final String ATOMIC = "atomic";
public static final String EVENTUAL = "eventual";
public static final String CRITICAL = "critical";
+ public static final String EVENTUAL_NB = "eventual_nb";
+ public static final String ALL = "all";
+ public static final String QUORUM = "quorum";
+ public static final String ONE = "one";
public static final String ATOMICDELETELOCK = "atomic_delete_lock";
public static final String DEFAULTKEYSPACENAME = "TBD";
private static final String XLATESTVERSION = "X-latestVersion";
@@ -74,12 +85,14 @@ public class MusicUtil {
public static final String UPSERT = "upsert";
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";
+ public static final String AUTHORIZATION = "Authorization";
private static final String LOCALHOST = "localhost";
private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties";
-
+
private static int myId = 0;
private static ArrayList<String> allIds = new ArrayList<>();
private static String publicIp = "";
@@ -90,6 +103,9 @@ public class MusicUtil {
private static int cassandraPort = 9042;
private static int notifytimeout = 30000;
private static int notifyinterval = 5000;
+ private static int cacheObjectMaxLife = -1;
+ private static String lockUsing = MusicUtil.CASSANDRA;
+ private static boolean isCadi = false;
private static boolean debug = true;
private static String version = "2.3.0";
@@ -98,31 +114,118 @@ public class MusicUtil {
private static long defaultLockLeasePeriod = 6000;
private static final String[] propKeys = new String[] { "zookeeper.host", "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","cassandra.port", "notify.timeout", "notify.interval" };
+ "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"};
+ private static final String[] cosistencyLevel = new String[] {
+ "ALL","EACH_QUORUM","QUORUM","LOCAL_QUORUM","ONE","TWO","THREE","LOCAL_ONE","ANY","SERIAL","LOCAL_SERIAL"};
+ private static final Map<String,ConsistencyLevel> consistencyName = new HashMap<>();
+ static {
+ consistencyName.put("ONE",ConsistencyLevel.ONE);
+ consistencyName.put("TWO",ConsistencyLevel.TWO);
+ consistencyName.put("THREE",ConsistencyLevel.THREE);
+ consistencyName.put("SERIAL",ConsistencyLevel.SERIAL);
+ consistencyName.put("ALL",ConsistencyLevel.ALL);
+ consistencyName.put("EACH_QUORUM",ConsistencyLevel.EACH_QUORUM);
+ consistencyName.put("QUORUM",ConsistencyLevel.QUORUM);
+ consistencyName.put("LOCAL_QUORUM",ConsistencyLevel.LOCAL_QUORUM);
+ consistencyName.put("LOCAL_ONE",ConsistencyLevel.LOCAL_ONE);
+ consistencyName.put("LOCAL_SERIAL",ConsistencyLevel.LOCAL_SERIAL);
+ }
private static String cassName = "cassandra";
private static String cassPwd;
private static String aafEndpointUrl = null;
- public static final ConcurrentMap<String, Long> zkNodeMap = new ConcurrentHashMap<>();
+ public static ConcurrentMap<String, Long> zkNodeMap = new ConcurrentHashMap<>();
+ private static String adminId = "username";
+ private static String adminPass= "password";
+ private static String aafAdminUrl= null;
+ private static String musicNamespace= "com.att.music.api";
+ private static String adminAafRole= "com.att.music.api.admin_api";
+
+ public static final long MusicEternityEpochMillis = 1533081600000L; // Wednesday, August 1, 2018 12:00:00 AM
+
+ public static final long MaxLockReferenceTimePart = 1000000000000L; // millis after eternity (eq sometime in 2050)
+
+ public static final long MaxCriticalSectionDurationMillis = 1L * 24 * 60 * 60 * 1000; // 1 day
+
+
+ public static String getLockUsing() {
+ return lockUsing;
+ }
+
+
+ public static void setLockUsing(String lockUsing) {
+ MusicUtil.lockUsing = lockUsing;
+ }
+
+ public static String getAafAdminUrl() {
+ return aafAdminUrl;
+ }
+
+
+ public static void setAafAdminUrl(String aafAdminUrl) {
+ MusicUtil.aafAdminUrl = aafAdminUrl;
+ }
+
+
+ public static String getMusicNamespace() {
+ return musicNamespace;
+ }
+
+
+ public static void setMusicNamespace(String musicNamespace) {
+ MusicUtil.musicNamespace = musicNamespace;
+ }
+
+
+ public static String getAdminAafRole() {
+ return adminAafRole;
+ }
+
+
+ public static void setAdminAafRole(String adminAafRole) {
+ MusicUtil.adminAafRole = adminAafRole;
+ }
+
+
+
+ public static String getAdminId() {
+ return adminId;
+ }
+
+
+ public static void setAdminId(String adminId) {
+ MusicUtil.adminId = adminId;
+ }
+
+
+ public static String getAdminPass() {
+ return adminPass;
+ }
+
+ public static void setAdminPass(String adminPass) {
+ MusicUtil.adminPass = adminPass;
+ }
+
private MusicUtil() {
throw new IllegalStateException("Utility Class");
}
/**
- *
+ *
* @return cassandra port
*/
public static int getCassandraPort() {
- return cassandraPort;
- }
+ return cassandraPort;
+ }
/**
* set cassandra port
* @param cassandraPort
*/
- public static void setCassandraPort(int cassandraPort) {
- MusicUtil.cassandraPort = cassandraPort;
- }
+ public static void setCassandraPort(int cassandraPort) {
+ MusicUtil.cassandraPort = cassandraPort;
+ }
/**
* @return the cassName
*/
@@ -145,7 +248,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @param aafEndpointUrl
*/
public static void setAafEndpointUrl(String aafEndpointUrl) {
@@ -153,7 +256,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @return
*/
public static int getMyId() {
@@ -161,7 +264,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @param myId
*/
public static void setMyId(int myId) {
@@ -169,7 +272,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @return
*/
public static List<String> getAllIds() {
@@ -177,7 +280,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @param allIds
*/
public static void setAllIds(List<String> allIds) {
@@ -185,7 +288,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @return
*/
public static String getPublicIp() {
@@ -193,7 +296,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @param publicIp
*/
public static void setPublicIp(String publicIp) {
@@ -201,7 +304,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @return
*/
public static List<String> getAllPublicIps() {
@@ -209,7 +312,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @param allPublicIps
*/
public static void setAllPublicIps(List<String> allPublicIps) {
@@ -219,7 +322,7 @@ public class MusicUtil {
/**
* Returns An array of property names that should be in the Properties
* files.
- *
+ *
* @return
*/
public static String[] getPropkeys() {
@@ -228,7 +331,7 @@ public class MusicUtil {
/**
* Get MusicRestIp - default = localhost property file value - music.rest.ip
- *
+ *
* @return
*/
public static String getMusicRestIp() {
@@ -237,7 +340,7 @@ public class MusicUtil {
/**
* Set MusicRestIp
- *
+ *
* @param musicRestIp
*/
public static void setMusicRestIp(String musicRestIp) {
@@ -247,7 +350,7 @@ public class MusicUtil {
/**
* Get MusicPropertiesFilePath - Default = /opt/music/music.properties
* property file value - music.properties
- *
+ *
* @return
*/
public static String getMusicPropertiesFilePath() {
@@ -256,7 +359,7 @@ public class MusicUtil {
/**
* Set MusicPropertiesFilePath
- *
+ *
* @param musicPropertiesFilePath
*/
public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
@@ -266,7 +369,7 @@ public class MusicUtil {
/**
* Get DefaultLockLeasePeriod - Default = 6000 property file value -
* lock.lease.period
- *
+ *
* @return
*/
public static long getDefaultLockLeasePeriod() {
@@ -275,7 +378,7 @@ public class MusicUtil {
/**
* Set DefaultLockLeasePeriod
- *
+ *
* @param defaultLockLeasePeriod
*/
public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
@@ -284,7 +387,7 @@ public class MusicUtil {
/**
* Set Debug
- *
+ *
* @param debug
*/
public static void setDebug(boolean debug) {
@@ -293,7 +396,7 @@ public class MusicUtil {
/**
* Is Debug - Default = true property file value - debug
- *
+ *
* @return
*/
public static boolean isDebug() {
@@ -302,7 +405,7 @@ public class MusicUtil {
/**
* Set Version
- *
+ *
* @param version
*/
public static void setVersion(String version) {
@@ -311,7 +414,7 @@ public class MusicUtil {
/**
* Return the version property file value - version
- *
+ *
* @return
*/
public static String getVersion() {
@@ -321,7 +424,7 @@ public class MusicUtil {
/**
* Get MyZkHost - Zookeeper Hostname - Default = localhost property file
* value - zookeeper.host
- *
+ *
* @return
*/
public static String getMyZkHost() {
@@ -330,7 +433,7 @@ public class MusicUtil {
/**
* Set MyZkHost - Zookeeper Hostname
- *
+ *
* @param myZkHost
*/
public static void setMyZkHost(String myZkHost) {
@@ -340,7 +443,7 @@ public class MusicUtil {
/**
* Get MyCassHost - Cassandra Hostname - Default = localhost property file
* value - cassandra.host
- *
+ *
* @return
*/
public static String getMyCassaHost() {
@@ -349,7 +452,7 @@ public class MusicUtil {
/**
* Set MyCassHost - Cassandra Hostname
- *
+ *
* @param myCassaHost
*/
public static void setMyCassaHost(String myCassaHost) {
@@ -358,7 +461,7 @@ public class MusicUtil {
/**
* Get DefaultMusicIp - Default = localhost property file value - music.ip
- *
+ *
* @return
*/
public static String getDefaultMusicIp() {
@@ -367,7 +470,7 @@ public class MusicUtil {
/**
* Set DefaultMusicIp
- *
+ *
* @param defaultMusicIp
*/
public static void setDefaultMusicIp(String defaultMusicIp) {
@@ -375,7 +478,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @return
*/
public static String getTestType() {
@@ -384,7 +487,7 @@ public class MusicUtil {
Scanner fileScanner = new Scanner(new File(""));
testType = fileScanner.next();// ignore the my id line
@SuppressWarnings("unused")
- String batchSize = fileScanner.next();// ignore the my public ip
+ String batchSize = fileScanner.next();// ignore the my public ip
// line
fileScanner.close();
} catch (FileNotFoundException e) {
@@ -395,7 +498,7 @@ public class MusicUtil {
}
/**
- *
+ *
* @param time
*/
public static void sleep(long time) {
@@ -409,7 +512,7 @@ public class MusicUtil {
/**
* Utility function to check if the query object is valid.
- *
+ *
* @param withparams
* @param queryObject
* @return
@@ -439,7 +542,7 @@ public class MusicUtil {
}
@SuppressWarnings("unchecked")
- public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
+ public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
String value = "";
switch (type.getName()) {
@@ -465,15 +568,15 @@ public class MusicUtil {
}
/**
- *
+ *
* @param colType
* @param valueObj
* @return
- * @throws MusicTypeConversionException
+ * @throws MusicTypeConversionException
* @throws Exception
*/
@SuppressWarnings("unchecked")
- public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception {
+ public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception {
String valueObjString = valueObj + "";
switch (colType.getName()) {
case UUID:
@@ -492,8 +595,10 @@ public class MusicUtil {
return Boolean.parseBoolean(valueObjString);
case MAP:
return (Map<String, Object>) valueObj;
+ case LIST:
+ return (List<Object>)valueObj;
case BLOB:
-
+
default:
return valueObjString;
}
@@ -503,11 +608,11 @@ public class MusicUtil {
ByteBuffer buffer = ByteBuffer.wrap(valueObj);
return buffer;
}
-
+
/**
*
* Utility function to parse json map into sql like string
- *
+ *
* @param jMap
* @param lineDelimiter
* @return
@@ -540,13 +645,13 @@ public class MusicUtil {
}
return major;
}
-
+
/**
* Currently this will build a header with X-latestVersion, X-minorVersion and X-pathcVersion
* X-latestVerstion will be equal to the latest full version.
* X-minorVersion - will be equal to the latest minor version.
* X-pathVersion - will be equal to the latest patch version.
- * Future plans will change this.
+ * Future plans will change this.
* @param response
* @param major
* @param minor
@@ -558,78 +663,156 @@ public class MusicUtil {
String versionIn = buildVersion(major,minor,patch);
String version = MusicUtil.getVersion();
String[] verArray = version.split("\\.",3);
- if ( minor != null ) {
+ if ( minor != null ) {
response.header(XMINORVERSION,minor);
} else {
response.header(XMINORVERSION,verArray[1]);
- }
+ }
if ( patch != null ) {
response.header(XPATCHVERSION,patch);
} else {
response.header(XPATCHVERSION,verArray[2]);
- }
+ }
response.header(XLATESTVERSION,version);
logger.info(EELFLoggerDelegate.applicationLogger,"Version In:" + versionIn);
return response;
}
+
+
+ public static Map<String,String> extractBasicAuthentication(String authorization){
+ Map<String,String> authValues = new HashMap<>();
+ if(authorization == null) {
+ authValues.put("ERROR", "Authorization cannot be null");
+ return authValues;
+ }
+ authorization = authorization.replaceFirst("Basic", "");
+ String decoded = Base64.base64Decode(authorization);
+ StringTokenizer token = new StringTokenizer(decoded, ":");
+ authValues.put(MusicUtil.USERID, token.nextToken());
+ authValues.put(MusicUtil.PASSWORD,token.nextToken());
+ return authValues;
+
+ }
+
+ public static boolean isValidConsistency(String consistency) {
+ for (String string : cosistencyLevel) {
+ if (string.equalsIgnoreCase(consistency))
+ return true;
+ }
+ return false;
+
+ }
+
+ public static ConsistencyLevel getConsistencyLevel(String consistency) {
+ return consistencyName.get(consistency.toUpperCase());
+ }
+
+ public static void loadProperties() throws Exception {
+ Properties prop = new Properties();
+ InputStream input = null;
+ try {
+ // load the properties file
+ input = MusicUtil.class.getClassLoader().getResourceAsStream("music.properties");
+ prop.load(input);
+ } catch (Exception ex) {
+ logger.error(EELFLoggerDelegate.errorLogger, "Unable to find properties file.");
+ throw new Exception();
+ } finally {
+ if (input != null) {
+ try {
+ input.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ // 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")));
+ MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty("notify.timeout")));
+ MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty("notify.interval")));
+ MusicUtil.setCacheObjectMaxLife(Integer.parseInt(prop.getProperty("cacheobject.maxlife")));
+ }
+
+ public static void setNotifyInterval(int notifyinterval) {
+ MusicUtil.notifyinterval = notifyinterval;
+ }
+ public static void setNotifyTimeOut(int notifytimeout) {
+ MusicUtil.notifytimeout = notifytimeout;
+ }
+
+ public static int getNotifyInterval() {
+ return MusicUtil.notifyinterval;
+ }
+
+ public static int getNotifyTimeout() {
+ return MusicUtil.notifytimeout;
+ }
+
+ public static int getCacheObjectMaxLife() {
+ return MusicUtil.cacheObjectMaxLife;
+ }
+
+ public static void setCacheObjectMaxLife(int cacheObjectMaxLife) {
+ MusicUtil.cacheObjectMaxLife = cacheObjectMaxLife;
+ }
+ /**
+ * Given the time of write for an update in a critical section, this method provides a transformed timestamp
+ * that ensures that a previous lock holder who is still alive can never corrupt a later critical section.
+ * The main idea is to us the lock reference to clearly demarcate the timestamps across critical sections.
+ * @param the UUID lock reference associated with the write.
+ * @param the long timeOfWrite which is the actual time at which the write took place
+ * @throws MusicServiceException
+ * @throws MusicQueryException
+ */
+ public static long v2sTimeStampInMicroseconds(long ordinal, long timeOfWrite) throws MusicServiceException, MusicQueryException {
+ // TODO: use acquire time instead of music eternity epoch
+ long ts = ordinal * MaxLockReferenceTimePart + (timeOfWrite - MusicEternityEpochMillis);
+
+ return ts;
+ }
- public static Map<String,String> extractBasicAuthentication(String authorization){
-
- Map<String,String> authValues = new HashMap<>();
- authorization = authorization.replaceFirst("Basic", "");
- String decoded = Base64.base64Decode(authorization);
- StringTokenizer token = new StringTokenizer(decoded, ":");
- authValues.put(MusicUtil.USERID, token.nextToken());
- authValues.put(MusicUtil.PASSWORD,token.nextToken());
- return authValues;
-
+ 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();
}
- public static void loadProperties() throws Exception {
- Properties prop = new Properties();
- InputStream input = null;
- try {
- // load the properties file
- input = MusicUtil.class.getClassLoader().getResourceAsStream("music.properties");
- prop.load(input);
- } catch (Exception ex) {
- logger.error(EELFLoggerDelegate.errorLogger, "Unable to find properties file.");
- throw new Exception();
- } finally {
- if (input != null) {
- try {
- input.close();
- } catch (IOException e) {
- logger.error(EELFLoggerDelegate.applicationLogger,"Load properties failed "+e.getMessage(),e);
- }
- }
- }
- // 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")));
- MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty("notify.timeout")));
- MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty("notify.interval")));
-
- }
+ /**
+ * @param lockName
+ * @return
+ */
+ public static Map<String, Object> validateLock(String lockName) {
+ Map<String, Object> resultMap = new HashMap<>();
+ String[] locks = lockName.split("\\.");
+ if(locks.length < 3) {
+ resultMap.put("Error", "Invalid lock. Please make sure lock is of the type keyspaceName.tableName.primaryKey");
+ return resultMap;
+ }
+ String keyspace= locks[0];
+ if(keyspace.startsWith("$"))
+ keyspace = keyspace.substring(1);
+ resultMap.put("keyspace",keyspace);
+ return resultMap;
+ }
+
+
+ public static void setIsCadi(boolean isCadi) {
+ // TODO Auto-generated method stub
+ MusicUtil.isCadi = isCadi;
+ }
- public static void setNotifyInterval(int notifyinterval) {
- MusicUtil.notifyinterval = notifyinterval;
- }
- public static void setNotifyTimeOut(int notifytimeout) {
- MusicUtil.notifytimeout = notifytimeout;
- }
-
- public static int getNotifyInterval() {
- return MusicUtil.notifyinterval;
- }
-
- public static int getNotifyTimeout() {
- return MusicUtil.notifytimeout;
-
- }
+ public static boolean getIsCadi() {
+ return MusicUtil.isCadi;
+ }
+
}
+
diff --git a/src/main/java/org/onap/music/main/PropertiesListener.java b/src/main/java/org/onap/music/main/PropertiesListener.java
index 026790ee..c5e8c37c 100755
--- a/src/main/java/org/onap/music/main/PropertiesListener.java
+++ b/src/main/java/org/onap/music/main/PropertiesListener.java
@@ -20,6 +20,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.main;
import java.io.FileInputStream;
@@ -28,17 +29,33 @@ 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;
-public class PropertiesListener implements ServletContextListener {
+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
@@ -120,15 +137,37 @@ public class PropertiesListener implements ServletContextListener {
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;
+ MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty(key)));
+ break;
case "notify.timeout":
- MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty(key)));
- break;
+ 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);
@@ -136,7 +175,7 @@ public class PropertiesListener implements ServletContextListener {
}
}
} catch (IOException e) {
- logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR);
logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
}
@@ -148,10 +187,71 @@ public class PropertiesListener implements ServletContextListener {
"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
new file mode 100644
index 00000000..ee10db42
--- /dev/null
+++ b/src/main/java/org/onap/music/main/PropertiesLoader.java
@@ -0,0 +1,192 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.main;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.onap.music.eelf.logging.EELFLoggerDelegate;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
+import org.springframework.stereotype.Component;
+
+@PropertySource(value = {"file:/opt/app/music/etc/music.properties", "classpath:/project.properties"})
+@Component
+public class PropertiesLoader implements InitializingBean {
+
+ @Value("${zookeeper.host}")
+ private String zookeeperHost;
+
+ @Value("${cassandra.host}")
+ public String cassandraHost;
+
+ @Value("${music.ip}")
+ public String musicIp;
+
+ @Value("${debug}")
+ public String debug;
+
+ @Value("${version}")
+ public String version;
+
+ @Value("${music.rest.ip}")
+ public String musicRestIp;
+
+ @Value("${music.properties}")
+ public String musicProperties;
+
+ @Value("${lock.lease.period}")
+ public String lockLeasePeriod;
+
+ @Value("${public.ip}")
+ public String publicIp;
+
+ @Value("${my.id}")
+ public String myId;
+
+ @Value("${all.ids}")
+ public String allIds;
+
+ @Value("${all.public.ips}")
+ public String allPublicIps;
+
+ @Value("${cassandra.user}")
+ public String cassandraUser;
+
+ @Value("${cassandra.password}")
+ public String cassandraPassword;
+
+ @Value("${aaf.endpoint.url}")
+ public String aafEndpointUrl;
+
+ @Value("${admin.username}")
+ public String adminUsername;
+
+ @Value("${admin.password}")
+ public String adminPassword;
+
+ @Value("${cassandra.port}")
+ public String cassandraPort;
+
+ @Value("${aaf.admin.url}")
+ public String aafAdminUrl;
+
+ @Value("${music.namespace}")
+ public String musicNamespace;
+
+ @Value("${admin.aaf.role}")
+ public String adminAafRole;
+
+ @Value("${notify.interval}")
+ public String notifyInterval;
+
+ @Value("${notify.timeout}")
+ public String notifyTimeout;
+
+ @Value("${cadi}")
+ public String isCadi;
+
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesLoader.class);
+
+ @Bean
+ public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
+ //return new PropertySourcesPlaceholderConfigurer();
+ PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
+ pspc.setIgnoreResourceNotFound(true);
+ pspc.setIgnoreUnresolvablePlaceholders(true);
+ return pspc;
+ }
+
+ public void loadProperties () {
+ if(aafAdminUrl != null && !aafAdminUrl.equals("${aaf.admin.url}"))
+ MusicUtil.setAafAdminUrl(aafAdminUrl);
+ if(aafEndpointUrl != null && !aafEndpointUrl.equals("${aaf.endpoint.url}"))
+ MusicUtil.setAafEndpointUrl(aafEndpointUrl);
+ if(adminAafRole != null && !adminAafRole.equals("${admin.aaf.role}"))
+ MusicUtil.setAdminAafRole(adminAafRole);
+ //MusicUtil.setAdminId(adminId);
+ if(adminPassword != null && !adminPassword.equals("${admin.password}"))
+ MusicUtil.setAdminPass(adminPassword);
+ if(adminUsername != null && !adminUsername.equals("${admin.username}"))
+ MusicUtil.setAdminId(adminUsername);
+ if(allIds != null && !allIds.equals("${all.ids}")) {
+ String[] ids = allIds.split(":");
+ MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids)));
+ }
+ if(allPublicIps != null && !allPublicIps.equals("${all.public.ips}")) {
+ String[] ips = allPublicIps.split(":");
+ if (ips.length == 1) {
+ // Future use
+ } else if (ips.length > 1) {
+ MusicUtil.setAllPublicIps(
+ new ArrayList<String>(Arrays.asList(ips)));
+ }
+ }
+ if(cassandraPort != null && !cassandraPort.equals("${cassandra.port}"))
+ MusicUtil.setCassandraPort(Integer.parseInt(cassandraPort));
+ if(cassandraUser != null && !cassandraUser.equals("${cassandra.user}"))
+ MusicUtil.setCassName(cassandraUser);
+ if(cassandraPassword != null && !cassandraPassword.equals("${cassandra.password}"))
+ MusicUtil.setCassPwd(cassandraPassword);
+ if(debug != null && !debug.equals("${debug}"))
+ MusicUtil.setDebug(Boolean.parseBoolean(debug));
+ if(lockLeasePeriod != null && !lockLeasePeriod.equals("${lock.lease.period}"))
+ MusicUtil.setDefaultLockLeasePeriod(Long.parseLong(lockLeasePeriod));
+ if(musicIp != null && !musicIp.equals("${music.ip}"))
+ MusicUtil.setDefaultMusicIp(musicIp);
+ if(musicNamespace != null && !musicNamespace.equals("${music.namespace}"))
+ MusicUtil.setMusicNamespace(musicNamespace);
+ if(musicProperties != null && !musicProperties.equals("${music.properties}"))
+ MusicUtil.setMusicPropertiesFilePath(musicProperties);
+ if(musicRestIp != null && !musicRestIp.equals("${music.rest.ip}"))
+ MusicUtil.setMusicRestIp(musicRestIp);
+ if(cassandraHost != null && !cassandraHost.equals("${cassandra.host}"))
+ MusicUtil.setMyCassaHost(cassandraHost);
+ 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}"))
+ MusicUtil.setNotifyTimeOut(Integer.parseInt(notifyTimeout));
+ if(allPublicIps != null && !allPublicIps.equals("${public.ip}"))
+ MusicUtil.setPublicIp(allPublicIps);
+ if(version != null && !version.equals("${version}"))
+ MusicUtil.setVersion(version);
+ if(isCadi != null && !isCadi.equals("${cadi}"))
+ MusicUtil.setIsCadi(Boolean.parseBoolean(isCadi));
+ }
+
+
+ @Override
+ public void afterPropertiesSet() throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
+}
diff --git a/src/main/java/org/onap/music/main/ResultType.java b/src/main/java/org/onap/music/main/ResultType.java
index 61ba0295..f5ef2070 100644
--- a/src/main/java/org/onap/music/main/ResultType.java
+++ b/src/main/java/org/onap/music/main/ResultType.java
@@ -19,13 +19,14 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.main;
public enum ResultType {
SUCCESS("Success"), FAILURE("Failure"),
- SYNTAXERROR("SyntaxError"), EXCEPTION("Exception"),
- BODYMISSING("Incomplete Request body. Please correct your input request and retry.");
-
+ SYNTAXERROR("SyntaxError"), EXCEPTION("Exception"),
+ BODYMISSING("Incomplete Request body. Please correct your input request and retry.");
+
private String result;
ResultType(String result) {
diff --git a/src/main/java/org/onap/music/main/ReturnType.java b/src/main/java/org/onap/music/main/ReturnType.java
index 33f68d7d..f02dabbf 100644
--- a/src/main/java/org/onap/music/main/ReturnType.java
+++ b/src/main/java/org/onap/music/main/ReturnType.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.main;
import java.util.HashMap;
diff --git a/src/main/java/org/onap/music/response/jsonobjects/JsonResponse.java b/src/main/java/org/onap/music/response/jsonobjects/JsonResponse.java
index 207911d7..8e589efe 100644
--- a/src/main/java/org/onap/music/response/jsonobjects/JsonResponse.java
+++ b/src/main/java/org/onap/music/response/jsonobjects/JsonResponse.java
@@ -19,12 +19,13 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.response.jsonobjects;
import java.util.HashMap;
import java.util.Map;
-import org.onap.music.lockingservice.MusicLockState.LockStatus;
+import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus;
import org.onap.music.main.ResultType;
@@ -34,7 +35,7 @@ import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "JsonResponse", description = "General Response JSON")
public class JsonResponse {
- /* Status is required */
+ /* Status is required */
private ResultType status;
/* Standard informational fields */
@@ -64,7 +65,7 @@ public class JsonResponse {
this.status = status;
}
- /**
+ /**
*
* @return
*/
@@ -125,7 +126,7 @@ public class JsonResponse {
* @return the music version
*/
public String getMusicVersion() {
- return this.musicVersion;
+ return this.musicVersion;
}
/**
@@ -134,20 +135,20 @@ public class JsonResponse {
* @return
*/
public JsonResponse setMusicVersion(String version) {
- this.musicVersion = version;
- return this;
+ this.musicVersion = version;
+ return this;
}
public Map<String, HashMap<String, Object>> getDataResult() {
- return this.dataResult;
+ return this.dataResult;
}
public JsonResponse setDataResult(Map<String, HashMap<String, Object>> map) {
- this.dataResult = map;
- return this;
+ this.dataResult = map;
+ return this;
}
- /**
+ /**
*
* @return
*/
@@ -226,36 +227,40 @@ public class JsonResponse {
public Map<String, Object> toMap() {
Map<String, Object> fullMap = new HashMap<>();
fullMap.put("status", status);
+/*<<<<<<< HEAD
if (error!=null) {
fullMap.put("error", error);
}
if (message!=null) {
fullMap.put("message", message);
}
+=======*/
+ if (error!=null && !"".equals(error)) {fullMap.put("error", error);}
+ if (message!=null) {fullMap.put("message", message);}
if (musicVersion!=null) {
fullMap.put("version", musicVersion);
}
if (dataResult!=null) {
- fullMap.put("result", dataResult);
+ fullMap.put("result", dataResult);
}
if (lock!=null) {
- Map<String, Object> lockMap = new HashMap<>();
- if (lock!=null) {
- lockMap.put("lock", lock);
- }
- if (lockStatus!=null) {
- lockMap.put("lock-status", lockStatus);
- }
- if (lockHolder!=null) {
- lockMap.put("lock-holder", lockHolder);
- }
- if (lockLease!=null) {
- lockMap.put("lock-lease", lockLease);
- }
- fullMap.put("lock", lockMap);
+ Map<String, Object> lockMap = new HashMap<>();
+ if (lock!=null) {
+ lockMap.put("lock", lock);
+ }
+ if (lockStatus!=null) {
+ lockMap.put("lock-status", lockStatus);
+ }
+ if (lockHolder!=null) {
+ lockMap.put("lock-holder", lockHolder);
+ }
+ if (lockLease!=null) {
+ lockMap.put("lock-lease", lockLease);
+ }
+ fullMap.put("lock", lockMap);
}
return fullMap;
diff --git a/src/main/java/org/onap/music/rest/Application.java b/src/main/java/org/onap/music/rest/Application.java
new file mode 100644
index 00000000..5375155b
--- /dev/null
+++ b/src/main/java/org/onap/music/rest/Application.java
@@ -0,0 +1,79 @@
+/*
+ * ============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;
+
+public class Application {
+
+ private String application_name;
+ private String username;
+ private String password;
+ private String keyspace_name;
+ private boolean is_aaf;
+ private String uuid;
+ private boolean is_api;
+
+ public String getApplication_name() {
+ return application_name;
+ }
+ public void setApplication_name(String application_name) {
+ this.application_name = application_name;
+ }
+ public String getUsername() {
+ return username;
+ }
+ public void setUsername(String username) {
+ this.username = username;
+ }
+ public String getPassword() {
+ return password;
+ }
+ public void setPassword(String password) {
+ this.password = password;
+ }
+ public String getKeyspace_name() {
+ return keyspace_name;
+ }
+ public void setKeyspace_name(String keyspace_name) {
+ this.keyspace_name = keyspace_name;
+ }
+ public boolean isIs_aaf() {
+ return is_aaf;
+ }
+ public void setIs_aaf(boolean is_aaf) {
+ this.is_aaf = is_aaf;
+ }
+ public String getUuid() {
+ return uuid;
+ }
+ public void setUuid(String uuid) {
+ this.uuid = uuid;
+ }
+ public boolean getIs_api() {
+ return is_api;
+ }
+ public void setIs_api(boolean is_api) {
+ this.is_api = is_api;
+ }
+
+
+}
diff --git a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java
index 8e4facd2..289cab06 100755
--- a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java
+++ b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java
@@ -9,45 +9,33 @@
* 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.nio.ByteBuffer;
-import java.nio.charset.Charset;
-import java.security.KeyManagementException;
-import java.security.NoSuchAlgorithmException;
-import java.security.SecureRandom;
-import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
import java.util.UUID;
-import javax.net.ssl.HostnameVerifier;
-import javax.net.ssl.HttpsURLConnection;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLSession;
-import javax.net.ssl.TrustManager;
-import javax.net.ssl.X509TrustManager;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
@@ -57,44 +45,31 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
-import org.codehaus.jackson.map.ObjectMapper;
import org.mindrot.jbcrypt.BCrypt;
+import org.onap.music.authentication.MusicAuthentication;
import org.onap.music.datastore.PreparedQueryObject;
-import org.onap.music.datastore.jsonobjects.JSONCallbackResponse;
-import org.onap.music.datastore.jsonobjects.JSONObject;
-import org.onap.music.datastore.jsonobjects.JsonCallback;
-import org.onap.music.datastore.jsonobjects.JsonNotification;
-import org.onap.music.datastore.jsonobjects.JsonNotifyClientResponse;
import org.onap.music.datastore.jsonobjects.JsonOnboard;
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.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;
-import org.onap.music.main.ReturnType;
import org.onap.music.response.jsonobjects.JsonResponse;
-import com.datastax.driver.core.ColumnDefinitions;
-import com.datastax.driver.core.ColumnDefinitions.Definition;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
-import com.datastax.driver.core.TableMetadata;
-import com.datastax.driver.core.exceptions.InvalidQueryException;
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.api.client.config.ClientConfig;
-import com.sun.jersey.api.client.config.DefaultClientConfig;
-import com.sun.jersey.api.json.JSONConfiguration;
-import com.sun.jersey.client.urlconnection.HTTPSProperties;
-import com.sun.jersey.core.util.Base64;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+//import java.util.Base64.Encoder;
+//import java.util.Base64.Decoder;
@Path("/v2/admin")
// @Path("/v{version: [0-9]+}/admin")
@@ -103,16 +78,21 @@ import io.swagger.annotations.ApiOperation;
public class RestMusicAdminAPI {
private static EELFLoggerDelegate logger =
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;
+
/*
* API to onboard an application with MUSIC. This is the mandatory first step.
- *
+ *
*/
@POST
@Path("/onboardAppWithMusic")
@ApiOperation(value = "Onboard application", response = String.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- public Response onboardAppWithMusic(JsonOnboard jsonObj) throws Exception {
+ public Response onboardAppWithMusic(JsonOnboard jsonObj,
+ @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization) throws Exception {
+ logger.info(EELFLoggerDelegate.errorLogger, "oboarding app");
ResponseBuilder response =
Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
Map<String, Object> resultMap = new HashMap<>();
@@ -120,8 +100,22 @@ public class RestMusicAdminAPI {
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, "", AppMessages.MISSINGINFO,
+ 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.");
@@ -129,24 +123,24 @@ public class RestMusicAdminAPI {
}
PreparedQueryObject pQuery = new PreparedQueryObject();
- pQuery.appendQueryString(
- "select uuid from admin.keyspace_master where application_name = ? allow filtering");
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
- ResultSet rs = MusicCore.get(pQuery);
- if (!rs.all().isEmpty()) {
- resultMap.put("Exception", "Application " + appName
- + " has already been onboarded. Please contact admin.");
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
- }
-
- pQuery = new PreparedQueryObject();
+ /*
+ * pQuery.appendQueryString(
+ * "select uuid from admin.keyspace_master where application_name = ? allow filtering"
+ * ); pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
+ * appName)); ResultSet rs = MusicCore.get(pQuery); if (!rs.all().isEmpty()) {
+ * logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA
+ * ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
+ * response.status(Status.BAD_REQUEST); return response.entity(new
+ * JsonResponse(ResultType.FAILURE).setError("Application " + appName +
+ * " has already been onboarded. Please contact admin.").toMap()).build(); }
+ */
+ //pQuery = new PreparedQueryObject();
String uuid = CachingUtil.generateUUID();
pQuery.appendQueryString(
"INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
+ "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
- MusicUtil.DEFAULTKEYSPACENAME));
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),keyspace_name));
pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt())));
@@ -155,9 +149,10 @@ public class RestMusicAdminAPI {
String returnStr = MusicCore.eventualPut(pQuery).toString();
if (returnStr.contains("Failure")) {
- resultMap.put("Exception",
- "Oops. Something wrong with onboarding process. Please retry later or contact admin.");
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
+ response.status(Status.BAD_REQUEST);
+ return response.entity(new JsonResponse(ResultType.FAILURE).setError("Oops. Something wrong with onboarding process. "
+ + "Please retry later or contact admin.").toMap()).build();
}
CachingUtil.updateisAAFCache(appName, isAAF);
resultMap.put("Success", "Your application " + appName + " has been onboarded with MUSIC.");
@@ -171,15 +166,29 @@ public class RestMusicAdminAPI {
@ApiOperation(value = "Search Onboard application", response = String.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- public Response getOnboardedInfoSearch(JsonOnboard jsonObj) throws Exception {
+ 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());
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, "", AppMessages.MISSINGINFO,
+ 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);
resultMap.put("Exception",
"Unauthorized: Please check the request parameters. Enter atleast one of the following parameters: appName(ns), aid, isAAF.");
@@ -215,13 +224,14 @@ public class RestMusicAdminAPI {
}
if (resultMap.isEmpty()) {
if (uuid != null) {
- resultMap.put("Exception",
- "Please make sure Aid is correct and application is onboarded.");
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
+ response.status(Status.BAD_REQUEST);
+ return response.entity(new JsonResponse(ResultType.FAILURE).setError("Please make sure Aid is correct and application is onboarded.").toMap()).build();
+
} else {
- resultMap.put("Exception",
- "Application is not onboarded. Please make sure all the information is correct.");
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
+ response.status(Status.BAD_REQUEST);
+ return response.entity(new JsonResponse(ResultType.FAILURE).setError("Application is not onboarded. Please make sure all the information is correct.").toMap()).build();
}
}
return response.status(Status.OK).entity(resultMap).build();
@@ -233,32 +243,48 @@ public class RestMusicAdminAPI {
@ApiOperation(value = "Delete Onboard application", response = String.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- public Response deleteOnboardApp(JsonOnboard jsonObj) throws Exception {
+ 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());
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();
+ }
if (appName == null && aid == null) {
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO,
+ logger.error(EELFLoggerDelegate.errorLogger, "Please make sure either appName(ns) or Aid is present", AppMessages.MISSINGINFO,
ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
resultMap.put("Exception", "Please make sure either appName(ns) or Aid is present");
return response.status(Status.BAD_REQUEST).entity(resultMap).build();
}
if (aid != null) {
- pQuery.appendQueryString(
- "SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?");
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
- UUID.fromString(aid)));
- Row row = MusicCore.get(pQuery).one();
- if (row != null) {
- String ks = row.getString("keyspace_name");
- if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) {
- PreparedQueryObject queryObject = new PreparedQueryObject();
- queryObject.appendQueryString("DROP KEYSPACE IF EXISTS " + ks + ";");
- MusicCore.nonKeyRelatedPut(queryObject, consistency);
- }
+ if ( KEYSPACE_ACTIVE ) {
+ pQuery.appendQueryString(
+ "SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?");
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
+ UUID.fromString(aid)));
+ Row row = MusicCore.get(pQuery).one();
+ if (row != null) {
+ String ks = row.getString("keyspace_name");
+ if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) {
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ queryObject.appendQueryString("DROP KEYSPACE IF EXISTS " + ks + ";");
+ MusicCore.nonKeyRelatedPut(queryObject, consistency);
+ }
+ }
}
pQuery = new PreparedQueryObject();
pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ? IF EXISTS");
@@ -270,7 +296,7 @@ public class RestMusicAdminAPI {
} else {
resultMap.put("Exception",
"Oops. Something went wrong. Please make sure Aid is correct or Application is onboarded");
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA,
+ logger.error(EELFLoggerDelegate.errorLogger, "Oops. Something went wrong. Please make sure Aid is correct or Application is onboarded", AppMessages.INCORRECTDATA,
ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
return response.status(Status.BAD_REQUEST).entity(resultMap).build();
@@ -287,7 +313,7 @@ public class RestMusicAdminAPI {
if (rows.isEmpty()) {
resultMap.put("Exception",
"Application not found. Please make sure Application exists.");
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA,
+ logger.error(EELFLoggerDelegate.errorLogger, "Application not found. Please make sure Application exists.", AppMessages.INCORRECTDATA,
ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
return response.status(Status.BAD_REQUEST).entity(resultMap).build();
} else if (rows.size() == 1) {
@@ -315,7 +341,7 @@ public class RestMusicAdminAPI {
} else {
resultMap.put("Failure",
"More than one Aid exists for this application, so please provide Aid.");
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MULTIPLERECORDS,
+ logger.error(EELFLoggerDelegate.errorLogger, "More than one Aid exists for this application, so please provide Aid.", AppMessages.MULTIPLERECORDS,
ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
return response.status(Status.BAD_REQUEST).entity(resultMap).build();
}
@@ -327,7 +353,8 @@ public class RestMusicAdminAPI {
@ApiOperation(value = "Update Onboard application", response = String.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- public Response updateOnboardApp(JsonOnboard jsonObj) throws Exception {
+ 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());
Map<String, Object> resultMap = new HashMap<>();
String aid = jsonObj.getAid();
@@ -337,10 +364,22 @@ 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, "", AppMessages.MISSINGDATA,
+ logger.error(EELFLoggerDelegate.errorLogger, "Please make sure Aid is present", AppMessages.MISSINGDATA,
ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
return response.status(Status.BAD_REQUEST).entity(resultMap).build();
}
@@ -348,7 +387,7 @@ public class RestMusicAdminAPI {
if (appName == null && userId == null && password == null && isAAF == null) {
resultMap.put("Exception",
"No parameters found to update. Please update atleast one parameter.");
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
+ logger.error(EELFLoggerDelegate.errorLogger, "No parameters found to update. Please update atleast one parameter.", AppMessages.MISSINGDATA,
ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
return response.status(Status.BAD_REQUEST).entity(resultMap).build();
}
@@ -362,7 +401,7 @@ public class RestMusicAdminAPI {
if (!rs.all().isEmpty()) {
resultMap.put("Exception", "Application " + appName
+ " has already been onboarded. Please contact admin.");
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.ALREADYEXIST,
+ logger.error(EELFLoggerDelegate.errorLogger, "Application " + appName+"has already been onboarded. Please contact admin.", AppMessages.ALREADYEXIST,
ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
return response.status(Status.BAD_REQUEST).entity(resultMap).build();
}
@@ -398,7 +437,7 @@ public class RestMusicAdminAPI {
} else {
resultMap.put("Exception",
"Oops. Something went wrong. Please make sure Aid is correct and application is onboarded");
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA,
+ logger.error(EELFLoggerDelegate.errorLogger, "Oops. Something went wrong. Please make sure Aid is correct and application is onboarded", AppMessages.INCORRECTDATA,
ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
return response.status(Status.BAD_REQUEST).entity(resultMap).build();
}
@@ -406,482 +445,63 @@ public class RestMusicAdminAPI {
return response.status(Status.OK).entity(resultMap).build();
}
- Client client = Client.create();
- ObjectMapper mapper = new ObjectMapper();
-
- @POST
- @Path("/callbackOps")
- @Produces(MediaType.APPLICATION_JSON)
- @Consumes(MediaType.APPLICATION_JSON)
- public Response callbackOps(final JSONObject inputJsonObj) {
- ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
- Map<String, Object> resultMap = new HashMap<>();
- new Thread(new Runnable() {
- public void run() {
- makeAsyncCall(inputJsonObj);
- }
- }).start();
-
- return response.status(Status.OK).entity(resultMap).build();
- }
-
- private Response makeAsyncCall(JSONObject inputJsonObj) {
- ResponseBuilder response = Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
- Map<String, Object> resultMap = new HashMap<>();
- try {
- logger.info(EELFLoggerDelegate.applicationLogger, "Got notification: " + inputJsonObj.getData());
- logger.info("Got notification: " + inputJsonObj.getData());
- String dataStr = inputJsonObj.getData();
- JSONCallbackResponse jsonResponse = mapper.readValue(dataStr, JSONCallbackResponse.class);
- String operation = jsonResponse.getOperation();
- Map<String, String> changeValueMap = jsonResponse.getChangeValue();
- String ksTableName = jsonResponse.getFull_table(); //conductor.plans
- if("admin.notification_master".equals(ksTableName)) {
- CachingUtil.updateCallbackNotifyList(new ArrayList<String>());
- return response.status(Status.OK).entity(resultMap).build();
- }
- List<String> inputUpdateList = jsonResponse.getUpdateList();
-
- String field_value = null;
- List<String> notifiyList = CachingUtil.getCallbackNotifyList();
- if(notifiyList == null || notifiyList.isEmpty()) {
- logger.info("Is cache empty? reconstructing Object from cache..");
- constructJsonCallbackFromCache();
- }
- notifiyList = CachingUtil.getCallbackNotifyList();
- JsonCallback baseRequestObj = null;
-
- if("update".equals(operation)) {
- for(String element: inputUpdateList) {
- baseRequestObj = CachingUtil.getCallBackCache(element);
- if(baseRequestObj != null) {
- logger.info("Found the element that was changed... "+element);
- break;
- }
- }
-
- List<String> updateList = jsonResponse.getUpdateList();
- for(String element : updateList) {
- if(notifiyList.contains(element)) {
- logger.info("Found the notifyOn property: "+element);
- field_value = element;
- break;
- }
- }
- if(baseRequestObj == null || field_value == null) {
- for(String element: inputUpdateList) {
- String[] elementArr = element.split(":");
- String newElement = null;
- if(elementArr.length >= 2) {
- newElement = elementArr[0]+":"+elementArr[1];
- }
- baseRequestObj = CachingUtil.getCallBackCache(newElement);
- if(baseRequestObj != null) {
- logger.info("Found the element that was changed... "+newElement);
- break;
- }
- }
- for(String element : updateList) {
- String[] elementArr = element.split(":");
- String newElement = null;
- if(elementArr.length >= 2) {
- newElement = elementArr[0]+":"+elementArr[1];
- }
- if(notifiyList.contains(newElement)) {
- logger.info("Found the notifyOn property: "+newElement);
- field_value = newElement;
- break;
- }
- }
- }
- } else {
- field_value = jsonResponse.getFull_table();
- baseRequestObj = CachingUtil.getCallBackCache(field_value);
- }
-
- if(baseRequestObj == null || field_value == null) {
- resultMap.put("Exception",
- "Oops. Something went wrong. Please make sure Callback properties are onboarded.");
- logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA,
- ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
- }
- logger.info(EELFLoggerDelegate.applicationLogger, "Going through list: "+operation+ " && List: "+jsonResponse.getUpdateList());
-
- String key = "admin" + "." + "notification_master" + "." + baseRequestObj.getUuid();
- String lockId = MusicCore.createLockReference(key);
- ReturnType lockAcqResult = MusicCore.acquireLock(key, lockId);
- if(!"SUCCESS".equals(lockAcqResult.getResult().toString())) {
- logger.error(EELFLoggerDelegate.errorLogger, "Some other node is notifying the caller..: ");
- }
-
- logger.info(EELFLoggerDelegate.applicationLogger, operation+ ": Operation :: changeValue: "+changeValueMap);
- if("update".equals(operation)) {
- String notifyWhenChangeIn = baseRequestObj.getNotifyWhenChangeIn(); // conductor.plans.status
- if(null!=field_value && field_value.equals(notifyWhenChangeIn)) {
- notifyCallBackAppl(jsonResponse, baseRequestObj);
- }
- } else if("delete".equals(operation)) {
- String notifyWhenDeletesIn = baseRequestObj.getNotifyWhenDeletesIn(); // conductor.plans.status
- if(null!=field_value && field_value.equals(notifyWhenDeletesIn)) {
- notifyCallBackAppl(jsonResponse, baseRequestObj);
- }
- } else if("insert".equals(operation)) {
- String notifyWhenInsertsIn = baseRequestObj.getNotifyWhenInsertsIn(); // conductor.plans.status
- if(null!=field_value && field_value.equals(notifyWhenInsertsIn)) {
- notifyCallBackAppl(jsonResponse, baseRequestObj);
- }
- }
- MusicCore.releaseLock(lockId, true);
- } catch(Exception e) {
- e.printStackTrace();
- logger.error(EELFLoggerDelegate.errorLogger, "Exception while notifying...."+e.getMessage());
- }
- logger.info(EELFLoggerDelegate.applicationLogger, "callback is completed. Notification was sent from Music...");
- return response.status(Status.OK).entity(resultMap).build();
- }
-
- private void notifyCallBackAppl(JSONCallbackResponse jsonResponse, JsonCallback baseRequestObj) throws Exception {
- int notifytimeout = MusicUtil.getNotifyTimeout();
- int notifyinterval = MusicUtil.getNotifyInterval();
- String endpoint = baseRequestObj.getApplicationNotificationEndpoint();
- String username = baseRequestObj.getApplicationUsername();
- String password = baseRequestObj.getApplicationPassword();
- JsonNotification jsonNotification = constructJsonNotification(jsonResponse, baseRequestObj);
- jsonNotification.setPassword("************");
- jsonNotification.setOperation_type(jsonResponse.getOperation());
- logger.info(EELFLoggerDelegate.applicationLogger, "Notification Response sending is: "+jsonNotification);
- logger.info("Notification Response sending is: "+jsonNotification);
- jsonNotification.setPassword(baseRequestObj.getApplicationPassword());
- client.resource(endpoint);
- String authData = username+":"+password;
- byte[] plainCredsBytes = authData.getBytes();
- byte[] base64CredsBytes = Base64.encode(plainCredsBytes);
- String base64Creds = new String(base64CredsBytes);
- ClientConfig config = new DefaultClientConfig();
- config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
- ClientResponse response = null;
- WebResource service = null;
- boolean ok = false;
- try {
- Client client = Client.create(config);
- TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
- public X509Certificate[] getAcceptedIssuers(){return null;}
- public void checkClientTrusted(X509Certificate[] certs, String authType){}
- public void checkServerTrusted(X509Certificate[] certs, String authType){}
- }};
-
- // Install the all-trusting trust manager
- try {
- SSLContext sc = SSLContext.getInstance("TLS");
- sc.init(null, trustAllCerts, new SecureRandom());
- HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- }
-
- try {
- SSLContext sslcontext = SSLContext.getInstance( "TLS" );
- sslcontext.init( null, null, null );
- Map<String, Object> properties = config.getProperties();
- HTTPSProperties httpsProperties = new HTTPSProperties(
- new HostnameVerifier() {
- @Override
- public boolean verify( String s, SSLSession sslSession ) {
- return true;
- }
- }, sslcontext
- );
- properties.put( HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, httpsProperties );
- HttpsURLConnection.setDefaultHostnameVerifier (new HostnameVerifier() {
- @Override
- public boolean verify(String hostname, SSLSession session) {
- return true;
- }
- });
- Client.create( config );
- }
- catch ( KeyManagementException | NoSuchAlgorithmException e ) {
- throw new RuntimeException( e );
- }
-
- service = client.resource(endpoint);
-
- response = service.header("Authorization", "Basic " + base64Creds).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, jsonNotification);
-
- } catch (Exception chf) {
- logger.info(EELFLoggerDelegate.applicationLogger, "Is Service down?");
- logger.info("An Exception occured while notifying. "+chf+ " : "+chf.getMessage() +" ...Retrying for: "+notifytimeout);
- }
- if(response != null && response.getStatus() == 200) ok = true;
- if(!ok) {
- long now= System.currentTimeMillis();
- long end = now+notifytimeout;
- while(! ok) {
- logger.info(EELFLoggerDelegate.applicationLogger, "retrying since error in notifying callback for "+notifytimeout+"ms");
- logger.info("retrying since error in notifying callback.. response status: "+ (response == null ? "404" : response.getStatus()));
- try {
- ok = true;
- response = service.header("Authorization", "Basic " + base64Creds).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, jsonNotification);
- if(response != null && response.getStatus() == 200) ok = true;
- else if(System.currentTimeMillis() < end) {
- try{ Thread.sleep(notifyinterval); } catch(Exception e1) {}
- ok = false;
- }
- }catch (Exception e) {
- logger.info(EELFLoggerDelegate.applicationLogger, "Retry until "+(end-System.currentTimeMillis()));
- if(response == null && System.currentTimeMillis() < end) ok = false;
- else ok = true;
- try{ Thread.sleep(notifyinterval); } catch(Exception e1) {}
- }
- }
- }
-
- if(response == null) {
- logger.error(EELFLoggerDelegate.errorLogger, "Can NOT notify the caller as caller failed to respond..");
- return;
- }
- try {
- JsonNotifyClientResponse responseStr = response.getEntity(JsonNotifyClientResponse.class);
- logger.info(EELFLoggerDelegate.applicationLogger, "Response from Notified client: "+responseStr);
- logger.info("Response from Notified client: "+responseStr);
- } catch(Exception e) {
- logger.info("Exception while reading response from Caller");
- logger.error("Exception while reading response from Caller");
- logger.error(EELFLoggerDelegate.errorLogger, "Can NOT notify the caller as caller failed to respond..");
- }
- }
-
- private JsonNotification constructJsonNotification(JSONCallbackResponse jsonResponse, JsonCallback baseRequestObj) {
-
- JsonNotification jsonNotification = new JsonNotification();
- try {
- jsonNotification.setNotify_field(baseRequestObj.getNotifyOn());
- jsonNotification.setEndpoint(baseRequestObj.getApplicationNotificationEndpoint());
- jsonNotification.setUsername(baseRequestObj.getApplicationUsername());
- jsonNotification.setPassword(baseRequestObj.getApplicationPassword());
- String pkValue = jsonResponse.getPrimary_key();
-
- String[] fullNotifyArr = baseRequestObj.getNotifyOn().split(":");
-
- String[] tableArr = fullNotifyArr[0].split("\\.");
- TableMetadata tableInfo = MusicCore.returnColumnMetadata(tableArr[0], tableArr[1]);
- DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType();
- String primaryId = tableInfo.getPrimaryKey().get(0).getName();
-
- Map<String, String> responseBodyMap = baseRequestObj.getResponseBody();
- for (Entry<String, String> entry : new HashSet<>(responseBodyMap.entrySet())) {
- String trimmed = entry.getKey().trim();
- if (!trimmed.equals(entry.getKey())) {
- responseBodyMap.remove(entry.getKey());
- responseBodyMap.put(trimmed, entry.getValue());
- }
- }
-
- Set<String> keySet = responseBodyMap.keySet();
- Map<String, String> newMap = new HashMap<>();
- if(responseBodyMap.size() == 1 && responseBodyMap.containsKey("")) {
- jsonNotification.setResponse_body(newMap);
- return jsonNotification;
- }
- logger.info(EELFLoggerDelegate.applicationLogger, "responseBodyMap is not blank: "+responseBodyMap);
- String cql = "select *";
- /*for(String keys: keySet) {
- cql = cql + keys + ",";
- }*/
- //cql = cql.substring(0, cql.length()-1);
- cql = cql + " FROM "+fullNotifyArr[0]+" WHERE "+primaryId+" = ?";
- logger.info(EELFLoggerDelegate.applicationLogger, "CQL in constructJsonNotification: "+cql);
- PreparedQueryObject pQuery = new PreparedQueryObject();
- pQuery.appendQueryString(cql);
- pQuery.addValue(MusicUtil.convertToActualDataType(primaryIdType, pkValue));
- Row row = MusicCore.get(pQuery).one();
- if(row != null) {
- ColumnDefinitions colInfo = row.getColumnDefinitions();
- for (Definition definition : colInfo) {
- String colName = definition.getName();
- if(keySet.contains(colName)) {
- DataType colType = definition.getType();
- Object valueObj = MusicCore.getDSHandle().getColValue(row, colName, colType);
- Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
- logger.info(colName+" : "+valueString);
- newMap.put(colName, valueString.toString());
- keySet.remove(colName);
- }
- }
- }
- if(! keySet.isEmpty()) {
- Iterator<String> iterator = keySet.iterator();
- while (iterator.hasNext()) {
- String element = iterator.next();
- newMap.put(element,"COLUMN_NOT_FOUND");
- }
- }
-
- if("delete".equals(jsonResponse.getOperation()) || newMap.isEmpty()) {
- newMap.put(primaryId, pkValue);
- }
- jsonNotification.setResponse_body(newMap);
- } catch(Exception e) {
- logger.error(e.getMessage(), e);
- e.printStackTrace();
- }
- return jsonNotification;
- }
-
- private void constructJsonCallbackFromCache() throws Exception{
- PreparedQueryObject pQuery = new PreparedQueryObject();
- JsonCallback jsonCallback = null;
- List<String> notifyList = new java.util.ArrayList<>();
- String cql =
- "select id, endpoint_userid, endpoint_password, notify_to_endpoint, notify_insert_on,"
- + " notify_delete_on, notify_update_on, request, notifyon from admin.notification_master allow filtering";
- pQuery.appendQueryString(cql);
-
- ResultSet rs = MusicCore.get(pQuery);
- Iterator<Row> it = rs.iterator();
- while (it.hasNext()) {
- Row row = (Row) it.next();
- String endpoint = row.getString("notify_to_endpoint");
- String username = row.getString("endpoint_userid");
- ByteBuffer passwordBytes = row.getBytes("endpoint_password");
- String insert = row.getString("notify_insert_on");
- String delete = row.getString("notify_delete_on");
- String update = row.getString("notify_update_on");
- String request = row.getString("request");
- String notifyon = row.getString("notifyon");
- String uuid = row.getUUID("id").toString();
- notifyList.add(notifyon);
- jsonCallback = new JsonCallback();
- jsonCallback.setApplicationNotificationEndpoint(endpoint);
-
- Charset charset = Charset.forName("ISO-8859-1");
- String decodedPwd = charset.decode(passwordBytes).toString();
- jsonCallback.setApplicationPassword(decodedPwd);
- jsonCallback.setApplicationUsername(username);
- jsonCallback.setNotifyOn(notifyon);
- jsonCallback.setNotifyWhenInsertsIn(insert);
- jsonCallback.setNotifyWhenDeletesIn(delete);
- jsonCallback.setNotifyWhenChangeIn(update);
- jsonCallback.setUuid(uuid);
- logger.info(EELFLoggerDelegate.applicationLogger, "From DB. Saved request_body: "+request);
- request = request.substring(1, request.length()-1);
- String[] keyValuePairs = request.split(",");
- Map<String,String> responseBody = new HashMap<>();
-
- for(String pair : keyValuePairs) {
- String[] entry = pair.split("=");
- String val = "";
- if(entry.length == 2)
- val = entry[1];
- responseBody.put(entry[0], val);
- }
- logger.info(EELFLoggerDelegate.applicationLogger, "After parsing. Saved request_body: "+responseBody);
- jsonCallback.setResponseBody(responseBody);
- logger.info(EELFLoggerDelegate.applicationLogger, "Updating Cache with updateCallBackCache: "+notifyon+ " :::: "+jsonCallback);
- CachingUtil.updateCallBackCache(notifyon, jsonCallback);
- }
- CachingUtil.updateCallbackNotifyList(notifyList);
- }
-
- @POST
- @Path("/onboardCallback")
+ //Dashboard related calls
+ @GET
+ @Path("/getall")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
- public Response addCallback(JsonNotification jsonNotification) {
- Map<String, Object> resultMap = new HashMap<>();
+ public List<Application> getall(@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization) throws MusicServiceException{
+ List<Application> appList = new ArrayList<>();
ResponseBuilder response =
- Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
- String username = jsonNotification.getUsername();
- String password = jsonNotification.getPassword();
- String endpoint = jsonNotification.getEndpoint();
- String notify_field = jsonNotification.getNotify_field();
- Map<String, String> responseBody = jsonNotification.getResponse_body();
- String triggerName = jsonNotification.getTriggerName();
- if(triggerName == null || triggerName.length() == 0)
- triggerName = "MusicTrigger";
-
- String[] allFields = notify_field.split(":");
- String inserts = null;
- String updates = null;
- String deletes = null;
- String tableName = null;
- if(allFields.length >= 2) {
- inserts = updates = notify_field;
- } else if(allFields.length == 1) {
- inserts = deletes = notify_field;;
+ Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ queryObject.appendQueryString("SELECT * FROM " + "admin" + "." + "keyspace_master" + ";");
+ ResultSet results = MusicCore.get(queryObject);
+ for(Row row : results) {
+ Application app = new Application();
+ app.setApplication_name(row.getString("application_name"));
+ app.setIs_aaf(row.getBool("is_aaf"));
+ app.setIs_api(row.getBool("is_api"));
+ app.setUsername(row.getString("username"));
+ app.setKeyspace_name(row.getString("keyspace_name"));
+ app.setUuid(row.getUUID("uuid").toString());
+ appList.add(app);
}
- tableName = allFields[0];
- String cql = "CREATE TRIGGER IF NOT EXISTS musictrigger ON "+tableName+" Using '"+triggerName+"'";
- PreparedQueryObject pQuery = new PreparedQueryObject();
+ return appList;
+
+ //return app;
- String uuid = CachingUtil.generateUUID();
- try {
- pQuery.appendQueryString(
- "INSERT INTO admin.notification_master (id, endpoint_userid, endpoint_password, notify_to_endpoint, "
- + "notifyon, notify_insert_on, notify_delete_on, notify_update_on, request, current_notifier) VALUES (?,?,?,?,?,?,?,?,?,?)");
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), username));
- Charset charset = Charset.forName("ISO-8859-1");
- ByteBuffer decodedPwd = charset.encode(password);
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.blob(), decodedPwd.array()));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), endpoint));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), notify_field));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), inserts));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), deletes));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), updates));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), responseBody));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), MusicCore.getMyHostId()));
- MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL);
- JsonCallback jsonCallback = new JsonCallback();
- jsonCallback.setUuid(uuid);
- jsonCallback.setApplicationNotificationEndpoint(endpoint);
- jsonCallback.setApplicationPassword(password);
- jsonCallback.setApplicationUsername(username);
- jsonCallback.setNotifyOn(notify_field);
- jsonCallback.setNotifyWhenChangeIn(updates);
- jsonCallback.setNotifyWhenDeletesIn(deletes);
- jsonCallback.setNotifyWhenInsertsIn(inserts);
- jsonCallback.setResponseBody(responseBody);
- CachingUtil.updateCallBackCache(notify_field, jsonCallback);
- pQuery = new PreparedQueryObject();
- pQuery.appendQueryString(cql);
- MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL);
- logger.info(EELFLoggerDelegate.applicationLogger, "Created trigger");
- } catch (InvalidQueryException e) {
- logger.error(EELFLoggerDelegate.errorLogger,"Exception callback_api table not configured."+e.getMessage());
- resultMap.put("Exception", "Please make sure admin.notification_master table is configured.");
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
- } catch(Exception e) {
- logger.error(e.getMessage(), e);
- e.printStackTrace();
- resultMap.put("Exception", "Exception Occured.");
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
- }
- return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Callback api successfully registered").toMap()).build();
}
-
@DELETE
- @Path("/onboardCallback")
+ @Path("/delete")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
- public Response deleteCallbackProp(JsonNotification jsonNotification) {
+ public boolean delete(@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
+ @ApiParam(value = "uuid", required = true) @HeaderParam("uuid") String uuid) throws Exception {
ResponseBuilder response =
- Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
- String notifyOn = jsonNotification.getNotify_field();
- PreparedQueryObject pQuery = new PreparedQueryObject();
+ Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ queryObject.appendQueryString("delete from admin.keyspace_master where uuid=?");
+ queryObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),uuid));
+ ResultType result;
try {
- pQuery.appendQueryString("DELETE FROM admin.notification_master WHERE notifyon = ?");
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), notifyOn));
- MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL);
- } catch(Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
- return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setMessage("Callback api registration failed").toMap()).build();
+ result = MusicCore.nonKeyRelatedPut(queryObject, "eventual");
+ }catch(Exception ex) {
+ return false;
}
- return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Callback api successfully deleted").toMap()).build();
+ 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
index 60305bfb..f2946c14 100644
--- a/src/main/java/org/onap/music/rest/RestMusicBmAPI.java
+++ b/src/main/java/org/onap/music/rest/RestMusicBmAPI.java
@@ -1,22 +1,25 @@
/*
- * ============LICENSE_START========================================== org.onap.music
- * =================================================================== Copyright (c) 2017 AT&T
- * Intellectual Property
- * Modifications Copyright (C) 2018 IBM.
+ * ============LICENSE_START==========================================
+ * org.onap.music
* ===================================================================
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.rest;
import java.util.List;
@@ -39,6 +42,8 @@ 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;
@@ -66,7 +71,7 @@ public class RestMusicBmAPI {
@Path("/purezk/{name}")
@Consumes(MediaType.APPLICATION_JSON)
public void pureZkCreate(@PathParam("name") String nodeName) throws Exception {
- MusicCore.pureZkCreate("/" + nodeName);
+ MusicZKCore.pureZkCreate("/" + nodeName);
}
@@ -83,7 +88,7 @@ public class RestMusicBmAPI {
throws Exception {
logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zk normal update-------------------------");
long start = System.currentTimeMillis();
- MusicCore.pureZkWrite(nodeName, insObj.serialize());
+ MusicZKCore.pureZkWrite(nodeName, insObj.serialize());
long end = System.currentTimeMillis();
logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk normal update:" + (end - start) + " ms");
}
@@ -98,7 +103,7 @@ public class RestMusicBmAPI {
@Path("/purezk/{name}")
@Consumes(MediaType.TEXT_PLAIN)
public byte[] pureZkGet(@PathParam("name") String nodeName) throws Exception {
- return MusicCore.pureZkRead(nodeName);
+ return MusicZKCore.pureZkRead(nodeName);
}
/**
@@ -135,13 +140,19 @@ public class RestMusicBmAPI {
if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId);
- MusicCore.pureZkWrite(lockname, data);
+ 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);
@@ -189,7 +200,7 @@ public class RestMusicBmAPI {
ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockName, lockId, leasePeriod);
if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
logger.info("acquired lock with id " + lockId);
- MusicCore.pureZkRead(nodeName);
+ MusicZKCore.pureZkRead(nodeName);
boolean voluntaryRelease = true;
MusicCore.releaseLock(lockId, voluntaryRelease);
} else {
@@ -225,7 +236,7 @@ public class RestMusicBmAPI {
+ "-------------------------");
PreparedQueryObject queryObject = new PreparedQueryObject();
Map<String, Object> valuesMap = insObj.getValues();
- TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
+ TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
String vectorTs = "'" + Thread.currentThread().getId() + System.currentTimeMillis() + "'";
String fieldValueString = "vector_ts= ? ,";
queryObject.addValue(vectorTs);
@@ -290,7 +301,7 @@ public class RestMusicBmAPI {
long jsonParseCompletionTime = System.currentTimeMillis();
boolean operationResult = true;
- MusicCore.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type"));
+ MusicDataStoreHandle.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type"));
long actualUpdateCompletionTime = System.currentTimeMillis();
diff --git a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java
index a8fd1fc2..507bcd40 100755
--- a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java
+++ b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java
@@ -7,18 +7,19 @@
* 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.nio.ByteBuffer;
@@ -48,6 +49,7 @@ import javax.ws.rs.core.UriInfo;
import org.apache.commons.lang3.StringUtils;
import org.mindrot.jbcrypt.BCrypt;
+import org.onap.music.authentication.MusicAuthentication;
import org.onap.music.datastore.PreparedQueryObject;
import org.onap.music.datastore.jsonobjects.JsonDelete;
import org.onap.music.datastore.jsonobjects.JsonInsert;
@@ -56,17 +58,20 @@ import org.onap.music.datastore.jsonobjects.JsonTable;
import org.onap.music.datastore.jsonobjects.JsonUpdate;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.exceptions.MusicLockingException;
+import org.onap.music.exceptions.MusicQueryException;
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.main.MusicCore.Condition;
+import org.onap.music.datastore.Condition;
+import org.onap.music.datastore.MusicDataStoreHandle;
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;
@@ -76,6 +81,8 @@ import com.datastax.driver.core.TableMetadata;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
+import io.swagger.annotations.ApiResponses;
+import io.swagger.annotations.ApiResponse;
/* Version 2 Class */
//@Path("/v{version: [0-9]+}/keyspaces")
@@ -90,7 +97,7 @@ public class RestMusicDataAPI {
* (e.g. if the full version is 1.24.5, X-minorVersion = "24") - Is optional for the client on
* request; however, this header should be provided if the client needs to take advantage of
* MINOR incremented version functionality - Is mandatory for the server on response
- *
+ *
*** X-patchVersion *** - Used only to communicate a PATCH version in a response for
* troubleshooting purposes only, and will not be provided by the client on request - This will
* be the latest PATCH version of the MINOR requested by the client, or the latest PATCH version
@@ -110,7 +117,9 @@ public class RestMusicDataAPI {
private static final String XPATCHVERSION = "X-patchVersion";
private static final String NS = "ns";
private static final String VERSION = "v2";
-
+ // Set to true in env like ONAP. Where access to creating and dropping keyspaces exist.
+ private static final boolean KEYSPACE_ACTIVE = false;
+
private class RowIdentifier {
public String primarKeyValue;
public StringBuilder rowIdString;
@@ -129,7 +138,7 @@ public class RestMusicDataAPI {
/**
* Create Keyspace REST
- *
+ *
* @param kspObject
* @param keyspaceName
* @return
@@ -137,7 +146,7 @@ public class RestMusicDataAPI {
*/
@POST
@Path("/{name}")
- @ApiOperation(value = "Create Keyspace", response = String.class)
+ @ApiOperation(value = "Create Keyspace", response = String.class,hidden = true)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
//public Map<String, Object> createKeySpace(
@@ -145,122 +154,142 @@ public class RestMusicDataAPI {
@ApiParam(value = "Major Version",required = true) @PathParam("version") String version,
@ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
@ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
@ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns,
JsonKeySpace kspObject,
@ApiParam(value = "Keyspace Name",required = true) @PathParam("name") String keyspaceName) {
- ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
- Map<String, Object> authMap = CachingUtil.verifyOnboarding(ns, userId, password);
- if (!authMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"authMap has an error. verifyOnboarding may have failed silently", 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(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 = MusicCore.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,"", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
+ ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
+ EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
+ logger.info(EELFLoggerDelegate.applicationLogger,"In Create Keyspace " + keyspaceName);
+ if ( KEYSPACE_ACTIVE ) {
+ logger.info(EELFLoggerDelegate.applicationLogger,"Creating Keyspace " + keyspaceName);
+ Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
+ String userId = userCredentials.get(MusicUtil.USERID);
+ String password = userCredentials.get(MusicUtil.PASSWORD);
+ Map<String, Object> authMap = CachingUtil.verifyOnboarding(ns, userId, password);
+ 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();
}
- }
-
- String consistency = MusicUtil.EVENTUAL;// for now this needs only
- // eventual consistency
-
- PreparedQueryObject queryObject = new PreparedQueryObject();
- long start = System.currentTimeMillis();
- Map<String, Object> replicationInfo = kspObject.getReplicationInfo();
- String repString = null;
- try {
- repString = "{" + MusicUtil.jsonMaptoSqlString(replicationInfo, ",") + "}";
- } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
-
- }
- queryObject.appendQueryString(
- "CREATE KEYSPACE " + keyspaceName + " WITH replication = " + repString);
- if (kspObject.getDurabilityOfWrites() != null) {
+ 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();
+ }
+ }
+
+ String consistency = MusicUtil.EVENTUAL;// for now this needs only
+ // eventual consistency
+
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL) && kspObject.getConsistencyInfo().get("consistency") != null) {
+ if(MusicUtil.isValidConsistency(kspObject.getConsistencyInfo().get("consistency")))
+ queryObject.setConsistency(kspObject.getConsistencyInfo().get("consistency"));
+ else
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.SYNTAXERROR).setError("Invalid Consistency type").toMap()).build();
+ }
+ long start = System.currentTimeMillis();
+ Map<String, Object> replicationInfo = kspObject.getReplicationInfo();
+ String repString = null;
+ try {
+ repString = "{" + MusicUtil.jsonMaptoSqlString(replicationInfo, ",") + "}";
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
+
+ }
queryObject.appendQueryString(
- " AND durable_writes = " + kspObject.getDurabilityOfWrites());
- }
-
- queryObject.appendQueryString(";");
- long end = System.currentTimeMillis();
- logger.info(EELFLoggerDelegate.applicationLogger,
- "Time taken for setting up query in create keyspace:" + (end - start));
-
- ResultType result = ResultType.FAILURE;
- try {
- result = MusicCore.nonKeyRelatedPut(queryObject, consistency);
- logger.info(EELFLoggerDelegate.applicationLogger, "result = " + result);
- } catch ( MusicServiceException ex) {
- logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("err:" + ex.getMessage()).toMap()).build();
- }
-
- try {
- queryObject = new PreparedQueryObject();
- queryObject.appendQueryString("CREATE ROLE IF NOT EXISTS '" + userId
- + "' WITH PASSWORD = '" + password + "' AND LOGIN = true;");
- MusicCore.nonKeyRelatedPut(queryObject, consistency);
- queryObject = new PreparedQueryObject();
- queryObject.appendQueryString("GRANT ALL PERMISSIONS on KEYSPACE " + keyspaceName
- + " to '" + userId + "'");
+ "CREATE KEYSPACE " + keyspaceName + " WITH replication = " + repString);
+ if (kspObject.getDurabilityOfWrites() != null) {
+ queryObject.appendQueryString(
+ " AND durable_writes = " + kspObject.getDurabilityOfWrites());
+ }
+
queryObject.appendQueryString(";");
- MusicCore.nonKeyRelatedPut(queryObject, consistency);
- } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ long end = System.currentTimeMillis();
+ logger.info(EELFLoggerDelegate.applicationLogger,
+ "Time taken for setting up query in create keyspace:" + (end - start));
+
+ ResultType result = ResultType.FAILURE;
+ try {
+ result = MusicCore.nonKeyRelatedPut(queryObject, consistency);
+ logger.info(EELFLoggerDelegate.applicationLogger, "result = " + result);
+ } catch ( MusicServiceException ex) {
+ logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("err:" + ex.getMessage()).toMap()).build();
+ }
+
+ try {
+ queryObject = new PreparedQueryObject();
+ queryObject.appendQueryString("CREATE ROLE IF NOT EXISTS '" + userId
+ + "' WITH PASSWORD = '" + password + "' AND LOGIN = true;");
+ MusicCore.nonKeyRelatedPut(queryObject, consistency);
+ queryObject = new PreparedQueryObject();
+ queryObject.appendQueryString("GRANT ALL PERMISSIONS on KEYSPACE " + keyspaceName
+ + " to '" + userId + "'");
+ queryObject.appendQueryString(";");
+ MusicCore.nonKeyRelatedPut(queryObject, consistency);
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ }
+
+ try {
+ boolean isAAF = Boolean.valueOf(CachingUtil.isAAFApplication(ns));
+ String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt());
+ queryObject = new PreparedQueryObject();
+ 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.text(), keyspaceName));
+ queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns));
+ queryObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
+ queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd));
+ queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
+ queryObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
+ CachingUtil.updateMusicCache(keyspaceName, ns);
+ CachingUtil.updateMusicValidateCache(ns, userId, hashedpwd);
+ MusicCore.eventualPut(queryObject);
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ return response.status(Response.Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();
+ }
+
+ return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Keyspace " + keyspaceName + " Created").toMap()).build();
+ } else {
+ String vError = "Keyspace Creation no longer supported after versions 3.2.x. Contact DBA to create the keyspace.";
+ logger.info(EELFLoggerDelegate.applicationLogger,vError);
+ logger.error(EELFLoggerDelegate.errorLogger,vError, AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ return response.status(Response.Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(vError).toMap()).build();
}
-
- try {
- boolean isAAF = Boolean.valueOf(CachingUtil.isAAFApplication(ns));
- String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt());
- queryObject = new PreparedQueryObject();
- 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.text(), keyspaceName));
- queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns));
- queryObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
- queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd));
- queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
- queryObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
- CachingUtil.updateMusicCache(keyspaceName, ns);
- CachingUtil.updateMusicValidateCache(ns, userId, hashedpwd);
- MusicCore.eventualPut(queryObject);
- } catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
- return response.status(Response.Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
}
- return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Keyspace " + keyspaceName + " Created").toMap()).build();
}
/**
- *
+ *
* @param kspObject
* @param keyspaceName
* @return
@@ -268,71 +297,84 @@ public class RestMusicDataAPI {
*/
@DELETE
@Path("/{name}")
- @ApiOperation(value = "Delete Keyspace", response = String.class)
+ @ApiOperation(value = "Delete Keyspace", response = String.class,hidden=true)
@Produces(MediaType.APPLICATION_JSON)
- //public Map<String, Object> dropKeySpace(
public Response dropKeySpace(
@ApiParam(value = "Major Version",required = true) @PathParam("version") String version,
@ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
@ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
@ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns,
@ApiParam(value = "Keyspace Name",required = true) @PathParam("name") String keyspaceName) throws Exception {
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
- Map<String, Object> authMap = MusicCore.autheticateUser(ns, userId, password,keyspaceName, aid, "dropKeySpace");
- if (authMap.containsKey("aid"))
- authMap.remove("aid");
- if (!authMap.isEmpty()) {
- return response.status(Status.UNAUTHORIZED).entity(authMap).build();
- }
-
- String consistency = MusicUtil.EVENTUAL;// for now this needs only
- // eventual
- // consistency
- String appName = CachingUtil.getAppName(keyspaceName);
- String uuid = CachingUtil.getUuidFromMusicCache(keyspaceName);
- PreparedQueryObject pQuery = new PreparedQueryObject();
- pQuery.appendQueryString(
- "select count(*) as count from admin.keyspace_master where application_name=? allow filtering;");
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
- Row row = MusicCore.get(pQuery).one();
- long count = row.getLong(0);
-
- if (count == 0) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
- return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Keyspace not found. Please make sure keyspace exists.").toMap()).build();
- // Admin Functions:
- } else if (count == 1) {
- pQuery = new PreparedQueryObject();
+ 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();
+ }
+
+ String consistency = MusicUtil.EVENTUAL;// for now this needs only
+ // eventual
+ // consistency
+ String appName = CachingUtil.getAppName(keyspaceName);
+ String uuid = CachingUtil.getUuidFromMusicCache(keyspaceName);
+ PreparedQueryObject pQuery = new PreparedQueryObject();
pQuery.appendQueryString(
- "UPDATE admin.keyspace_master SET keyspace_name=? where uuid = ?;");
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
- MusicUtil.DEFAULTKEYSPACENAME));
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
- MusicCore.nonKeyRelatedPut(pQuery, consistency);
+ "select count(*) as count from admin.keyspace_master where application_name=? allow filtering;");
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
+ Row row = MusicCore.get(pQuery).one();
+ long count = row.getLong(0);
+
+ if (count == 0) {
+ logger.error(EELFLoggerDelegate.errorLogger,"Keyspace not found. Please make sure keyspace exists.", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Keyspace not found. Please make sure keyspace exists.").toMap()).build();
+ // Admin Functions:
+ } else if (count == 1) {
+ pQuery = new PreparedQueryObject();
+ pQuery.appendQueryString(
+ "UPDATE admin.keyspace_master SET keyspace_name=? where uuid = ?;");
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
+ MusicUtil.DEFAULTKEYSPACENAME));
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
+ MusicCore.nonKeyRelatedPut(pQuery, consistency);
+ } else {
+ pQuery = new PreparedQueryObject();
+ pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?");
+ pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
+ MusicCore.nonKeyRelatedPut(pQuery, consistency);
+ }
+
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ queryObject.appendQueryString("DROP KEYSPACE " + keyspaceName + ";");
+ ResultType result = MusicCore.nonKeyRelatedPut(queryObject, consistency);
+ if ( result.equals(ResultType.FAILURE) ) {
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(result).setError("Error Deleteing Keyspace " + keyspaceName).toMap()).build();
+ }
+ return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Keyspace " + keyspaceName + " Deleted").toMap()).build();
} else {
- pQuery = new PreparedQueryObject();
- pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?");
- pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
- MusicCore.nonKeyRelatedPut(pQuery, consistency);
+ String vError = "Keyspace Droping no longer supported after versions 3.2.x. Contact DBA to drop the keyspace.";
+ logger.info(EELFLoggerDelegate.applicationLogger,vError);
+ logger.error(EELFLoggerDelegate.errorLogger,vError, AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ return response.status(Response.Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(vError).toMap()).build();
}
-
- PreparedQueryObject queryObject = new PreparedQueryObject();
- queryObject.appendQueryString("DROP KEYSPACE " + keyspaceName + ";");
- ResultType result = MusicCore.nonKeyRelatedPut(queryObject, consistency);
- if ( result.equals(ResultType.FAILURE) ) {
- return response.status(Status.BAD_REQUEST).entity(new JsonResponse(result).setError("Error Deleteing Keyspace " + keyspaceName).toMap()).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
}
- return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Keyspace " + keyspaceName + " Deleted").toMap()).build();
}
/**
- *
+ *
* @param tableObj
* @param version
* @param keyspace
@@ -342,31 +384,41 @@ public class RestMusicDataAPI {
* @throws Exception
*/
@POST
- @Path("/{keyspace}/tables/{tablename}")
+ @Path("/{keyspace: .*}/tables/{tablename: .*}")
@ApiOperation(value = "Create Table", response = String.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
- //public Map<String, Object> createTable(
+ @ApiResponses(value={
+ @ApiResponse(code= 400, message = "Will return JSON response with message"),
+ @ApiResponse(code= 401, message = "Unautorized User")
+ })
public Response createTable(
@ApiParam(value = "Major Version",required = true) @PathParam("version") String version,
@ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
@ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
JsonTable tableObj,
@ApiParam(value = "Keyspace Name",required = true) @PathParam("keyspace") String keyspace,
@ApiParam(value = "Table Name",required = true) @PathParam("tablename") String tablename) throws Exception {
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
- Map<String, Object> authMap = MusicCore.autheticateUser(ns, userId, password, keyspace,
+ if((keyspace == null || keyspace == null) || (tablename.isEmpty() || tablename.isEmpty())){
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
+ .setError("one or more path parameters are not set, please check and try again")
+ .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,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
+ 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();
}
String consistency = MusicUtil.EVENTUAL;
@@ -389,19 +441,19 @@ public class RestMusicDataAPI {
for (Map.Entry<String, String> entry : fields.entrySet()) {
if (entry.getKey().equals("PRIMARY KEY")) {
primaryKey = entry.getValue(); // replaces primaryKey
- primaryKey.trim();
+ primaryKey = primaryKey.trim();
} else {
if (counter == 0 ) fieldsString.append("" + entry.getKey() + " " + entry.getValue() + "");
- else fieldsString.append("," + entry.getKey() + " " + entry.getValue() + "");
+ else fieldsString.append("," + entry.getKey() + " " + entry.getValue() + "");
}
- if (counter != (fields.size() - 1) ) {
-
- counter = counter + 1;
- } else {
+ if (counter != (fields.size() - 1) ) {
+
+ counter = counter + 1;
+ } else {
if((primaryKey != null) && (partitionKey == null)) {
- primaryKey.trim();
+ primaryKey = primaryKey.trim();
int count1 = StringUtils.countMatches(primaryKey, ')');
int count2 = StringUtils.countMatches(primaryKey, '(');
if (count1 != count2) {
@@ -410,44 +462,43 @@ public class RestMusicDataAPI {
.toMap()).build();
}
- if ( primaryKey.indexOf('(') == -1 || ( count2 == 1 && (primaryKey.lastIndexOf(")") +1) == primaryKey.length() ) )
+ 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="";
+ 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="";
}
} else { // not null and has ) before the last char
- partitionKey= primaryKey.substring(0,primaryKey.indexOf(')'));
- partitionKey=partitionKey.replaceAll("[\\(]+","");
- partitionKey.trim();
- clusterKey= primaryKey.substring(primaryKey.indexOf(')'));
- clusterKey=clusterKey.replaceAll("[\\(]+","");
- clusterKey=clusterKey.replaceAll("[\\)]+","");
- clusterKey.trim();
- if (clusterKey.indexOf(",") == 0) clusterKey=clusterKey.substring(1);
- clusterKey.trim();
- if (clusterKey.equals(",") ) clusterKey=""; // print error if needed ( ... ),)
-
- }
+ 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(
+ 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();
+ .toMap()).build();
- }
+ }
if (partitionKey.isEmpty() ) primaryKey="";
else if (clusterKey.isEmpty() ) primaryKey=" (" + partitionKey + ")";
@@ -504,38 +555,39 @@ public class RestMusicDataAPI {
}
}
- String clusteringOrder = tableObj.getClusteringOrder();
+ String clusteringOrder = tableObj.getClusteringOrder();
- if (clusteringOrder != null && !(clusteringOrder.isEmpty())) {
- String[] arrayClusterOrder = clusteringOrder.split("[,]+");
+ if (clusteringOrder != null && !(clusteringOrder.isEmpty())) {
+ 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();
- }
- // add validation for column names in cluster key
- }
+ 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);
- else propertiesString.append(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 {
logger.warn("Skipping clustering order=("+clusteringOrder+ ") since clustering key is empty ");
}
} //if non empty
-
- queryObject.appendQueryString(
- "CREATE TABLE " + keyspace + "." + tablename + " " + fieldsString);
+
+ queryObject.appendQueryString(
+ "CREATE TABLE " + keyspace + "." + tablename + " " + fieldsString);
if (propertiesString != null && propertiesString.length()>0 )
@@ -543,8 +595,7 @@ public class RestMusicDataAPI {
queryObject.appendQueryString(";");
ResultType result = ResultType.FAILURE;
try {
- //logger.info("cjc query="+queryObject.getQuery());
- result = MusicCore.nonKeyRelatedPut(queryObject, consistency);
+ result = MusicCore.createTable(keyspace, tablename, queryObject, consistency);
} catch (MusicServiceException ex) {
logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.MUSICSERVICEERROR);
response.status(Status.BAD_REQUEST);
@@ -553,11 +604,14 @@ public class RestMusicDataAPI {
if ( result.equals(ResultType.FAILURE) ) {
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(result).setError("Error Creating Table " + tablename).toMap()).build();
}
- return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("TableName " + tablename + " Created under keyspace " + keyspace).toMap()).build();
+ return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("TableName " + tablename.trim() + " Created under keyspace " + keyspace.trim()).toMap()).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
- *
+ *
* @param keyspace
* @param tablename
* @param fieldName
@@ -565,30 +619,36 @@ public class RestMusicDataAPI {
* @throws Exception
*/
@POST
- @Path("/{keyspace}/tables/{tablename}/index/{field}")
+ @Path("/{keyspace: .*}/tables/{tablename: .*}/index/{field: .*}")
@ApiOperation(value = "Create Index", response = String.class)
@Produces(MediaType.APPLICATION_JSON)
public Response createIndex(
@ApiParam(value = "Major Version",required = true) @PathParam("version") String version,
@ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
@ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
@ApiParam(value = "Keyspace Name",required = true) @PathParam("keyspace") String keyspace,
@ApiParam(value = "Table Name",required = true) @PathParam("tablename") String tablename,
@ApiParam(value = "Field Name",required = true) @PathParam("field") String fieldName,
@Context UriInfo info) throws Exception {
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
- Map<String, Object> authMap = MusicCore.autheticateUser(ns, userId, password, keyspace,aid, "createIndex");
+ if((keyspace == null || keyspace.isEmpty()) || (tablename == null || tablename.isEmpty()) || (fieldName == null || fieldName.isEmpty())){
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
+ .setError("one or more path parameters are not set, please check and try again")
+ .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,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
+ 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();
}
@@ -599,7 +659,7 @@ public class RestMusicDataAPI {
PreparedQueryObject query = new PreparedQueryObject();
query.appendQueryString("Create index if not exists " + indexName + " on " + keyspace + "."
+ tablename + " (" + fieldName + ");");
-
+
ResultType result = ResultType.FAILURE;
try {
result = MusicCore.nonKeyRelatedPut(query, "eventual");
@@ -613,10 +673,13 @@ public class RestMusicDataAPI {
} else {
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(result).setError("Unknown Error in create index.").toMap()).build();
}
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
- *
+ *
* @param insObj
* @param keyspace
* @param tablename
@@ -624,7 +687,7 @@ public class RestMusicDataAPI {
* @throws Exception
*/
@POST
- @Path("/{keyspace}/tables/{tablename}/rows")
+ @Path("/{keyspace: .*}/tables/{tablename: .*}/rows")
@ApiOperation(value = "Insert Into Table", response = String.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@@ -632,7 +695,7 @@ public class RestMusicDataAPI {
@ApiParam(value = "Major Version",required = true) @PathParam("version") String version,
@ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
@ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
JsonInsert insObj,
@@ -640,24 +703,30 @@ public class RestMusicDataAPI {
required = true) @PathParam("keyspace") String keyspace,
@ApiParam(value = "Table Name",
required = true) @PathParam("tablename") String tablename) {
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
+ if((keyspace == null || keyspace.isEmpty()) || (tablename == null || tablename.isEmpty())){
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
+ .setError("one or more path parameters are not set, please check and try again")
+ .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 = MusicCore.autheticateUser(ns, userId, password, keyspace,
+ authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace,
aid, "insertIntoTable");
} catch (Exception e) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
+ 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,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
+ 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();
}
@@ -665,7 +734,7 @@ public class RestMusicDataAPI {
PreparedQueryObject queryObject = new PreparedQueryObject();
TableMetadata tableInfo = null;
try {
- tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
+ tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
if(tableInfo == null) {
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Table name doesn't exists. Please check the table name.").toMap()).build();
}
@@ -704,7 +773,7 @@ public class RestMusicDataAPI {
logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
}
valueString.append("?");
-
+
queryObject.addValue(formattedValue);
if (counter == valuesMap.size() - 1) {
@@ -716,14 +785,14 @@ public class RestMusicDataAPI {
}
counter = counter + 1;
}
-
+
//blobs..
if(objectMap != null) {
for (Map.Entry<String, byte[]> entry : objectMap.entrySet()) {
- if(counter > 0) {
- fieldsString.replace(fieldsString.length()-1, fieldsString.length(), ",");
- valueString.replace(valueString.length()-1, valueString.length(), ",");
- }
+ if(counter > 0) {
+ fieldsString.replace(fieldsString.length()-1, fieldsString.length(), ",");
+ valueString.replace(valueString.length()-1, valueString.length(), ",");
+ }
fieldsString.append("" + entry.getKey());
byte[] valueObj = entry.getValue();
if (primaryKeyName.equals(entry.getKey())) {
@@ -734,12 +803,12 @@ public class RestMusicDataAPI {
DataType colType = tableInfo.getColumn(entry.getKey()).getType();
ByteBuffer formattedValue = null;
-
+
if(colType.toString().toLowerCase().contains("blob"))
- formattedValue = MusicUtil.convertToActualDataType(colType, valueObj);
-
+ formattedValue = MusicUtil.convertToActualDataType(colType, valueObj);
+
valueString.append("?");
-
+
queryObject.addValue(formattedValue);
counter = counter + 1;
/*if (counter == valuesMap.size() - 1) {
@@ -750,15 +819,15 @@ public class RestMusicDataAPI {
valueString.append(",");
//}
} }
-
+
if(primaryKey == null || primaryKey.length() <= 0) {
logger.error(EELFLoggerDelegate.errorLogger, "Some required partition key parts are missing: "+primaryKeyName );
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.SYNTAXERROR).setError("Some required partition key parts are missing: "+primaryKeyName).toMap()).build();
}
fieldsString.replace(fieldsString.length()-1, fieldsString.length(), ")");
- valueString.replace(valueString.length()-1, valueString.length(), ")");
-
+ valueString.replace(valueString.length()-1, valueString.length(), ")");
+
queryObject.appendQueryString("INSERT INTO " + keyspace + "." + tablename + " "
+ fieldsString + " VALUES " + valueString);
@@ -788,6 +857,13 @@ public class RestMusicDataAPI {
ReturnType result = null;
String consistency = insObj.getConsistencyInfo().get("type");
+ if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL) && insObj.getConsistencyInfo().get("consistency") != null) {
+ if(MusicUtil.isValidConsistency(insObj.getConsistencyInfo().get("consistency")))
+ queryObject.setConsistency(insObj.getConsistencyInfo().get("consistency"));
+ else
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.SYNTAXERROR).setError("Invalid Consistency type").toMap()).build();
+ }
+ queryObject.setOperation("insert");
try {
if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) {
result = MusicCore.eventualPut(queryObject);
@@ -801,6 +877,7 @@ public class RestMusicDataAPI {
}
result = MusicCore.criticalPut(keyspace, tablename, primaryKey, queryObject, lockId,null);
} else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) {
+
result = MusicCore.atomicPut(keyspace, tablename, primaryKey, queryObject, null);
}
@@ -812,25 +889,32 @@ public class RestMusicDataAPI {
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();
}
-
+
if (result==null) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,"Null result - Please Contact admin", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Null result - Please Contact admin").toMap()).build();
+ }else if(result.getResult() == ResultType.FAILURE) {
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(result.getResult()).setError(result.getMessage()).toMap()).build();
}
return response.status(Status.OK).entity(new JsonResponse(result.getResult()).setMessage("Insert Successful").toMap()).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
- *
+ *
* @param insObj
* @param keyspace
* @param tablename
* @param info
* @return
+ * @throws MusicServiceException
+ * @throws MusicQueryException
* @throws Exception
*/
@PUT
- @Path("/{keyspace}/tables/{tablename}/rows")
+ @Path("/{keyspace: .*}/tables/{tablename: .*}/rows")
@ApiOperation(value = "Update Table", response = String.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@@ -841,7 +925,7 @@ public class RestMusicDataAPI {
required = false) @HeaderParam(XMINORVERSION) String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam(NS) String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
@@ -850,15 +934,21 @@ public class RestMusicDataAPI {
required = true) @PathParam("keyspace") String keyspace,
@ApiParam(value = "Table Name",
required = true) @PathParam("tablename") String tablename,
- @Context UriInfo info) {
+ @Context UriInfo info) throws MusicQueryException, MusicServiceException {
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
+ if((keyspace == null || keyspace.isEmpty()) || (tablename == null || tablename.isEmpty())){
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
+ .setError("one or more path parameters are not set, please check and try again")
+ .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 = MusicCore.autheticateUser(ns, userId, password, keyspace,
+ authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace,
aid, "updateTable");
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
@@ -867,13 +957,14 @@ public class RestMusicDataAPI {
if (authMap.containsKey("aid"))
authMap.remove("aid");
if (!authMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ 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();
}
long startTime = System.currentTimeMillis();
String operationId = UUID.randomUUID().toString();// just for infoging
// purposes.
String consistency = updateObj.getConsistencyInfo().get("type");
+
logger.info(EELFLoggerDelegate.applicationLogger, "--------------Music " + consistency
+ " update-" + operationId + "-------------------------");
// obtain the field value pairs of the update
@@ -883,13 +974,13 @@ public class RestMusicDataAPI {
TableMetadata tableInfo;
try {
- tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
+ tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
} catch (MusicServiceException e) {
logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();
}
if (tableInfo == null) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,"Table information not found. Please check input for table name= "+tablename, AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
.setError("Table information not found. Please check input for table name= "
+ keyspace + "." + tablename).toMap()).build();
@@ -969,12 +1060,19 @@ public class RestMusicDataAPI {
selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " WHERE "
+ rowId.rowIdString + ";");
selectQuery.addValue(rowId.primarKeyValue);
- conditionInfo = new MusicCore.Condition(updateObj.getConditions(), selectQuery);
+ conditionInfo = new Condition(updateObj.getConditions(), selectQuery);
}
ReturnType operationResult = null;
long jsonParseCompletionTime = System.currentTimeMillis();
+ if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL) && updateObj.getConsistencyInfo().get("consistency") != null) {
+ if(MusicUtil.isValidConsistency(updateObj.getConsistencyInfo().get("consistency")))
+ queryObject.setConsistency(updateObj.getConsistencyInfo().get("consistency"));
+ else
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.SYNTAXERROR).setError("Invalid Consistency type").toMap()).build();
+ }
+ queryObject.setOperation("update");
if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL))
operationResult = MusicCore.eventualPut(queryObject);
else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) {
@@ -1004,6 +1102,8 @@ public class RestMusicDataAPI {
logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();
}
+ }else if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL_NB)) {
+ operationResult = MusicCore.eventualPut_nb(queryObject, keyspace, tablename, rowId.primarKeyValue);
}
long actualUpdateCompletionTime = System.currentTimeMillis();
@@ -1019,7 +1119,7 @@ public class RestMusicDataAPI {
timingString = timingString + lockManagementTime;
}
logger.info(EELFLoggerDelegate.applicationLogger, timingString);
-
+
if (operationResult==null) {
logger.error(EELFLoggerDelegate.errorLogger,"Null result - Please Contact admin", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Null result - Please Contact admin").toMap()).build();
@@ -1030,20 +1130,24 @@ public class RestMusicDataAPI {
logger.error(EELFLoggerDelegate.errorLogger,operationResult.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(operationResult.getResult()).setError(operationResult.getMessage()).toMap()).build();
}
-
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
- *
+ *
* @param delObj
* @param keyspace
* @param tablename
* @param info
* @return
+ * @throws MusicServiceException
+ * @throws MusicQueryException
* @throws Exception
*/
@DELETE
- @Path("/{keyspace}/tables/{tablename}/rows")
+ @Path("/{keyspace: .*}/tables/{tablename: .*}/rows")
@ApiOperation(value = "Delete From table", response = String.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@@ -1054,7 +1158,7 @@ public class RestMusicDataAPI {
required = false) @HeaderParam(XMINORVERSION) String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam(NS) String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
@@ -1063,15 +1167,21 @@ public class RestMusicDataAPI {
required = true) @PathParam("keyspace") String keyspace,
@ApiParam(value = "Table Name",
required = true) @PathParam("tablename") String tablename,
- @Context UriInfo info) {
+ @Context UriInfo info) throws MusicQueryException, MusicServiceException {
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
+ if((keyspace == null || keyspace.isEmpty()) || (tablename == null || tablename.isEmpty())){
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
+ .setError("one or more path parameters are not set, please check and try again")
+ .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 = MusicCore.autheticateUser(ns, userId, password, keyspace,
+ authMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspace,
aid, "deleteFromTable");
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
@@ -1080,11 +1190,11 @@ public class RestMusicDataAPI {
if (authMap.containsKey("aid"))
authMap.remove("aid");
if (!authMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ 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(delObj == null) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.WARN, ErrorTypes.DATAERROR);
+ 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();
}
PreparedQueryObject queryObject = new PreparedQueryObject();
@@ -1135,12 +1245,22 @@ public class RestMusicDataAPI {
selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " WHERE "
+ rowId.rowIdString + ";");
selectQuery.addValue(rowId.primarKeyValue);
- conditionInfo = new MusicCore.Condition(delObj.getConditions(), selectQuery);
+ conditionInfo = new Condition(delObj.getConditions(), selectQuery);
}
String consistency = delObj.getConsistencyInfo().get("type");
+
+ if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL) && delObj.getConsistencyInfo().get("consistency")!=null) {
+
+ if(MusicUtil.isValidConsistency(delObj.getConsistencyInfo().get("consistency")))
+ queryObject.setConsistency(delObj.getConsistencyInfo().get("consistency"));
+ else
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.SYNTAXERROR).setError("Invalid Consistency type").toMap()).build();
+ }
+
ReturnType operationResult = null;
+ queryObject.setOperation("delete");
try {
if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL))
operationResult = MusicCore.eventualPut(queryObject);
@@ -1161,6 +1281,9 @@ public class RestMusicDataAPI {
else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) {
operationResult = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, rowId.primarKeyValue,
queryObject, conditionInfo);
+ }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);
@@ -1168,26 +1291,29 @@ public class RestMusicDataAPI {
.setError("Unable to perform Delete operation. Exception from music").toMap()).build();
}
if (operationResult==null) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,"Null result - Please Contact admin", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Null result - Please Contact admin").toMap()).build();
}
if (operationResult.getResult().equals(ResultType.SUCCESS)) {
return response.status(Status.OK).entity(new JsonResponse(operationResult.getResult()).setMessage(operationResult.getMessage()).toMap()).build();
} else {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,operationResult.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(operationResult.getMessage()).toMap()).build();
}
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
- *
+ *
* @param tabObj
* @param keyspace
* @param tablename
* @throws Exception
*/
@DELETE
- @Path("/{keyspace}/tables/{tablename}")
+ @Path("/{keyspace: .*}/tables/{tablename: .*}")
@ApiOperation(value = "Drop Table", response = String.class)
@Produces(MediaType.APPLICATION_JSON)
public Response dropTable(
@@ -1197,7 +1323,7 @@ public class RestMusicDataAPI {
required = false) @HeaderParam(XMINORVERSION) String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam(NS) String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
@@ -1205,17 +1331,23 @@ public class RestMusicDataAPI {
required = true) @PathParam("keyspace") String keyspace,
@ApiParam(value = "Table Name",
required = true) @PathParam("tablename") String tablename) throws Exception {
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
+ if((keyspace == null || keyspace.isEmpty()) || (tablename == null || tablename.isEmpty())){
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
+ .setError("one or more path parameters are not set, please check and try again")
+ .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 =
- MusicCore.autheticateUser(ns, userId, password, keyspace, aid, "dropTable");
+ MusicAuthentication.autheticateUser(ns, userId, password, keyspace, aid, "dropTable");
if (authMap.containsKey("aid"))
authMap.remove("aid");
if (!authMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ 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();
}
String consistency = "eventual";// for now this needs only eventual
@@ -1228,11 +1360,13 @@ public class RestMusicDataAPI {
logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build();
}
-
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
- *
+ *
* @param selObj
* @param keyspace
* @param tablename
@@ -1240,7 +1374,7 @@ public class RestMusicDataAPI {
* @return
*/
@PUT
- @Path("/{keyspace}/tables/{tablename}/rows/criticalget")
+ @Path("/{keyspace: .*}/tables/{tablename: .*}/rows/criticalget")
@ApiOperation(value = "Select Critical", response = Map.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@@ -1251,7 +1385,7 @@ public class RestMusicDataAPI {
required = false) @HeaderParam(XMINORVERSION) String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam(NS) String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
@@ -1261,16 +1395,22 @@ public class RestMusicDataAPI {
@ApiParam(value = "Table Name",
required = true) @PathParam("tablename") String tablename,
@Context UriInfo info) throws Exception {
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
- Map<String, Object> authMap = MusicCore.autheticateUser(ns, userId, password, keyspace,aid, "selectCritical");
+ if((keyspace == null || keyspace.isEmpty()) || (tablename == null || tablename.isEmpty())){
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
+ .setError("one or more path parameters are not set, please check and try again")
+ .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,"Error while authentication... ", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ 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();
}
String lockId = selObj.getConsistencyInfo().get("lockId");
@@ -1303,20 +1443,22 @@ 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(MusicCore.marshallResults(results)).toMap()).build();
+ return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicDataStoreHandle.marshallResults(results)).toMap()).build();
}
return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setError("No data found").toMap()).build();
-
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
- *
+ *
* @param keyspace
* @param tablename
* @param info
@@ -1324,36 +1466,42 @@ public class RestMusicDataAPI {
* @throws Exception
*/
@GET
- @Path("/{keyspace}/tables/{tablename}/rows")
+ @Path("/{keyspace: .*}/tables/{tablename: .*}/rows")
@ApiOperation(value = "Select All or Select Specific", response = Map.class)
@Produces(MediaType.APPLICATION_JSON)
public Response select(
- @ApiParam(value = "Major Version",
- required = true) @PathParam("version") String version,
- @ApiParam(value = "Minor Version",
- required = false) @HeaderParam(XMINORVERSION) String minorVersion,
- @ApiParam(value = "Patch Version",
- required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
- @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(MusicUtil.AUTHORIZATION) String authorization,
- @ApiParam(value = "Keyspace Name",
- required = true) @PathParam("keyspace") String keyspace,
- @ApiParam(value = "Table Name",
- required = true) @PathParam("tablename") String tablename,
- @Context UriInfo info) throws Exception {
+ @ApiParam(value = "Major Version",
+ required = true) @PathParam("version") String version,
+ @ApiParam(value = "Minor Version",
+ required = false) @HeaderParam(XMINORVERSION) String minorVersion,
+ @ApiParam(value = "Patch Version",
+ required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
+ @ApiParam(value = "Application namespace",
+ required = true) @HeaderParam(NS) String ns,
+ @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
+ @ApiParam(value = "Keyspace Name",
+ required = true) @PathParam("keyspace") String keyspace,
+ @ApiParam(value = "Table Name",
+ required = true) @PathParam("tablename") String tablename,
+ @Context UriInfo info) throws Exception {
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
-
- Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
- String userId = userCredentials.get(MusicUtil.USERID);
- String password = userCredentials.get(MusicUtil.PASSWORD);
+ if((keyspace == null || keyspace.isEmpty()) || (tablename == null || tablename.isEmpty())){
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
+ .setError("one or more path parameters are not set, please check and try again")
+ .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 =
- MusicCore.autheticateUser(ns, userId, password, keyspace, aid, "select");
+ MusicAuthentication.autheticateUser(ns, userId, password, keyspace, aid, "select");
if (authMap.containsKey("aid"))
authMap.remove("aid");
if (!authMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
+ 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();
}
PreparedQueryObject queryObject = new PreparedQueryObject();
@@ -1366,7 +1514,7 @@ public class RestMusicDataAPI {
queryObject = selectSpecificQuery(VERSION, minorVersion, patchVersion, aid, ns,
userId, password, keyspace, tablename, info, limit);
} catch (MusicServiceException ex) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build();
}
}
@@ -1374,18 +1522,20 @@ public class RestMusicDataAPI {
try {
ResultSet results = MusicCore.get(queryObject);
if(results.getAvailableWithoutFetching() >0) {
- return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicCore.marshallResults(results)).toMap()).build();
+ return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicDataStoreHandle.marshallResults(results)).toMap()).build();
}
- return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicCore.marshallResults(results)).setError("No data found").toMap()).build();
+ 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,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build();
}
-
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
- *
+ *
* @param keyspace
* @param tablename
* @param info
@@ -1415,7 +1565,7 @@ public class RestMusicDataAPI {
}
/**
- *
+ *
* @param keyspace
* @param tablename
* @param rowParams
@@ -1428,7 +1578,7 @@ public class RestMusicDataAPI {
throws MusicServiceException {
StringBuilder rowSpec = new StringBuilder();
int counter = 0;
- TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename);
+ TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
if (tableInfo == null) {
logger.error(EELFLoggerDelegate.errorLogger,
"Table information not found. Please check input for table name= "
diff --git a/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java b/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java
index f6dc4e57..a9b4c7af 100644
--- a/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java
+++ b/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java
@@ -21,6 +21,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.rest;
import java.util.HashMap;
@@ -54,81 +55,81 @@ import io.swagger.annotations.ApiParam;
-@Path("/v{version: [0-9]+}/service")
+@Path("/v2/service")
@Api(value="Healthcheck Api")
public class RestMusicHealthCheckAPI {
-
-
- private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
- private static final String activeStatus = "ACTIVE";
-
- @GET
- @Path("/pingCassandra/{consistency}")
- @ApiOperation(value = "Get Health Status", response = Map.class)
- @Produces(MediaType.APPLICATION_JSON)
- public Response cassandraStatus(@Context HttpServletResponse response, @ApiParam(value = "Consistency level",
+
+
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
+ private static final String ACTIVE_STATUS = "ACTIVE";
+
+ @GET
+ @Path("/pingCassandra/{consistency}")
+ @ApiOperation(value = "Get Health Status", response = Map.class)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response cassandraStatus(@Context HttpServletResponse response, @ApiParam(value = "Consistency level",
required = true) @PathParam("consistency") String consistency) {
- logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Cassandra");
-
- Map<String, Object> resultMap = new HashMap<>();
- if(ConsistencyLevel.valueOf(consistency) == null) {
- resultMap.put("INVALID", "Consistency level is invalid...");
- return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
- }
- MusicHealthCheck cassHealthCheck = new MusicHealthCheck();
- String status = cassHealthCheck.getCassandraStatus(consistency);
- if(status.equals(activeStatus)) {
- resultMap.put(activeStatus, "Cassandra Running and Listening to requests");
- return Response.status(Status.OK).entity(resultMap).build();
- } else {
- resultMap.put("INACTIVE", "One or more nodes in the Cluster is/are down or not responding.");
- return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
- }
-
-
-
- }
-
- @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(activeStatus)) {
- resultMap.put(activeStatus, "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)
- public Response musicHealthCheck() {
- 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(activeStatus)) {
- resultMap.put("ZooKeeper", "Active");
- }else {
- resultMap.put("ZooKeeper", "Inactive");
- }
- status = healthCheck.getCassandraStatus(ConsistencyLevel.ANY.toString());
- if(status.equals(activeStatus)) {
- resultMap.put("Cassandra", "Active");
- } else {
- resultMap.put("Cassandra", "Inactive");
- }
- resultMap.put("MUSIC", "Active");
- return Response.status(Status.OK).entity(resultMap).build();
- }
+ logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Cassandra");
+
+ Map<String, Object> resultMap = new HashMap<>();
+ if(ConsistencyLevel.valueOf(consistency) == null) {
+ resultMap.put("INVALID", "Consistency level is invalid...");
+ return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ }
+ MusicHealthCheck cassHealthCheck = new MusicHealthCheck();
+ String status = cassHealthCheck.getCassandraStatus(consistency);
+ if(status.equals(ACTIVE_STATUS)) {
+ resultMap.put(ACTIVE_STATUS, "Cassandra Running and Listening to requests");
+ return Response.status(Status.OK).entity(resultMap).build();
+ } else {
+ resultMap.put("INACTIVE", "One or more nodes in the Cluster is/are down or not responding.");
+ return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ }
+
+
+
+ }
+
+ @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)
+ public Response musicHealthCheck() {
+ 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());
+ if(status.equals(ACTIVE_STATUS)) {
+ resultMap.put("Cassandra", "Active");
+ } else {
+ resultMap.put("Cassandra", "Inactive");
+ }
+ resultMap.put("MUSIC", "Active");
+ return Response.status(Status.OK).entity(resultMap).build();
+ }
}
diff --git a/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java b/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java
index 70583baa..0bb2368a 100644
--- a/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java
+++ b/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.rest;
import java.util.Map;
@@ -35,12 +36,14 @@ import javax.ws.rs.core.MediaType;
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.datastore.jsonobjects.JsonLeasedLock;
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.MusicLockState;
+import org.onap.music.lockingservice.cassandra.MusicLockState;
import org.onap.music.main.MusicCore;
import org.onap.music.main.MusicUtil;
import org.onap.music.main.ResultType;
@@ -85,24 +88,28 @@ public class RestMusicLocksAPI {
@ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam("ns") String ns) throws Exception{
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
Map<String, Object> resultMap = MusicCore.validateLock(lockName);
- if (resultMap.containsKey("Exception")) {
+ if (resultMap.containsKey("Error")) {
logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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);
+ 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 = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+ resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
"createLockReference");
if (resultMap.containsKey("aid"))
resultMap.remove("aid");
if (!resultMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
- return response.status(Status.UNAUTHORIZED).entity(resultMap).build();
+ 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();
}
ResultType status = ResultType.SUCCESS;
String lockId = MusicCore.createLockReference(lockName);
@@ -113,6 +120,9 @@ public class RestMusicLocksAPI {
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError("Lock Id is null").toMap()).build();
}
return response.status(Status.OK).entity(new JsonResponse(status).setLock(lockId).toMap()).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
@@ -137,24 +147,28 @@ public class RestMusicLocksAPI {
@ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam("ns") String ns) throws Exception{
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
Map<String, Object> resultMap = MusicCore.validateLock(lockId);
- if (resultMap.containsKey("Exception")) {
+ if (resultMap.containsKey("Error")) {
logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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);
+ 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 = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+ resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
"accquireLock");
if (resultMap.containsKey("aid"))
resultMap.remove("aid");
if (!resultMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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();
}
try {
String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
@@ -169,6 +183,9 @@ public class RestMusicLocksAPI {
logger.error(EELFLoggerDelegate.errorLogger,AppMessages.INVALIDLOCK + lockId, ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Unable to aquire lock").toMap()).build();
}
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
@@ -187,25 +204,29 @@ public class RestMusicLocksAPI {
@ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam("ns") String ns) throws Exception{
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
Map<String, Object> resultMap = MusicCore.validateLock(lockId);
- if (resultMap.containsKey("Exception")) {
+ if (resultMap.containsKey("Error")) {
logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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);
+ 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 = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+ resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
"accquireLockWithLease");
if (resultMap.containsKey("aid"))
resultMap.remove("aid");
if (!resultMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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();
}
String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod());
@@ -217,6 +238,9 @@ public class RestMusicLocksAPI {
return response.entity(new JsonResponse(lockLeaseStatus.getResult()).setLock(lockName)
.setMessage(lockLeaseStatus.getMessage())
.setLockLease(String.valueOf(lockObj.getLeasePeriod())).toMap()).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
@@ -234,24 +258,28 @@ public class RestMusicLocksAPI {
@ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam("ns") String ns) throws Exception{
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
Map<String, Object> resultMap = MusicCore.validateLock(lockName);
- if (resultMap.containsKey("Exception")) {
+ if (resultMap.containsKey("Error")) {
logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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);
+ 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 = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+ resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
"currentLockHolder");
if (resultMap.containsKey("aid"))
resultMap.remove("aid");
if (!resultMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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();
}
String who = MusicCore.whoseTurnIsIt(lockName);
ResultType status = ResultType.SUCCESS;
@@ -263,6 +291,9 @@ public class RestMusicLocksAPI {
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
}
return response.status(Status.OK).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
@GET
@@ -278,43 +309,61 @@ public class RestMusicLocksAPI {
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
@ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
- required = true) @HeaderParam("ns") String ns,
- @ApiParam(value = "userId",
- required = true) @HeaderParam("userId") String userId,
- @ApiParam(value = "Password",
- required = true) @HeaderParam("password") String password) throws Exception{
+ required = true) @HeaderParam("ns") String ns) throws Exception{
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
Map<String, Object> resultMap = MusicCore.validateLock(lockName);
- if (resultMap.containsKey("Exception")) {
+ if (resultMap.containsKey("Error")) {
logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ response.status(Status.BAD_REQUEST);
+ return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
}
String keyspaceName = (String) resultMap.get("keyspace");
+ EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
resultMap.remove("keyspace");
- resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
- "currentLockState");
-
+ 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.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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();
}
- MusicLockState mls = MusicCore.getMusicLockState(lockName);
- Map<String,Object> returnMap = null;
- JsonResponse jsonResponse = new JsonResponse(ResultType.FAILURE).setLock(lockName);
- if(mls == null) {
- jsonResponse.setError("");
- jsonResponse.setMessage("No lock object created yet..");
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(jsonResponse.toMap()).build();
- } else {
- jsonResponse.setStatus(ResultType.SUCCESS);
- jsonResponse.setLockStatus(mls.getLockStatus());
- jsonResponse.setLockHolder(mls.getLockHolder());
- return response.status(Status.OK).entity(jsonResponse.toMap()).build();
+ String who = MusicCore.whoseTurnIsIt(lockName);
+ ResultType status = ResultType.SUCCESS;
+ String error = "";
+ if ( who == null ) {
+ status = ResultType.FAILURE;
+ error = "There was a problem getting the lock holder";
+ logger.error(EELFLoggerDelegate.errorLogger,"There was a problem getting the lock holder", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
+ return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
+ }
+ return response.status(Status.OK).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
}
+
+ //MusicLockState mls = MusicZKCore.getMusicLockState(lockName);
+// Map<String,Object> returnMap = null;
+// JsonResponse jsonResponse = new JsonResponse(ResultType.FAILURE).setLock(lockName);
+// if(mls == null) {
+// jsonResponse.setError("");
+// jsonResponse.setMessage("No lock object created yet..");
+// logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
+// return response.status(Status.BAD_REQUEST).entity(jsonResponse.toMap()).build();
+// } else {
+// jsonResponse.setStatus(ResultType.SUCCESS);
+// jsonResponse.setLockStatus(mls.getLockStatus());
+// jsonResponse.setLockHolder(mls.getLockHolder());
+// return response.status(Status.OK).entity(jsonResponse.toMap()).build();
+// }
+
}
/**
@@ -337,24 +386,28 @@ public class RestMusicLocksAPI {
@ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam("ns") String ns) throws Exception{
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
Map<String, Object> resultMap = MusicCore.validateLock(lockId);
- if (resultMap.containsKey("Exception")) {
+ if (resultMap.containsKey("Error")) {
logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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);
+ 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 = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+ resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
"unLock");
if (resultMap.containsKey("aid"))
resultMap.remove("aid");
if (!resultMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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();
}
boolean voluntaryRelease = true;
MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease);
@@ -376,6 +429,9 @@ public class RestMusicLocksAPI {
response.status(Status.BAD_REQUEST);
}
return response.entity(returnMap).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
/**
@@ -394,31 +450,38 @@ public class RestMusicLocksAPI {
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
@ApiParam(value = "Application namespace",
required = true) @HeaderParam("ns") String ns) throws Exception{
+ try {
ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
Map<String, Object> resultMap = MusicCore.validateLock(lockName);
- if (resultMap.containsKey("Exception")) {
+ if (resultMap.containsKey("Error")) {
logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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);
+ 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 = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+ resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
"deleteLock");
if (resultMap.containsKey("aid"))
resultMap.remove("aid");
if (!resultMap.isEmpty()) {
- logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
- return response.status(Status.BAD_REQUEST).entity(resultMap).build();
+ 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();
}
try{
- MusicCore.deleteLock(lockName);
+ MusicCore.deleteLock(lockName);
}catch (Exception e) {
return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();
- }
+ }
return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).toMap()).build();
+ } finally {
+ EELFLoggerDelegate.mdcRemove("keyspace");
+ }
}
}
diff --git a/src/main/java/org/onap/music/rest/RestMusicQAPI.java b/src/main/java/org/onap/music/rest/RestMusicQAPI.java
index a883534e..4164f27f 100755
--- a/src/main/java/org/onap/music/rest/RestMusicQAPI.java
+++ b/src/main/java/org/onap/music/rest/RestMusicQAPI.java
@@ -1,21 +1,25 @@
/*
- * ============LICENSE_START========================================== org.onap.music
- * =================================================================== Copyright (c) 2017 AT&T
- * Intellectual Property ===================================================================Modifications Copyright (c) 2018 IBM
- * Intellectual Property =================================================================== *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.rest;
import java.util.HashMap;
@@ -45,6 +49,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.apache.commons.lang3.StringUtils;
+import org.onap.music.datastore.MusicDataStoreHandle;
import org.onap.music.datastore.PreparedQueryObject;
import com.datastax.driver.core.ResultSet;
import org.onap.music.exceptions.MusicServiceException;
@@ -52,13 +57,15 @@ 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("{version}/priorityq/")
+@Path("/v2/priorityq/")
@Api(value = "Q Api")
public class RestMusicQAPI {
@@ -74,37 +81,29 @@ public class RestMusicQAPI {
*/
@POST
- @Path("/keyspaces/{keyspace}/{qname}") // is it same as tablename?down
+ @Path("/keyspaces/{keyspace}/{qname}") // qname same as tablename
@ApiOperation(value = "Create Q", response = String.class)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
-
- /* old
- @POST
- @Path("/keyspaces/{keyspace}/{qname}")
- @ApiOperation(value = "", response = Void.class)
- @Consumes(MediaType.APPLICATION_JSON)
- */
public Response createQ(
- // public Map<String,Object> createQ(
@ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
@ApiParam(value = "Minor Version",
required = false) @HeaderParam("X-minorVersion") String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam("X-patchVersion") String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
@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);
ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion);
Map<String, String> fields = tableObj.getFields();
- if (fields == null) {
+ if (fields == null) { // || (!fields.containsKey("order")) ){
logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
return response.status(Status.BAD_REQUEST)
@@ -151,19 +150,19 @@ public class RestMusicQAPI {
}
if ((primaryKey!=null) && (partitionKey == null)) {
- primaryKey.trim();
- int count1 = StringUtils.countMatches(primaryKey, ')');
- int count2 = StringUtils.countMatches(primaryKey, '(');
+ 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("CreateQ 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.indexOf('(') == -1 || ( count2 == 1 && (primaryKey.lastIndexOf(')') +1) == primaryKey.length() ) )
{
if (primaryKey.contains(",") ) {
- partitionKey= primaryKey.substring(0,primaryKey.indexOf(","));
+ partitionKey= primaryKey.substring(0,primaryKey.indexOf(','));
partitionKey=partitionKey.replaceAll("[\\(]+","");
clusteringKey=primaryKey.substring(primaryKey.indexOf(',')+1); // make sure index
clusteringKey=clusteringKey.replaceAll("[)]+", "");
@@ -176,18 +175,14 @@ public class RestMusicQAPI {
} else {
partitionKey= primaryKey.substring(0,primaryKey.indexOf(')'));
partitionKey=partitionKey.replaceAll("[\\(]+","");
- partitionKey.trim();
+ partitionKey = partitionKey.trim();
clusteringKey= primaryKey.substring(primaryKey.indexOf(')'));
clusteringKey=clusteringKey.replaceAll("[\\(]+","");
clusteringKey=clusteringKey.replaceAll("[\\)]+","");
- clusteringKey.trim();
- if (clusteringKey.indexOf(",") == 0) {
- clusteringKey=clusteringKey.substring(1);
- }
- clusteringKey.trim();
- if (",".equals(clusteringKey) ) {
- clusteringKey=""; // print error if needed ( ... ),)
- }
+ clusteringKey = clusteringKey.trim();
+ if (clusteringKey.indexOf(',') == 0) clusteringKey=clusteringKey.substring(1);
+ clusteringKey = clusteringKey.trim();
+ if (clusteringKey.equals(",") ) clusteringKey=""; // print error if needed ( ... ),)
}
}
@@ -240,16 +235,13 @@ public class RestMusicQAPI {
required = false) @HeaderParam("X-minorVersion") String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam("X-patchVersion") String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
JsonInsert insObj,
@ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace,
@ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename)
throws Exception {
-
- // check valuesMap.isEmpty and proceed
- // response.addHeader(xLatestVersion, MusicUtil.getVersion());
ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion);
if (insObj.getValues().isEmpty()) {
logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
@@ -281,13 +273,13 @@ public class RestMusicQAPI {
required = false) @HeaderParam("X-minorVersion") String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam("X-patchVersion") String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
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) {
+ @Context UriInfo info) throws Exception {
ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion);
if (updateObj.getValues().isEmpty()) {
@@ -327,15 +319,13 @@ public class RestMusicQAPI {
required = false) @HeaderParam("X-minorVersion") String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam("X-patchVersion") String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
- // @ApiParam(value = "userId", required = true) @HeaderParam("userId") String userId,
- // @ApiParam(value = "Password", required = true) @HeaderParam("password") String password,
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) {
+ @Context UriInfo info) throws Exception {
// added checking as per RestMusicDataAPI
ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion);
if (delObj == null) {
@@ -368,7 +358,7 @@ public class RestMusicQAPI {
required = false) @HeaderParam("X-minorVersion") String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam("X-patchVersion") String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@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,
@@ -390,7 +380,6 @@ public class RestMusicQAPI {
queryObject = new RestMusicDataAPI().selectSpecificQuery(version, minorVersion,
patchVersion, aid, ns, userId, password, keyspace, tablename, info, limit);
} catch (MusicServiceException ex) {
- logger.error(EELFLoggerDelegate.errorLogger, "MusicServiceException occured in peek"+ ex);
logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.UNKNOWNERROR,
ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
return response.status(Status.BAD_REQUEST)
@@ -402,9 +391,8 @@ public class RestMusicQAPI {
try {
ResultSet results = MusicCore.get(queryObject);
return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS)
- .setDataResult(MusicCore.marshallResults(results)).toMap()).build();
+ .setDataResult(MusicDataStoreHandle.marshallResults(results)).toMap()).build();
} catch (MusicServiceException ex) {
- logger.error(EELFLoggerDelegate.errorLogger, "MusicServiceException occured in peek"+ ex);
logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.UNKNOWNERROR,
ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR);
return response.status(Status.BAD_REQUEST)
@@ -428,20 +416,15 @@ public class RestMusicQAPI {
@Produces(MediaType.APPLICATION_JSON)
// public Map<String, HashMap<String, Object>> filter(
public Response filter(
- @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
- @ApiParam(value = "Minor Version",
- required = false) @HeaderParam("X-minorVersion") String minorVersion,
- @ApiParam(value = "Patch Version",
- required = false) @HeaderParam("X-patchVersion") String patchVersion,
- @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(MusicUtil.AUTHORIZATION) String authorization,
- // @ApiParam(value = "userId", required = true) @HeaderParam("userId") String userId,
- //@ApiParam(value = "Password", required = true) @HeaderParam("password") String password,
- @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 {
-
+ @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
+ @ApiParam(value = "Minor Version", required = false) @HeaderParam("X-minorVersion") String minorVersion,
+ @ApiParam(value = "Patch Version", required = false) @HeaderParam("X-patchVersion") String patchVersion,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
+ @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,
+ @Context UriInfo info) throws Exception {
return new RestMusicDataAPI().select(version, minorVersion, patchVersion, aid, ns, authorization, keyspace, tablename, info);// , limit)
}
@@ -463,17 +446,13 @@ public class RestMusicQAPI {
required = false) @HeaderParam("X-minorVersion") String minorVersion,
@ApiParam(value = "Patch Version",
required = false) @HeaderParam("X-patchVersion") String patchVersion,
- @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
+ @ApiParam(value = "AID", required = false) @HeaderParam("aid") String aid,
@ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
@ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
- // @ApiParam(value = "userId", required = true) @HeaderParam("userId") String userId,
- //@ApiParam(value = "Password", required = true) @HeaderParam("password") String password,
- // cjc JsonTable tabObj,
@ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace,
@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/rest/RestMusicTestAPI.java b/src/main/java/org/onap/music/rest/RestMusicTestAPI.java
index 287fa176..6d973acb 100644
--- a/src/main/java/org/onap/music/rest/RestMusicTestAPI.java
+++ b/src/main/java/org/onap/music/rest/RestMusicTestAPI.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.rest;
import java.util.HashMap;
@@ -41,27 +42,27 @@ import io.swagger.annotations.ApiOperation;
@Path("/v{version: [0-9]+}/test")
@Api(value="Test Api")
public class RestMusicTestAPI {
-
- @SuppressWarnings("unused")
+
+ @SuppressWarnings("unused")
private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicTestAPI.class);
- /**
- * Returns a test JSON. This will confirm that REST is working.
- * @return
- */
- @GET
- @ApiOperation(value = "Get Test", response = Map.class)
- @Produces(MediaType.APPLICATION_JSON)
- public Map<String, HashMap<String, String>> simpleTests(
- @Context HttpServletResponse response) {
- response.addHeader("X-latestVersion",MusicUtil.getVersion());
- Map<String, HashMap<String, String>> testMap = new HashMap<>();
- for(int i=0; i < 3; i++){
- HashMap<String, String> innerMap = new HashMap<>();
- innerMap.put(i+"", i+1+"");
- innerMap.put(i+1+"", i+2+"");
- testMap.put(i+"", innerMap);
- }
- return testMap;
- }
+ /**
+ * Returns a test JSON. This will confirm that REST is working.
+ * @return
+ */
+ @GET
+ @ApiOperation(value = "Get Test", response = Map.class)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Map<String, HashMap<String, String>> simpleTests(
+ @Context HttpServletResponse response) {
+ response.addHeader("X-latestVersion",MusicUtil.getVersion());
+ Map<String, HashMap<String, String>> testMap = new HashMap<>();
+ for(int i=0; i < 3; i++){
+ HashMap<String, String> innerMap = new HashMap<>();
+ innerMap.put("Music Version",MusicUtil.getVersion());
+ innerMap.put(i+1+"", i+2+"");
+ testMap.put(i+"", innerMap);
+ }
+ return testMap;
+ }
}
diff --git a/src/main/java/org/onap/music/rest/RestMusicVersionAPI.java b/src/main/java/org/onap/music/rest/RestMusicVersionAPI.java
index b9754f61..74b8e5dd 100644
--- a/src/main/java/org/onap/music/rest/RestMusicVersionAPI.java
+++ b/src/main/java/org/onap/music/rest/RestMusicVersionAPI.java
@@ -19,6 +19,7 @@
* ============LICENSE_END=============================================
* ====================================================================
*/
+
package org.onap.music.rest;
import java.util.Map;
@@ -35,6 +36,9 @@ import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.main.MusicUtil;
import org.onap.music.main.ResultType;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -43,18 +47,18 @@ import io.swagger.annotations.ApiOperation;
@Api(value="Version Api")
public class RestMusicVersionAPI {
- private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicVersionAPI.class);
-
- /**
- * Get the version of MUSIC
- * @return
- */
- @GET
- @ApiOperation(value = "Get Version", response = Map.class)
- @Produces(MediaType.APPLICATION_JSON)
- public Map<String,Object> version(@Context HttpServletResponse response) {
- logger.info("Replying to request for MUSIC version with MUSIC:" + MusicUtil.getVersion());
- response.addHeader("X-latestVersion",MusicUtil.getVersion());
- return new JsonResponse(ResultType.SUCCESS).setMusicVersion("MUSIC:" + MusicUtil.getVersion()).toMap();
- }
+ private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicVersionAPI.class);
+
+ /**
+ * Get the version of MUSIC
+ * @return
+ */
+ @GET
+ @ApiOperation(value = "Get Version", response = Map.class)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Map<String,Object> version(@Context HttpServletResponse response) {
+ logger.info("Replying to request for MUSIC version with MUSIC:" + MusicUtil.getVersion());
+ response.addHeader("X-latestVersion",MusicUtil.getVersion());
+ return new JsonResponse(ResultType.SUCCESS).setMusicVersion("MUSIC:" + MusicUtil.getVersion()).toMap();
+ }
} \ No newline at end of file
diff --git a/src/main/java/org/onap/music/service/MusicCoreService.java b/src/main/java/org/onap/music/service/MusicCoreService.java
new file mode 100644
index 00000000..affdc7f2
--- /dev/null
+++ b/src/main/java/org/onap/music/service/MusicCoreService.java
@@ -0,0 +1,110 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.service;
+
+import java.util.List;
+import java.util.Map;
+
+import org.onap.music.datastore.PreparedQueryObject;
+import org.onap.music.exceptions.MusicLockingException;
+import org.onap.music.exceptions.MusicQueryException;
+import org.onap.music.exceptions.MusicServiceException;
+import org.onap.music.lockingservice.cassandra.MusicLockState;
+import org.onap.music.main.ResultType;
+import org.onap.music.main.ReturnType;
+import org.onap.music.datastore.*;
+
+import com.datastax.driver.core.ResultSet;
+
+public interface MusicCoreService {
+
+
+ // Core Music Database Methods
+
+
+ public ReturnType eventualPut(PreparedQueryObject queryObject);
+
+ public ReturnType eventualPut_nb(PreparedQueryObject queryObject,String keyspace,String tablename,String primaryKey);
+
+ public ReturnType criticalPut(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject, String lockId, Condition conditionInfo);
+
+ public ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency)
+ throws MusicServiceException;
+
+ public ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException;
+
+ public ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException, MusicQueryException;
+
+ public ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException;
+
+ public ResultSet atomicGetWithDeleteLock(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException;
+
+ public ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject, Condition conditionInfo)
+ throws MusicLockingException, MusicQueryException, MusicServiceException;
+
+ public ResultSet criticalGet(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject, String lockId) throws MusicServiceException;
+
+ // Core Music Locking Service Methods
+
+ public String createLockReference(String fullyQualifiedKey); // lock name
+
+ public ReturnType acquireLockWithLease(String key, String lockReference, long leasePeriod)
+ throws MusicLockingException, MusicQueryException, MusicServiceException; // key,lock id,time
+
+ public ReturnType acquireLock(String key, String lockReference)
+ throws MusicLockingException, MusicQueryException, MusicServiceException; // key,lock id
+
+ public ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject,
+ String consistency) throws MusicServiceException;
+
+ public ResultSet quorumGet(PreparedQueryObject query);
+
+ public String whoseTurnIsIt(String fullyQualifiedKey);// lock name
+
+ public void destroyLockRef(String lockId);
+
+ //public MusicLockState destroyLockRef(String fullyQualifiedKey, String lockReference); // lock name, lock id
+
+ //public MusicLockState voluntaryReleaseLock(String fullyQualifiedKey, String lockReference)
+ // throws MusicLockingException;// lock name,lock id
+
+ public void deleteLock(String lockName) throws MusicLockingException;
+
+ //public MusicLockState forciblyReleaseLock(String fullyQualifiedKey, String lockReference) throws MusicLockingException, MusicServiceException, MusicQueryException;
+
+ public List<String> getLockQueue(String fullyQualifiedKey)
+ throws MusicServiceException, MusicQueryException, MusicLockingException;
+
+ public long getLockQueueSize(String fullyQualifiedKey)
+ throws MusicServiceException, MusicQueryException, MusicLockingException;
+
+ public Map<String, Object> validateLock(String lockName);
+
+ public MusicLockState releaseLock(String lockId, boolean voluntaryRelease);
+}
diff --git a/src/main/java/org/onap/music/service/impl/MusicCassaCore.java b/src/main/java/org/onap/music/service/impl/MusicCassaCore.java
new file mode 100644
index 00000000..8737a060
--- /dev/null
+++ b/src/main/java/org/onap/music/service/impl/MusicCassaCore.java
@@ -0,0 +1,762 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.service.impl;
+
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import org.onap.music.datastore.MusicDataStore;
+import org.onap.music.datastore.MusicDataStoreHandle;
+import org.onap.music.datastore.PreparedQueryObject;
+import org.onap.music.eelf.logging.EELFLoggerDelegate;
+import org.onap.music.eelf.logging.format.AppMessages;
+import org.onap.music.eelf.logging.format.ErrorSeverity;
+import org.onap.music.eelf.logging.format.ErrorTypes;
+import org.onap.music.exceptions.MusicLockingException;
+import org.onap.music.exceptions.MusicQueryException;
+import org.onap.music.exceptions.MusicServiceException;
+import org.onap.music.lockingservice.cassandra.CassaLockStore;
+import org.onap.music.lockingservice.cassandra.CassaLockStore.LockObject;
+import org.onap.music.lockingservice.cassandra.MusicLockState;
+import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus;
+import org.onap.music.main.MusicUtil;
+import org.onap.music.main.ResultType;
+import org.onap.music.main.ReturnType;
+import org.onap.music.service.MusicCoreService;
+
+import com.datastax.driver.core.ColumnDefinitions;
+import com.datastax.driver.core.ColumnDefinitions.Definition;
+import com.datastax.driver.core.DataType;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+import com.datastax.driver.core.TableMetadata;
+import com.google.common.util.concurrent.Monitor.Guard;
+
+import org.onap.music.datastore.*;
+
+public class MusicCassaCore implements MusicCoreService {
+
+ public static CassaLockStore mLockHandle = null;;
+ private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicCassaCore.class);
+ private static boolean unitTestRun=true;
+ private static MusicCassaCore musicCassaCoreInstance = null;
+
+ private MusicCassaCore() {
+
+ }
+ public static MusicCassaCore getInstance() {
+
+ if(musicCassaCoreInstance == null) {
+ musicCassaCoreInstance = new MusicCassaCore();
+ }
+ return musicCassaCoreInstance;
+ }
+
+ public static CassaLockStore getLockingServiceHandle() throws MusicLockingException {
+ logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring lock store handle");
+ long start = System.currentTimeMillis();
+
+ if (mLockHandle == null) {
+ try {
+ mLockHandle = new CassaLockStore(MusicDataStoreHandle.getDSHandle());
+ } catch (Exception e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKHANDLE,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
+ throw new MusicLockingException("Failed to aquire Locl store handle " + e);
+ }
+ }
+ long end = System.currentTimeMillis();
+ logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire lock store handle:" + (end - start) + " ms");
+ return mLockHandle;
+ }
+
+
+
+ public String createLockReference(String fullyQualifiedKey) {
+ String[] splitString = fullyQualifiedKey.split("\\.");
+ String keyspace = splitString[0];
+ String table = splitString[1];
+ String lockName = splitString[2];
+
+ logger.info(EELFLoggerDelegate.applicationLogger,"Creating lock reference for lock name:" + lockName);
+ long start = System.currentTimeMillis();
+ String lockReference = null;
+ try {
+ lockReference = "" + getLockingServiceHandle().genLockRefandEnQueue(keyspace, table, lockName);
+ } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
+ e.printStackTrace();
+ }
+ long end = System.currentTimeMillis();
+ logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to create lock reference:" + (end - start) + " ms");
+ return lockReference;
+ }
+
+
+ public ReturnType acquireLockWithLease(String fullyQualifiedKey, String lockReference, long leasePeriod) throws MusicLockingException, MusicQueryException, MusicServiceException {
+ evictExpiredLockHolder(fullyQualifiedKey,leasePeriod);
+ return acquireLock(fullyQualifiedKey, lockReference);
+ }
+
+ private void evictExpiredLockHolder(String fullyQualifiedKey, long leasePeriod) throws MusicLockingException, MusicQueryException, MusicServiceException {
+
+ String[] splitString = fullyQualifiedKey.split("\\.");
+ String keyspace = splitString[0];
+ String table = splitString[1];
+ String primaryKeyValue = splitString[2];
+
+ LockObject currentLockHolderObject = getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue);
+
+ /* Release the lock of the previous holder if it has expired. if the update to the acquire time has not reached due to network delays, simply use the create time as the
+ * reference*/
+
+ long referenceTime = Math.max(Long.parseLong(currentLockHolderObject.acquireTime), Long.parseLong(currentLockHolderObject.createTime));
+ if((System.currentTimeMillis() - referenceTime) > leasePeriod) {
+ forciblyReleaseLock(fullyQualifiedKey, currentLockHolderObject.lockRef+"");
+ logger.info(EELFLoggerDelegate.applicationLogger, currentLockHolderObject.lockRef+" forcibly released");
+ }
+ }
+
+ private static ReturnType isTopOfLockStore(String keyspace, String table, String primaryKeyValue, String lockReference) throws MusicLockingException, MusicQueryException, MusicServiceException {
+
+ //return failure to lock holders too early or already evicted from the lock store
+ String topOfLockStoreS = getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue).lockRef;
+ long topOfLockStoreL = Long.parseLong(topOfLockStoreS);
+ long lockReferenceL = Long.parseLong(lockReference);
+
+ if(lockReferenceL > topOfLockStoreL) {
+ logger.info(EELFLoggerDelegate.applicationLogger, lockReference+" is not the lock holder yet");
+ return new ReturnType(ResultType.FAILURE, lockReference+" is not the lock holder yet");
+ }
+
+
+ if(lockReferenceL < topOfLockStoreL) {
+ logger.info(EELFLoggerDelegate.applicationLogger, lockReference+" is no longer/or was never in the lock store queue");
+ return new ReturnType(ResultType.FAILURE, lockReference+" is no longer/or was never in the lock store queue");
+ }
+
+ return new ReturnType(ResultType.SUCCESS, lockReference+" is top of lock store");
+ }
+
+ public ReturnType acquireLock(String fullyQualifiedKey, String lockId)
+ throws MusicLockingException, MusicQueryException, MusicServiceException {
+ String[] splitString = lockId.split("\\.");
+ String keyspace = splitString[0].substring(1);//remove '$'
+ String table = splitString[1];
+ String primaryKeyValue = splitString[2].substring(0, splitString[2].lastIndexOf("$"));
+ fullyQualifiedKey = lockId.substring(1, lockId.lastIndexOf("$"));
+ String lockRef = lockId.substring(lockId.lastIndexOf("$")+1); //lockRef is "$" to end
+
+ ReturnType result = isTopOfLockStore(keyspace, table, primaryKeyValue, lockRef);
+
+ if(result.getResult().equals(ResultType.FAILURE))
+ return result;//not top of the lock store q
+
+ //check to see if the value of the key has to be synced in case there was a forceful release
+ String syncTable = keyspace+".unsyncedKeys_"+table;
+ String query = "select * from "+syncTable+" where key='"+fullyQualifiedKey+"';";
+ PreparedQueryObject readQueryObject = new PreparedQueryObject();
+ readQueryObject.appendQueryString(query);
+ ResultSet results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(readQueryObject);
+ if (results.all().size() != 0) {
+ logger.info("In acquire lock: Since there was a forcible release, need to sync quorum!");
+ try {
+ syncQuorum(keyspace, table, primaryKeyValue);
+ } catch (Exception e) {
+ StringWriter sw = new StringWriter();
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR506E] Failed to aquire lock ",ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
+ String exceptionAsString = sw.toString();
+ return new ReturnType(ResultType.FAILURE, "Exception thrown while syncing key:\n" + exceptionAsString);
+ }
+ String cleanQuery = "delete from music_internal.unsynced_keys where key='"+fullyQualifiedKey+"';";
+ PreparedQueryObject deleteQueryObject = new PreparedQueryObject();
+ deleteQueryObject.appendQueryString(cleanQuery);
+ MusicDataStoreHandle.getDSHandle().executePut(deleteQueryObject, "critical");
+ }
+
+ getLockingServiceHandle().updateLockAcquireTime(keyspace, table, primaryKeyValue, lockRef);
+
+ return new ReturnType(ResultType.SUCCESS, lockRef+" is the lock holder for the key");
+ }
+
+
+
+ /**
+ *
+ * @param tableQueryObject
+ * @param consistency
+ * @return Boolean Indicates success or failure
+ * @throws MusicServiceException
+ *
+ *
+ */
+ public ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject, String consistency) throws MusicServiceException {
+ boolean result = false;
+
+ try {
+ //create shadow locking table
+ result = getLockingServiceHandle().createLockQueue(keyspace, table);
+ if(result == false)
+ return ResultType.FAILURE;
+
+ result = false;
+
+ //create table to track unsynced_keys
+ table = "unsyncedKeys_"+table;
+
+ String tabQuery = "CREATE TABLE IF NOT EXISTS "+keyspace+"."+table
+ + " ( key text,PRIMARY KEY (key) );";
+ System.out.println(tabQuery);
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+
+ queryObject.appendQueryString(tabQuery);
+ result = false;
+ result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, "eventual");
+
+
+ //create actual table
+ result = MusicDataStoreHandle.getDSHandle().executePut(tableQueryObject, consistency);
+ } catch (MusicQueryException | MusicServiceException | MusicLockingException ex) {
+ logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ throw new MusicServiceException(ex.getMessage());
+ }
+ return result?ResultType.SUCCESS:ResultType.FAILURE;
+ }
+
+ private static void syncQuorum(String keyspace, String table, String primaryKeyValue) throws Exception {
+ logger.info(EELFLoggerDelegate.applicationLogger,"Performing sync operation---");
+ PreparedQueryObject selectQuery = new PreparedQueryObject();
+ PreparedQueryObject updateQuery = new PreparedQueryObject();
+
+ // get the primary key d
+ TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, table);
+ String primaryKeyName = tableInfo.getPrimaryKey().get(0).getName();// we only support single
+ // primary key
+ DataType primaryKeyType = tableInfo.getPrimaryKey().get(0).getType();
+ Object cqlFormattedPrimaryKeyValue =
+ MusicUtil.convertToActualDataType(primaryKeyType, primaryKeyValue);
+
+ // get the row of data from a quorum
+ selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + table + " WHERE "
+ + primaryKeyName + "= ?" + ";");
+ selectQuery.addValue(cqlFormattedPrimaryKeyValue);
+ ResultSet results = null;
+ try {
+ results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(selectQuery);
+ // write it back to a quorum
+ Row row = results.one();
+ ColumnDefinitions colInfo = row.getColumnDefinitions();
+ int totalColumns = colInfo.size();
+ int counter = 1;
+ StringBuilder fieldValueString = new StringBuilder("");
+ for (Definition definition : colInfo) {
+ String colName = definition.getName();
+ if (colName.equals(primaryKeyName))
+ continue;
+ DataType colType = definition.getType();
+ Object valueObj = MusicDataStoreHandle.getDSHandle().getColValue(row, colName, colType);
+ Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
+ fieldValueString.append(colName + " = ?");
+ updateQuery.addValue(valueString);
+ if (counter != (totalColumns - 1))
+ fieldValueString.append(",");
+ counter = counter + 1;
+ }
+ updateQuery.appendQueryString("UPDATE " + keyspace + "." + table + " SET "
+ + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";");
+ updateQuery.addValue(cqlFormattedPrimaryKeyValue);
+
+ MusicDataStoreHandle.getDSHandle().executePut(updateQuery, "critical");
+ } catch (MusicServiceException | MusicQueryException e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.QUERYERROR +""+updateQuery ,ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR);
+ }
+ }
+
+
+
+
+ /**
+ *
+ * @param query
+ * @return ResultSet
+ */
+ public ResultSet quorumGet(PreparedQueryObject query) {
+ ResultSet results = null;
+ try {
+ results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(query);
+ } catch (MusicServiceException | MusicQueryException e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.MAJOR, ErrorTypes.GENERALSERVICEERROR);
+
+ }
+ return results;
+
+ }
+
+
+
+ /**
+ *
+ * @param fullyQualifiedKey lockName
+ * @return
+ */
+ public String whoseTurnIsIt(String fullyQualifiedKey) {
+ String[] splitString = fullyQualifiedKey.split("\\.");
+ String keyspace = splitString[0];
+ String table = splitString[1];
+ String primaryKeyValue = splitString[2];
+ try {
+ return "$" + fullyQualifiedKey + "$"
+ + getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue).lockRef;
+ } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+fullyQualifiedKey ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
+ }
+ return null;
+ }
+
+ /**
+ *
+ * @param lockReference
+ * @return
+ */
+ public static String getLockNameFromId(String lockReference) {
+ StringTokenizer st = new StringTokenizer(lockReference);
+ return st.nextToken("$");
+ }
+
+ @Override
+ public void destroyLockRef(String lockId) {
+ long start = System.currentTimeMillis();
+ String fullyQualifiedKey = lockId.substring(1, lockId.lastIndexOf("$"));
+ String lockRef = lockId.substring(lockId.lastIndexOf('$')+1);
+ String[] splitString = fullyQualifiedKey.split("\\.");
+ String keyspace = splitString[0];
+ String table = splitString[1];
+ String primaryKeyValue = splitString[2];
+ try {
+ getLockingServiceHandle().deQueueLockRef(keyspace, table, primaryKeyValue, lockRef);
+ } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DESTROYLOCK+lockRef ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
+ }
+ long end = System.currentTimeMillis();
+ logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to destroy lock reference:" + (end - start) + " ms");
+ }
+
+ public MusicLockState destroyLockRef(String fullyQualifiedKey, String lockReference) {
+ long start = System.currentTimeMillis();
+ String[] splitString = fullyQualifiedKey.split("\\.");
+ String keyspace = splitString[0];
+ String table = splitString[1];
+ String primaryKeyValue = splitString[2];
+ try {
+ getLockingServiceHandle().deQueueLockRef(keyspace, table, primaryKeyValue, lockReference);
+ } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DESTROYLOCK+lockReference ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
+ }
+ long end = System.currentTimeMillis();
+ logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to destroy lock reference:" + (end - start) + " ms");
+ return new MusicLockState(LockStatus.UNLOCKED, "");
+ }
+
+ @Override
+ public MusicLockState releaseLock(String lockId, boolean voluntaryRelease) {
+ String fullyQualifiedKey = lockId.substring(1, lockId.lastIndexOf("$"));
+ String lockRef = lockId.substring(lockId.lastIndexOf('$')+1);
+ if (voluntaryRelease) {
+ return voluntaryReleaseLock(fullyQualifiedKey, lockRef);
+ } else {
+ return forciblyReleaseLock(fullyQualifiedKey, lockRef);
+ }
+ }
+
+ public MusicLockState voluntaryReleaseLock(String fullyQualifiedKey, String lockReference) {
+ return destroyLockRef(fullyQualifiedKey, lockReference);
+ }
+
+ public MusicLockState forciblyReleaseLock(String fullyQualifiedKey, String lockReference) {
+ String[] splitString = fullyQualifiedKey.split("\\.");
+ String keyspace = splitString[0];
+ String table = splitString[1];
+
+ //leave a signal that this key could potentially be unsynchronized
+ String syncTable = keyspace+".unsyncedKeys_"+table;
+ PreparedQueryObject queryObject = new PreparedQueryObject();
+ String values = "(?)";
+ queryObject.addValue(fullyQualifiedKey);
+ String insQuery = "insert into "+syncTable+" (key) values "+values+";";
+ queryObject.appendQueryString(insQuery);
+ try {
+ MusicDataStoreHandle.getDSHandle().executePut(queryObject, "critical");
+ } catch (Exception e) {
+ logger.error("Cannot forcibly release lock: " + fullyQualifiedKey + " " + lockReference + ". "
+ + e.getMessage());
+ }
+
+ //now release the lock
+ return destroyLockRef(fullyQualifiedKey, lockReference);
+ }
+
+ /**
+ *
+ * @param lockName
+ * @throws MusicLockingException
+ */
+ public void deleteLock(String lockName) throws MusicLockingException {
+ //deprecated
+ }
+
+ // Prepared Query Additions.
+
+ /**
+ *
+ * @param queryObject
+ * @return ReturnType
+ * @throws MusicServiceException
+ */
+ public ReturnType eventualPut(PreparedQueryObject queryObject) {
+ boolean result = false;
+ try {
+ result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, MusicUtil.EVENTUAL);
+ } catch (MusicServiceException | MusicQueryException ex) {
+ logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() + " " + ex.getCause() + " " + ex);
+ return new ReturnType(ResultType.FAILURE, ex.getMessage());
+ }
+ if (result) {
+ return new ReturnType(ResultType.SUCCESS, "Eventual Operation Successfully performed");
+ } else {
+ return new ReturnType(ResultType.FAILURE, "Eventual Operation failed to perform");
+ }
+ }
+
+ /**
+ *
+ * @param queryObject
+ * @return ReturnType
+ * @throws MusicServiceException
+ */
+ public ReturnType eventualPut_nb(PreparedQueryObject queryObject,String keyspace,String tablename,String primaryKey) {
+ boolean result = false;
+ long guard = 0;
+ PreparedQueryObject getGaurd = new PreparedQueryObject();
+ getGaurd.appendQueryString("SELECT guard FROM "+keyspace+".lockq_"+tablename+ " WHERE key = ? ;");
+ getGaurd.addValue(primaryKey);
+ try {
+ ResultSet getGaurdResult = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(getGaurd);
+ Row row = getGaurdResult.one();
+ if (row != null) {
+ guard = row.getLong("guard");
+ long timeOfWrite = System.currentTimeMillis();
+ long ts = MusicUtil.v2sTimeStampInMicroseconds(guard, timeOfWrite);
+ String query = queryObject.getQuery();
+ if (!queryObject.getQuery().contains("USING TIMESTAMP")) {
+ if (queryObject.getOperation().equalsIgnoreCase("delete"))
+ query = query.replaceFirst("WHERE", " USING TIMESTAMP " + ts + " WHERE ");
+ else
+ query = query.replaceFirst("SET", "USING TIMESTAMP " + ts + " SET");
+ }
+ queryObject.replaceQueryString(query);
+ }
+
+ } catch (MusicServiceException | MusicQueryException e) {
+ logger.error(EELFLoggerDelegate.applicationLogger,e.getMessage());
+ }
+ 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 keyspace
+ * @param table
+ * @param primaryKeyValue
+ * @param queryObject
+ * @param lockId
+ * @return
+ */
+ public ReturnType criticalPut(String keyspace, String table, String primaryKeyValue,
+ PreparedQueryObject queryObject, String lockId, Condition conditionInfo) {
+ long start = System.currentTimeMillis();
+ try {
+ ReturnType result = isTopOfLockStore(keyspace, table, primaryKeyValue,
+ lockId.substring(lockId.lastIndexOf("$")+1));
+ if(result.getResult().equals(ResultType.FAILURE))
+ return result;//not top of the lock store q
+
+ if (conditionInfo != null)
+ try {
+ if (conditionInfo.testCondition() == false)
+ return new ReturnType(ResultType.FAILURE,
+ "Lock acquired but the condition is not true");
+ } catch (Exception e) {
+ return new ReturnType(ResultType.FAILURE,
+ "Exception thrown while checking the condition, check its sanctity:\n"
+ + e.getMessage());
+ }
+
+ String query = queryObject.getQuery();
+ long timeOfWrite = System.currentTimeMillis();
+ long lockOrdinal = Long.parseLong(lockId.substring(lockId.lastIndexOf("$")+1));
+ long ts = MusicUtil.v2sTimeStampInMicroseconds(lockOrdinal, timeOfWrite);
+ // TODO: use Statement instead of modifying query
+ if (!queryObject.getQuery().contains("USING TIMESTAMP")) {
+ if (queryObject.getOperation().equalsIgnoreCase("delete"))
+ query = query.replaceFirst("WHERE", " USING TIMESTAMP " + ts + " WHERE ");
+ else if (queryObject.getOperation().equalsIgnoreCase("insert"))
+ query = query.replaceFirst(";", " USING TIMESTAMP " + ts + " ; ");
+ else
+ query = query.replaceFirst("SET", "USING TIMESTAMP " + ts + " SET");
+ }
+ queryObject.replaceQueryString(query);
+ MusicDataStore dsHandle = MusicDataStoreHandle.getDSHandle();
+ dsHandle.executePut(queryObject, MusicUtil.CRITICAL);
+ long end = System.currentTimeMillis();
+ logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms");
+ }catch (MusicQueryException | MusicServiceException | MusicLockingException e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
+ return new ReturnType(ResultType.FAILURE,
+ "Exception thrown while doing the critical put\n"
+ + e.getMessage());
+ }
+ return new ReturnType(ResultType.SUCCESS, "Update performed");
+ }
+
+
+ /**
+ *
+ * @param queryObject
+ * @param consistency
+ * @return Boolean Indicates success or failure
+ * @throws MusicServiceException
+ *
+ *
+ */
+ public ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException {
+ // this is mainly for some functions like keyspace creation etc which does not
+ // really need the bells and whistles of Music locking.
+ boolean result = false;
+ try {
+ result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, consistency);
+ } catch (MusicQueryException | MusicServiceException ex) {
+ logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(), AppMessages.UNKNOWNERROR,
+ ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ throw new MusicServiceException(ex.getMessage());
+ }
+ return result ? ResultType.SUCCESS : ResultType.FAILURE;
+ }
+
+ /**
+ * This method performs DDL operation on cassandra.
+ *
+ * @param queryObject query object containing prepared query and values
+ * @return ResultSet
+ * @throws MusicServiceException
+ */
+ public ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException {
+ ResultSet results = null;
+ try {
+ results = MusicDataStoreHandle.getDSHandle().executeOneConsistencyGet(queryObject);
+ } catch (MusicQueryException | MusicServiceException e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
+ throw new MusicServiceException(e.getMessage());
+ }
+ return results;
+ }
+
+ /**
+ * This method performs DDL operations on cassandra, if the the resource is available. Lock ID
+ * is used to check if the resource is free.
+ *
+ * @param keyspace name of the keyspace
+ * @param table name of the table
+ * @param primaryKeyValue primary key value
+ * @param queryObject query object containing prepared query and values
+ * @param lockId lock ID to check if the resource is free to perform the operation.
+ * @return ResultSet
+ */
+ public ResultSet criticalGet(String keyspace, String table, String primaryKeyValue,
+ PreparedQueryObject queryObject, String lockId) throws MusicServiceException {
+ ResultSet results = null;
+
+ try {
+ ReturnType result = isTopOfLockStore(keyspace, table, primaryKeyValue,
+ lockId.substring(lockId.lastIndexOf("$")+1));
+ if(result.getResult().equals(ResultType.FAILURE))
+ return null;//not top of the lock store q
+ results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryObject);
+ } catch (MusicQueryException | MusicServiceException | MusicLockingException e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
+ }
+ return results;
+ }
+
+ /**
+ * This method performs DML operation on cassandra, when the lock of the dd is acquired.
+ *
+ * @param keyspaceName name of the keyspace
+ * @param tableName name of the table
+ * @param primaryKey primary key value
+ * @param queryObject query object containing prepared query and values
+ * @return ReturnType
+ * @throws MusicLockingException
+ * @throws MusicServiceException
+ * @throws MusicQueryException
+ */
+ public ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException, MusicQueryException, MusicServiceException {
+ long start = System.currentTimeMillis();
+ String fullyQualifiedKey = keyspaceName + "." + tableName + "." + primaryKey;
+ String lockId = createLockReference(fullyQualifiedKey);
+ long lockCreationTime = System.currentTimeMillis();
+ ReturnType lockAcqResult = acquireLock(fullyQualifiedKey, lockId);
+ long lockAcqTime = System.currentTimeMillis();
+
+ if (!lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
+ logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId);
+ voluntaryReleaseLock(fullyQualifiedKey,lockId);
+ return lockAcqResult;
+ }
+
+ logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId);
+ String lockRef = lockId.substring(lockId.lastIndexOf("$"));
+ ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey,
+ queryObject, lockRef, conditionInfo);
+ long criticalPutTime = System.currentTimeMillis();
+ voluntaryReleaseLock(fullyQualifiedKey,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;
+ }
+
+
+
+
+ /**
+ * This method performs DDL operation on cassasndra, when the lock for the resource is acquired.
+ *
+ * @param keyspaceName name of the keyspace
+ * @param tableName name of the table
+ * @param primaryKey primary key value
+ * @param queryObject query object containing prepared query and values
+ * @return ResultSet
+ * @throws MusicServiceException
+ * @throws MusicLockingException
+ * @throws MusicQueryException
+ */
+ public ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException, MusicQueryException {
+ String fullyQualifiedKey = keyspaceName + "." + tableName + "." + primaryKey;
+ String lockId = createLockReference(fullyQualifiedKey);
+ long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
+ ReturnType lockAcqResult = acquireLock(fullyQualifiedKey, lockId);
+ if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
+ logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId);
+ String lockRef = lockId.substring(lockId.lastIndexOf("$"));
+ ResultSet result =
+ criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockRef);
+ voluntaryReleaseLock(fullyQualifiedKey,lockId);
+ return result;
+ } else {
+ voluntaryReleaseLock(fullyQualifiedKey,lockId);
+ logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId);
+ return null;
+ }
+ }
+
+
+
+ /**
+ * @param lockName
+ * @return
+ */
+ public Map<String, Object> validateLock(String lockName) {
+ Map<String, Object> resultMap = new HashMap<>();
+ String[] locks = lockName.split("\\.");
+ if(locks.length < 3) {
+ resultMap.put("Error", "Invalid lock. Please make sure lock is of the type keyspaceName.tableName.primaryKey");
+ return resultMap;
+ }
+ String keyspace= locks[0];
+ if(keyspace.startsWith("$"))
+ keyspace = keyspace.substring(1);
+ resultMap.put("keyspace",keyspace);
+ return resultMap;
+ }
+
+
+ public static void main(String[] args) {
+ String x = "axe top";
+ x = x.replaceFirst("top", "sword");
+ System.out.print(x); //returns sword pickaxe
+ }
+
+
+
+ @Override
+ public ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException {
+ //Deprecated
+ return null;
+ }
+ @Override
+ public List<String> getLockQueue(String fullyQualifiedKey)
+ throws MusicServiceException, MusicQueryException, MusicLockingException {
+ String[] splitString = fullyQualifiedKey.split("\\.");
+ String keyspace = splitString[0];
+ String table = splitString[1];
+ String primaryKeyValue = splitString[2];
+
+ return getLockingServiceHandle().getLockQueue(keyspace, table, primaryKeyValue);
+ }
+ @Override
+ public long getLockQueueSize(String fullyQualifiedKey)
+ throws MusicServiceException, MusicQueryException, MusicLockingException {
+ String[] splitString = fullyQualifiedKey.split("\\.");
+ String keyspace = splitString[0];
+ String table = splitString[1];
+ String primaryKeyValue = splitString[2];
+
+ return getLockingServiceHandle().getLockQueueSize(keyspace, table, primaryKeyValue);
+ }
+ @Override
+ public ResultSet atomicGetWithDeleteLock(String keyspaceName, String tableName, String primaryKey,
+ PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException {
+ //deprecated
+ return null;
+ }
+
+}
diff --git a/src/main/java/org/onap/music/service/impl/MusicZKCore.java b/src/main/java/org/onap/music/service/impl/MusicZKCore.java
new file mode 100644
index 00000000..1662b20c
--- /dev/null
+++ b/src/main/java/org/onap/music/service/impl/MusicZKCore.java
@@ -0,0 +1,937 @@
+/*
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ * Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=============================================
+ * ====================================================================
+ */
+
+package org.onap.music.service.impl;
+
+
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.commons.jcs.access.CacheAccess;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.KeeperException.NoNodeException;
+import org.onap.music.datastore.MusicDataStore;
+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.ColumnDefinitions;
+import com.datastax.driver.core.ColumnDefinitions.Definition;
+import com.datastax.driver.core.DataType;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+import com.datastax.driver.core.TableMetadata;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+
+/**
+ * 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);
+ ResultSet results = null;
+ try {
+ results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(selectQuery);
+ // write it back to a quorum
+ Row row = results.one();
+ ColumnDefinitions colInfo = row.getColumnDefinitions();
+ int totalColumns = colInfo.size();
+ int counter = 1;
+ StringBuilder fieldValueString = new StringBuilder("");
+ for (Definition definition : colInfo) {
+ String colName = definition.getName();
+ if (colName.equals(primaryKeyName))
+ continue;
+ DataType colType = definition.getType();
+ Object valueObj = MusicDataStoreHandle.getDSHandle().getColValue(row, colName, colType);
+ Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
+ fieldValueString.append(colName + " = ?");
+ updateQuery.addValue(valueString);
+ if (counter != (totalColumns - 1))
+ fieldValueString.append(",");
+ counter = counter + 1;
+ }
+ updateQuery.appendQueryString("UPDATE " + keyspaceName + "." + tableName + " SET "
+ + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";");
+ updateQuery.addValue(cqlFormattedPrimaryKeyValue);
+
+ MusicDataStoreHandle.getDSHandle().executePut(updateQuery, "critical");
+ } catch (MusicServiceException | MusicQueryException e) {
+ logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.QUERYERROR +""+updateQuery ,ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR);
+ }
+ }
+
+
+
+
+ /**
+ *
+ * @param query
+ * @return ResultSet
+ */
+ public ResultSet quorumGet(PreparedQueryObject query) {
+ ResultSet results = null;
+ try {
+ results = MusicDataStoreHandle.getDSHandle().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) {
+ Map<String, Object> resultMap = new HashMap<>();
+ String[] locks = lockName.split("\\.");
+ if(locks.length < 3) {
+ resultMap.put("Error", "Invalid lock. Please make sure lock is of the type keyspaceName.tableName.primaryKey");
+ return resultMap;
+ }
+ String keyspace= locks[0];
+ if(keyspace.startsWith("$"))
+ keyspace = keyspace.substring(1);
+ resultMap.put("keyspace",keyspace);
+ return resultMap;
+ }
+
+
+
+ @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);
+ }
+
+
+
+
+}