diff options
Diffstat (limited to 'catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema')
5 files changed, 242 insertions, 151 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 53d71fe3cf..8c09e55c9d 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 @@ -7,9 +7,9 @@ * 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. @@ -40,17 +40,6 @@ import java.util.function.Supplier; public class SdcSchemaBuilder { - /** - * 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()); - private SdcSchemaUtils sdcSchemaUtils; private Supplier<Configuration.CassandrConfig> cassandraConfigSupplier; @@ -58,6 +47,16 @@ public class SdcSchemaBuilder { 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()); //TODO remove after 1707_OS migration private static void handle1707OSMigration(Map<String, Map<String, List<String>>> cassndraMetadata, Map<String, List<ITableDescription>> schemeData){ @@ -66,12 +65,12 @@ public class SdcSchemaBuilder { list.add(new OldExternalApiEventTableDesc()); schemeData.put("attaudit", list); } - + } /** * the method creates all keyspaces, tables and indexes in case they do not * already exist. the method can be run multiple times. the method uses the - * internal enums and external configuration for its operation * + * internal enums and external configuration for its operation * * @return true if the create operation was successful */ public boolean createSchema() { @@ -142,7 +141,7 @@ public class SdcSchemaBuilder { * creation of a map conting the names of keyspaces tabls and indexes * already defined in the cassandra keyspacename -> tablename -> list of * indexes info - * + * * @param keyspacesMetadata * cassndra mmetadata * @return a map of maps of lists holding parsed info @@ -156,7 +155,7 @@ public class SdcSchemaBuilder { .map(IndexMetadata::getName) .collect(Collectors.toList()))))); } - + private static Map<String, Map<String, List<String>>> getMetadataTablesStructure( List<KeyspaceMetadata> keyspacesMetadata) { return keyspacesMetadata.stream() @@ -172,7 +171,7 @@ public class SdcSchemaBuilder { * the method builds an index name according to a defined logic * <table> * _<column>_idx - * + * * @param table: table name * @param column: column name * @return string name of the index @@ -187,7 +186,7 @@ public class SdcSchemaBuilder { * @param iTableDescriptions: a list of table description we want to create * @param keyspaceMetadate: the current tables that exist in the cassandra under this keyspace * @param session: the session object used for the execution of the query. - * @param existingTablesMetadata + * @param existingTablesMetadata * the current tables columns that exist in the cassandra under this * keyspace */ @@ -250,8 +249,8 @@ public class SdcSchemaBuilder { * @param columnDescription */ private static void alterTable(Session session, Map<String, List<String>> existingTablesMetadata, - ITableDescription tableDescription, String tableName, - Map<String, ImmutablePair<DataType, Boolean>> columnDescription) { + ITableDescription tableDescription, String tableName, + Map<String, ImmutablePair<DataType, Boolean>> columnDescription) { List<String> definedTableColumns = existingTablesMetadata.get(tableName); //add column to casandra if was added to table definition for (Map.Entry<String, ImmutablePair<DataType, Boolean>> column : columnDescription.entrySet()) { @@ -269,7 +268,7 @@ public class SdcSchemaBuilder { /** * the method create the keyspace in case it does not already exists the * method uses configurtion to select the needed replication strategy - * + * * @param keyspace: name of the keyspace we want to create * @param cassndraMetadata: cassndra metadata * @param session: the session object used for the execution of the query. @@ -307,7 +306,7 @@ public class SdcSchemaBuilder { /** * the method retries the schem info from the enums describing the tables - * + * * @return a map of keyspaces to there table info */ private static Map<String, List<ITableDescription>> getSchemeData() { @@ -326,14 +325,14 @@ public class SdcSchemaBuilder { } /** - * the methoed creates the query string for the given keyspace the methoed + * the methoed creates the query string for the given keyspace the methoed * valides the given data according the the requirments of the replication * strategy SimpleStrategy: "CREATE KEYSPACE IF NOT EXISTS * <keyspaceName></keyspaceName> WITH replication = * {'class':'SimpleStrategy', 'replication_factor':2};" SimpleStrategy: * "CREATE KEYSPACE IF NOT EXISTS <keyspaceName></keyspaceName> WITH * replication = {'class':'NetworkTopologyStrategy', 'dc1' : 2 ,dc2 : 2 };" - * + * * @param keyspace * name of the keyspace we want to create * @param keyspaceInfo @@ -390,9 +389,9 @@ public class SdcSchemaBuilder { public enum ReplicationStrategy { NETWORK_TOPOLOGY_STRATEGY("NetworkTopologyStrategy"), SIMPLE_STRATEGY("SimpleStrategy"); - private String strategyName; + public String strategyName; - ReplicationStrategy(String strategyName) { + private ReplicationStrategy(String strategyName) { this.strategyName = strategyName; } @@ -400,4 +399,5 @@ public class SdcSchemaBuilder { return strategyName; } } + } 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 e6b091b72f..3d68980d73 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 @@ -22,8 +22,13 @@ package org.openecomp.sdc.be.dao.cassandra.schema; import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Metadata; +import com.datastax.driver.core.ProtocolVersion; import com.datastax.driver.core.Session; import com.datastax.driver.core.SocketOptions; +import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; +import com.datastax.driver.core.policies.LoadBalancingPolicy; +import com.datastax.driver.core.policies.TokenAwarePolicy; import org.openecomp.sdc.be.config.Configuration; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.common.log.wrappers.Logger; @@ -34,12 +39,31 @@ import java.util.function.Supplier; public class SdcSchemaUtils { private static Logger log = Logger.getLogger(SdcSchemaUtils.class.getName()); + private Cluster cluster; + private boolean isConnected; + + + + public SdcSchemaUtils() { + super(); + try { + isConnected = false; + cluster = createCluster(); + isConnected = true; + } catch (Exception e) { + log.info("** CassandraClient isn't connected. error is {}", e); + } + + log.info("** cluster created"); + } /** * the method creates the cluster object using the supplied cassandra nodes * in the configuration * * @return cluster object our null in case of an invalid configuration + * + * */ public Cluster createCluster() { final Configuration.CassandrConfig config = getCassandraConfig(); @@ -53,41 +77,104 @@ public class SdcSchemaUtils { Cluster.Builder clusterBuilder = Cluster.builder(); nodes.forEach(node -> clusterBuilder.addContactPoint(node).withPort(cassandraPort)); - clusterBuilder.withMaxSchemaAgreementWaitSeconds(60); - - 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.withMaxSchemaAgreementWaitSeconds(60); + + setSocketOptions(clusterBuilder, config); + if(!enableAuthentication(clusterBuilder, config)){ + return null; + } + + if(!enableSsl(clusterBuilder, config)){ + return null; + } + setLocalDc(clusterBuilder, config); + + return clusterBuilder.build(); + } + + /** + * + * @return + */ + public Session connect() { + Session session = null; + if (cluster != null) { + try { + session = cluster.connect(); + + } catch (Throwable e) { + log.debug("Failed to connect cluster, error :", e); + } - clusterBuilder.withCredentials(username, password); } - if (config.isSsl()) { + return session; + } + + public Metadata getMetadata(){ + if (cluster != null){ + return cluster.getMetadata(); + } + return null; + } + + private void setLocalDc(Cluster.Builder clusterBuilder, Configuration.CassandrConfig config) { + String localDataCenter = config.getLocalDataCenter(); + if (localDataCenter != null) { + log.info("localDatacenter was provided, setting Cassndra clint to use datacenter: {} as local.", + localDataCenter); + LoadBalancingPolicy tokenAwarePolicy = new TokenAwarePolicy( + DCAwareRoundRobinPolicy.builder().withLocalDc(localDataCenter).build()); + clusterBuilder.withLoadBalancingPolicy(tokenAwarePolicy); + } else { + log.info( + "localDatacenter was provided, the driver will use the datacenter of the first contact point that was reached at initialization"); + } + } + + private boolean enableSsl(Cluster.Builder clusterBuilder, Configuration.CassandrConfig config) { + boolean ssl = config.isSsl(); + if (ssl) { 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; + log.error("ssl is enabled but truststorePath or truststorePassword were not supplied."); + return false; + } else { + System.setProperty("javax.net.ssl.trustStore", truststorePath); + System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword); + clusterBuilder.withSSL(); } - System.setProperty("javax.net.ssl.trustStore", truststorePath); - System.setProperty("javax.net.ssl.trustStorePassword", truststorePassword); - clusterBuilder.withSSL(); + } + return true; + } + + + private void setSocketOptions(Cluster.Builder clusterBuilder, Configuration.CassandrConfig config) { SocketOptions socketOptions =new SocketOptions(); Integer socketConnectTimeout = config.getSocketConnectTimeout(); if( socketConnectTimeout!=null ){ log.info("SocketConnectTimeout was provided, setting Cassandra client to use SocketConnectTimeout: {} .",socketConnectTimeout); socketOptions.setConnectTimeoutMillis(socketConnectTimeout); } - Integer socketReadTimeout = config.getSocketReadTimeout(); - if( socketReadTimeout != null ){ - log.info("SocketReadTimeout was provided, setting Cassandra client to use SocketReadTimeout: {} .",socketReadTimeout); - socketOptions.setReadTimeoutMillis(socketReadTimeout); - } clusterBuilder.withSocketOptions(socketOptions); - return clusterBuilder.build(); + } + + private boolean enableAuthentication(Cluster.Builder clusterBuilder, Configuration.CassandrConfig config) { + boolean authenticate = config.isAuthenticate(); + + if (authenticate) { + String username = config.getUsername(); + String password = config.getPassword(); + if (username == null || password == null) { + log.error("authentication is enabled but username or password were not supplied."); + return false; + } else { + clusterBuilder.withCredentials(username, password); + } + + } + return true; } public boolean executeStatement(String statement) { @@ -118,5 +205,13 @@ public class SdcSchemaUtils { Configuration.CassandrConfig getCassandraConfig() { return ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig(); } + + + public void closeCluster() { + if (isConnected) { + cluster.close(); + } + log.info("** CassandraClient cluster closed"); + } } diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/Table.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/Table.java index b0209d2a13..a11bcb3881 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/Table.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/Table.java @@ -35,7 +35,6 @@ public enum Table { DISTRIBUTION_DEPLOY_EVENT(new DistribDeployEventTableDesc()), DISTRIBUTION_GET_UEB_CLUSTER_EVENT(new GetUebClusterEventTableDesc()), AUTH_EVENT(new AuthEventTableDescription()), - CONSUMER_EVENT(new ConsumerEventTableDefinition()), CATEGORY_EVENT(new CategoryEventTableDescription()), GET_USERS_LIST_EVENT(new GetUsersListEventTableDesc()), GET_CATEGORY_HIERARCHY_EVENT(new GetCatHierEventTableDesc()), @@ -43,7 +42,8 @@ public enum Table { SDC_SCHEMA_FILES(new SdcSchemaFilesTableDescription()), SDC_REPO(new MigrationTasksTableDescription()), SDC_OPERATIONAL_ENVIRONMENT(new OperationalEnvironmentsTableDescription()), - AUDIT_ECOMP_OPERATIONAL_ENVIRONMENT(new EcompOperationalEnvironmentEventTableDesc()); + AUDIT_ECOMP_OPERATIONAL_ENVIRONMENT(new EcompOperationalEnvironmentEventTableDesc()), + FEATURE_TOGGLE_STATE(new FeatureToggleEventTableDesc()); ITableDescription tableDescription; diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/ConsumerEventTableDefinition.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/ConsumerEventTableDefinition.java deleted file mode 100644 index f829724f4e..0000000000 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/ConsumerEventTableDefinition.java +++ /dev/null @@ -1,101 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.openecomp.sdc.be.dao.cassandra.schema.tables; - -import com.datastax.driver.core.DataType; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.openecomp.sdc.be.dao.cassandra.schema.ITableDescription; -import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class ConsumerEventTableDefinition implements ITableDescription { - - @Override - public List<ImmutablePair<String, DataType>> primaryKeys() { - List<ImmutablePair<String, DataType>> keys = new ArrayList<>(); - keys.add(new ImmutablePair<>(TIMEBASED_UUID_FIELD, DataType.timeuuid())); - return keys; - } - - @Override - public List<ImmutablePair<String, DataType>> clusteringKeys() { - List<ImmutablePair<String, DataType>> keys = new ArrayList<>(); - keys.add(new ImmutablePair<>(TIMESTAMP_FIELD, DataType.timestamp())); - return keys; - } - - @Override - public Map<String, ImmutablePair<DataType, Boolean>> getColumnDescription() { - Map<String, ImmutablePair<DataType, Boolean>> columns = new HashMap<>(); - - for (DEEFieldsDescription field : DEEFieldsDescription.values()) { - columns.put(field.getName(), new ImmutablePair<>(field.type, field.indexed)); - } - - return columns; - } - - @Override - public String getKeyspace() { - return AuditingTypesConstants.AUDIT_KEYSPACE; - } - - @Override - public String getTableName() { - return AuditingTypesConstants.CONSUMER_EVENT_TYPE; - } - - enum DEEFieldsDescription { - ACTION("action", DataType.varchar(), true), - STATUS("status", DataType.varchar(), false), - DESCRIPTION("description", DataType.varchar(), false), - ECOMP_USER("ecomp_user", DataType.varchar(), false), - MODIFIER("modifier", DataType.varchar(), false), - REQUEST_ID("request_id", DataType.varchar(), false); - - private String name; - private DataType type; - private boolean indexed; - - DEEFieldsDescription(String name, DataType type, boolean indexed) { - this.name = name; - this.type = type; - this.indexed = indexed; - } - - public String getName() { - return name; - } - - public DataType getType() { - return type; - } - - public boolean isIndexed() { - return indexed; - } - } - -} diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/FeatureToggleEventTableDesc.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/FeatureToggleEventTableDesc.java new file mode 100644 index 0000000000..4c18f98179 --- /dev/null +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/FeatureToggleEventTableDesc.java @@ -0,0 +1,97 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.openecomp.sdc.be.dao.cassandra.schema.tables; + +import com.datastax.driver.core.DataType; +import com.google.common.collect.Lists; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.openecomp.sdc.be.dao.cassandra.schema.ITableDescription; +import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class FeatureToggleEventTableDesc implements ITableDescription { + + private static final String FEATURE_NAME = "feature_name"; + + @Override + public List<ImmutablePair<String, DataType>> primaryKeys() { + List<ImmutablePair<String, DataType>> keys = new ArrayList<>(); + keys.add(new ImmutablePair<>(FEATURE_NAME, DataType.varchar())); + return keys; + } + + @Override + public List<ImmutablePair<String, DataType>> clusteringKeys() { + return Lists.newArrayList(); + } + + @Override + public Map<String, ImmutablePair<DataType, Boolean>> getColumnDescription() { + Map<String, ImmutablePair<DataType, Boolean>> columns = new HashMap<>(); + Arrays.stream(FeatureToggleEventFieldsDescription.values()) + .forEach(column -> columns.put(column.getName(), ImmutablePair.of(column.getType(), column.isIndexed()))); + return columns; + } + + @Override + public String getKeyspace() { + return AuditingTypesConstants.REPO_KEYSPACE; + } + + @Override + public String getTableName() { + return AuditingTypesConstants.FEATURE_TOGGLE_STATE; + } + + enum FeatureToggleEventFieldsDescription { + ENABLED("enabled", DataType.varchar(), false), + STRATEGY_ID("strategy_id", DataType.varchar(), false), + PARAMETERS("parameters", DataType.varchar(), false); + + private String name; + private DataType type; + private boolean indexed; + + FeatureToggleEventFieldsDescription(String name, DataType type, boolean indexed) { + this.name = name; + this.type = type; + this.indexed = indexed; + } + + public String getName() { + return name; + } + + public DataType getType() { + return type; + } + + public boolean isIndexed() { + return indexed; + } + + } +} |