kea-custom-hooks
FeM custom hooks libraries for Kea DHCP
AdminDBClient.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <shared_mutex>
6#include <stdexcept>
7#include <string_view>
8#include <vector>
9
10#include <asiolink/io_address.h>
11#include <dhcp/hwaddr.h>
12
13#include "kch_config.h"
14
15namespace ahri
16{
17struct AdminDBClientPrivate;
18
28{
29public:
30 using dhcp_update_callback_t = std::function<void()>;
31
44 {
45 enum class Type : int
46 {
47 ADD,
48 REMOVE
49 };
50
53 isc::asiolink::IOAddress ipv4;
54 isc::dhcp::HWAddr mac;
55 size_t subnet_id{0};
71 };
72
73 constexpr static const char* NOTIFICATION_CHANNEL_NAME{"dhcp"};
74 constexpr static const size_t BELOW_LOWEST_INCREMENTAL_UPDATE_ID{0};
76 constexpr static const size_t UNDEFINED_SUBNET_ID{0};
77 constexpr static const std::chrono::days DEFAULT_PRUNE_BEFORE{7};
78
79 constexpr static const std::chrono::seconds NOTIFICATION_RECEIVER_TIMEOUT{1};
80 // We need some sleep to others trying to access pqxx don't starve
81 constexpr static const std::chrono::milliseconds NOTIFICATION_RECEIVER_SLEEP{500};
82
86 AdminDBClient(std::string_view host,
87 std::string_view port,
88 std::string_view user,
89 std::string_view password,
90 std::string_view database,
91 size_t last_processed_host_update_id = 0);
92 AdminDBClient(const AdminDBClient&) = delete;
96 TEST_VIRTUAL ~AdminDBClient();
97
106 TEST_VIRTUAL void handle_notifications(const dhcp_update_callback_t& cb);
107
121 TEST_VIRTUAL void prune_old_host_updates(std::chrono::days prune_before = DEFAULT_PRUNE_BEFORE);
136 TEST_VIRTUAL std::vector<HostUpdate> fetch_all_host_reservations();
146 TEST_VIRTUAL std::vector<HostUpdate> fetch_pending_host_updates();
154 TEST_VIRTUAL size_t fetch_latest_available_update_id();
156
163 static inline HostUpdate make_host_update(HostUpdate::Type type, const std::string& mac,
164 const std::string& ipv4, size_t incremental_update_id,
165 size_t subnet_id = 0)
166 {
167 return HostUpdate{.type = type,
168 .ipv4 = isc::asiolink::IOAddress(ipv4),
169 .mac = isc::dhcp::HWAddr::fromText(mac),
170 .subnet_id = subnet_id,
171 .incremental_update_id = incremental_update_id};
172 }
173
174 static inline HostUpdate::Type host_update_type_from_string(std::string_view type_str)
175 {
176 if (type_str == "assign") {
178 }
179 if (type_str == "unassign") {
181 }
182 throw std::invalid_argument(
183 "ahri::AdminDBClient: Type string doesn't match any "
184 "ahri::AdminDBClient::HostUpdate::Type");
185 }
186
187private:
188 std::unique_ptr<AdminDBClientPrivate> p_impl;
189};
190} // namespace ahri
Client for interaction with a single AdminDB server.
AdminDBClient & operator=(const AdminDBClient &)=delete
TEST_VIRTUAL std::vector< HostUpdate > fetch_pending_host_updates()
Fetch all not-yet-processed host updates.
static constexpr const size_t UNDEFINED_SUBNET_ID
Subnet ID for an entry where the subnet ID is NULL.
static constexpr const size_t BELOW_LOWEST_INCREMENTAL_UPDATE_ID
TEST_VIRTUAL size_t fetch_latest_available_update_id()
Fetch the maximum available incremental update identifier.
AdminDBClient & operator=(AdminDBClient &&)=delete
AdminDBClient(const AdminDBClient &)=delete
std::function< void()> dhcp_update_callback_t
static constexpr const std::chrono::seconds NOTIFICATION_RECEIVER_TIMEOUT
static HostUpdate::Type host_update_type_from_string(std::string_view type_str)
static HostUpdate make_host_update(HostUpdate::Type type, const std::string &mac, const std::string &ipv4, size_t incremental_update_id, size_t subnet_id=0)
Construct a host update from the inputs.
AdminDBClient(AdminDBClient &&)=delete
TEST_VIRTUAL std::vector< HostUpdate > fetch_all_host_reservations()
Fetch a complete set of all host reservations from the AdminDB server.
static constexpr const std::chrono::milliseconds NOTIFICATION_RECEIVER_SLEEP
static constexpr const std::chrono::days DEFAULT_PRUNE_BEFORE
TEST_VIRTUAL void handle_notifications(const dhcp_update_callback_t &cb)
Handle notifications for the DHCP server by invoking the given function.
TEST_VIRTUAL void prune_old_host_updates(std::chrono::days prune_before=DEFAULT_PRUNE_BEFORE)
Prune old host updates from the AdminDB host update table.
static constexpr const char * NOTIFICATION_CHANNEL_NAME
TEST_VIRTUAL ~AdminDBClient()
AdminDBClient(std::string_view host, std::string_view port, std::string_view user, std::string_view password, std::string_view database, size_t last_processed_host_update_id=0)
Create the client and connect to the AdminDB server with the given connection settings.
size_t incremental_update_id
An incremental update identifier.
Type type
Whether the given host reservation shall be added or removed.
isc::asiolink::IOAddress ipv4