Kea 2.7.5
tracking_lease_mgr.h
Go to the documentation of this file.
1// Copyright (C) 2023-2024 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 TRACKING_LEASE_MGR_H
8#define TRACKING_LEASE_MGR_H
9
10#include <asiolink/io_address.h>
11#include <dhcpsrv/lease_mgr.h>
12#include <boost/multi_index_container.hpp>
13#include <boost/multi_index/composite_key.hpp>
14#include <boost/multi_index/indexed_by.hpp>
15#include <boost/multi_index/member.hpp>
16#include <boost/multi_index/hashed_index.hpp>
17#include <boost/multi_index/ordered_index.hpp>
18#include <boost/multi_index/sequenced_index.hpp>
19#include <boost/shared_ptr.hpp>
20#include <functional>
21#include <string>
22#include <unordered_set>
23
24namespace isc {
25namespace dhcp {
26
76class TrackingLeaseMgr : public LeaseMgr {
77public:
78
83 friend class LeaseMgrFactory;
84
91
97 typedef std::function<void(LeasePtr)> CallbackFn;
98
122
123protected:
124
131 typedef boost::multi_index_container<
132 Callback,
133 boost::multi_index::indexed_by<
134 boost::multi_index::ordered_non_unique<
135 boost::multi_index::composite_key<
136 Callback,
137 boost::multi_index::member<Callback, CallbackType, &Callback::type>,
138 boost::multi_index::member<Callback, SubnetID, &Callback::subnet_id>,
139 boost::multi_index::member<Callback, Lease::Type, &Callback::lease_type>
140 >
141 >,
142 boost::multi_index::ordered_non_unique<
143 boost::multi_index::composite_key<
144 Callback,
145 boost::multi_index::member<Callback, SubnetID, &Callback::subnet_id>,
146 boost::multi_index::member<Callback, Lease::Type, &Callback::lease_type>
147 >
148 >
149 >
151
153 typedef boost::shared_ptr<CallbackContainer> CallbackContainerPtr;
154
157
175 bool tryLock(const LeasePtr& lease);
176
182 void unlock(const LeasePtr& lease);
183
184public:
185
191 bool isLocked(const LeasePtr& lease);
192
193protected:
194
203 void trackAddLease(const LeasePtr& lease);
204
213 void trackUpdateLease(const LeasePtr& lease);
214
223 void trackDeleteLease(const LeasePtr& lease);
224
225public:
226
237 void registerCallback(CallbackType type, std::string owner, SubnetID subnet_id,
238 Lease::Type lease_type, CallbackFn callback_fn);
239
248 void registerCallback(CallbackType type, std::string owner, Lease::Type lease_type,
249 CallbackFn callback_fn);
250
255 void unregisterCallbacks(SubnetID subnet_id, Lease::Type lease_type);
256
259
266 bool hasCallbacks() const;
267
268protected:
269
274 static std::string callbackTypeToString(CallbackType type);
275
283 void runCallbacks(CallbackType type, const LeasePtr& lease);
284
292 void runCallbacksForSubnetID(CallbackType type, SubnetID subnet_id,
293 const LeasePtr& lease);
294
297
302 std::unordered_set<asiolink::IOAddress, asiolink::IOAddress::Hash> locked_leases_;
303};
304
306typedef std::unique_ptr<TrackingLeaseMgr> TrackingLeaseMgrPtr;
307
308} // end of namespace isc::dhcp
309} // end of namespace isc
310
311#endif // TRACKING_LEASE_MGR_H
Lease Manager Factory.
Abstract Lease Manager.
Definition lease_mgr.h:248
Introduces callbacks into the LeaseMgr.
CallbackType
An enumeration differentiating between lease write operations.
bool isLocked(const LeasePtr &lease)
Checks if the lease is locked.
void trackAddLease(const LeasePtr &lease)
Invokes the callbacks when a new lease is added.
void unlock(const LeasePtr &lease)
Attempts to unlock a lease.
void trackUpdateLease(const LeasePtr &lease)
Invokes the callbacks when a lease is updated.
void runCallbacks(CallbackType type, const LeasePtr &lease)
Runs registered callbacks of the particular type.
void trackDeleteLease(const LeasePtr &lease)
Invokes the callbacks when a lease is deleted.
boost::multi_index_container< Callback, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< boost::multi_index::composite_key< Callback, boost::multi_index::member< Callback, CallbackType, &Callback::type >, boost::multi_index::member< Callback, SubnetID, &Callback::subnet_id >, boost::multi_index::member< Callback, Lease::Type, &Callback::lease_type > > >, boost::multi_index::ordered_non_unique< boost::multi_index::composite_key< Callback, boost::multi_index::member< Callback, SubnetID, &Callback::subnet_id >, boost::multi_index::member< Callback, Lease::Type, &Callback::lease_type > > > > > CallbackContainer
A multi-index container holding registered callbacks.
void unregisterCallbacks(SubnetID subnet_id, Lease::Type lease_type)
Unregisters all callbacks for a given subnet identifier.
boost::shared_ptr< CallbackContainer > CallbackContainerPtr
Pointer to the callback container.
static std::string callbackTypeToString(CallbackType type)
Converts callback type to string for logging purposes.
void registerCallback(CallbackType type, std::string owner, SubnetID subnet_id, Lease::Type lease_type, CallbackFn callback_fn)
Registers a callback function for a subnet.
bool tryLock(const LeasePtr &lease)
Attempts to lock a lease.
std::unordered_set< asiolink::IOAddress, asiolink::IOAddress::Hash > locked_leases_
A set of locked leases.
void runCallbacksForSubnetID(CallbackType type, SubnetID subnet_id, const LeasePtr &lease)
Runs registered callbacks of the particular type for a subnet id.
CallbackContainerPtr callbacks_
The multi-index container holding registered callbacks.
void unregisterAllCallbacks()
Unregisters all callbacks.
std::function< void(LeasePtr)> CallbackFn
Type of a callback function invoked upon a lease insertion, update or deletion.
bool hasCallbacks() const
Checks if any callbacks have been registered.
An abstract API for lease database.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:25
std::unique_ptr< TrackingLeaseMgr > TrackingLeaseMgrPtr
TrackingLeaseMgr pointer.
Defines the logger used by the top-level component of kea-lfc.
Type
Type of lease or pool.
Definition lease.h:46
A structure representing a registered callback.
std::string owner
An entity owning callback registration (e.g., FLQ allocator).
SubnetID subnet_id
Subnet identifier associated with the callback.
Lease::Type lease_type
Lease types for which the callback should be invoked.
CallbackType type
Callback type (i.e., lease add, update, delete).