Kea 1.5.0
command_creator.cc
Go to the documentation of this file.
1// Copyright (C) 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#include <config.h>
8
9#include <command_creator.h>
12#include <boost/pointer_cast.hpp>
13
14using namespace isc::data;
15using namespace isc::dhcp;
16
17namespace isc {
18namespace ha {
19
21CommandCreator::createDHCPDisable(const unsigned int max_period,
22 const HAServerType& server_type) {
23 ElementPtr args;
24 // max-period is optional. A value of 0 means that it is not specified.
25 if (max_period > 0) {
26 args = Element::createMap();
27 args->set("max-period", Element::create(static_cast<long int>(max_period)));
28 }
29 ConstElementPtr command = config::createCommand("dhcp-disable", args);
30 insertService(command, server_type);
31 return (command);
32}
33
36 ConstElementPtr command = config::createCommand("dhcp-enable");
37 insertService(command, server_type);
38 return (command);
39}
40
43 ConstElementPtr command = config::createCommand("ha-heartbeat");
44 insertService(command, server_type);
45 return (command);
46}
47
50 ElementPtr lease_as_json = lease4.toElement();
51 insertLeaseExpireTime(lease_as_json);
52 lease_as_json->set("force-create", Element::create(true));
53 ConstElementPtr command = config::createCommand("lease4-update", lease_as_json);
54 insertService(command, HAServerType::DHCPv4);
55 return (command);
56}
57
60 ElementPtr lease_as_json = lease4.toElement();
61 insertLeaseExpireTime(lease_as_json);
62 ConstElementPtr command = config::createCommand("lease4-del", lease_as_json);
63 insertService(command, HAServerType::DHCPv4);
64 return (command);
65}
66
69 ConstElementPtr command = config::createCommand("lease4-get-all");
70 insertService(command, HAServerType::DHCPv4);
71 return (command);
72}
73
76 const uint32_t limit) {
77 // Zero value is not allowed.
78 if (limit == 0) {
79 isc_throw(BadValue, "limit value for lease4-get-page command must not be 0");
80 }
81
82 // Get the last lease returned on the previous page. A null pointer means that
83 // we're fetching first page. In that case a keyword "start" is used to indicate
84 // that first page should be returned.
85 ElementPtr from_element = Element::create(last_lease4 ? last_lease4->addr_.toText() : "start");
86 // Set the maximum size of the page.
87 ElementPtr limit_element = Element::create(static_cast<long long int>(limit));
88 // Put both parameters into arguments map.
90 args->set("from", from_element);
91 args->set("limit", limit_element);
92
93 // Create the command.
94 ConstElementPtr command = config::createCommand("lease4-get-page", args);
95 insertService(command, HAServerType::DHCPv4);
96 return (command);
97}
98
101 ElementPtr lease_as_json = lease6.toElement();
102 insertLeaseExpireTime(lease_as_json);
103 lease_as_json->set("force-create", Element::create(true));
104 ConstElementPtr command = config::createCommand("lease6-update", lease_as_json);
105 insertService(command, HAServerType::DHCPv6);
106 return (command);
107}
108
111 ElementPtr lease_as_json = lease6.toElement();
112 insertLeaseExpireTime(lease_as_json);
113 ConstElementPtr command = config::createCommand("lease6-del", lease_as_json);
114 insertService(command, HAServerType::DHCPv6);
115 return (command);
116}
117
120 ConstElementPtr command = config::createCommand("lease6-get-all");
121 insertService(command, HAServerType::DHCPv6);
122 return (command);
123}
124
127 const uint32_t limit) {
128 // Zero value is not allowed.
129 if (limit == 0) {
130 isc_throw(BadValue, "limit value for lease6-get-page command must not be 0");
131 }
132
133 // Get the last lease returned on the previous page. A null pointer means that
134 // we're fetching first page. In that case a keyword "start" is used to indicate
135 // that first page should be returned.
136 ElementPtr from_element = Element::create(last_lease6 ? last_lease6->addr_.toText() : "start");
137 // Set the maximum size of the page.
138 ElementPtr limit_element = Element::create(static_cast<long long int>(limit));
139 // Put both parameters into arguments map.
141 args->set("from", from_element);
142 args->set("limit", limit_element);
143
144 // Create the command.
145 ConstElementPtr command = config::createCommand("lease6-get-page", args);
146 insertService(command, HAServerType::DHCPv6);
147 return (command);
148}
149
150void
151CommandCreator::insertLeaseExpireTime(ElementPtr& lease) {
152 if ((lease->getType() != Element::map) ||
153 (!lease->contains("cltt") || (lease->get("cltt")->getType() != Element::integer) ||
154 (!lease->contains("valid-lft") ||
155 (lease->get("valid-lft")->getType() != Element::integer)))) {
156 isc_throw(Unexpected, "invalid lease format");
157 }
158
159 int64_t cltt = lease->get("cltt")->intValue();
160 int64_t valid_lifetime = lease->get("valid-lft")->intValue();
161 int64_t expire = cltt + valid_lifetime;
162 lease->set("expire", Element::create(expire));
163 lease->remove("cltt");
164}
165
166void
167CommandCreator::insertService(ConstElementPtr& command,
168 const HAServerType& server_type) {
170 const std::string service_name = (server_type == HAServerType::DHCPv4 ? "dhcp4" : "dhcp6");
171 service->add(Element::create(service_name));
172
173 // We have no better way of setting a new element here than
174 // doing const pointer cast. That's another reason why this
175 // functionality could be moved to the core code. We don't
176 // do it however, because we want to minimize concurrent
177 // code changes in the premium and core Kea repos.
178 (boost::const_pointer_cast<Element>(command))->set("service", service);
179}
180
181} // end of namespace ha
182} // 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:223
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:268
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:263
static data::ConstElementPtr createLease4Delete(const dhcp::Lease4 &lease4)
Creates lease4-del command.
static data::ConstElementPtr createHeartbeat(const HAServerType &server_type)
Creates ha-heartbeat command for DHCP server.
static data::ConstElementPtr createLease4Update(const dhcp::Lease4 &lease4)
Creates lease4-update command.
static data::ConstElementPtr createDHCPDisable(const unsigned int max_period, const HAServerType &server_type)
Creates dhcp-disable command for DHCP server.
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 createLease4GetAll()
Creates lease4-get-all command.
static data::ConstElementPtr createLease6GetPage(const dhcp::Lease6Ptr &lease6, const uint32_t limit)
Creates lease6-get-page command.
static data::ConstElementPtr createDHCPEnable(const HAServerType &server_type)
Creates dhcp-enable command for DHCP server.
static data::ConstElementPtr createLease6GetAll()
Creates lease6-get-all command.
static data::ConstElementPtr createLease4GetPage(const dhcp::Lease4Ptr &lease4, const uint32_t limit)
Creates lease4-get-page command.
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:23
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:463
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:248
HAServerType
Lists possible server types for which HA service is created.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Structure that holds a lease for IPv4 address.
Definition: lease.h:256
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition: lease.cc:401
Structure that holds a lease for IPv6 address and/or prefix.
Definition: lease.h:471
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition: lease.cc:616