cfg_shared_networks.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef CFG_SHARED_NETWORKS_H
  7. #define CFG_SHARED_NETWORKS_H
  8. #include <cc/cfg_to_element.h>
  9. #include <cc/data.h>
  10. #include <exceptions/exceptions.h>
  11. #include <dhcpsrv/shared_network.h>
  12. #include <boost/shared_ptr.hpp>
  13. #include <string>
  14. namespace isc {
  15. namespace dhcp {
  16. /// @brief This class holds configuration of shared networks.
  17. ///
  18. /// This is a generic class implementing basic functions such as shared network
  19. /// addition, removal and retrieval. It also dumps configuration in the JSON
  20. /// format.
  21. ///
  22. /// There are specializations of this class implemented as
  23. /// @ref CfgSharedNetworks4 and @ref CfgSharedNetworks6 for IPv4 and IPv6 cases
  24. /// repspectively.
  25. ///
  26. /// @tparam Type of the pointer to a shared network, i.e. @ref SharedNetwork4Ptr
  27. /// or @ref SharedNetwork6Ptr.
  28. template<typename SharedNetworkPtrType, typename SharedNetworkCollection>
  29. class CfgSharedNetworks : public data::CfgToElement {
  30. public:
  31. /// @brief Adds new shared network to the configuration.
  32. ///
  33. /// @param network Pointer to a network
  34. ///
  35. /// @throw isc::BadValue when name is a duplicate of existing network's
  36. /// name.
  37. void add(const SharedNetworkPtrType& network) {
  38. if (getByName(network->getName())) {
  39. isc_throw(BadValue, "duplicate network '" << network->getName() <<
  40. "' found in the configuration");
  41. }
  42. networks_.push_back(network);
  43. }
  44. /// @brief Deletes shared network from the configuration.
  45. ///
  46. /// @param name Name of the network to be deleted.
  47. ///
  48. /// @throw isc::BadValue if the network can't be found.
  49. void del(const std::string& name) {
  50. auto& index = networks_.template get<SharedNetworkNameIndexTag>();
  51. auto shared_network = index.find(name);
  52. if (shared_network != index.end()) {
  53. index.erase(shared_network);
  54. } else {
  55. isc_throw(BadValue, "unable to delete non-existing network '"
  56. << name << "' from shared networks configuration");
  57. }
  58. }
  59. /// @brief Retrieves shared network by name.
  60. ///
  61. /// @param name Name of the network to be retrieved.
  62. ///
  63. /// @return Pointer to the shared network or null pointer if the network
  64. /// is not found.
  65. SharedNetworkPtrType getByName(const std::string& name) const {
  66. const auto& index = networks_.template get<SharedNetworkNameIndexTag>();
  67. auto shared_network = index.find(name);
  68. if (shared_network != index.cend()) {
  69. return (*shared_network);
  70. }
  71. return (SharedNetworkPtrType());
  72. }
  73. /// @brief Unparses shared networks configuration.
  74. ///
  75. /// @return Element object representing a list of shared networks held
  76. /// within configuration. The networks are sorted by their names.
  77. virtual data::ElementPtr toElement() const {
  78. data::ElementPtr list = data::Element::createList();
  79. // Insert shared networks sorted by their names into the list.
  80. const auto& index = networks_.template get<SharedNetworkNameIndexTag>();
  81. for (auto shared_network = index.begin(); shared_network != index.end();
  82. ++shared_network) {
  83. list->add((*shared_network)->toElement());
  84. }
  85. return (list);
  86. }
  87. protected:
  88. /// @brief Multi index container holding shared networks.
  89. SharedNetworkCollection networks_;
  90. };
  91. /// @brief Represents configuration of IPv4 shared networks.
  92. class CfgSharedNetworks4 : public CfgSharedNetworks<SharedNetwork4Ptr,
  93. SharedNetwork4Collection> {
  94. public:
  95. /// @brief Returns pointer to all configured shared networks.
  96. const SharedNetwork4Collection* getAll() const {
  97. return (&networks_);
  98. }
  99. };
  100. /// @brief Pointer to the configuration of IPv4 shared networks.
  101. typedef boost::shared_ptr<CfgSharedNetworks4> CfgSharedNetworks4Ptr;
  102. /// @brief Represents configuration of IPv6 shared networks.
  103. class CfgSharedNetworks6 : public CfgSharedNetworks<SharedNetwork6Ptr,
  104. SharedNetwork6Collection> {
  105. public:
  106. /// @brief Returns pointer to all configured shared networks.
  107. const SharedNetwork6Collection* getAll() const {
  108. return (&networks_);
  109. }
  110. };
  111. /// @brief Pointer to the configuration of IPv6 shared networks.
  112. typedef boost::shared_ptr<CfgSharedNetworks6> CfgSharedNetworks6Ptr;
  113. } // end of namespace isc::dhcp
  114. } // end of namespace isc
  115. #endif // CFG_SHARED_NETWORKS_H