client_class_def.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (C) 2015-2016 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. #include "client_class_def.h"
  7. #include <boost/foreach.hpp>
  8. namespace isc {
  9. namespace dhcp {
  10. //********** ClientClassDef ******************//
  11. ClientClassDef::ClientClassDef(const std::string& name,
  12. const ExpressionPtr& match_expr,
  13. const CfgOptionPtr& cfg_option)
  14. : name_(name), match_expr_(match_expr), cfg_option_(cfg_option),
  15. next_server_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()) {
  16. // Name can't be blank
  17. if (name_.empty()) {
  18. isc_throw(BadValue, "Client Class name cannot be blank");
  19. }
  20. // We permit an empty expression for now. This will likely be useful
  21. // for automatic classes such as vendor class.
  22. // For classes without options, make sure we have an empty collection
  23. if (!cfg_option_) {
  24. cfg_option_.reset(new CfgOption());
  25. }
  26. }
  27. ClientClassDef::ClientClassDef(const ClientClassDef& rhs)
  28. : name_(rhs.name_), match_expr_(ExpressionPtr()),
  29. cfg_option_(new CfgOption()),
  30. next_server_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()) {
  31. if (rhs.match_expr_) {
  32. match_expr_.reset(new Expression());
  33. *match_expr_ = *(rhs.match_expr_);
  34. }
  35. if (rhs.cfg_option_) {
  36. rhs.cfg_option_->copyTo(*cfg_option_);
  37. }
  38. next_server_ = rhs.next_server_;
  39. sname_ = rhs.sname_;
  40. filename_ = rhs.filename_;
  41. }
  42. ClientClassDef::~ClientClassDef() {
  43. }
  44. std::string
  45. ClientClassDef::getName() const {
  46. return (name_);
  47. }
  48. void
  49. ClientClassDef::setName(const std::string& name) {
  50. name_ = name;
  51. }
  52. const ExpressionPtr&
  53. ClientClassDef::getMatchExpr() const {
  54. return (match_expr_);
  55. }
  56. void
  57. ClientClassDef::setMatchExpr(const ExpressionPtr& match_expr) {
  58. match_expr_ = match_expr;
  59. }
  60. const CfgOptionPtr&
  61. ClientClassDef::getCfgOption() const {
  62. return (cfg_option_);
  63. }
  64. void
  65. ClientClassDef::setCfgOption(const CfgOptionPtr& cfg_option) {
  66. cfg_option_ = cfg_option;
  67. }
  68. bool
  69. ClientClassDef::equals(const ClientClassDef& other) const {
  70. return ((name_ == other.name_) &&
  71. ((!match_expr_ && !other.match_expr_) ||
  72. (match_expr_ && other.match_expr_ &&
  73. (*match_expr_ == *(other.match_expr_)))) &&
  74. ((!cfg_option_ && !other.cfg_option_) ||
  75. (cfg_option_ && other.cfg_option_ &&
  76. (*cfg_option_ == *other.cfg_option_))) &&
  77. (next_server_ == other.next_server_) &&
  78. (sname_ == other.sname_) &&
  79. (filename_ == other.filename_));
  80. }
  81. std::ostream& operator<<(std::ostream& os, const ClientClassDef& x) {
  82. os << "ClientClassDef:" << x.getName();
  83. return (os);
  84. }
  85. //********** ClientClassDictionary ******************//
  86. ClientClassDictionary::ClientClassDictionary()
  87. : classes_(new ClientClassDefMap()) {
  88. }
  89. ClientClassDictionary::ClientClassDictionary(const ClientClassDictionary& rhs)
  90. : classes_(new ClientClassDefMap()) {
  91. BOOST_FOREACH(ClientClassMapPair cclass, *(rhs.classes_)) {
  92. ClientClassDefPtr copy(new ClientClassDef(*(cclass.second)));
  93. addClass(copy);
  94. }
  95. }
  96. ClientClassDictionary::~ClientClassDictionary() {
  97. }
  98. void
  99. ClientClassDictionary::addClass(const std::string& name,
  100. const ExpressionPtr& match_expr,
  101. const CfgOptionPtr& cfg_option,
  102. asiolink::IOAddress next_server,
  103. const std::string& sname,
  104. const std::string& filename) {
  105. ClientClassDefPtr cclass(new ClientClassDef(name, match_expr, cfg_option));
  106. cclass->setNextServer(next_server);
  107. cclass->setSname(sname);
  108. cclass->setFilename(filename);
  109. addClass(cclass);
  110. }
  111. void
  112. ClientClassDictionary::addClass(ClientClassDefPtr& class_def) {
  113. if (!class_def) {
  114. isc_throw(BadValue, "ClientClassDictionary::addClass "
  115. " - class definition cannot be null");
  116. }
  117. if (findClass(class_def->getName())) {
  118. isc_throw(DuplicateClientClassDef, "Client Class: "
  119. << class_def->getName() << " has already been defined");
  120. }
  121. (*classes_)[class_def->getName()] = class_def;
  122. }
  123. ClientClassDefPtr
  124. ClientClassDictionary::findClass(const std::string& name) const {
  125. ClientClassDefMap::iterator it = classes_->find(name);
  126. if (it != classes_->end()) {
  127. return (*it).second;
  128. }
  129. return(ClientClassDefPtr());
  130. }
  131. void
  132. ClientClassDictionary::removeClass(const std::string& name) {
  133. classes_->erase(name);
  134. }
  135. const ClientClassDefMapPtr&
  136. ClientClassDictionary::getClasses() const {
  137. return (classes_);
  138. }
  139. bool
  140. ClientClassDictionary::equals(const ClientClassDictionary& other) const {
  141. if (classes_->size() != other.classes_->size()) {
  142. return (false);
  143. }
  144. ClientClassDefMap::iterator this_class = classes_->begin();
  145. ClientClassDefMap::iterator other_class = other.classes_->begin();
  146. while (this_class != classes_->end() &&
  147. other_class != other.classes_->end()) {
  148. if (!(*this_class).second || !(*other_class).second ||
  149. (*(*this_class).second) != (*(*other_class).second)) {
  150. return false;
  151. }
  152. ++this_class;
  153. ++other_class;
  154. }
  155. return (true);
  156. }
  157. } // namespace isc::dhcp
  158. } // namespace isc