client_class_def.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #ifndef CLIENT_CLASS_DEF_H
  15. #define CLIENT_CLASS_DEF_H
  16. #include <dhcpsrv/cfg_option.h>
  17. #include <eval/token.h>
  18. #include <exceptions/exceptions.h>
  19. #include <string>
  20. /// @file client_class_def.h
  21. ///
  22. /// @brief Defines classes for storing client class definitions
  23. ///
  24. /// The file defines the class, ClientClassDef, which houses the
  25. /// information for single client class such as the class name, the
  26. /// logical expression used to identify members of the class, and options
  27. /// that may be attributed to class members.
  28. ///
  29. /// In addition it defines a container class, ClientClassDictionary, which
  30. /// is houses class definitions keyed by class name.
  31. ///
  32. namespace isc {
  33. namespace dhcp {
  34. /// @brief Error that occurs when an attempt is made to add a duplicate class
  35. /// to a class dictionary.
  36. class DuplicateClientClassDef : public isc::Exception {
  37. public:
  38. DuplicateClientClassDef(const char* file, size_t line, const char* what)
  39. : isc::Exception(file, line, what) {}
  40. };
  41. /// @brief Embodies a single client class definition
  42. class ClientClassDef {
  43. public:
  44. /// @brief Constructor
  45. ///
  46. /// @param name Name to assign to this class
  47. /// @param match_expr Expression the class will use to determine membership
  48. /// @param options Collection of options members should be given
  49. ClientClassDef(const std::string& name, const ExpressionPtr& match_expr,
  50. const CfgOptionPtr& options = CfgOptionPtr());
  51. /// Copy constructor
  52. ClientClassDef(const ClientClassDef& rhs);
  53. /// @brief Destructor
  54. virtual ~ClientClassDef();
  55. /// @brief Fetches the class's name
  56. std::string getName() const;
  57. /// @brief Sets the class's name
  58. ///
  59. /// @param name the name to assign the class
  60. void setName(const std::string& name);
  61. /// @brief Fetches the class's match expression
  62. const ExpressionPtr& getMatchExpr() const;
  63. /// @brief Sets the class's match expression
  64. ///
  65. /// @param match_expr the expression to assign the class
  66. void setMatchExpr(const ExpressionPtr& match_expr);
  67. /// @brief Fetches the class's option collection
  68. const CfgOptionPtr& getCfgOption() const;
  69. /// @brief Sets the class's option collection
  70. ///
  71. /// @param options the option collection to assign the class
  72. void setCfgOption(const CfgOptionPtr& cfg_option);
  73. /// @brief Compares two @c ClientClassDef objects for equality.
  74. ///
  75. /// @param other Other client class definition to compare to.
  76. ///
  77. /// @return true if objects are equal, false otherwise.
  78. bool equals(const ClientClassDef& other) const;
  79. /// @brief Equality operator.
  80. ///
  81. /// @param other Other client class definition to compare to.
  82. ///
  83. /// @return true if the definitions equal, false otherwise.
  84. bool operator==(const ClientClassDef& other) const {
  85. return (equals(other));
  86. }
  87. /// @brief Inequality operator.
  88. ///
  89. /// @param other Other client class definition to compare to.
  90. ///
  91. /// @return true if the definitions are not equal, false otherwise.
  92. bool operator!=(const ClientClassDef& other) const {
  93. return (!(equals(other)));
  94. }
  95. /// @brief Provides a convenient text representation of the class
  96. friend std::ostream& operator<<(std::ostream& os, const ClientClassDef& x);
  97. private:
  98. /// @brief Unique text identifier by which this class is known.
  99. std::string name_;
  100. /// @brief The logical expression which deteremines membership in
  101. /// this class.
  102. ExpressionPtr match_expr_;
  103. /// @brief The option data configuration for this class
  104. CfgOptionPtr cfg_option_;
  105. };
  106. /// @brief a pointer to an ClientClassDef
  107. typedef boost::shared_ptr<ClientClassDef> ClientClassDefPtr;
  108. /// @brief Defines a map of ClientClassDef's, keyed by the class name.
  109. typedef std::map<std::string,ClientClassDefPtr> ClientClassDefMap;
  110. /// @brief Defines a pointer to a ClientClassDictionary
  111. typedef boost::shared_ptr<ClientClassDefMap> ClientClassDefMapPtr;
  112. /// @brief Defines a pair for working wiht ClientClassMap
  113. typedef std::pair<std::string, ClientClassDefPtr> ClientClassMapPair;
  114. /// @brief Maintains a list of ClientClassDef's
  115. class ClientClassDictionary {
  116. public:
  117. /// @brief Constructor
  118. ClientClassDictionary();
  119. ClientClassDictionary(const ClientClassDictionary& rhs);
  120. /// @brief Destructor
  121. ~ClientClassDictionary();
  122. /// @brief Adds a new class to the list
  123. ///
  124. /// @param name Name to assign to this class
  125. /// @param match_expr Expression the class will use to determine membership
  126. /// @param options Collection of options members should be given
  127. ///
  128. /// @throw DuplicateClientClassDef if class already exists within the
  129. /// dictionary. See @ref dhcp::ClientClassDef::ClientClassDef() for
  130. /// others.
  131. void addClass(const std::string& name, const ExpressionPtr& match_expr,
  132. const CfgOptionPtr& options);
  133. /// @brief Adds a new class to the list
  134. ///
  135. /// @param class_def pointer to class definition to add
  136. ///
  137. /// @throw DuplicateClientClassDef if class already exists within the
  138. /// dictionary, BadValue if the pointer is empty.
  139. void addClass(ClientClassDefPtr& class_def);
  140. /// @brief Fetches the class definition for a given class name
  141. ///
  142. /// @param name the name of the desired class
  143. ///
  144. /// @return ClientClassDefPtr to the desired class if found, or
  145. /// an empty pointer if not.
  146. ClientClassDefPtr findClass(const std::string& name) const;
  147. /// @brief Removes a given class definition from the dictionary
  148. ///
  149. /// Removes the class defintion from the map if it exists, otherwise
  150. /// no harm, no foul.
  151. ///
  152. /// @param name the name of the class to remove
  153. void removeClass(const std::string& name);
  154. /// @brief Fetches the dictionary's map of classes
  155. ///
  156. /// @return ClientClassDefMapPtr to the map of classes
  157. const ClientClassDefMapPtr& getClasses() const;
  158. /// @brief Compares two @c ClientClassDictionary objects for equality.
  159. ///
  160. /// @param other Other client class definition to compare to.
  161. ///
  162. /// @return true if descriptors equal, false otherwise.
  163. bool equals(const ClientClassDictionary& other) const;
  164. /// @brief Equality operator.
  165. ///
  166. /// @param other Other client class dictionary to compare to.
  167. ///
  168. /// @return true if the dictionaries are equal, false otherwise.
  169. bool operator==(const ClientClassDictionary& other) const {
  170. return (equals(other));
  171. }
  172. /// @brief Inequality operator.
  173. ///
  174. /// @param other Other client class dictionary to compare to.
  175. ///
  176. /// @return true if the dictionaries are not equal, false otherwise.
  177. bool operator!=(const ClientClassDictionary& other) const {
  178. return (!equals(other));
  179. }
  180. private:
  181. /// @brief Map of the class definitions
  182. ClientClassDefMapPtr classes_;
  183. };
  184. /// @brief Defines a pointer to a ClientClassDictionary
  185. typedef boost::shared_ptr<ClientClassDictionary> ClientClassDictionaryPtr;
  186. } // namespace isc::dhcp
  187. } // namespace isc
  188. #endif // CLIENT_CLASS_DEF_H