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>
10 #include <cc/command_interpreter.h>
11 #include <exceptions/exceptions.h>
12 #include <boost/pointer_cast.hpp>
13 
14 using namespace isc::data;
15 using namespace isc::dhcp;
16 
17 namespace isc {
18 namespace ha {
19 
21 CommandCreator::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 
35 CommandCreator::createDHCPEnable(const HAServerType& server_type) {
36  ConstElementPtr command = config::createCommand("dhcp-enable");
37  insertService(command, server_type);
38  return (command);
39 }
40 
42 CommandCreator::createHeartbeat(const HAServerType& server_type) {
43  ConstElementPtr command = config::createCommand("ha-heartbeat");
44  insertService(command, server_type);
45  return (command);
46 }
47 
49 CommandCreator::createLease4Update(const Lease4& lease4) {
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 
59 CommandCreator::createLease4Delete(const Lease4& lease4) {
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 
68 CommandCreator::createLease4GetAll() {
69  ConstElementPtr command = config::createCommand("lease4-get-all");
70  insertService(command, HAServerType::DHCPv4);
71  return (command);
72 }
73 
75 CommandCreator::createLease4GetPage(const Lease4Ptr& last_lease4,
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 
100 CommandCreator::createLease6Update(const Lease6& lease6) {
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 
110 CommandCreator::createLease6Delete(const Lease6& lease6) {
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 
119 CommandCreator::createLease6GetAll() {
120  ConstElementPtr command = config::createCommand("lease6-get-all");
121  insertService(command, HAServerType::DHCPv6);
122  return (command);
123 }
124 
126 CommandCreator::createLease6GetPage(const Lease6Ptr& last_lease6,
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 
150 void
151 CommandCreator::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 
166 void
167 CommandCreator::insertService(ConstElementPtr& command,
168  const HAServerType& server_type) {
169  ElementPtr service = Element::createList();
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
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition: exceptions/exceptions.h:153
isc::dhcp::Lease4Ptr
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:245
isc::dhcp::Lease6Ptr
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:460
isc::data
Definition: cfg_to_element.h:25
isc::data::Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:268
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::dhcp::Lease6
Structure that holds a lease for IPv6 address and/or prefix.
Definition: lease.h:471
isc::config::createCommand
ConstElementPtr createCommand(const std::string &command)
Creates a standard command message with no argument (of the form { "command": "my_command" })
Definition: command_interpreter.cc:137
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition: exceptions/exceptions.h:132
command_interpreter.h
isc::dhcp
Definition: ctrl_dhcp4_srv.cc:75
command_creator.h
isc::ha::HAServerType
HAServerType
Lists possible server types for which HA service is created.
Definition: ha_server_type.h:14
isc::data::Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:263
isc::dhcp::Lease4::toElement
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition: lease.cc:401
isc::data::Element::map
@ map
Definition: data.h:151
exceptions.h
isc::data::Element::integer
@ integer
Definition: data.h:151
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::data::Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
isc::dhcp::Lease6::toElement
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition: lease.cc:616
isc::dhcp::Lease4
Structure that holds a lease for IPv4 address.
Definition: lease.h:256