Kea 2.5.6
command_creator.cc
Go to the documentation of this file.
1// Copyright (C) 2018-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#include <config.h>
8
9#include <command_creator.h>
12#include <boost/pointer_cast.hpp>
13
14using namespace isc::data;
15using namespace isc::dhcp;
16using namespace std;
17
18namespace isc {
19namespace ha {
20
21unordered_set<string> CommandCreator::ha_commands4_ = {
22 "list-commands", "status-get",
23 "dhcp-disable", "dhcp-enable",
24 "ha-reset", "ha-heartbeat",
25 "lease4-update", "lease4-del",
26 "lease4-get-all", "lease4-get-page",
27 "ha-maintenance-notify", "ha-sync-complete-notify"
28};
29
30unordered_set<string> CommandCreator::ha_commands6_ = {
31 "list-commands", "status-get",
32 "dhcp-disable", "dhcp-enable",
33 "ha-reset", "ha-heartbeat",
34 "lease6-bulk-apply",
35 "lease6-update", "lease6-del",
36 "lease6-get-all", "lease6-get-page",
37 "ha-maintenance-notify", "ha-sync-complete-notify"
38};
39
41CommandCreator::createDHCPDisable(const unsigned int origin,
42 const unsigned int max_period,
43 const HAServerType& server_type) {
44 ElementPtr args;
45 args = Element::createMap();
46 args->set("origin", Element::create(origin));
47 // max-period is optional. A value of 0 means that it is not specified.
48 if (max_period > 0) {
49 args->set("max-period", Element::create(static_cast<long int>(max_period)));
50 }
51 ConstElementPtr command = config::createCommand("dhcp-disable", args);
52 insertService(command, server_type);
53 return (command);
54}
55
57CommandCreator::createDHCPEnable(const unsigned int origin,
58 const HAServerType& server_type) {
59 ElementPtr args;
60 args = Element::createMap();
61 args->set("origin", Element::create(origin));
62 ConstElementPtr command = config::createCommand("dhcp-enable", args);
63 insertService(command, server_type);
64 return (command);
65}
66
68CommandCreator::createHAReset(const std::string& server_name,
69 const HAServerType& server_type) {
70 auto args = Element::createMap();
71 args->set("server-name", Element::create(server_name));
72 ConstElementPtr command = config::createCommand("ha-reset", args);
73 insertService(command, server_type);
74 return (command);
75}
76
78CommandCreator::createHeartbeat(const std::string& server_name,
79 const HAServerType& server_type) {
80 auto args = Element::createMap();
81 args->set("server-name", Element::create(server_name));
82 ConstElementPtr command = config::createCommand("ha-heartbeat", args);
83 insertService(command, server_type);
84 return (command);
85}
86
89 ElementPtr lease_as_json = lease4.toElement();
90 insertLeaseExpireTime(lease_as_json);
91 lease_as_json->set("force-create", Element::create(true));
92 lease_as_json->set("origin", Element::create("ha-partner"));
93 ConstElementPtr command = config::createCommand("lease4-update", lease_as_json);
94 insertService(command, HAServerType::DHCPv4);
95 return (command);
96}
97
100 ElementPtr lease_as_json = lease4.toElement();
101 insertLeaseExpireTime(lease_as_json);
102 lease_as_json->set("origin", Element::create("ha-partner"));
103 ConstElementPtr command = config::createCommand("lease4-del", lease_as_json);
104 insertService(command, HAServerType::DHCPv4);
105 return (command);
106}
107
110 ConstElementPtr command = config::createCommand("lease4-get-all");
111 insertService(command, HAServerType::DHCPv4);
112 return (command);
113}
114
117 const uint32_t limit) {
118 // Zero value is not allowed.
119 if (limit == 0) {
120 isc_throw(BadValue, "limit value for lease4-get-page command must not be 0");
121 }
122
123 // Get the last lease returned on the previous page. A null pointer means that
124 // we're fetching first page. In that case a keyword "start" is used to indicate
125 // that first page should be returned.
126 ElementPtr from_element = Element::create(last_lease4 ? last_lease4->addr_.toText() : "start");
127 // Set the maximum size of the page.
128 ElementPtr limit_element = Element::create(static_cast<long long int>(limit));
129 // Put both parameters into arguments map.
131 args->set("from", from_element);
132 args->set("limit", limit_element);
133
134 // Create the command.
135 ConstElementPtr command = config::createCommand("lease4-get-page", args);
136 insertService(command, HAServerType::DHCPv4);
137 return (command);
138}
139
142 const Lease6CollectionPtr& deleted_leases) {
143 ElementPtr deleted_leases_list = Element::createList();
144 for (auto const& lease : *deleted_leases) {
145 ElementPtr lease_as_json = lease->toElement();
146 insertLeaseExpireTime(lease_as_json);
147 deleted_leases_list->add(lease_as_json);
148 }
149
150 ElementPtr leases_list = Element::createList();
151 for (auto const& lease : *leases) {
152 ElementPtr lease_as_json = lease->toElement();
153 insertLeaseExpireTime(lease_as_json);
154 leases_list->add(lease_as_json);
155 }
156
158 args->set("deleted-leases", deleted_leases_list);
159 args->set("leases", leases_list);
160 args->set("origin", Element::create("ha-partner"));
161
162 ConstElementPtr command = config::createCommand("lease6-bulk-apply", args);
163 insertService(command, HAServerType::DHCPv6);
164 return (command);
165}
166
169 ElementPtr deleted_leases_list = Element::createList();
170 ElementPtr leases_list = Element::createList();
171
173 Lease6Ptr lease;
174 while ((lease = boost::dynamic_pointer_cast<Lease6>(leases.pop(op_type)))) {
175 ElementPtr lease_as_json = lease->toElement();
176 insertLeaseExpireTime(lease_as_json);
177 if (op_type == LeaseUpdateBacklog::DELETE) {
178 deleted_leases_list->add(lease_as_json);
179 } else {
180 leases_list->add(lease_as_json);
181 }
182 }
183
185 args->set("deleted-leases", deleted_leases_list);
186 args->set("leases", leases_list);
187 args->set("origin", Element::create("ha-partner"));
188
189 ConstElementPtr command = config::createCommand("lease6-bulk-apply", args);
190 insertService(command, HAServerType::DHCPv6);
191 return (command);
192}
193
196 ElementPtr lease_as_json = lease6.toElement();
197 insertLeaseExpireTime(lease_as_json);
198 lease_as_json->set("force-create", Element::create(true));
199 lease_as_json->set("origin", Element::create("ha-partner"));
200 ConstElementPtr command = config::createCommand("lease6-update", lease_as_json);
201 insertService(command, HAServerType::DHCPv6);
202 return (command);
203}
204
207 ElementPtr lease_as_json = lease6.toElement();
208 insertLeaseExpireTime(lease_as_json);
209 lease_as_json->set("origin", Element::create("ha-partner"));
210 ConstElementPtr command = config::createCommand("lease6-del", lease_as_json);
211 insertService(command, HAServerType::DHCPv6);
212 return (command);
213}
214
217 ConstElementPtr command = config::createCommand("lease6-get-all");
218 insertService(command, HAServerType::DHCPv6);
219 return (command);
220}
221
224 const uint32_t limit) {
225 // Zero value is not allowed.
226 if (limit == 0) {
227 isc_throw(BadValue, "limit value for lease6-get-page command must not be 0");
228 }
229
230 // Get the last lease returned on the previous page. A null pointer means that
231 // we're fetching first page. In that case a keyword "start" is used to indicate
232 // that first page should be returned.
233 ElementPtr from_element = Element::create(last_lease6 ? last_lease6->addr_.toText() : "start");
234 // Set the maximum size of the page.
235 ElementPtr limit_element = Element::create(static_cast<long long int>(limit));
236 // Put both parameters into arguments map.
238 args->set("from", from_element);
239 args->set("limit", limit_element);
240
241 // Create the command.
242 ConstElementPtr command = config::createCommand("lease6-get-page", args);
243 insertService(command, HAServerType::DHCPv6);
244 return (command);
245}
246
248CommandCreator::createMaintenanceNotify(const std::string& server_name,
249 const bool cancel,
250 const HAServerType& server_type) {
251 auto args = Element::createMap();
252 args->set("server-name", Element::create(server_name));
253 args->set("cancel", Element::create(cancel));
254 auto command = config::createCommand("ha-maintenance-notify", args);
255 insertService(command, server_type);
256 return (command);
257}
258
261 const std::string& server_name,
262 const HAServerType& server_type) {
263 auto args = Element::createMap();
264 args->set("server-name", Element::create(server_name));
265 args->set("origin", Element::create(origin));
266 auto command = config::createCommand("ha-sync-complete-notify", args);
267 insertService(command, server_type);
268 return (command);
269}
270
271void
272CommandCreator::insertLeaseExpireTime(ElementPtr& lease) {
273 if ((lease->getType() != Element::map) ||
274 (!lease->contains("cltt") || (lease->get("cltt")->getType() != Element::integer) ||
275 (!lease->contains("valid-lft") ||
276 (lease->get("valid-lft")->getType() != Element::integer)))) {
277 isc_throw(Unexpected, "invalid lease format");
278 }
279
280 int64_t cltt = lease->get("cltt")->intValue();
281 int64_t valid_lifetime = lease->get("valid-lft")->intValue();
282 int64_t expire = cltt + valid_lifetime;
283 lease->set("expire", Element::create(expire));
284 lease->remove("cltt");
285}
286
287void
288CommandCreator::insertService(ConstElementPtr& command,
289 const HAServerType& server_type) {
291 const string service_name = (server_type == HAServerType::DHCPv4 ? "dhcp4" : "dhcp6");
292 service->add(Element::create(service_name));
293
294 // We have no better way of setting a new element here than
295 // doing const pointer cast. That's another reason why this
296 // functionality could be moved to the core code. We don't
297 // do it however, because we want to minimize concurrent
298 // code changes in the premium and core Kea repos.
299 (boost::const_pointer_cast<Element>(command))->set("service", service);
300}
301
302} // end of namespace ha
303} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when an unexpected error condition occurs.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:304
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:299
static data::ConstElementPtr createLease4Delete(const dhcp::Lease4 &lease4)
Creates lease4-del command.
static data::ConstElementPtr createHeartbeat(const std::string &server_name, const HAServerType &server_type)
Creates ha-heartbeat command for DHCP server.
static std::unordered_set< std::string > ha_commands4_
List of commands used by the High Availability in v4.
static data::ConstElementPtr createLease4Update(const dhcp::Lease4 &lease4)
Creates lease4-update command.
static data::ConstElementPtr createDHCPEnable(const unsigned int origin, const HAServerType &server_type)
Creates dhcp-enable command for DHCP server.
static data::ConstElementPtr createSyncCompleteNotify(const unsigned int origin, const std::string &server_name, const HAServerType &server_type)
Creates ha-sync-complete-notify command.
static data::ConstElementPtr createLease6Update(const dhcp::Lease6 &lease6)
Creates lease6-update command.
static data::ConstElementPtr createLease6Delete(const dhcp::Lease6 &lease6)
Creates lease6-del command.
static data::ConstElementPtr createLease6BulkApply(const dhcp::Lease6CollectionPtr &leases, const dhcp::Lease6CollectionPtr &deleted_leases)
Creates lease6-bulk-apply command.
static data::ConstElementPtr createLease4GetAll()
Creates lease4-get-all command.
static data::ConstElementPtr createLease6GetPage(const dhcp::Lease6Ptr &lease6, const uint32_t limit)
Creates lease6-get-page command.
static std::unordered_set< std::string > ha_commands6_
List of commands used by the High Availability in v6.
static data::ConstElementPtr createHAReset(const std::string &server_name, const HAServerType &server_type)
Creates ha-reset command.
static data::ConstElementPtr createLease6GetAll()
Creates lease6-get-all command.
static data::ConstElementPtr createMaintenanceNotify(const std::string &server_name, const bool cancel, const HAServerType &server_type)
Creates ha-maintenance-notify command.
static data::ConstElementPtr createDHCPDisable(const unsigned int origin, const unsigned int max_period, const HAServerType &server_type)
Creates dhcp-disable command for DHCP server.
static data::ConstElementPtr createLease4GetPage(const dhcp::Lease4Ptr &lease4, const uint32_t limit)
Creates lease4-get-page command.
Queue holding a backlog of unsent lease updates.
dhcp::LeasePtr pop(OpType &op_type)
Returns the next lease update and removes it from the queue.
OpType
Type of the lease update (operation type).
This file contains several functions and constants that are used for handling commands and responses ...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
ConstElementPtr createCommand(const std::string &command)
Creates a standard command message with no argument (of the form { "command": "my_command" })
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:505
boost::shared_ptr< Lease6Collection > Lease6CollectionPtr
A shared pointer to the collection of IPv6 leases.
Definition: lease.h:673
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:292
HAServerType
Lists possible server types for which HA service is created.
Defines the logger used by the top-level component of kea-lfc.
Structure that holds a lease for IPv4 address.
Definition: lease.h:300
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition: lease.cc:386
Structure that holds a lease for IPv6 address and/or prefix.
Definition: lease.h:513
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition: lease.cc:625