Kea  1.5.0
lease_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef LEASE_MGR_H
8 #define LEASE_MGR_H
9 
10 #include <asiolink/io_address.h>
11 #include <asiolink/io_service.h>
12 #include <database/db_exceptions.h>
13 #include <dhcp/duid.h>
14 #include <dhcp/option.h>
15 #include <dhcp/hwaddr.h>
16 #include <dhcpsrv/lease.h>
17 #include <dhcpsrv/subnet.h>
18 
19 #include <boost/noncopyable.hpp>
20 #include <boost/shared_ptr.hpp>
21 
22 #include <fstream>
23 #include <iostream>
24 #include <map>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
36 namespace isc {
37 namespace dhcp {
38 
40 typedef std::pair<uint32_t, uint32_t> VersionPair;
41 
44 public:
45 
51  explicit LeasePageSize(const size_t page_size);
52 
53  const size_t page_size_;
54 };
55 
61 struct LeaseStatsRow {
64  subnet_id_(0), lease_type_(Lease::TYPE_NA),
65  lease_state_(Lease::STATE_DEFAULT), state_count_(0) {
66  }
67 
75  LeaseStatsRow(const SubnetID& subnet_id, const uint32_t lease_state,
76  const int64_t state_count)
77  : subnet_id_(subnet_id), lease_type_(Lease::TYPE_NA),
78  lease_state_(lease_state), state_count_(state_count) {
79  }
80 
87  LeaseStatsRow(const SubnetID& subnet_id, const Lease::Type& lease_type,
88  const uint32_t lease_state, const int64_t state_count)
89  : subnet_id_(subnet_id), lease_type_(lease_type),
90  lease_state_(lease_state), state_count_(state_count) {
91  }
92 
94  bool operator< (const LeaseStatsRow &rhs) const {
95  if (subnet_id_ < rhs.subnet_id_) {
96  return (true);
97  }
98 
99  if (subnet_id_ == rhs.subnet_id_ &&
100  lease_type_ < rhs.lease_type_) {
101  return (true);
102  }
103 
104  if (subnet_id_ == rhs.subnet_id_ &&
105  lease_type_ == rhs.lease_type_ &&
106  lease_state_ < rhs.lease_state_) {
107  return (true);
108  }
109 
110  return (false);
111  }
112 
118  uint32_t lease_state_;
120  int64_t state_count_;
121 };
122 
129 public:
131  typedef enum {
136 
139  LeaseStatsQuery();
140 
147  LeaseStatsQuery(const SubnetID& subnet_id);
148 
157  LeaseStatsQuery(const SubnetID& first_subnet_id, const SubnetID& last_subnet_id);
158 
160  virtual ~LeaseStatsQuery() {};
161 
167  virtual void start() {};
168 
175  virtual bool getNextRow(LeaseStatsRow& row);
176 
179  return (first_subnet_id_);
180  };
181 
184  return (last_subnet_id_);
185  };
186 
191  return (select_mode_);
192  };
193 
194 protected:
197 
200 
201 private:
203  SelectMode select_mode_;
204 };
205 
207 typedef boost::shared_ptr<LeaseStatsQuery> LeaseStatsQueryPtr;
208 
210 typedef boost::shared_ptr<LeaseStatsRow> LeaseStatsRowPtr;
211 
222 class LeaseMgr {
223 public:
227  {}
228 
230  virtual ~LeaseMgr()
231  {}
232 
235  static std::string getDBVersion();
236 
248  virtual bool addLease(const Lease4Ptr& lease) = 0;
249 
261  virtual bool addLease(const Lease6Ptr& lease) = 0;
262 
275  virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const = 0;
276 
287  virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const = 0;
288 
299  virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
300  SubnetID subnet_id) const = 0;
301 
312  virtual Lease4Collection getLease4(const ClientId& clientid) const = 0;
313 
325  virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr,
326  SubnetID subnet_id) const = 0;
327 
337  virtual Lease4Ptr getLease4(const ClientId& clientid,
338  SubnetID subnet_id) const = 0;
339 
345  virtual Lease4Collection getLeases4(SubnetID subnet_id) const = 0;
346 
350  virtual Lease4Collection getLeases4() const = 0;
351 
376  virtual Lease4Collection
377  getLeases4(const asiolink::IOAddress& lower_bound_address,
378  const LeasePageSize& page_size) const = 0;
379 
391  const isc::asiolink::IOAddress& addr) const = 0;
392 
405  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
406  uint32_t iaid) const = 0;
407 
419  virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
420  uint32_t iaid, SubnetID subnet_id) const = 0;
421 
445  Lease6Ptr getLease6(Lease::Type type, const DUID& duid,
446  uint32_t iaid, SubnetID subnet_id) const;
447 
453  virtual Lease6Collection getLeases6(SubnetID subnet_id) const = 0;
454 
458  virtual Lease6Collection getLeases6() const = 0;
459 
464  virtual Lease6Collection getLeases6(const DUID& duid) const = 0;
465 
490  virtual Lease6Collection
491  getLeases6(const asiolink::IOAddress& lower_bound_address,
492  const LeasePageSize& page_size) const = 0;
493 
504  virtual void getExpiredLeases4(Lease4Collection& expired_leases,
505  const size_t max_leases) const = 0;
506 
517  virtual void getExpiredLeases6(Lease6Collection& expired_leases,
518  const size_t max_leases) const = 0;
519 
525  virtual void updateLease4(const Lease4Ptr& lease4) = 0;
526 
530  virtual void updateLease6(const Lease6Ptr& lease6) = 0;
531 
541  virtual bool deleteLease(const isc::asiolink::IOAddress& addr) = 0;
542 
550  virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs) = 0;
551 
559  virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs) = 0;
560 
580  void recountLeaseStats4();
581 
592 
602  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID& subnet_id);
603 
615  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID& first_subnet_id,
616  const SubnetID& last_subnet_id);
617 
637  void recountLeaseStats6();
638 
649 
659  virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID& subnet_id);
660 
672  virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID& first_subnet_id,
673  const SubnetID& last_subnet_id);
674 
682  virtual size_t wipeLeases4(const SubnetID& subnet_id) = 0;
683 
691  virtual size_t wipeLeases6(const SubnetID& subnet_id) = 0;
692 
698  virtual std::string getType() const = 0;
699 
706  virtual std::string getName() const = 0;
707 
713  virtual std::string getDescription() const = 0;
714 
729  virtual VersionPair getVersion() const = 0;
730 
735  virtual void commit() = 0;
736 
741  virtual void rollback() = 0;
742 
743 };
744 
745 } // namespace dhcp
746 } // namespace isc
747 
748 #endif // LEASE_MGR_H
isc::dhcp::LeaseMgr::wipeLeases4
virtual size_t wipeLeases4(const SubnetID &subnet_id)=0
Virtual method which removes specified leases.
isc::dhcp::LeasePageSize
Wraps value holding size of the page with leases.
Definition: lease_mgr.h:43
isc::dhcp::LeaseMgr::getName
virtual std::string getName() const =0
Returns backend name.
isc::dhcp::LeaseStatsQuery::last_subnet_id_
SubnetID last_subnet_id_
Last subnet_id in the selection criteria when a range is given.
Definition: lease_mgr.h:199
isc::dhcp::LeaseMgr::getLeases6
virtual Lease6Collection getLeases6(Lease::Type type, const DUID &duid, uint32_t iaid) const =0
Returns existing IPv6 leases for a given DUID+IA combination.
isc::dhcp::Lease4Ptr
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:245
isc::dhcp::LeaseMgr::getLease4
virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress &addr) const =0
Returns an IPv4 lease for specified IPv4 address.
isc::dhcp::LeaseMgr::startLeaseStatsQuery4
virtual LeaseStatsQueryPtr startLeaseStatsQuery4()
Creates and runs the IPv4 lease stats query for all subnets.
Definition: lease_mgr.cc:161
io_address.h
duid.h
isc::dhcp::LeaseStatsQuery::first_subnet_id_
SubnetID first_subnet_id_
First (or only) subnet_id in the selection criteria.
Definition: lease_mgr.h:192
isc::dhcp::LeaseMgr
Abstract Lease Manager.
Definition: lease_mgr.h:222
isc::dhcp::Lease6Ptr
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:460
isc::dhcp::LeaseMgr::wipeLeases6
virtual size_t wipeLeases6(const SubnetID &subnet_id)=0
Virtual method which removes specified leases.
isc::dhcp::LeaseStatsQuery::SelectMode
SelectMode
Defines the types of selection criteria supported.
Definition: lease_mgr.h:131
isc::dhcp::LeaseMgr::rollback
virtual void rollback()=0
Rollback Transactions.
isc::dhcp::LeaseMgr::startSubnetRangeLeaseStatsQuery6
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery6(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
Definition: lease_mgr.cc:284
isc::dhcp::LeaseMgr::getLeases6
virtual Lease6Collection getLeases6(const DUID &duid) const =0
Returns collection of leases for matching DUID.
isc::dhcp::Lease::Type
Type
Type of lease or pool.
Definition: lease.h:38
isc::dhcp::LeaseStatsQuery::start
virtual void start()
Executes the query.
Definition: lease_mgr.h:167
isc::dhcp::LeaseStatsQuery::SUBNET_RANGE
@ SUBNET_RANGE
Definition: lease_mgr.h:134
db_exceptions.h
isc::dhcp::HWAddr
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
isc::dhcp::LeaseStatsQuery::getFirstSubnetID
SubnetID getFirstSubnetID() const
Returns the value of first subnet ID specified (or zero)
Definition: lease_mgr.h:178
isc::dhcp::LeaseMgr::getDBVersion
static std::string getDBVersion()
Class method to return extended version info This class method must be redeclared and redefined in de...
Definition: lease_mgr.cc:290
isc::dhcp::LeaseStatsQuery::SINGLE_SUBNET
@ SINGLE_SUBNET
Definition: lease_mgr.h:133
isc::dhcp::VersionPair
std::pair< uint32_t, uint32_t > VersionPair
Pair containing major and minor versions.
Definition: lease_mgr.h:40
isc::dhcp::LeaseStatsQuery::LeaseStatsQuery
LeaseStatsQuery()
Default constructor The query created will return statistics for all subnets.
Definition: lease_mgr.cc:128
isc::dhcp::LeaseMgr::getDescription
virtual std::string getDescription() const =0
Returns description of the backend.
io_service.h
isc::dhcp::LeaseMgr::getExpiredLeases6
virtual void getExpiredLeases6(Lease6Collection &expired_leases, const size_t max_leases) const =0
Returns a collection of expired DHCPv6 leases.
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::dhcp::LeaseStatsQuery::~LeaseStatsQuery
virtual ~LeaseStatsQuery()
virtual destructor
Definition: lease_mgr.h:160
isc::dhcp::LeaseStatsRow::LeaseStatsRow
LeaseStatsRow()
Default constructor.
Definition: lease_mgr.h:63
isc::dhcp::LeaseStatsQuery::getSelectMode
SelectMode getSelectMode() const
Returns the selection criteria mode The value returned is based upon the constructor variant used and...
Definition: lease_mgr.h:190
isc::dhcp::LeaseMgr::~LeaseMgr
virtual ~LeaseMgr()
Destructor.
Definition: lease_mgr.h:230
isc::dhcp::LeaseMgr::startLeaseStatsQuery6
virtual LeaseStatsQueryPtr startLeaseStatsQuery6()
Creates and runs the IPv6 lease stats query for all subnets.
Definition: lease_mgr.cc:274
isc::dhcp::LeaseMgr::getLeases6
virtual Lease6Collection getLeases6(const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size) const =0
Returns range of IPv6 leases using paging.
isc::dhcp::ClientId
Holds Client identifier or client IPv4 address.
Definition: duid.h:111
isc::dhcp::LeasePageSize::LeasePageSize
LeasePageSize(const size_t page_size)
Constructor.
Definition: lease_mgr.cc:34
subnet.h
isc::dhcp::LeaseMgr::getVersion
virtual VersionPair getVersion() const =0
Returns backend version.
isc::dhcp::LeaseMgr::getLease4
virtual Lease4Collection getLease4(const isc::dhcp::HWAddr &hwaddr) const =0
Returns existing IPv4 leases for specified hardware address.
isc::dhcp::LeaseMgr::commit
virtual void commit()=0
Commit Transactions.
isc::dhcp::LeasePageSize::page_size_
const size_t page_size_
Holds page size.
Definition: lease_mgr.h:53
isc::dhcp::LeaseMgr::getLease4
virtual Lease4Ptr getLease4(const ClientId &clientid, SubnetID subnet_id) const =0
Returns existing IPv4 lease for specified client-id.
isc::dhcp::LeaseStatsRow::lease_state_
uint32_t lease_state_
The lease_state to which the count applies.
Definition: lease_mgr.h:118
isc::dhcp::LeaseMgr::getLeases4
virtual Lease4Collection getLeases4(const asiolink::IOAddress &lower_bound_address, const LeasePageSize &page_size) const =0
Returns range of IPv4 leases using paging.
isc::dhcp::LeaseStatsRow::LeaseStatsRow
LeaseStatsRow(const SubnetID &subnet_id, const uint32_t lease_state, const int64_t state_count)
Constructor.
Definition: lease_mgr.h:75
isc::dhcp::LeaseMgr::recountLeaseStats6
void recountLeaseStats6()
Recalculates per-subnet and global stats for IPv6 leases.
Definition: lease_mgr.cc:182
isc::dhcp::LeaseMgr::updateLease4
virtual void updateLease4(const Lease4Ptr &lease4)=0
Updates IPv4 lease.
isc::dhcp::LeaseStatsRow::subnet_id_
SubnetID subnet_id_
The subnet ID to which this data applies.
Definition: lease_mgr.h:114
isc::dhcp::LeaseMgr::deleteLease
virtual bool deleteLease(const isc::asiolink::IOAddress &addr)=0
Deletes a lease.
isc::dhcp::LeaseMgr::getType
virtual std::string getType() const =0
Return backend type.
isc::dhcp::LeaseMgr::startSubnetRangeLeaseStatsQuery4
virtual LeaseStatsQueryPtr startSubnetRangeLeaseStatsQuery4(const SubnetID &first_subnet_id, const SubnetID &last_subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
Definition: lease_mgr.cc:171
hwaddr.h
isc::dhcp::LeaseStatsQuery
Base class for fulfilling a statistical lease data query.
Definition: lease_mgr.h:128
isc::dhcp::LeaseMgr::getLeases6
virtual Lease6Collection getLeases6() const =0
Returns all IPv6 leases.
isc::dhcp::LeaseMgr::getLeases4
virtual Lease4Collection getLeases4(SubnetID subnet_id) const =0
Returns all IPv4 leases for the particular subnet identifier.
isc::dhcp::LeaseMgr::getLease4
virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr &hwaddr, SubnetID subnet_id) const =0
Returns existing IPv4 leases for specified hardware address and a subnet.
isc::dhcp::Lease4Collection
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition: lease.h:455
isc::dhcp::LeaseStatsQuery::getNextRow
virtual bool getNextRow(LeaseStatsRow &row)
Fetches the next row of data.
Definition: lease_mgr.cc:177
isc::dhcp::LeaseStatsRow::LeaseStatsRow
LeaseStatsRow(const SubnetID &subnet_id, const Lease::Type &lease_type, const uint32_t lease_state, const int64_t state_count)
Constructor.
Definition: lease_mgr.h:87
isc::dhcp::LeaseStatsRow::lease_type_
Lease::Type lease_type_
The lease_type to which the count applies.
Definition: lease_mgr.h:116
isc::dhcp::LeaseMgr::recountLeaseStats4
void recountLeaseStats4()
Recalculates per-subnet and global stats for IPv4 leases.
Definition: lease_mgr.cc:65
isc::dhcp::LeaseStatsQueryPtr
boost::shared_ptr< LeaseStatsQuery > LeaseStatsQueryPtr
Defines a pointer to a LeaseStatsQuery.
Definition: lease_mgr.h:207
isc::dhcp::LeaseMgr::getLeases6
virtual Lease6Collection getLeases6(Lease::Type type, const DUID &duid, uint32_t iaid, SubnetID subnet_id) const =0
Returns existing IPv6 lease for a given DUID+IA combination.
isc::dhcp::LeaseMgr::getLeases4
virtual Lease4Collection getLeases4() const =0
Returns all IPv4 leases.
isc::dhcp::DUID
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
isc::dhcp::LeaseMgr::getLease4
virtual Lease4Collection getLease4(const ClientId &clientid) const =0
Returns existing IPv4 lease for specified client-id.
isc::dhcp::LeaseMgr::getExpiredLeases4
virtual void getExpiredLeases4(Lease4Collection &expired_leases, const size_t max_leases) const =0
Returns a collection of expired DHCPv4 leases.
isc::dhcp::LeaseMgr::startSubnetLeaseStatsQuery4
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery4(const SubnetID &subnet_id)
Creates and runs the IPv4 lease stats query for a single subnet.
Definition: lease_mgr.cc:166
isc::dhcp::LeaseMgr::deleteExpiredReclaimedLeases4
virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs)=0
Deletes all expired and reclaimed DHCPv4 leases.
isc::dhcp::LeaseStatsQuery::getLastSubnetID
SubnetID getLastSubnetID() const
Returns the value of last subnet ID specified (or zero)
Definition: lease_mgr.h:183
isc::dhcp::LeaseStatsRowPtr
boost::shared_ptr< LeaseStatsRow > LeaseStatsRowPtr
Defines a pointer to a LeaseStatsRow.
Definition: lease_mgr.h:210
isc::dhcp::SubnetID
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24
option.h
isc::dhcp::LeaseStatsRow
Contains a single row of lease statistical data.
Definition: lease_mgr.h:61
isc::dhcp::LeaseMgr::getLease4
virtual Lease4Ptr getLease4(const ClientId &client_id, const HWAddr &hwaddr, SubnetID subnet_id) const =0
Returns existing IPv4 lease for a given client identifier, HW address and subnet identifier.
isc::dhcp::Lease6Collection
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:604
lease.h
isc::dhcp::Lease
a common structure for IPv4 and IPv6 leases
Definition: lease.h:35
isc::dhcp::LeaseMgr::getLeases6
virtual Lease6Collection getLeases6(SubnetID subnet_id) const =0
Returns all IPv6 leases for the particular subnet identifier.
isc::dhcp::LeaseStatsQuery::ALL_SUBNETS
@ ALL_SUBNETS
Definition: lease_mgr.h:132
isc::dhcp::LeaseStatsRow::operator<
bool operator<(const LeaseStatsRow &rhs) const
Less-than operator.
Definition: lease_mgr.h:94
isc::dhcp::LeaseMgr::addLease
virtual bool addLease(const Lease4Ptr &lease)=0
Adds an IPv4 lease.
isc::dhcp::LeaseMgr::addLease
virtual bool addLease(const Lease6Ptr &lease)=0
Adds an IPv6 lease.
isc::dhcp::LeaseMgr::updateLease6
virtual void updateLease6(const Lease6Ptr &lease6)=0
Updates IPv6 lease.
isc::dhcp::LeaseMgr::deleteExpiredReclaimedLeases6
virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs)=0
Deletes all expired and reclaimed DHCPv6 leases.
isc::dhcp::LeaseStatsRow::state_count_
int64_t state_count_
state_count The count of leases in the lease state
Definition: lease_mgr.h:120
isc::dhcp::LeaseMgr::LeaseMgr
LeaseMgr()
Constructor.
Definition: lease_mgr.h:226
isc::dhcp::LeaseMgr::getLease6
virtual Lease6Ptr getLease6(Lease::Type type, const isc::asiolink::IOAddress &addr) const =0
Returns existing IPv6 lease for a given IPv6 address.
isc::dhcp::LeaseMgr::startSubnetLeaseStatsQuery6
virtual LeaseStatsQueryPtr startSubnetLeaseStatsQuery6(const SubnetID &subnet_id)
Creates and runs the IPv6 lease stats query for a single subnet.
Definition: lease_mgr.cc:279