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>
8 #include <asiolink/io_address.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 
16 using namespace isc::asiolink;
17 using namespace isc::util;
18 
19 namespace isc {
20 namespace dhcp {
21 
22 const size_t CfgMgr::CONFIG_LIST_SIZE = 10;
23 
24 CfgMgr&
25 CfgMgr::instance() {
26  static CfgMgr cfg_mgr;
27  return (cfg_mgr);
28 }
29 
30 std::string CfgMgr::getDataDir() const {
31  return (datadir_);
32 }
33 
34 void
35 CfgMgr::setDataDir(const std::string& datadir) {
36  datadir_ = datadir;
37 }
38 
39 void
40 CfgMgr::setD2ClientConfig(D2ClientConfigPtr& new_config) {
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 
54 bool
55 CfgMgr::ddnsEnabled() {
56  return (d2_client_mgr_.ddnsEnabled());
57 }
58 
59 const D2ClientConfigPtr&
60 CfgMgr::getD2ClientConfig() const {
61  return (d2_client_mgr_.getD2ClientConfig());
62 }
63 
65 CfgMgr::getD2ClientMgr() {
66  return (d2_client_mgr_);
67 }
68 
69 void
70 CfgMgr::ensureCurrentAllocated() {
71  if (!configuration_ || configs_.empty()) {
72  configuration_.reset(new SrvConfig());
73  configs_.push_back(configuration_);
74  }
75 }
76 
77 void
78 CfgMgr::clear() {
79  if (configuration_) {
80  configuration_->removeStatistics();
81  }
82  configs_.clear();
83  ensureCurrentAllocated();
84 }
85 
86 void
87 CfgMgr::commit() {
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 
112 void
113 CfgMgr::rollback() {
114  ensureCurrentAllocated();
115  if (!configuration_->sequenceEquals(*configs_.back())) {
116  configs_.pop_back();
117  }
118 }
119 
120 void
121 CfgMgr::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 
154 CfgMgr::getCurrentCfg() {
155  ensureCurrentAllocated();
156  return (configuration_);
157 }
158 
160 CfgMgr::getStagingCfg() {
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 
169 CfgMgr::CfgMgr()
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
iface_mgr.h
libdhcp++.h
io_address.h
isc::util
Definition: edns.h:19
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
dhcpsrv_log.h
isc::dhcp::D2ClientConfigPtr
boost::shared_ptr< D2ClientConfig > D2ClientConfigPtr
Defines a pointer for D2ClientConfig instances.
Definition: d2_client_cfg.h:323
isc::dhcp::CfgMgr::~CfgMgr
virtual ~CfgMgr()
virtual destructor
Definition: cfgmgr.cc:176
isc::dhcp::D2ClientMgr
D2ClientMgr isolates Kea from the details of being a D2 client.
Definition: d2_client_mgr.h:79
cfgmgr.h
isc::OutOfRange
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Definition: exceptions/exceptions.h:115
isc::dhcp::SrvConfig
Specifies current DHCP configuration.
Definition: srv_config.h:44
isc::dhcp::SrvConfigPtr
boost::shared_ptr< SrvConfig > SrvConfigPtr
Non-const pointer to the SrvConfig.
Definition: srv_config.h:707
isc::dhcp::CfgMgr
Configuration Manager.
Definition: cfgmgr.h:69