From: Razvan Becheriu Date: Wed, 28 Feb 2018 08:29:23 +0000 (+0200) Subject: Merge remote-tracking branch 'isc-kea/master' into cassandra_improvements X-Git-Tag: kea5574_base~10^2~1^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2b76896bc7b2bdd931cd4414487a181e94b4b766;p=thirdparty%2Fkea.git Merge remote-tracking branch 'isc-kea/master' into cassandra_improvements --- 2b76896bc7b2bdd931cd4414487a181e94b4b766 diff --cc src/lib/dhcpsrv/cql_host_data_source.cc index 8e501a365e,e9073848b1..6fab69ab02 --- a/src/lib/dhcpsrv/cql_host_data_source.cc +++ b/src/lib/dhcpsrv/cql_host_data_source.cc @@@ -753,20 -592,11 +745,19 @@@ StatementMap CqlHostExchange::tagged_st "ALLOW FILTERING " }}, + {DELETE_HOST, + {DELETE_HOST, + "DELETE FROM host_reservations WHERE id = ? " + "IF EXISTS " + }} }; - CqlHostExchange::CqlHostExchange(CqlConnection& connection) - : host_(NULL), connection_(connection), id_(0), host_identifier_type_(0), - host_ipv4_subnet_id_(0), host_ipv6_subnet_id_(0), host_ipv4_address_(0), - host_ipv4_next_server_(0), + CqlHostExchange::CqlHostExchange() + : host_(NULL), id_(0), host_identifier_type_(0), host_ipv4_subnet_id_(0), - host_ipv6_subnet_id_(0), host_ipv4_address_(0), ++ host_ipv6_subnet_id_(0), host_ipv4_address_(0), host_ipv4_next_server_(0), + host_ipv4_server_hostname_(NULL_DHCP4_SERVER_HOSTNAME), + host_ipv4_boot_file_name_(NULL_DHCP4_BOOT_FILE_NAME), + user_context_(NULL_USER_CONTEXT), reserved_ipv6_prefix_length_(NULL_RESERVED_IPV6_PREFIX_LENGTH), reserved_ipv6_prefix_address_type_(NULL_RESERVED_IPV6_PREFIX_ADDRESS_TYPE), iaid_(NULL_IAID), option_universe_(NULL_OPTION_UNIVERSE), @@@ -1090,84 -855,25 +1081,63 @@@ CqlHostExchange::createBindForMutation( } } +void +CqlHostExchange::createBindForDelete(const HostPtr& host, + const OptionalValue& subnet_id, + const IPv6Resrv* const reservation, + const std::string& option_space, + const OptionDescriptor& option_descriptor, + StatementTag statement_tag, AnyArray& data) { + prepareExchange(host, subnet_id, reservation, option_space, option_descriptor); + + try { + // Add all parameters to bind array. + data.clear(); + + if (statement_tag == CqlHostExchange::DELETE_HOST) { + data.add(&id_); + } + + } catch (const Exception& ex) { + isc_throw(DbOperationError, + "CqlHostExchange::createBindForDelete(): " + "could not create bind array from host " + << host->getHostname() << ", reason: " << ex.what()); + } +} + - uint64_t - md5Hash(const std::string& input) { - - /// @todo: Convert this code to cryptolink calls and replace the - /// direct use fromn md5. - - // Prepare structures for MD5(). - const size_t word_size = MD5_DIGEST_LENGTH / sizeof(uint64_t); - uint64_t hash[word_size]; - unsigned char* digest = reinterpret_cast(hash); - unsigned char* string = reinterpret_cast(const_cast(input.c_str())); - std::fill(hash, hash + word_size, 0); - - // Get MD5 hash value. - MD5(string, input.size(), digest); - - // Return the first part of the hash value which still retains all - // properties of the full hash value. - return (hash[0]); - } - cass_int64_t CqlHostExchange::hashIntoId() const { - // Allocates a fixed maximum length in the stringstream for each - // aggregated field to avoid collisions between distinct entries. + // Add a separator between aggregated field to avoid collisions + // between distinct entries. // Get key. std::stringstream key_stream; - key_stream << host_ipv4_subnet_id_ << "-"; - key_stream << host_ipv6_subnet_id_ << "-"; - key_stream << host_ipv4_address_ << "-"; - key_stream << reserved_ipv6_prefix_address_ << "/"; - key_stream << reserved_ipv6_prefix_length_ << "-"; - key_stream << option_code_ << "-"; - key_stream << option_space_; + if (host_ipv4_address_) { + key_stream << std::setw(3 * DUID::MAX_DUID_LEN - 1) << std::setfill('-') + << "-"; + key_stream << std::setw(10) << std::setfill('-') << "-"; + } else { + key_stream << std::setw(3 * DUID::MAX_DUID_LEN - 1) << std::setfill('-') + << DUID(host_identifier_).toText(); + key_stream << std::setw(10) << std::setfill('-') << host_identifier_type_; + } + key_stream << std::setw(10) << std::setfill('-') << host_ipv4_subnet_id_; + key_stream << std::setw(10) << std::setfill('-') << host_ipv6_subnet_id_; + key_stream << std::setw(V4ADDRESS_TEXT_MAX_LEN) << std::setfill('-') + << host_ipv4_address_; + key_stream << std::setw(V6ADDRESS_TEXT_MAX_LEN) << std::setfill('-') + << reserved_ipv6_prefix_address_; + key_stream << std::setw(4) << std::setfill('-') + << reserved_ipv6_prefix_length_; + key_stream << std::setw(4) << std::setfill('-') << option_code_; + key_stream << std::setw(OPTION_SPACE_MAX_LENGTH) << std::setfill('-') + << option_space_; const std::string key = key_stream.str(); - const cass_int64_t md5 = static_cast(md5Hash(key)); + const cass_int64_t hash = static_cast(Hash64::hash(key)); - return (md5); + return (hash); } boost::any @@@ -2104,42 -1728,27 +2069,42 @@@ CqlHostDataSourceImpl::getHostCollectio return (result_collection); } -void -CqlHostDataSourceImpl::insertHost(const HostPtr& host, - const OptionalValue& subnet_id, - const IPv6Resrv* const reservation, - const std::string& option_space, - const OptionDescriptor& option_descriptor) { +bool +CqlHostDataSourceImpl::insertOrDeleteHost(bool insert, + const HostPtr& host, + const OptionalValue& subnet_id, + const IPv6Resrv* const reservation, + const std::string& option_space, + const OptionDescriptor& option_descriptor) { AnyArray assigned_values; - std::unique_ptr host_exchange(new CqlHostExchange(dbconn_)); + std::unique_ptr host_exchange(new CqlHostExchange()); try { - host_exchange->createBindForMutation( - host, subnet_id, reservation, option_space, option_descriptor, - CqlHostExchange::INSERT_HOST, assigned_values); + if (insert) { + host_exchange->createBindForMutation( + host, subnet_id, reservation, option_space, option_descriptor, + CqlHostExchange::INSERT_HOST, assigned_values); - host_exchange->executeMutation(dbconn_, assigned_values, - CqlHostExchange::INSERT_HOST); + host_exchange->executeMutation(dbconn_, assigned_values, CqlHostExchange::INSERT_HOST); + } else { + host_exchange->createBindForDelete( + host, subnet_id, reservation, option_space, option_descriptor, + CqlHostExchange::DELETE_HOST, assigned_values); + + + host_exchange->executeMutation(dbconn_, assigned_values, CqlHostExchange::DELETE_HOST); + } } catch (const StatementNotApplied& exception) { - isc_throw(DuplicateEntry, exception.what()); + if (insert) { + isc_throw(DuplicateEntry, exception.what()); + } else { + return (false); + } } + + return (true); } void diff --cc src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc index 0d1a0bf036,4e559c75cc..a38ef0b45a --- a/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/cql_lease_mgr_unittest.cc @@@ -741,10 -720,13 +742,8 @@@ TEST_F(CqlLeaseMgrTest, deleteExpiredRe testDeleteExpiredReclaimedLeases6(); } - // Verifies that IPv4 lease statistics can be recalculated. - /// @todo: uncomment this once stats recalculation is implemented - /// for Cassandra (see #5487) - TEST_F(CqlLeaseMgrTest, DISABLED_recountLeaseStats4) { -/// @brief Check that expired reclaimed DHCPv4 leases are removed. -TEST_F(CqlLeaseMgrTest, deleteExpiredReclaimedLeases4) { - testDeleteExpiredReclaimedLeases4(); -} - + /// @brief Verifies that IPv4 lease statistics can be recalculated. + TEST_F(CqlLeaseMgrTest, recountLeaseStats4) { testRecountLeaseStats4(); }