Kea 1.5.0
cfgmgr.cc
Go to the documentation of this file.
1// Copyright (C) 2012-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>
9#include <dhcp/iface_mgr.h>
10#include <dhcp/libdhcp++.h>
11#include <dhcpsrv/cfgmgr.h>
12#include <dhcpsrv/dhcpsrv_log.h>
13#include <sstream>
14#include <string>
15
16using namespace isc::asiolink;
17using namespace isc::util;
18
19namespace isc {
20namespace dhcp {
21
22const size_t CfgMgr::CONFIG_LIST_SIZE = 10;
23
24CfgMgr&
26 static CfgMgr cfg_mgr;
27 return (cfg_mgr);
28}
29
30std::string CfgMgr::getDataDir() const {
31 return (datadir_);
32}
33
34void
35CfgMgr::setDataDir(const std::string& datadir) {
36 datadir_ = datadir;
37}
38
39void
41 ensureCurrentAllocated();
42 // Note that D2ClientMgr::setD2Config() actually attempts to apply the
43 // configuration by stopping its sender and opening a new one and so
44 // forth per the new configuration.
45 d2_client_mgr_.setD2ClientConfig(new_config);
46
47 // Manager will throw if the set fails, if it succeeds
48 // we'll update our SrvConfig, configuration_, with the D2ClientConfig
49 // used. This is largely bookkeeping in case we ever want to compare
50 // configuration_ to another SrvConfig.
51 configuration_->setD2ClientConfig(new_config);
52}
53
54bool
56 return (d2_client_mgr_.ddnsEnabled());
57}
58
61 return (d2_client_mgr_.getD2ClientConfig());
62}
63
66 return (d2_client_mgr_);
67}
68
69void
70CfgMgr::ensureCurrentAllocated() {
71 if (!configuration_ || configs_.empty()) {
72 configuration_.reset(new SrvConfig());
73 configs_.push_back(configuration_);
74 }
75}
76
77void
79 if (configuration_) {
80 configuration_->removeStatistics();
81 }
82 configs_.clear();
83 ensureCurrentAllocated();
84}
85
86void
88
89
90 ensureCurrentAllocated();
91
92 // First we need to remove statistics. The new configuration can have fewer
93 // subnets. Also, it may change subnet-ids. So we need to remove them all
94 // and add it back.
95 configuration_->removeStatistics();
96
97 if (!configs_.back()->sequenceEquals(*configuration_)) {
98 configuration_ = configs_.back();
99 // Keep track of the maximum size of the configs history. Before adding
100 // new element, we have to remove the oldest one.
101 if (configs_.size() > CONFIG_LIST_SIZE) {
102 SrvConfigList::iterator it = configs_.begin();
103 std::advance(it, configs_.size() - CONFIG_LIST_SIZE);
104 configs_.erase(configs_.begin(), it);
105 }
106 }
107
108 // Now we need to set the statistics back.
109 configuration_->updateStatistics();
110}
111
112void
114 ensureCurrentAllocated();
115 if (!configuration_->sequenceEquals(*configs_.back())) {
116 configs_.pop_back();
117 }
118}
119
120void
121CfgMgr::revert(const size_t index) {
122 ensureCurrentAllocated();
123 if (index == 0) {
124 isc_throw(isc::OutOfRange, "invalid commit index 0 when reverting"
125 " to an old configuration");
126 } else if (index > configs_.size() - 1) {
127 isc_throw(isc::OutOfRange, "unable to revert to commit index '"
128 << index << "', only '" << configs_.size() - 1
129 << "' previous commits available");
130 }
131
132 // Let's rollback an existing configuration to make sure that the last
133 // configuration on the list is the current one. Note that all remaining
134 // operations in this function should be exception free so there shouldn't
135 // be a problem that the revert operation fails and the staging
136 // configuration is destroyed by this rollback.
137 rollback();
138
139 // Get the iterator to the current configuration and then advance to the
140 // desired one.
141 SrvConfigList::const_reverse_iterator it = configs_.rbegin();
142 std::advance(it, index);
143
144 // Copy the desired configuration to the new staging configuration. The
145 // staging configuration is re-created here because we rolled back earlier
146 // in this function.
147 (*it)->copy(*getStagingCfg());
148
149 // Make the staging configuration a current one.
150 commit();
151}
152
155 ensureCurrentAllocated();
156 return (configuration_);
157}
158
161 ensureCurrentAllocated();
162 if (configuration_->sequenceEquals(*configs_.back())) {
163 uint32_t sequence = configuration_->getSequence();
164 configs_.push_back(SrvConfigPtr(new SrvConfig(++sequence)));
165 }
166 return (configs_.back());
167}
168
170 : datadir_(DHCP_DATA_DIR), d2_client_mgr_(), family_(AF_INET) {
171 // DHCP_DATA_DIR must be set set with -DDHCP_DATA_DIR="..." in Makefile.am
172 // Note: the definition of DHCP_DATA_DIR needs to include quotation marks
173 // See AM_CPPFLAGS definition in Makefile.am
174}
175
177}
178
179}; // end of isc::dhcp namespace
180}; // end of isc namespace
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Configuration Manager.
Definition: cfgmgr.h:69
CfgMgr()
Protected constructor.
Definition: cfgmgr.cc:169
const D2ClientConfigPtr & getD2ClientConfig() const
Fetches the DHCP-DDNS configuration pointer.
Definition: cfgmgr.cc:60
D2ClientMgr & getD2ClientMgr()
Fetches the DHCP-DDNS manager.
Definition: cfgmgr.cc:65
void clear()
Removes current, staging and all previous configurations.
Definition: cfgmgr.cc:78
void setD2ClientConfig(D2ClientConfigPtr &new_config)
Updates the DHCP-DDNS client configuration to the given value.
Definition: cfgmgr.cc:40
void rollback()
Removes staging configuration.
Definition: cfgmgr.cc:113
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition: cfgmgr.cc:25
virtual ~CfgMgr()
virtual destructor
Definition: cfgmgr.cc:176
SrvConfigPtr getStagingCfg()
Returns a pointer to the staging configuration.
Definition: cfgmgr.cc:160
void setDataDir(const std::string &datadir)
Sets new data directory.
Definition: cfgmgr.cc:35
bool ddnsEnabled()
Convenience method for checking if DHCP-DDNS updates are enabled.
Definition: cfgmgr.cc:55
std::string getDataDir() const
returns path do the data directory
Definition: cfgmgr.cc:30
void revert(const size_t index)
Reverts to one of the previous configurations.
Definition: cfgmgr.cc:121
static const size_t CONFIG_LIST_SIZE
A number of configurations held by CfgMgr.
Definition: cfgmgr.h:75
void commit()
Commits the staging configuration.
Definition: cfgmgr.cc:87
SrvConfigPtr getCurrentCfg()
Returns a pointer to the current configuration.
Definition: cfgmgr.cc:154
D2ClientMgr isolates Kea from the details of being a D2 client.
Definition: d2_client_mgr.h:79
bool ddnsEnabled()
Convenience method for checking if DHCP-DDNS is enabled.
const D2ClientConfigPtr & getD2ClientConfig() const
Fetches the DHCP-DDNS configuration pointer.
void setD2ClientConfig(D2ClientConfigPtr &new_config)
Updates the DHCP-DDNS client configuration to the given value.
Specifies current DHCP configuration.
Definition: srv_config.h:44
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< D2ClientConfig > D2ClientConfigPtr
Defines a pointer for D2ClientConfig instances.
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
Definition: srv_config.h:707
Definition: edns.h:19
Defines the logger used by the top-level component of kea-dhcp-ddns.