11#include <boost/make_shared.hpp>
45 template<
typename SubnetPtrType,
typename SubnetCollectionType>
46 static void add(SubnetCollectionType& subnets,
const SubnetPtrType& subnet) {
50 " to a shared network");
54 if (getSubnet<SubnetPtrType>(subnets, subnet->getID())) {
56 " duplicated subnet identifier " << subnet->getID());
57 }
else if (getSubnet<SubnetPtrType>(subnets, subnet->toText())) {
59 " duplicated subnet prefix " << subnet->toText());
64 subnet->getSharedNetwork(network);
67 <<
" being added to a shared network"
68 " already belongs to a shared network");
72 static_cast<void>(subnets.insert(subnet));
95 template<
typename SubnetPtrType,
typename SubnetCollectionType>
96 static bool replace(SubnetCollectionType& subnets,
97 const SubnetPtrType& subnet) {
101 subnet->getSharedNetwork(network);
104 <<
" being replaced in a shared network"
105 " already belongs to a shared network");
109 const SubnetID& subnet_id = subnet->getID();
110 auto& index = subnets.template get<SubnetSubnetIdIndexTag>();
111 auto subnet_it = index.find(subnet_id);
112 if (subnet_it == index.end()) {
119 return (index.replace(subnet_it, subnet));
132 template<
typename SubnetPtrType,
typename SubnetCollectionType>
133 static SubnetPtrType del(SubnetCollectionType& subnets,
135 auto& index = subnets.template get<SubnetSubnetIdIndexTag>();
136 auto subnet_it = index.find(subnet_id);
137 if (subnet_it == index.end()) {
139 <<
" from shared network. Subnet doesn't belong"
140 " to this shared network");
142 auto subnet = *subnet_it;
143 index.erase(subnet_it);
158 template<
typename SubnetPtrType,
typename SubnetCollectionType>
159 static SubnetPtrType getSubnet(
const SubnetCollectionType& subnets,
161 auto const& index = subnets.template get<SubnetSubnetIdIndexTag>();
162 auto subnet_it = index.find(subnet_id);
163 if (subnet_it != index.cend()) {
168 return (SubnetPtrType());
183 template<
typename SubnetPtrType,
typename SubnetCollectionType>
184 static SubnetPtrType getSubnet(
const SubnetCollectionType& subnets,
185 const std::string& subnet_prefix) {
186 auto const& index = subnets.template get<SubnetPrefixIndexTag>();
187 auto subnet_it = index.find(subnet_prefix);
188 if (subnet_it != index.cend()) {
193 return (SubnetPtrType());
235 template<
typename SubnetPtrType,
typename SubnetCollectionType>
236 static SubnetPtrType getNextSubnet(
const SubnetCollectionType& subnets,
237 const SubnetPtrType& first_subnet,
241 if (subnets.empty()) {
242 return (SubnetPtrType());
248 auto const& index = subnets.template get<SubnetSubnetIdIndexTag>();
249 auto subnet_it = index.find(current_subnet);
250 if (subnet_it == index.cend()) {
252 <<
" within shared network");
256 if (++subnet_it == subnets.cend()) {
259 subnet_it = subnets.cbegin();
264 if ((*subnet_it)->getID() == first_subnet->getID()) {
265 return (SubnetPtrType());
300 template<
typename SubnetPtrType,
typename SubnetCollectionType>
301 static SubnetPtrType getPreferredSubnet(
const SubnetCollectionType& subnets,
302 const SubnetPtrType& selected_subnet,
305 auto preferred_subnet = selected_subnet;
306 for (
auto const& s : subnets) {
308 if (preferred_subnet == s) {
311 if (s->getClientClasses() != selected_subnet->getClientClasses()) {
314 auto current_subnet_state = s->getAllocationState(lease_type);
315 if (!current_subnet_state) {
318 auto preferred_subnet_state = preferred_subnet->getAllocationState(lease_type);
319 if (!preferred_subnet_state) {
325 if (current_subnet_state->getLastAllocatedTime() >
326 preferred_subnet_state->getLastAllocatedTime()) {
327 preferred_subnet = s;
330 return (preferred_subnet);
341 return (boost::make_shared<SharedNetwork4>(name));
346 Impl::add(subnets_, subnet);
348 subnet->setSharedNetwork(shared_from_this());
349 subnet->setSharedNetworkName(name_);
357 " to a shared network");
360 bool ret = Impl::replace(subnets_, subnet);
363 subnet->setSharedNetwork(shared_from_this());
364 subnet->setSharedNetworkName(name_);
367 old->setSharedNetworkName(
"");
374 Subnet4Ptr subnet = Impl::del<Subnet4Ptr>(subnets_, subnet_id);
376 subnet->setSharedNetworkName(
"");
381 for (
auto const& subnet : subnets_) {
383 subnet->setSharedNetworkName(
"");
390 return (Impl::getSubnet<Subnet4Ptr>(subnets_, subnet_id));
395 return (Impl::getSubnet<Subnet4Ptr>(subnets_, subnet_prefix));
400 const SubnetID& current_subnet)
const {
401 return (Impl::getNextSubnet(subnets_, first_subnet, current_subnet));
406 return (Impl::getPreferredSubnet(subnets_, selected_subnet,
Lease::TYPE_V4));
413 subnet = subnet->getNextSubnet(first_subnet, client_classes)) {
414 if (subnet->getMatchClientId()) {
426 if (!name_.empty()) {
431 for (
auto const& subnet : subnets_) {
432 subnet4->add(subnet->toElement());
435 map->set(
"subnet4", subnet4);
442 return (boost::make_shared<SharedNetwork6>(name));
447 Impl::add(subnets_, subnet);
449 subnet->setSharedNetwork(shared_from_this());
450 subnet->setSharedNetworkName(name_);
458 " to a shared network");
461 bool ret = Impl::replace(subnets_, subnet);
464 subnet->setSharedNetwork(shared_from_this());
465 subnet->setSharedNetworkName(name_);
468 old->setSharedNetworkName(
"");
475 Subnet6Ptr subnet = Impl::del<Subnet6Ptr>(subnets_, subnet_id);
477 subnet->setSharedNetworkName(
"");
482 for (
auto const& subnet : subnets_) {
490 return (Impl::getSubnet<Subnet6Ptr>(subnets_, subnet_id));
495 return (Impl::getSubnet<Subnet6Ptr>(subnets_, subnet_prefix));
500 const SubnetID& current_subnet)
const {
501 return (Impl::getNextSubnet(subnets_, first_subnet, current_subnet));
507 return (Impl::getPreferredSubnet(subnets_, selected_subnet, lease_type));
515 if (!name_.empty()) {
520 for (
auto const& subnet : subnets_) {
521 subnet6->add(subnet->toElement());
524 map->set(
"subnet6", subnet6);
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
static ElementPtr create(const Position &pos=ZERO_POSITION())
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Container for storing client class names.
Exception thrown upon attempt to add subnet with an ID that belongs to the subnet that already exists...
virtual data::ElementPtr toElement() const
Unparses network object.
virtual data::ElementPtr toElement() const
Unparses network object.
bool replace(const Subnet4Ptr &subnet)
Replaces IPv4 subnet in a shared network.
static SharedNetwork4Ptr create(const std::string &name)
Factory function creating an instance of the SharedNetwork4.
ConstSubnet4Ptr getNextSubnet(const ConstSubnet4Ptr &first_subnet, const SubnetID ¤t_subnet) const
Retrieves next available IPv4 subnet within shared network.
ConstSubnet4Ptr getPreferredSubnet(const ConstSubnet4Ptr &selected_subnet) const
Attempts to find a subnet which is more likely to include available leases than selected subnet.
virtual data::ElementPtr toElement() const
Unparses shared network object.
void add(const Subnet4Ptr &subnet)
Adds IPv4 subnet to a shared network.
void del(const SubnetID &subnet_id)
Removes subnet from a shared network.
Subnet4Ptr getSubnet(const SubnetID &subnet_id) const
Returns a subnet for a specified subnet id.
void delAll()
Removes all subnets from a shared network.
static bool subnetsIncludeMatchClientId(const ConstSubnet4Ptr &first_subnet, const ClientClasses &client_classes)
Checks if the shared network includes a subnet with the match client ID flag set to true.
static SharedNetwork6Ptr create(const std::string &name)
Factory function creating an instance of the SharedNetwork6.
ConstSubnet6Ptr getPreferredSubnet(const ConstSubnet6Ptr &selected_subnet, const Lease::Type &lease_type) const
Attempts to find a subnet which is more likely to include available leases than selected subnet.
Subnet6Ptr getSubnet(const SubnetID &subnet_id) const
Returns a subnet for a specified subnet id.
void del(const SubnetID &subnet_id)
Removes subnet from a shared network.
void add(const Subnet6Ptr &subnet)
Adds IPv6 subnet to a shared network.
ConstSubnet6Ptr getNextSubnet(const ConstSubnet6Ptr &first_subnet, const SubnetID ¤t_subnet) const
Retrieves next available IPv6 subnet within shared network.
virtual data::ElementPtr toElement() const
Unparses shared network object.
bool replace(const Subnet6Ptr &subnet)
Replaces IPv6 subnet in a shared network.
void delAll()
Removes all subnets from a shared network.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
boost::shared_ptr< const Subnet6 > ConstSubnet6Ptr
A const pointer to a Subnet6 object.
boost::shared_ptr< const Subnet4 > ConstSubnet4Ptr
A const pointer to a Subnet4 object.
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
boost::shared_ptr< SharedNetwork6 > SharedNetwork6Ptr
Pointer to SharedNetwork6 object.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
boost::shared_ptr< Network > NetworkPtr
Pointer to the Network object.
boost::shared_ptr< SharedNetwork4 > SharedNetwork4Ptr
Pointer to SharedNetwork4 object.
Defines the logger used by the top-level component of kea-lfc.
Type
Type of lease or pool.