summaryrefslogtreecommitdiffstats
path: root/catalog-dao/src/main/java
diff options
context:
space:
mode:
authorPiotr Darosz <piotr.darosz@nokia.com>2018-06-15 08:28:00 +0200
committerTal Gitelman <tg851x@intl.att.com>2018-08-29 13:27:45 +0000
commit20da3d02c6c5507761c257b6ec6f58dcfc27d05a (patch)
tree6fef3ccd72cb8c092adac410b77a5da25284055a /catalog-dao/src/main/java
parent8eff5a1ff79ca212a42d0e69326af7dc00b7c503 (diff)
Add tests for SdcSchema classes
Introduce Cassandra Unit tool and tests for schema-related classes Change-Id: I14da602c42056730e7992dd92b4da8b87dd9fb0d Issue-ID: SDC-1358 Signed-off-by: Piotr Darosz <piotr.darosz@nokia.com>
Diffstat (limited to 'catalog-dao/src/main/java')
-rw-r--r--catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java107
-rw-r--r--catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java68
2 files changed, 86 insertions, 89 deletions
diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java
index eaa0202be8..6c74543901 100644
--- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java
+++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java
@@ -16,6 +16,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
+ * Modifications copyright (c) 2018 Nokia
+ * ================================================================================
*/
package org.openecomp.sdc.be.dao.cassandra.schema;
@@ -27,18 +29,31 @@ import com.datastax.driver.core.schemabuilder.SchemaBuilder;
import com.datastax.driver.core.schemabuilder.SchemaStatement;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.openecomp.sdc.be.config.Configuration;
-import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.cassandra.schema.tables.OldExternalApiEventTableDesc;
+import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
import org.openecomp.sdc.common.log.wrappers.Logger;
import java.util.*;
import java.util.stream.Collectors;
+import java.util.function.Supplier;
public class SdcSchemaBuilder {
- private static final String CREATE_KEYSPACE_SIMPLE_STRATEGY = "CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'SimpleStrategy', %s};";
+ private SdcSchemaUtils sdcSchemaUtils;
+ private Supplier<Configuration.CassandrConfig> cassandraConfigSupplier;
+ public SdcSchemaBuilder(SdcSchemaUtils sdcSchemaUtils, Supplier<Configuration.CassandrConfig> cassandraConfigSupplier) {
+ this.sdcSchemaUtils = sdcSchemaUtils;
+ this.cassandraConfigSupplier = cassandraConfigSupplier;
+ }
+ /**
+ * creat key space statment for SimpleStrategy
+ */
+ private static final String CREATE_KEYSPACE_SIMPLE_STRATEGY = "CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'SimpleStrategy', %s};";
+ /**
+ * creat key space statment for NetworkTopologyStrategy
+ */
private static final String CREATE_KEYSPACE_NETWORK_TOPOLOGY_STRATEGY = "CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'NetworkTopologyStrategy', %s};";
private static Logger log = Logger.getLogger(SdcSchemaBuilder.class.getName());
@@ -58,22 +73,17 @@ public class SdcSchemaBuilder {
* internal enums and external configuration for its operation *
* @return true if the create operation was successful
*/
- public static boolean createSchema() {
- Cluster cluster = null;
- Session session = null;
- try {
+ public boolean createSchema() {
+ boolean res = false;
+ try(Cluster cluster = sdcSchemaUtils.createCluster();
+ Session session = cluster.connect()) {
log.info("creating Schema for Cassandra.");
- cluster = SdcSchemaUtils.createCluster();
- if (cluster == null) {
- return false;
- }
- session = cluster.connect();
List<KeyspaceMetadata> keyspacesMetadateFromCassandra = cluster.getMetadata().getKeyspaces();
if (keyspacesMetadateFromCassandra == null) {
- log.debug("filed to retrive a list of keyspaces from cassndra");
+ log.debug("filed to retrieve a list of keyspaces from cassandra");
return false;
}
- log.debug("retrived Cassndra metadata.");
+ log.debug("retrieved Cassandra metadata.");
Map<String, Map<String, List<String>>> cassndraMetadata = parseKeyspaceMetadata(keyspacesMetadateFromCassandra);
Map<String, Map<String, List<String>>> metadataTablesStructure = getMetadataTablesStructure(keyspacesMetadateFromCassandra);
Map<String, List<ITableDescription>> schemeData = getSchemeData();
@@ -87,54 +97,43 @@ public class SdcSchemaBuilder {
Map<String, List<String>> keyspaceMetadate = cassndraMetadata.get(keyspace.getKey());
createTables(keyspace.getValue(), keyspaceMetadate, session,metadataTablesStructure.get(keyspace.getKey()));
}
- return true;
+ res = true;
} catch (Exception e) {
- log.error(EcompLoggerErrorCode.SCHEMA_ERROR, "creating Schema for Cassandra", "Cassandra", e.getLocalizedMessage());
- } finally {
- if (session != null) {
- session.close();
- }
- if (cluster != null) {
- cluster.close();
- }
-
- }
-
- return false;
+ log.error(EcompLoggerErrorCode.SCHEMA_ERROR, "creating Schema for Cassandra", "Cassandra", e.getLocalizedMessage());
+ res = false;
+ }
+ return res;
}
- public static boolean deleteSchema() {
- Cluster cluster = null;
- Session session = null;
- try {
+ public boolean deleteSchema() {
+ boolean res = false;
+ try(Cluster cluster = sdcSchemaUtils.createCluster();
+ Session session = cluster.connect()) {
log.info("delete Data from Cassandra.");
- cluster = SdcSchemaUtils.createCluster();
- if (cluster == null) {
- return false;
- }
- session = cluster.connect();
List<KeyspaceMetadata> keyspacesMetadateFromCassandra = cluster.getMetadata().getKeyspaces();
if (keyspacesMetadateFromCassandra == null) {
- log.debug("filed to retrive a list of keyspaces from cassndra");
+ log.debug("filed to retrieve a list of keyspaces from cassandra");
return false;
}
- log.debug("retrived Cassndra metadata.");
+ log.debug("retrieved Cassandra metadata.");
Map<String, Map<String, List<String>>> cassndraMetadata = parseKeyspaceMetadata(keyspacesMetadateFromCassandra);
- log.info("Cassandra Metadata: {}" ,cassndraMetadata);
- return true;
- } catch (Exception e) {
- log.error(EcompLoggerErrorCode.SCHEMA_ERROR, "deleting Schema for Cassandra", "Cassandra", e.getLocalizedMessage());
- } finally {
- if (session != null) {
- session.close();
- }
- if (cluster != null) {
- cluster.close();
- }
+ log.info("Cassandra Metadata: {}" ,cassndraMetadata);
+ cassndraMetadata.forEach((k, v) -> {
+ if (AuditingTypesConstants.TITAN_KEYSPACE.equals(k)) {
+ // session.execute("")
+ } else if (AuditingTypesConstants.ARTIFACT_KEYSPACE.equals(k)) {
- }
+ } else if (AuditingTypesConstants.AUDIT_KEYSPACE.equals(k)) {
- return false;
+ }
+ });
+
+ System.out.println(cassndraMetadata);
+ res = true;
+ } catch (Exception e) {
+ log.error(EcompLoggerErrorCode.SCHEMA_ERROR, "deleting Schema for Cassandra", "Cassandra", e.getLocalizedMessage());
+ }
+ return res;
}
/**
@@ -191,7 +190,7 @@ public class SdcSchemaBuilder {
* the current tables columns that exist in the cassandra under this
* keyspace
*/
- private static void createTables(List<ITableDescription> iTableDescriptions, Map<String, List<String>> keyspaceMetadate, Session session,
+ private static void createTables(List<ITableDescription> iTableDescriptions, Map<String, List<String>> keyspaceMetadate, Session session,
Map<String, List<String>> existingTablesMetadata) {
for (ITableDescription tableDescription : iTableDescriptions) {
String tableName = tableDescription.getTableName().toLowerCase();
@@ -261,7 +260,7 @@ public class SdcSchemaBuilder {
Alter alter = SchemaBuilder.alterTable(tableDescription.getKeyspace(),tableDescription.getTableName());
SchemaStatement addColumn = alter.addColumn(columnName).type(column.getValue().getLeft());
log.trace("exacuting :{}", addColumn);
- session.execute(addColumn);
+ session.execute(addColumn);
}
}
}
@@ -275,8 +274,8 @@ public class SdcSchemaBuilder {
* @param session: the session object used for the execution of the query.
* @return true in case the operation was successful
*/
- private static boolean createKeyspace(String keyspace, Map<String, Map<String, List<String>>> cassndraMetadata, Session session) {
- List<Configuration.CassandrConfig.KeyspaceConfig> keyspaceConfigList = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getKeySpaces();
+ private boolean createKeyspace(String keyspace, Map<String, Map<String, List<String>>> cassndraMetadata, Session session) {
+ List<Configuration.CassandrConfig.KeyspaceConfig> keyspaceConfigList = cassandraConfigSupplier.get().getKeySpaces();
log.info("creating keyspace:{}.", keyspace);
if (!cassndraMetadata.keySet().contains(keyspace)) {
return createKeyspaceIfNotExists(keyspace, session, keyspaceConfigList);
diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java
index 434a6e6eab..7531ad4c0a 100644
--- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java
+++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java
@@ -16,17 +16,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
+ * Modifications copyright (c) 2018 Nokia
+ * ================================================================================
*/
-
package org.openecomp.sdc.be.dao.cassandra.schema;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SocketOptions;
+import org.openecomp.sdc.be.config.Configuration;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.common.log.wrappers.Logger;
import java.util.List;
+import java.util.function.Supplier;
public class SdcSchemaUtils {
@@ -38,8 +41,9 @@ public class SdcSchemaUtils {
*
* @return cluster object our null in case of an invalid configuration
*/
- public static Cluster createCluster() {
- List<String> nodes = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getCassandraHosts();
+ public Cluster createCluster() {
+ final Configuration.CassandrConfig config = getCassandraConfig();
+ List<String> nodes = config.getCassandraHosts();
if (nodes == null) {
log.info("no nodes were supplied in configuration.");
return null;
@@ -50,20 +54,18 @@ public class SdcSchemaUtils {
clusterBuilder.withMaxSchemaAgreementWaitSeconds(60);
- boolean authenticate = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isAuthenticate();
- if (authenticate) {
- String username = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getUsername();
- String password = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getPassword();
+ if (config.isAuthenticate()) {
+ String username = config.getUsername();
+ String password = config.getPassword();
if (username == null || password == null) {
log.info("authentication is enabled but username or password were not supplied.");
return null;
}
clusterBuilder.withCredentials(username, password);
}
- boolean ssl = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isSsl();
- if (ssl) {
- String truststorePath = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePath();
- String truststorePassword = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePassword();
+ if (config.isSsl()) {
+ String truststorePath = config.getTruststorePath();
+ String truststorePassword = config.getTruststorePassword();
if (truststorePath == null || truststorePassword == null) {
log.info("ssl is enabled but truststorePath or truststorePassword were not supplied.");
return null;
@@ -73,12 +75,12 @@ public class SdcSchemaUtils {
clusterBuilder.withSSL();
}
SocketOptions socketOptions =new SocketOptions();
- Integer socketConnectTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketConnectTimeout();
+ Integer socketConnectTimeout = config.getSocketConnectTimeout();
if( socketConnectTimeout!=null ){
log.info("SocketConnectTimeout was provided, setting Cassandra client to use SocketConnectTimeout: {} .",socketConnectTimeout);
socketOptions.setConnectTimeoutMillis(socketConnectTimeout);
}
- Integer socketReadTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketReadTimeout();
+ Integer socketReadTimeout = config.getSocketReadTimeout();
if( socketReadTimeout != null ){
log.info("SocketReadTimeout was provided, setting Cassandra client to use SocketReadTimeout: {} .",socketReadTimeout);
socketOptions.setReadTimeoutMillis(socketReadTimeout);
@@ -87,37 +89,33 @@ public class SdcSchemaUtils {
return clusterBuilder.build();
}
- public static boolean executeStatement(String statement) {
- return executeStatements(statement);
+ public boolean executeStatement(String statement) {
+ return executeStatement(this::createCluster, statement);
}
- public static boolean executeStatements(String ... statements) {
- Cluster cluster = null;
- Session session = null;
- try {
- cluster = createCluster();
- if (cluster == null) {
- return false;
- }
- session = cluster.connect();
+ public boolean executeStatements(String ... statements) {
+ return executeStatements(this::createCluster, statements);
+ }
+
+ boolean executeStatement(Supplier<Cluster> clusterSupplier, String statement) {
+ return executeStatements(clusterSupplier, statement);
+ }
+
+ boolean executeStatements(Supplier<Cluster> clusterSupplier, String ... statements) {
+ try(Cluster cluster = clusterSupplier.get();
+ Session session = cluster.connect()) {
for (String statement : statements) {
session.execute(statement);
}
return true;
} catch (RuntimeException e) {
- log.error(String.format("could not execute statements"), e);
- return false;
- } finally {
- if (session != null) {
- session.close();
- }
- if (cluster != null) {
- cluster.close();
- }
-
+ log.error("could not execute statements", e);
}
+ return false;
}
-
+ Configuration.CassandrConfig getCassandraConfig() {
+ return ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig();
+ }
}