Kea  1.5.0
cfg_shared_networks.h
Go to the documentation of this file.
1 // Copyright (C) 2017 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:
36 
43  void add(const SharedNetworkPtrType& network) {
44  if (getByName(network->getName())) {
45  isc_throw(BadValue, "duplicate network '" << network->getName() <<
46  "' found in the configuration");
47  }
48 
49  networks_.push_back(network);
50  }
51 
57  void del(const std::string& name) {
58  auto& index = networks_.template get<SharedNetworkNameIndexTag>();
59  auto shared_network = index.find(name);
60  if (shared_network != index.end()) {
61  // Delete all subnets from the network
62  (*shared_network)->delAll();
63 
64  // Then delete the network from the networks list.
65  index.erase(shared_network);
66  } else {
67  isc_throw(BadValue, "unable to delete non-existing network '"
68  << name << "' from shared networks configuration");
69  }
70  }
71 
78  SharedNetworkPtrType getByName(const std::string& name) const {
79  const auto& index = networks_.template get<SharedNetworkNameIndexTag>();
80  auto shared_network = index.find(name);
81  if (shared_network != index.cend()) {
82  return (*shared_network);
83  }
84  return (SharedNetworkPtrType());
85  }
86 
91  virtual data::ElementPtr toElement() const {
93 
94  // Insert shared networks sorted by their names into the list.
95  const auto& index = networks_.template get<SharedNetworkNameIndexTag>();
96  for (auto shared_network = index.begin(); shared_network != index.end();
97  ++shared_network) {
98  list->add((*shared_network)->toElement());
99  }
100  return (list);
101  }
102 
103 protected:
104 
106  SharedNetworkCollection networks_;
107 };
108 
110 class CfgSharedNetworks4 : public CfgSharedNetworks<SharedNetwork4Ptr,
111  SharedNetwork4Collection> {
112 public:
113 
116  return (&networks_);
117  }
118 
125  bool hasNetworkWithServerId(const asiolink::IOAddress& server_id) const;
126 
127 
128 };
129 
131 typedef boost::shared_ptr<CfgSharedNetworks4> CfgSharedNetworks4Ptr;
132 
134 class CfgSharedNetworks6 : public CfgSharedNetworks<SharedNetwork6Ptr,
135  SharedNetwork6Collection> {
136 public:
137 
140  return (&networks_);
141  }
142 };
143 
145 typedef boost::shared_ptr<CfgSharedNetworks6> CfgSharedNetworks6Ptr;
146 
147 
148 } // end of namespace isc::dhcp
149 } // end of namespace isc
150 
151 #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.
boost::multi_index_container< SharedNetwork6Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SharedNetworkRandomAccessIndexTag > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SharedNetworkNameIndexTag >, boost::multi_index::const_mem_fun< SharedNetwork6, std::string, &SharedNetwork6::getName > > >> SharedNetwork6Collection
Multi index container holding shared networks.
void del(const std::string &name)
Deletes shared network from the configuration.
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.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
boost::multi_index_container< SharedNetwork4Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SharedNetworkRandomAccessIndexTag > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SharedNetworkNameIndexTag >, boost::multi_index::const_mem_fun< SharedNetwork4, std::string, &SharedNetwork4::getName > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SharedNetworkServerIdIndexTag >, boost::multi_index::const_mem_fun< Network4, asiolink::IOAddress, &Network4::getServerId > > >> SharedNetwork4Collection
Multi index container holding shared networks.
SharedNetworkPtrType getByName(const std::string &name) const
Retrieves shared network by name.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:263
const SharedNetwork4Collection * getAll() const
Returns pointer to all configured shared networks.
#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...
const SharedNetwork6Collection * getAll() const
Returns pointer to all configured shared networks.
boost::shared_ptr< CfgSharedNetworks4 > CfgSharedNetworks4Ptr
Pointer to the configuration of IPv4 shared networks.
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.
virtual data::ElementPtr toElement() const
Unparses shared networks configuration.