client_class_def.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. namespace isc {
  16. namespace dhcp {
  17. //********** ClientClassDef ******************//
  18. ClientClassDef::ClientClassDef(const std::string& name,
  19. const ExpressionPtr& match_expr,
  20. const OptionCollectionPtr& options)
  21. : name_(name), match_expr_(match_expr), options_(options) {
  22. // Name can't be blank
  23. if (name_.empty()) {
  24. isc_throw(BadValue, "ClientClassDef name cannot be empty");
  25. }
  26. // @todo Does it make sense for a class to NOT have match expression?
  27. // For classes without options, make sure we have an empty collection
  28. if (!options_) {
  29. options_.reset(new OptionCollection());
  30. }
  31. }
  32. ClientClassDef::~ClientClassDef() {
  33. }
  34. std::string
  35. ClientClassDef::getName() const {
  36. return (name_);
  37. }
  38. void
  39. ClientClassDef::setName(const std::string& name) {
  40. name_ = name;
  41. }
  42. const ExpressionPtr&
  43. ClientClassDef::getMatchExpr() const {
  44. return (match_expr_);
  45. }
  46. void
  47. ClientClassDef::setMatchExpr(const ExpressionPtr& match_expr) {
  48. match_expr_ = match_expr;
  49. }
  50. const OptionCollectionPtr&
  51. ClientClassDef::getOptions() const {
  52. return (options_);
  53. }
  54. void
  55. ClientClassDef::setOptions(const OptionCollectionPtr& options) {
  56. options_ = options;
  57. }
  58. OptionPtr
  59. ClientClassDef::findOption(uint16_t option_code) const {
  60. if (options_) {
  61. isc::dhcp::OptionCollection::iterator it = options_->find(option_code);
  62. if (it != options_->end()) {
  63. return ((*it).second);
  64. }
  65. }
  66. return (OptionPtr());
  67. }
  68. std::ostream& operator<<(std::ostream& os, const ClientClassDef& x) {
  69. os << "ClientClassDef:" << x.getName();
  70. return (os);
  71. }
  72. //********** ClientClassDictionary ******************//
  73. ClientClassDictionary::ClientClassDictionary()
  74. : classes_(new ClientClassDefMap()) {
  75. }
  76. ClientClassDictionary::~ClientClassDictionary() {
  77. }
  78. void
  79. ClientClassDictionary::addClass(const std::string& name,
  80. const ExpressionPtr& match_expr,
  81. const OptionCollectionPtr& options) {
  82. ClientClassDefPtr cclass(new ClientClassDef(name, match_expr, options));
  83. addClass(cclass);
  84. }
  85. void
  86. ClientClassDictionary::addClass(ClientClassDefPtr& class_def) {
  87. if (!class_def) {
  88. isc_throw(BadValue, "ClientClassDictionary::addClass "
  89. " - class definition cannot be null");
  90. }
  91. if (findClass(class_def->getName())) {
  92. isc_throw(DuplicateClientClassDef, "Client Class: "
  93. << class_def->getName() << " has already been defined");
  94. }
  95. (*classes_)[class_def->getName()] = class_def;
  96. }
  97. ClientClassDefPtr
  98. ClientClassDictionary::findClass(const std::string& name) const {
  99. ClientClassDefMap::iterator it = classes_->find(name);
  100. if (it != classes_->end()) {
  101. return (*it).second;
  102. }
  103. return(ClientClassDefPtr());
  104. }
  105. void
  106. ClientClassDictionary::removeClass(const std::string& name) {
  107. classes_->erase(name);
  108. }
  109. const ClientClassDefMapPtr&
  110. ClientClassDictionary::getClasses() const {
  111. return (classes_);
  112. }
  113. } // namespace isc::dhcp
  114. } // namespace isc