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