client_class_def.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include "client_class_def.h"
  15. #include <boost/foreach.hpp>
  16. namespace isc {
  17. namespace dhcp {
  18. //********** ClientClassDef ******************//
  19. ClientClassDef::ClientClassDef(const std::string& name,
  20. const ExpressionPtr& match_expr,
  21. const CfgOptionPtr& cfg_option)
  22. : name_(name), match_expr_(match_expr), cfg_option_(cfg_option) {
  23. // Name can't be blank
  24. if (name_.empty()) {
  25. isc_throw(BadValue, "Client Class name cannot be blank");
  26. }
  27. // We permit an empty expression for now. This will likely be useful
  28. // for automatic classes such as vendor class.
  29. // For classes without options, make sure we have an empty collection
  30. if (!cfg_option_) {
  31. cfg_option_.reset(new CfgOption());
  32. }
  33. }
  34. ClientClassDef::ClientClassDef(const ClientClassDef& rhs)
  35. : name_(rhs.name_), match_expr_(ExpressionPtr()),
  36. cfg_option_(new CfgOption()) {
  37. if (rhs.match_expr_) {
  38. match_expr_.reset(new Expression());
  39. *match_expr_ = *(rhs.match_expr_);
  40. }
  41. if (rhs.cfg_option_) {
  42. rhs.cfg_option_->copyTo(*cfg_option_);
  43. }
  44. }
  45. ClientClassDef::~ClientClassDef() {
  46. }
  47. std::string
  48. ClientClassDef::getName() const {
  49. return (name_);
  50. }
  51. void
  52. ClientClassDef::setName(const std::string& name) {
  53. name_ = name;
  54. }
  55. const ExpressionPtr&
  56. ClientClassDef::getMatchExpr() const {
  57. return (match_expr_);
  58. }
  59. void
  60. ClientClassDef::setMatchExpr(const ExpressionPtr& match_expr) {
  61. match_expr_ = match_expr;
  62. }
  63. const CfgOptionPtr&
  64. ClientClassDef::getCfgOption() const {
  65. return (cfg_option_);
  66. }
  67. void
  68. ClientClassDef::setCfgOption(const CfgOptionPtr& cfg_option) {
  69. cfg_option_ = cfg_option;
  70. }
  71. bool
  72. ClientClassDef::equals(const ClientClassDef& other) const {
  73. return ((name_ == other.name_) &&
  74. ((!match_expr_ && !other.match_expr_) ||
  75. (match_expr_ && other.match_expr_ &&
  76. (*match_expr_ == *(other.match_expr_)))) &&
  77. ((!cfg_option_ && !other.cfg_option_) ||
  78. (cfg_option_ && other.cfg_option_ &&
  79. (*cfg_option_ == *other.cfg_option_))));
  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. ClientClassDefPtr cclass(new ClientClassDef(name, match_expr, cfg_option));
  103. addClass(cclass);
  104. }
  105. void
  106. ClientClassDictionary::addClass(ClientClassDefPtr& class_def) {
  107. if (!class_def) {
  108. isc_throw(BadValue, "ClientClassDictionary::addClass "
  109. " - class definition cannot be null");
  110. }
  111. if (findClass(class_def->getName())) {
  112. isc_throw(DuplicateClientClassDef, "Client Class: "
  113. << class_def->getName() << " has already been defined");
  114. }
  115. (*classes_)[class_def->getName()] = class_def;
  116. }
  117. ClientClassDefPtr
  118. ClientClassDictionary::findClass(const std::string& name) const {
  119. ClientClassDefMap::iterator it = classes_->find(name);
  120. if (it != classes_->end()) {
  121. return (*it).second;
  122. }
  123. return(ClientClassDefPtr());
  124. }
  125. void
  126. ClientClassDictionary::removeClass(const std::string& name) {
  127. classes_->erase(name);
  128. }
  129. const ClientClassDefMapPtr&
  130. ClientClassDictionary::getClasses() const {
  131. return (classes_);
  132. }
  133. bool
  134. ClientClassDictionary::equals(const ClientClassDictionary& other) const {
  135. if (classes_->size() != other.classes_->size()) {
  136. return (false);
  137. }
  138. ClientClassDefMap::iterator this_class = classes_->begin();
  139. ClientClassDefMap::iterator other_class = other.classes_->begin();
  140. while (this_class != classes_->end() &&
  141. other_class != other.classes_->end()) {
  142. if (!(*this_class).second || !(*other_class).second ||
  143. (*(*this_class).second) != (*(*other_class).second)) {
  144. return false;
  145. }
  146. ++this_class;
  147. ++other_class;
  148. }
  149. return (true);
  150. }
  151. } // namespace isc::dhcp
  152. } // namespace isc