Kea  2.3.7
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 
556  OptionContainerPtr getAll(const std::string& option_space) const;
557 
564  OptionContainerPtr getAll(const uint32_t vendor_id) const;
565 
583  template<typename Selector>
584  OptionDescriptor get(const Selector& key,
585  const uint16_t option_code) const {
586 
587  // Check for presence of options.
588  OptionContainerPtr options = getAll(key);
589  if (!options || options->empty()) {
590  return (OptionDescriptor(false, false));
591  }
592 
593  // Some options present, locate the one we are interested in.
594  const OptionContainerTypeIndex& idx = options->get<1>();
595  OptionContainerTypeIndex::const_iterator od_itr = idx.find(option_code);
596  if (od_itr == idx.end()) {
597  return (OptionDescriptor(false, false));
598  }
599 
600  return (*od_itr);
601  }
602 
614  template<typename Selector>
615  OptionDescriptorList getList(const Selector& key,
616  const uint16_t option_code) const {
617 
619  // Check for presence of options.
620  OptionContainerPtr options = getAll(key);
621  if (!options || options->empty()) {
622  return (list);
623  }
624 
625  // Some options present, locate the one we are interested in.
626  const OptionContainerTypeIndex& idx = options->get<1>();
627  OptionContainerTypeRange range = idx.equal_range(option_code);
628  // This code copies descriptors and can be optimized not doing this.
629  for (OptionContainerTypeIndex::const_iterator od_itr = range.first;
630  od_itr != range.second; ++od_itr) {
631  list.push_back(*od_itr);
632  }
633 
634  return (list);
635  }
636 
647  size_t del(const std::string& option_space, const uint16_t option_code);
648 
655  size_t del(const uint32_t vendor_id, const uint16_t option_code);
656 
678  size_t del(const uint64_t id);
679 
687  std::list<std::string> getOptionSpaceNames() const {
688  return (options_.getOptionSpaceNames());
689  }
690 
692  std::list<uint32_t> getVendorIds() const {
693  return (vendor_options_.getOptionSpaceNames());
694  }
695 
703  std::list<std::string> getVendorIdsSpaceNames() const;
704 
708  virtual isc::data::ElementPtr toElement() const;
709 
718  toElementWithMetadata(const bool include_metadata) const;
719 
720 private:
721 
733  void encapsulateInternal(const std::string& option_space);
734 
743  void encapsulateInternal(const OptionPtr& option);
744 
758  template <typename Selector>
759  void mergeInternal(const OptionSpaceContainer<OptionContainer,
760  OptionDescriptor, Selector>& src_container,
762  OptionDescriptor, Selector>& dest_container) const;
763 
766  std::string> OptionSpaceCollection;
768  OptionSpaceCollection options_;
769 
772  uint32_t> VendorOptionSpaceCollection;
774  VendorOptionSpaceCollection vendor_options_;
775 };
776 
778 
779 typedef boost::shared_ptr<CfgOption> CfgOptionPtr;
781 
783 typedef boost::shared_ptr<const CfgOption> ConstCfgOptionPtr;
784 
786 typedef std::list<ConstCfgOptionPtr> CfgOptionList;
787 
789 
790 }
791 }
792 
793 #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
void encapsulate()
Appends encapsulated options to top-level options.
Definition: cfg_option.cc:250
void replace(const OptionDescriptor &desc, const std::string &option_space)
Replaces the instance of an option within this collection.
Definition: cfg_option.cc:100
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:169
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: cfg_option.cc:424
void createOptions(CfgOptionDefPtr cfg_def)
Re-create the option in each descriptor based on given definitions.
Definition: cfg_option.cc:154
OptionDescriptor get(const Selector &key, const uint16_t option_code) const
Returns option for the specified key and option code.
Definition: cfg_option.h:584
isc::data::ElementPtr toElementWithMetadata(const bool include_metadata) const
Unparse a configuration object with optionally including the metadata.
Definition: cfg_option.cc:429
OptionDescriptorList getList(const Selector &key, const uint16_t option_code) const
Returns options for the specified key and option code.
Definition: cfg_option.h:615
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:338
bool empty() const
Indicates the object is empty.
Definition: cfg_option.cc:58
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:234
void copyTo(CfgOption &other) const
Copies this configuration to another configuration.
Definition: cfg_option.cc:242
std::list< std::string > getOptionSpaceNames() const
Returns a list of configured option space names.
Definition: cfg_option.h:687
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:125
std::list< uint32_t > getVendorIds() const
Returns a list of all configured vendor identifiers.
Definition: cfg_option.h:692
OptionContainerPtr getAll(const std::string &option_space) const
Returns all options for the specified option space.
Definition: cfg_option.cc:328
void merge(CfgOptionDefPtr cfg_def, CfgOption &other)
Merges another option configuration into this one.
Definition: cfg_option.cc:139
bool equals(const CfgOption &other) const
Check if configuration is equal to other configuration.
Definition: cfg_option.cc:63
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:69
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:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:780
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:786
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:783
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