Kea  1.9.9-git
cfg_shared_networks.h
Go to the documentation of this file.
1 // Copyright (C) 2017-2019 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 #ifndef CFG_SHARED_NETWORKS_H
8 #define CFG_SHARED_NETWORKS_H
9 
10 #include <asiolink/io_address.h>
11 #include <cc/cfg_to_element.h>
12 #include <cc/data.h>
13 #include <exceptions/exceptions.h>
14 #include <dhcpsrv/shared_network.h>
15 #include <boost/shared_ptr.hpp>
16 #include <string>
17 
18 namespace isc {
19 namespace dhcp {
20 
33 template<typename SharedNetworkPtrType, typename SharedNetworkCollection>
35 public:
37  const SharedNetworkCollection* getAll() const {
38  return (&networks_);
39  }
40 
47  void add(const SharedNetworkPtrType& network) {
48  if (getByName(network->getName())) {
49  isc_throw(BadValue, "duplicate network '" << network->getName() <<
50  "' found in the configuration");
51  }
52 
53  static_cast<void>(networks_.push_back(network));
54  }
55 
61  void del(const std::string& name) {
62  auto& index = networks_.template get<SharedNetworkNameIndexTag>();
63  auto shared_network = index.find(name);
64  if (shared_network != index.end()) {
65  // Delete all subnets from the network
66  (*shared_network)->delAll();
67 
68  // Then delete the network from the networks list.
69  index.erase(shared_network);
70  } else {
71  isc_throw(BadValue, "unable to delete non-existing network '"
72  << name << "' from shared networks configuration");
73  }
74  }
75 
88  uint64_t del(const uint64_t id) {
89  auto& index = networks_.template get<SharedNetworkIdIndexTag>();
90  auto sn_range = index.equal_range(id);
91 
92  // For each shared network found, dereference the subnets belonging
93  // to it.
94  for (auto it = sn_range.first; it != sn_range.second; ++it) {
95  (*it)->delAll();
96  }
97 
98  // Remove the shared networks.
99  return (static_cast<uint64_t>(index.erase(id)));
100  }
101 
108  SharedNetworkPtrType getByName(const std::string& name) const {
109  const auto& index = networks_.template get<SharedNetworkNameIndexTag>();
110  auto shared_network = index.find(name);
111  if (shared_network != index.cend()) {
112  return (*shared_network);
113  }
114  return (SharedNetworkPtrType());
115  }
116 
121  virtual data::ElementPtr toElement() const {
123 
124  // Insert shared networks sorted by their names into the list.
125  const auto& index = networks_.template get<SharedNetworkNameIndexTag>();
126  for (auto shared_network = index.begin(); shared_network != index.end();
127  ++shared_network) {
128  list->add((*shared_network)->toElement());
129  }
130  return (list);
131  }
132 
162  void merge(CfgOptionDefPtr cfg_def, CfgSharedNetworks& other) {
163  auto& index = networks_.template get<SharedNetworkNameIndexTag>();
164 
165  // Iterate over the subnets to be merged. They will replace the existing
166  // subnets with the same id. All new subnets will be inserted into this
167  // configuration.
168  auto other_networks = other.getAll();
169  for (auto other_network = other_networks->begin();
170  other_network != other_networks->end(); ++other_network) {
171 
172  // In theory we should drop subnet assignments from "other". The
173  // idea being those that come from the CB should not have subnets_
174  // populated. We will quietly throw them away, just in case.
175  (*other_network)->delAll();
176 
177  // Check if the other network exists in this config.
178  auto existing_network = index.find((*other_network)->getName());
179  if (existing_network != index.end()) {
180 
181  // Somehow the same instance is in both, skip it.
182  if (*existing_network == *other_network) {
183  continue;
184  }
185 
186  // Network exists, which means we're updating it.
187  // First we need to move its subnets to the new
188  // version of the network.
189  const auto subnets = (*existing_network)->getAllSubnets();
190 
191  auto copy_subnets(*subnets);
192  for (auto subnet = copy_subnets.cbegin(); subnet != copy_subnets.cend(); ++subnet) {
193  (*existing_network)->del((*subnet)->getID());
194  (*other_network)->add(*subnet);
195  }
196 
197  // Now we discard the existing copy of the network.
198  index.erase(existing_network);
199  }
200 
201  // Create the network's options based on the given definitions.
202  (*other_network)->getCfgOption()->createOptions(cfg_def);
203 
204  // Add the new/updated nework.
205  static_cast<void>(networks_.push_back(*other_network));
206  }
207  }
208 
209 protected:
210 
212  SharedNetworkCollection networks_;
213 };
214 
216 class CfgSharedNetworks4 : public CfgSharedNetworks<SharedNetwork4Ptr,
217  SharedNetwork4Collection> {
218 public:
225  bool hasNetworkWithServerId(const asiolink::IOAddress& server_id) const;
226 };
227 
229 typedef boost::shared_ptr<CfgSharedNetworks4> CfgSharedNetworks4Ptr;
230 
232 class CfgSharedNetworks6 : public CfgSharedNetworks<SharedNetwork6Ptr,
233  SharedNetwork6Collection> {
234 };
235 
237 typedef boost::shared_ptr<CfgSharedNetworks6> CfgSharedNetworks6Ptr;
238 
239 
240 } // end of namespace isc::dhcp
241 } // end of namespace isc
242 
243 #endif // CFG_SHARED_NETWORKS_H
SharedNetworkCollection networks_
Multi index container holding shared networks.
void add(const SharedNetworkPtrType &network)
Adds new shared network to the configuration.
boost::shared_ptr< CfgSharedNetworks6 > CfgSharedNetworks6Ptr
Pointer to the configuration of IPv6 shared networks.
const SharedNetworkCollection * getAll() const
Returns pointer to all configured shared networks.
void del(const std::string &name)
Deletes shared network from the configuration.
SharedNetworkPtrType getByName(const std::string &name) const
Retrieves shared network by name.
Represents configuration of IPv4 shared networks.
uint64_t del(const uint64_t id)
Deletes shared networks from the configuration by id.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:262
bool hasNetworkWithServerId(const asiolink::IOAddress &server_id) const
Checks if specified server identifier has been specified for any network.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
boost::shared_ptr< CfgSharedNetworks4 > CfgSharedNetworks4Ptr
Pointer to the configuration of IPv4 shared networks.
virtual data::ElementPtr toElement() const
Unparses shared networks configuration.
Abstract class for configuration Cfg_* classes.
This class holds configuration of shared networks.
Represents configuration of IPv6 shared networks.
Defines the logger used by the top-level component of kea-dhcp-ddns.
void merge(CfgOptionDefPtr cfg_def, CfgSharedNetworks &other)
Merges specified shared network configuration into this configuration.