}
}
- // Basic lease access methods. Obtain leases from the database using various
- // criteria.
+ void
+ CqlLease6Exchange::getExpiredLeases(const size_t &max_leases,
+ Lease6Collection &expired_leases) {
+ // Set up the WHERE clause value
+ cass_int32_t keep_state = Lease::STATE_EXPIRED_RECLAIMED;
+ cass_int64_t timestamp = static_cast<cass_int64_t>(time(NULL));
+
+ // If the number of leases is 0, we will return all leases. This is
+ // achieved by setting the limit to a very high value.
+ cass_int32_t limit = max_leases > 0u ?
+ static_cast<cass_int32_t>(max_leases) :
+ std::numeric_limits<cass_int32_t>::max();
+
+ for (cass_int32_t state = Lease::STATE_DEFAULT;
+ state <= Lease::STATE_EXPIRED_RECLAIMED; state++) {
+ if (state == keep_state) {
+ continue;
+ }
+
+ AnyArray data;
+ data.add(&state);
+ data.add(×tamp);
+ data.add(&limit);
+
+ // Retrieve leases from the database.
+ Lease6Collection temp_collection;
+ getLeaseCollection(CqlLease6Exchange::GET_LEASE6_EXPIRE, data,
+ temp_collection);
+
+ for (Lease6Ptr &lease : temp_collection) {
+ expired_leases.push_back(lease);
+ }
+ }
+ }
+
+ CqlLeaseMgr::CqlLeaseMgr(const DatabaseConnection::ParameterMap ¶meters)
+ : LeaseMgr(), dbconn_(parameters) {
+ dbconn_.openDatabase();
+ dbconn_.prepareStatements(CqlLease4Exchange::tagged_statements_);
+ dbconn_.prepareStatements(CqlLease6Exchange::tagged_statements_);
+ dbconn_.prepareStatements(CqlVersionExchange::tagged_statements_);
+ }
+
+ CqlLeaseMgr::~CqlLeaseMgr() {
+ // There is no need to close the database in this destructor: it is
+ // closed in the destructor of the dbconn_ member variable.
+ }
+
+ std::string
+ CqlLeaseMgr::getDBVersion() {
+ std::stringstream tmp;
+ tmp << "CQL backend " << CQL_SCHEMA_VERSION_MAJOR;
+ tmp << "." << CQL_SCHEMA_VERSION_MINOR;
+ tmp << ", library cassandra";
+ return tmp.str();
+ }
+
+ bool
+ CqlLeaseMgr::addLease(const Lease4Ptr &lease) {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_ADD_ADDR4)
+ .arg(lease->addr_.toText());
+
+ AnyArray data;
+
+ std::unique_ptr<CqlLease4Exchange> exchange4(new CqlLease4Exchange(dbconn_));
+ exchange4->createBindForInsert(lease, data);
+ try {
+ exchange4->executeMutation(dbconn_, data, CqlLease4Exchange::INSERT_LEASE4);
+ } catch (const Exception &exception) {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_LEASE_EXCEPTION_THROWN)
+ .arg(exception.what());
+ return false;
+ }
+ return true;
+ }
+
+ bool
+ CqlLeaseMgr::addLease(const Lease6Ptr &lease) {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_ADD_ADDR6)
+ .arg(lease->addr_.toText());
+
+ AnyArray data;
+
+ std::unique_ptr<CqlLease6Exchange> exchange6(new CqlLease6Exchange(dbconn_));
+ exchange6->createBindForInsert(lease, data);
+ try {
+ exchange6->executeMutation(dbconn_, data, CqlLease6Exchange::INSERT_LEASE6);
+ } catch (const Exception &exception) {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_LEASE_EXCEPTION_THROWN)
+ .arg(exception.what());
+ return false;
+ }
+ return true;
+ }
Lease4Ptr
- CqlLeaseMgr::getLease4(const isc::asiolink::IOAddress& addr) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_CQL_GET_ADDR4).arg(addr.toText());
+ CqlLeaseMgr::getLease4(const IOAddress &addr) const {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET_ADDR4)
+ .arg(addr.toText());
// Set up the WHERE clause value
- CqlDataArray data;
+ AnyArray data;
- cass_int32_t addr4_data = static_cast<cass_int32_t>(addr.toUint32());
- data.add(reinterpret_cast<void*>(&addr4_data));
+ cass_int32_t address = static_cast<cass_int32_t>(addr.toUint32());
+ data.add(&address);
- // Get the data
+ // Get the data.
Lease4Ptr result;
- getLease(GET_LEASE4_ADDR, data, result);
+
+ std::unique_ptr<CqlLease4Exchange> exchange4(new CqlLease4Exchange(dbconn_));
+ exchange4->getLease(CqlLease4Exchange::GET_LEASE4_ADDR, data, result);
- return (result);
+ return result;
}
Lease4Collection
- CqlLeaseMgr::getLease4(const HWAddr& hwaddr) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_CQL_GET_HWADDR).arg(hwaddr.toText());
+ CqlLeaseMgr::getLease4(const HWAddr &hwaddr) const {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET_HWADDR)
+ .arg(hwaddr.toText());
// Set up the WHERE clause value
- CqlDataArray data;
+ AnyArray data;
- std::vector<cass_byte_t> hwaddr_data = hwaddr.hwaddr_;
- data.add(reinterpret_cast<void*>(&hwaddr_data));
+ CassBlob hwaddr_data(hwaddr.hwaddr_);
+ data.add(&hwaddr_data);
- // Get the data
+ // Get the data.
Lease4Collection result;
- getLeaseCollection(GET_LEASE4_HWADDR, data, result);
+ std::unique_ptr<CqlLease4Exchange> exchange4(new CqlLease4Exchange(dbconn_));
+ exchange4->getLeaseCollection(CqlLease4Exchange::GET_LEASE4_HWADDR, data, result);
- return (result);
+ return result;
}
Lease4Ptr
- CqlLeaseMgr::getLease4(const HWAddr& hwaddr, SubnetID subnet_id) const {
+ CqlLeaseMgr::getLease4(const HWAddr &hwaddr, SubnetID subnet_id) const {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
DHCPSRV_CQL_GET_SUBID_HWADDR)
- .arg(subnet_id).arg(hwaddr.toText());
+ .arg(subnet_id)
+ .arg(hwaddr.toText());
// Set up the WHERE clause value
- CqlDataArray data;
+ AnyArray data;
- std::vector<cass_byte_t> hwaddr_data = hwaddr.hwaddr_;
- data.add(reinterpret_cast<void*>(&hwaddr_data));
+ CassBlob hwaddr_data(hwaddr.hwaddr_);
+ data.add(&hwaddr_data);
cass_int32_t subnet_id_data = static_cast<cass_int32_t>(subnet_id);
- data.add(reinterpret_cast<void*>(&subnet_id_data));
+ data.add(&subnet_id_data);
- // Get the data
+ // Get the data.
Lease4Ptr result;
- getLease(GET_LEASE4_HWADDR_SUBID, data, result);
+ std::unique_ptr<CqlLease4Exchange> exchange4(new CqlLease4Exchange(dbconn_));
+ exchange4->getLease(CqlLease4Exchange::GET_LEASE4_HWADDR_SUBID, data, result);
- return (result);
+ return result;
}
Lease4Collection
- CqlLeaseMgr::getLease4(const ClientId& clientid) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_CQL_GET_CLIENTID).arg(clientid.toText());
+ CqlLeaseMgr::getLease4(const ClientId &clientid) const {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET_CLIENTID)
+ .arg(clientid.toText());
// Set up the WHERE clause value
- CqlDataArray data;
+ AnyArray data;
- std::vector<cass_byte_t> client_id_data = clientid.getClientId();
- data.add(reinterpret_cast<void*>(&client_id_data));
+ CassBlob client_id_data(clientid.getClientId());
+ data.add(&client_id_data);
- // Get the data
+ // Get the data.
Lease4Collection result;
- getLeaseCollection(GET_LEASE4_CLIENTID, data, result);
+ std::unique_ptr<CqlLease4Exchange> exchange4(new CqlLease4Exchange(dbconn_));
+ exchange4->getLeaseCollection(CqlLease4Exchange::GET_LEASE4_CLIENTID, data, result);
- return (result);
+ return result;
}
Lease4Ptr
}
Lease4Ptr
- CqlLeaseMgr::getLease4(const ClientId& clientid, SubnetID subnet_id) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_CQL_GET_SUBID_CLIENTID)
- .arg(subnet_id).arg(clientid.toText());
+ CqlLeaseMgr::getLease4(const ClientId &clientid, SubnetID subnet_id) const {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET_SUBID_CLIENTID)
+ .arg(subnet_id)
+ .arg(clientid.toText());
// Set up the WHERE clause value
- CqlDataArray data;
+ AnyArray data;
- std::vector<uint8_t> client_id_data = clientid.getClientId();
- data.add(reinterpret_cast<void*>(&client_id_data));
+ CassBlob client_id_data(clientid.getClientId());
+ data.add(&client_id_data);
cass_int32_t subnet_id_data = static_cast<cass_int32_t>(subnet_id);
- data.add(reinterpret_cast<void*>(&subnet_id_data));
+ data.add(&subnet_id_data);
- // Get the data
+ // Get the data.
Lease4Ptr result;
- getLease(GET_LEASE4_CLIENTID_SUBID, data, result);
+ std::unique_ptr<CqlLease4Exchange> exchange4(new CqlLease4Exchange(dbconn_));
+ exchange4->getLease(CqlLease4Exchange::GET_LEASE4_CLIENTID_SUBID, data, result);
- return (result);
+ return result;
}
+ Lease4Collection
+ CqlLeaseMgr::getLeases4(SubnetID) const {
+ isc_throw(NotImplemented, "getLeases4(subnet_id) is not implemented");
+ }
+
+ Lease4Collection
+ CqlLeaseMgr::getLeases4() const {
+ isc_throw(NotImplemented, "getLeases4() is not implemented");
+ }
+
Lease6Ptr
- CqlLeaseMgr::getLease6(Lease::Type lease_type,
- const isc::asiolink::IOAddress& addr) const {
+ CqlLeaseMgr::getLease6(Lease::Type lease_type, const IOAddress &addr) const {
std::string addr_data = addr.toText();
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_CQL_GET_ADDR6).arg(addr_data)
- .arg(lease_type);
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET_ADDR6)
+ .arg(addr_data)
+ .arg(lease_type);
// Set up the WHERE clause value
- CqlDataArray data;
+ AnyArray data;
if (addr_data.size() > ADDRESS6_TEXT_MAX_LEN) {
- isc_throw(BadValue, "getLease6(): " <<
- "address " << addr_data << " of length " <<
- addr_data.size() << " exceeds maximum allowed length of " <<
- ADDRESS6_TEXT_MAX_LEN);
+ isc_throw(BadValue,
+ "CqlLeaseMgr::getLease6(): "
+ "address "
+ << addr_data << " of length " << addr_data.size()
+ << " exceeds maximum allowed length of "
+ << ADDRESS6_TEXT_MAX_LEN);
}
- data.add(reinterpret_cast<void*>(&addr_data));
+ data.add(&addr_data);
cass_int32_t lease_type_data = static_cast<cass_int32_t>(lease_type);
- data.add(reinterpret_cast<void*>(&lease_type_data));
+ data.add(&lease_type_data);
Lease6Ptr result;
- getLease(GET_LEASE6_ADDR, data, result);
+ std::unique_ptr<CqlLease6Exchange> exchange6(new CqlLease6Exchange(dbconn_));
+ exchange6->getLease(CqlLease6Exchange::GET_LEASE6_ADDR, data, result);
- return (result);
+ return result;
}
Lease6Collection
- CqlLeaseMgr::getLeases6(Lease::Type lease_type,
- const DUID& duid, uint32_t iaid) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_CQL_GET_IAID_DUID).arg(iaid).arg(duid.toText())
- .arg(lease_type);
+ CqlLeaseMgr::getLeases6(Lease::Type lease_type, const DUID &duid, uint32_t iaid) const {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET_IAID_DUID)
+ .arg(iaid)
+ .arg(duid.toText())
+ .arg(lease_type);
// Set up the WHERE clause value
- CqlDataArray data;
-
- std::vector<cass_byte_t> duid_data = duid.getDuid();
- data.add(reinterpret_cast<void*>(&duid_data));
+ AnyArray data;
+ CassBlob duid_data(duid.getDuid());
cass_int32_t iaid_data = static_cast<cass_int32_t>(iaid);
- data.add(reinterpret_cast<void*>(&iaid_data));
+
+ data.add(&duid_data);
+ data.add(&iaid_data);
cass_int32_t lease_type_data = static_cast<cass_int32_t>(lease_type);
- data.add(reinterpret_cast<void*>(&lease_type_data));
+ data.add(&lease_type_data);
- // ... and get the data
+ // Get the data.
Lease6Collection result;
- getLeaseCollection(GET_LEASE6_DUID_IAID, data, result);
+ std::unique_ptr<CqlLease6Exchange> exchange6(new CqlLease6Exchange(dbconn_));
+ exchange6->getLeaseCollection(CqlLease6Exchange::GET_LEASE6_DUID_IAID, data, result);
- return (result);
+ return result;
}
Lease6Collection
- CqlLeaseMgr::getLeases6(Lease::Type lease_type,
- const DUID& duid, uint32_t iaid,
- SubnetID subnet_id) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_CQL_GET_IAID_SUBID_DUID)
- .arg(iaid).arg(subnet_id).arg(duid.toText())
- .arg(lease_type);
+ CqlLeaseMgr::getLeases6(Lease::Type lease_type, const DUID &duid, uint32_t iaid,
+ SubnetID subnet_id) const {
+ LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_GET_IAID_SUBID_DUID)
+ .arg(iaid)
+ .arg(subnet_id)
+ .arg(duid.toText())
+ .arg(lease_type);
// Set up the WHERE clause value
- CqlDataArray data;
-
- std::vector<cass_byte_t> duid_data = duid.getDuid();
- data.add(reinterpret_cast<void*>(&duid_data));
+ AnyArray data;
+ CassBlob duid_data(duid.getDuid());
cass_int32_t iaid_data = static_cast<cass_int32_t>(iaid);
- data.add(reinterpret_cast<void*>(&iaid_data));
- cass_int32_t subnet_id_data = static_cast<cass_int32_t>(subnet_id);
- data.add(reinterpret_cast<void*>(&subnet_id_data));
+ data.add(&duid_data);
+ data.add(&iaid_data);
cass_int32_t lease_type_data = static_cast<cass_int32_t>(lease_type);
- data.add(reinterpret_cast<void*>(&lease_type_data));
+ data.add(&lease_type_data);
- // ... and get the data
+ cass_int32_t subnet_id_data = static_cast<cass_int32_t>(subnet_id);
+ data.add(&subnet_id_data);
+
+ // Get the data.
Lease6Collection result;
- getLeaseCollection(GET_LEASE6_DUID_IAID_SUBID, data, result);
+ std::unique_ptr<CqlLease6Exchange> exchange6(new CqlLease6Exchange(dbconn_));
+ exchange6->getLeaseCollection(CqlLease6Exchange::GET_LEASE6_DUID_IAID_SUBID, data, result);
- return (result);
+ return result;
}
void