client_class_def.cc 4.8 KB

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