Kea  2.5.3
cfg_option.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2023 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 
7 #ifndef CFG_OPTION_H
8 #define CFG_OPTION_H
9 
10 #include <dhcp/option.h>
12 #include <cc/cfg_to_element.h>
13 #include <cc/stamped_element.h>
14 #include <cc/user_context.h>
15 #include <dhcpsrv/cfg_option_def.h>
16 #include <dhcpsrv/key_from_key.h>
17 #include <boost/multi_index_container.hpp>
18 #include <boost/multi_index/hashed_index.hpp>
19 #include <boost/multi_index/ordered_index.hpp>
20 #include <boost/multi_index/sequenced_index.hpp>
21 #include <boost/multi_index/mem_fun.hpp>
22 #include <boost/multi_index/member.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include <stdint.h>
25 #include <list>
26 #include <string>
27 #include <vector>
28 
29 namespace isc {
30 namespace dhcp {
31 
32 class OptionDescriptor;
33 
35 typedef boost::shared_ptr<OptionDescriptor> OptionDescriptorPtr;
36 
38 typedef std::vector<OptionDescriptor> OptionDescriptorList;
39 
47 public:
50 
56 
63  bool cancelled_;
64 
78  std::string formatted_value_;
79 
89  std::string space_name_;
90 
98  OptionDescriptor(const OptionPtr& opt, bool persist, bool cancel,
99  const std::string& formatted_value = "",
101  : data::StampedElement(), option_(opt), persistent_(persist),
102  cancelled_(cancel), formatted_value_(formatted_value),
103  space_name_() {
104  setContext(user_context);
105  };
106 
111  OptionDescriptor(bool persist, bool cancel)
112  : data::StampedElement(), option_(OptionPtr()), persistent_(persist),
113  cancelled_(cancel), formatted_value_(), space_name_() {};
114 
119  : data::StampedElement(desc),
120  option_(desc.option_),
121  persistent_(desc.persistent_),
122  cancelled_(desc.cancelled_),
124  space_name_(desc.space_name_) {
125  setContext(desc.getContext());
126  };
127 
132  if (this != &other) {
133  // Not self-assignment.
134  data::StampedElement::operator=(other);
135  option_ = other.option_;
136  persistent_ = other.persistent_;
137  cancelled_ = other.cancelled_;
139  space_name_ = other.space_name_;
140  setContext(other.getContext());
141  }
142  return (*this);
143  }
144 
154  static OptionDescriptorPtr create(const OptionPtr& opt,
155  bool persist,
156  bool cancel,
157  const std::string& formatted_value = "",
158  data::ConstElementPtr user_context =
160 
167  static OptionDescriptorPtr create(bool persist, bool cancel);
168 
174  static OptionDescriptorPtr create(const OptionDescriptor& desc);
175 
181  bool equals(const OptionDescriptor& other) const;
182 
188  bool operator==(const OptionDescriptor& other) const {
189  return (equals(other));
190  }
191 
197  bool operator!=(const OptionDescriptor& other) const {
198  return (!equals(other));
199  }
200 };
201 
231 typedef boost::multi_index_container<
232  // Container comprises elements of OptionDescriptor type.
233  OptionDescriptor,
234  // Here we start enumerating various indexes.
235  boost::multi_index::indexed_by<
236  // Sequenced index allows accessing elements in the same way
237  // as elements in std::list.
238  // Sequenced is an index #0.
239  boost::multi_index::sequenced<>,
240  // Start definition of index #1.
241  boost::multi_index::hashed_non_unique<
242  // KeyFromKeyExtractor is the index key extractor that allows
243  // accessing option type being held by the OptionPtr through
244  // OptionDescriptor structure.
245  KeyFromKeyExtractor<
246  // Use option type as the index key. The type is held
247  // in OptionPtr object so we have to call Option::getType
248  // to retrieve this key for each element.
249  boost::multi_index::const_mem_fun<
250  Option,
251  uint16_t,
253  >,
254  // Indicate that OptionPtr is a member of
255  // OptionDescriptor structure.
256  boost::multi_index::member<
257  OptionDescriptor,
258  OptionPtr,
260  >
261  >
262  >,
263  // Start definition of index #2.
264  // Use 'persistent' struct member as a key.
265  boost::multi_index::hashed_non_unique<
266  boost::multi_index::member<
267  OptionDescriptor,
268  bool,
270  >
271  >,
272  // Start definition of index #3.
273  // Use BaseStampedElement::getModificationTime as a key.
274  boost::multi_index::ordered_non_unique<
275  boost::multi_index::const_mem_fun<
277  boost::posix_time::ptime,
279  >
280  >,
281 
282  // Start definition of index #4.
283  // Use BaseStampedElement::getId as a key.
284  boost::multi_index::hashed_non_unique<
285  boost::multi_index::tag<OptionIdIndexTag>,
286  boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
288  >,
289  // Start definition of index #5.
290  // Use 'cancelled' struct member as a key.
291  boost::multi_index::hashed_non_unique<
292  boost::multi_index::member<
293  OptionDescriptor,
294  bool,
296  >
297  >
298  >
300 
302 typedef boost::shared_ptr<OptionContainer> OptionContainerPtr;
304 typedef OptionContainer::nth_index<1>::type OptionContainerTypeIndex;
308 typedef std::pair<OptionContainerTypeIndex::const_iterator,
309  OptionContainerTypeIndex::const_iterator> OptionContainerTypeRange;
311 typedef OptionContainer::nth_index<2>::type OptionContainerPersistIndex;
315 typedef std::pair<OptionContainerPersistIndex::const_iterator,
316  OptionContainerPersistIndex::const_iterator> OptionContainerPersistRange;
318 typedef OptionContainer::nth_index<5>::type OptionContainerCancelIndex;
322 typedef std::pair<OptionContainerCancelIndex::const_iterator,
323  OptionContainerCancelIndex::const_iterator> OptionContainerCancelRange;
324 
352 public:
353 
355  CfgOption();
356 
360  bool empty() const;
361 
364 
365  bool equals(const CfgOption& other) const;
371 
377  bool operator==(const CfgOption& other) const {
378  return (equals(other));
379  }
380 
386  bool operator!=(const CfgOption& other) const {
387  return (!equals(other));
388  }
389 
391 
417  void add(const OptionPtr& option, const bool persistent,
418  const bool cancelled, const std::string& option_space,
419  const uint64_t id = 0);
420 
429  void add(const OptionDescriptor& desc, const std::string& option_space);
430 
444  void replace(const OptionDescriptor& desc, const std::string& option_space);
445 
465  void merge(CfgOptionDefPtr cfg_def, CfgOption& other);
466 
476  void createOptions(CfgOptionDefPtr cfg_def);
477 
518  static bool createDescriptorOption(CfgOptionDefPtr cfg_def, const std::string& space,
519  OptionDescriptor& opt_desc);
520 
529  void mergeTo(CfgOption& other) const;
530 
536  void copyTo(CfgOption& other) const;
537 
544  void encapsulate();
545 
549  bool isEncapsulated() const {
550  return (encapsulated_);
551  }
552 
563  OptionContainerPtr getAll(const std::string& option_space) const;
564 
571  OptionContainerPtr getAll(const uint32_t vendor_id) const;
572 
584  OptionContainerPtr getAllCombined(const std::string& option_space) const;
585 
603  template<typename Selector>
604  OptionDescriptor get(const Selector& key,
605  const uint16_t option_code) const {
606 
607  // Check for presence of options.
608  OptionContainerPtr options = getAll(key);
609  if (!options || options->empty()) {
610  return (OptionDescriptor(false, false));
611  }
612 
613  // Some options present, locate the one we are interested in.
614  const OptionContainerTypeIndex& idx = options->get<1>();
615  OptionContainerTypeIndex::const_iterator od_itr = idx.find(option_code);
616  if (od_itr == idx.end()) {
617  return (OptionDescriptor(false, false));
618  }
619 
620  return (*od_itr);
621  }
622 
634  template<typename Selector>
635  OptionDescriptorList getList(const Selector& key,
636  const uint16_t option_code) const {
637 
639  // Check for presence of options.
640  OptionContainerPtr options = getAll(key);
641  if (!options || options->empty()) {
642  return (list);
643  }
644 
645  // Some options present, locate the one we are interested in.
646  const OptionContainerTypeIndex& idx = options->get<1>();
647  OptionContainerTypeRange range = idx.equal_range(option_code);
648  // This code copies descriptors and can be optimized not doing this.
649  for (OptionContainerTypeIndex::const_iterator od_itr = range.first;
650  od_itr != range.second; ++od_itr) {
651  list.push_back(*od_itr);
652  }
653 
654  return (list);
655  }
656 
667  size_t del(const std::string& option_space, const uint16_t option_code);
668 
675  size_t del(const uint32_t vendor_id, const uint16_t option_code);
676 
698  size_t del(const uint64_t id);
699 
707  std::list<std::string> getOptionSpaceNames() const {
708  return (options_.getOptionSpaceNames());
709  }
710 
712  std::list<uint32_t> getVendorIds() const {
713  return (vendor_options_.getOptionSpaceNames());
714  }
715 
723  std::list<std::string> getVendorIdsSpaceNames() const;
724 
728  virtual isc::data::ElementPtr toElement() const;
729 
738  toElementWithMetadata(const bool include_metadata) const;
739 
740 private:
741 
753  void encapsulateInternal(const std::string& option_space);
754 
763  void encapsulateInternal(const OptionPtr& option);
764 
778  template <typename Selector>
779  void mergeInternal(const OptionSpaceContainer<OptionContainer,
780  OptionDescriptor, Selector>& src_container,
782  OptionDescriptor, Selector>& dest_container) const;
783 
785  bool encapsulated_;
786 
789  std::string> OptionSpaceCollection;
791  OptionSpaceCollection options_;
792 
795  uint32_t> VendorOptionSpaceCollection;
797  VendorOptionSpaceCollection vendor_options_;
798 };
799 
801 
802 typedef boost::shared_ptr<CfgOption> CfgOptionPtr;
804 
806 typedef boost::shared_ptr<const CfgOption> ConstCfgOptionPtr;
807 
809 typedef std::list<ConstCfgOptionPtr> CfgOptionList;
810 
812 
813 }
814 }
815 
816 #endif // CFG_OPTION_H
This class represents configuration element which is associated with database identifier and the modi...
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
uint64_t getId() const
Returns element's database identifier.
This class represents configuration element which is associated with database identifier,...
Represents option data configuration for the DHCP server.
Definition: cfg_option.h:351
bool isEncapsulated() const
Checks if options have been encapsulated.
Definition: cfg_option.h:549
OptionContainerPtr getAllCombined(const std::string &option_space) const
Returns all non-vendor or vendor options for the specified option space.
Definition: cfg_option.cc:351
void encapsulate()
Appends encapsulated options to top-level options.
Definition: cfg_option.cc:262
void replace(const OptionDescriptor &desc, const std::string &option_space)
Replaces the instance of an option within this collection.
Definition: cfg_option.cc:101
static bool createDescriptorOption(CfgOptionDefPtr cfg_def, const std::string &space, OptionDescriptor &opt_desc)
Creates an option descriptor's option based on a set of option defs.
Definition: cfg_option.cc:176
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: cfg_option.cc:446
void createOptions(CfgOptionDefPtr cfg_def)
Re-create the option in each descriptor based on given definitions.
Definition: cfg_option.cc:161
OptionDescriptor get(const Selector &key, const uint16_t option_code) const
Returns option for the specified key and option code.
Definition: cfg_option.h:604
isc::data::ElementPtr toElementWithMetadata(const bool include_metadata) const
Unparse a configuration object with optionally including the metadata.
Definition: cfg_option.cc:451
OptionDescriptorList getList(const Selector &key, const uint16_t option_code) const
Returns options for the specified key and option code.
Definition: cfg_option.h:635
size_t del(const std::string &option_space, const uint16_t option_code)
Deletes option for the specified option space and option code.
Definition: cfg_option.cc:360
bool empty() const
Indicates the object is empty.
Definition: cfg_option.cc:59
bool operator==(const CfgOption &other) const
Equality operator.
Definition: cfg_option.h:377
bool operator!=(const CfgOption &other) const
Inequality operator.
Definition: cfg_option.h:386
void mergeTo(CfgOption &other) const
Merges this configuration to another configuration.
Definition: cfg_option.cc:246
void copyTo(CfgOption &other) const
Copies this configuration to another configuration.
Definition: cfg_option.cc:254
std::list< std::string > getOptionSpaceNames() const
Returns a list of configured option space names.
Definition: cfg_option.h:707
CfgOption()
default constructor
Definition: cfg_option.cc:54
std::list< std::string > getVendorIdsSpaceNames() const
Returns a list of option space names for configured vendor ids.
Definition: cfg_option.cc:126
std::list< uint32_t > getVendorIds() const
Returns a list of all configured vendor identifiers.
Definition: cfg_option.h:712
OptionContainerPtr getAll(const std::string &option_space) const
Returns all options for the specified option space.
Definition: cfg_option.cc:341
void merge(CfgOptionDefPtr cfg_def, CfgOption &other)
Merges another option configuration into this one.
Definition: cfg_option.cc:140
bool equals(const CfgOption &other) const
Check if configuration is equal to other configuration.
Definition: cfg_option.cc:64
void add(const OptionPtr &option, const bool persistent, const bool cancelled, const std::string &option_space, const uint64_t id=0)
Adds instance of the option to the configuration.
Definition: cfg_option.cc:70
Option descriptor.
Definition: cfg_option.h:46
OptionPtr option_
Option instance.
Definition: cfg_option.h:49
OptionDescriptor & operator=(const OptionDescriptor &other)
Assignment operator.
Definition: cfg_option.h:131
bool operator!=(const OptionDescriptor &other) const
Inequality operator.
Definition: cfg_option.h:197
std::string space_name_
Option space name.
Definition: cfg_option.h:89
OptionDescriptor(const OptionPtr &opt, bool persist, bool cancel, const std::string &formatted_value="", data::ConstElementPtr user_context=data::ConstElementPtr())
Constructor.
Definition: cfg_option.h:98
bool equals(const OptionDescriptor &other) const
Checks if the one descriptor is equal to another.
Definition: cfg_option.cc:46
bool cancelled_
Cancelled flag.
Definition: cfg_option.h:63
std::string formatted_value_
Option value in textual (CSV) format.
Definition: cfg_option.h:78
static OptionDescriptorPtr create(const OptionPtr &opt, bool persist, bool cancel, const std::string &formatted_value="", data::ConstElementPtr user_context=data::ConstElementPtr())
Factory function creating an instance of the OptionDescriptor.
Definition: cfg_option.cc:27
OptionDescriptor(bool persist, bool cancel)
Constructor.
Definition: cfg_option.h:111
bool operator==(const OptionDescriptor &other) const
Equality operator.
Definition: cfg_option.h:188
OptionDescriptor(const OptionDescriptor &desc)
Copy constructor.
Definition: cfg_option.h:118
bool persistent_
Persistence flag.
Definition: cfg_option.h:55
Simple container for option spaces holding various items.
std::list< Selector > getOptionSpaceNames() const
Get a list of existing option spaces.
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:26
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:803
std::pair< OptionContainerTypeIndex::const_iterator, OptionContainerTypeIndex::const_iterator > OptionContainerTypeRange
Pair of iterators to represent the range of options having the same option type value.
Definition: cfg_option.h:309
OptionContainer::nth_index< 1 >::type OptionContainerTypeIndex
Type of the index #1 - option type.
Definition: cfg_option.h:304
std::map< std::string, OptionSpacePtr > OptionSpaceCollection
A collection of option spaces.
Definition: option_space.h:34
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
std::vector< OptionDescriptor > OptionDescriptorList
A list of option descriptors.
Definition: cfg_option.h:38
OptionContainer::nth_index< 5 >::type OptionContainerCancelIndex
Type of the index #5 - option cancellation flag.
Definition: cfg_option.h:318
std::pair< OptionContainerPersistIndex::const_iterator, OptionContainerPersistIndex::const_iterator > OptionContainerPersistRange
Pair of iterators to represent the range of options having the same persistency flag.
Definition: cfg_option.h:316
boost::shared_ptr< OptionDescriptor > OptionDescriptorPtr
A pointer to option descriptor.
Definition: cfg_option.h:32
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
Definition: cfg_option.h:302
OptionContainer::nth_index< 2 >::type OptionContainerPersistIndex
Type of the index #2 - option persistency flag.
Definition: cfg_option.h:311
boost::multi_index_container< OptionDescriptor, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_non_unique< KeyFromKeyExtractor< boost::multi_index::const_mem_fun< Option, uint16_t, &Option::getType >, boost::multi_index::member< OptionDescriptor, OptionPtr, &OptionDescriptor::option_ > > >, boost::multi_index::hashed_non_unique< boost::multi_index::member< OptionDescriptor, bool, &OptionDescriptor::persistent_ > >, boost::multi_index::ordered_non_unique< boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< OptionIdIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, uint64_t, &data::BaseStampedElement::getId > >, boost::multi_index::hashed_non_unique< boost::multi_index::member< OptionDescriptor, bool, &OptionDescriptor::cancelled_ > > >> OptionContainer
Multi index container for DHCP option descriptors.
Definition: cfg_option.h:299
std::pair< OptionContainerCancelIndex::const_iterator, OptionContainerCancelIndex::const_iterator > OptionContainerCancelRange
Pair of iterators to represent the range of options having the same cancellation flag.
Definition: cfg_option.h:323
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition: cfg_option.h:809
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:806
Defines the logger used by the top-level component of kea-lfc.
Abstract class for configuration Cfg_* classes.
Base class for user context.
Definition: user_context.h:22
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
Definition: user_context.h:24
void setContext(const data::ConstElementPtr &ctx)
Sets user context.
Definition: user_context.h:30