Kea  2.3.3-git
cfg_option.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2022 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 
33 
35 typedef boost::shared_ptr<OptionDescriptor> OptionDescriptorPtr;
36 
38 typedef std::vector<OptionDescriptor> OptionDescriptorList;
39 
47 public:
50 
56 
70  std::string formatted_value_;
71 
81  std::string space_name_;
82 
89  OptionDescriptor(const OptionPtr& opt, bool persist,
90  const std::string& formatted_value = "",
92  : data::StampedElement(), option_(opt), persistent_(persist),
93  formatted_value_(formatted_value),
94  space_name_() {
95  setContext(user_context);
96  };
97 
101  OptionDescriptor(bool persist)
102  : data::StampedElement(), option_(OptionPtr()), persistent_(persist),
103  formatted_value_(), space_name_() {};
104 
109  : data::StampedElement(desc),
110  option_(desc.option_),
111  persistent_(desc.persistent_),
112  formatted_value_(desc.formatted_value_),
113  space_name_(desc.space_name_) {
114  setContext(desc.getContext());
115  };
116 
121  if (this != &other) {
122  // Not self-assignment.
123  data::StampedElement::operator=(other);
124  option_ = other.option_;
125  persistent_ = other.persistent_;
126  formatted_value_ = other.formatted_value_;
127  space_name_ = other.space_name_;
128  setContext(other.getContext());
129  }
130  return (*this);
131  }
132 
141  static OptionDescriptorPtr create(const OptionPtr& opt,
142  bool persist,
143  const std::string& formatted_value = "",
144  data::ConstElementPtr user_context =
146 
152  static OptionDescriptorPtr create(bool persist);
153 
159  static OptionDescriptorPtr create(const OptionDescriptor& desc);
160 
166  bool equals(const OptionDescriptor& other) const;
167 
173  bool operator==(const OptionDescriptor& other) const {
174  return (equals(other));
175  }
176 
182  bool operator!=(const OptionDescriptor& other) const {
183  return (!equals(other));
184  }
185 };
186 
214 typedef boost::multi_index_container<
215  // Container comprises elements of OptionDescriptor type.
217  // Here we start enumerating various indexes.
218  boost::multi_index::indexed_by<
219  // Sequenced index allows accessing elements in the same way
220  // as elements in std::list.
221  // Sequenced is an index #0.
222  boost::multi_index::sequenced<>,
223  // Start definition of index #1.
224  boost::multi_index::hashed_non_unique<
225  // KeyFromKeyExtractor is the index key extractor that allows
226  // accessing option type being held by the OptionPtr through
227  // OptionDescriptor structure.
229  // Use option type as the index key. The type is held
230  // in OptionPtr object so we have to call Option::getType
231  // to retrieve this key for each element.
232  boost::multi_index::const_mem_fun<
233  Option,
234  uint16_t,
236  >,
237  // Indicate that OptionPtr is a member of
238  // OptionDescriptor structure.
239  boost::multi_index::member<
241  OptionPtr,
243  >
244  >
245  >,
246  // Start definition of index #2.
247  // Use 'persistent' struct member as a key.
248  boost::multi_index::hashed_non_unique<
249  boost::multi_index::member<
251  bool,
253  >
254  >,
255  // Start definition of index #3.
256  // Use BaseStampedElement::getModificationTime as a key.
257  boost::multi_index::ordered_non_unique<
258  boost::multi_index::const_mem_fun<
260  boost::posix_time::ptime,
262  >
263  >,
264 
265  // Start definition of index #4.
266  // Use BaseStampedElement::getId as a key.
267  boost::multi_index::hashed_non_unique<
268  boost::multi_index::tag<OptionIdIndexTag>,
269  boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
271  >
272  >
274 
276 typedef boost::shared_ptr<OptionContainer> OptionContainerPtr;
278 typedef OptionContainer::nth_index<1>::type OptionContainerTypeIndex;
282 typedef std::pair<OptionContainerTypeIndex::const_iterator,
283  OptionContainerTypeIndex::const_iterator> OptionContainerTypeRange;
285 typedef OptionContainer::nth_index<2>::type OptionContainerPersistIndex;
289 typedef std::pair<OptionContainerPersistIndex::const_iterator,
290  OptionContainerPersistIndex::const_iterator> OptionContainerPersistRange;
291 
319 public:
320 
322  CfgOption();
323 
327  bool empty() const;
328 
331 
332  bool equals(const CfgOption& other) const;
338 
344  bool operator==(const CfgOption& other) const {
345  return (equals(other));
346  }
347 
353  bool operator!=(const CfgOption& other) const {
354  return (!equals(other));
355  }
356 
358 
382  void add(const OptionPtr& option, const bool persistent,
383  const std::string& option_space,
384  const uint64_t id = 0);
385 
394  void add(const OptionDescriptor& desc, const std::string& option_space);
395 
409  void replace(const OptionDescriptor& desc, const std::string& option_space);
410 
430  void merge(CfgOptionDefPtr cfg_def, CfgOption& other);
431 
441  void createOptions(CfgOptionDefPtr cfg_def);
442 
483  static bool createDescriptorOption(CfgOptionDefPtr cfg_def, const std::string& space,
484  OptionDescriptor& opt_desc);
485 
494  void mergeTo(CfgOption& other) const;
495 
501  void copyTo(CfgOption& other) const;
502 
509  void encapsulate();
510 
521  OptionContainerPtr getAll(const std::string& option_space) const;
522 
529  OptionContainerPtr getAll(const uint32_t vendor_id) const;
530 
548  template<typename Selector>
549  OptionDescriptor get(const Selector& key,
550  const uint16_t option_code) const {
551 
552  // Check for presence of options.
553  OptionContainerPtr options = getAll(key);
554  if (!options || options->empty()) {
555  return (OptionDescriptor(false));
556  }
557 
558  // Some options present, locate the one we are interested in.
559  const OptionContainerTypeIndex& idx = options->get<1>();
560  OptionContainerTypeIndex::const_iterator od_itr = idx.find(option_code);
561  if (od_itr == idx.end()) {
562  return (OptionDescriptor(false));
563  }
564 
565  return (*od_itr);
566  }
567 
579  template<typename Selector>
580  OptionDescriptorList getList(const Selector& key,
581  const uint16_t option_code) const {
582 
583  OptionDescriptorList list;
584  // Check for presence of options.
585  OptionContainerPtr options = getAll(key);
586  if (!options || options->empty()) {
587  return (list);
588  }
589 
590  // Some options present, locate the one we are interested in.
591  const OptionContainerTypeIndex& idx = options->get<1>();
592  OptionContainerTypeRange range = idx.equal_range(option_code);
593  // This code copies descriptors and can be optimized not doing this.
594  for (OptionContainerTypeIndex::const_iterator od_itr = range.first;
595  od_itr != range.second; ++od_itr) {
596  list.push_back(*od_itr);
597  }
598 
599  return (list);
600  }
601 
612  size_t del(const std::string& option_space, const uint16_t option_code);
613 
620  size_t del(const uint32_t vendor_id, const uint16_t option_code);
621 
643  size_t del(const uint64_t id);
644 
652  std::list<std::string> getOptionSpaceNames() const {
653  return (options_.getOptionSpaceNames());
654  }
655 
657  std::list<uint32_t> getVendorIds() const {
658  return (vendor_options_.getOptionSpaceNames());
659  }
660 
668  std::list<std::string> getVendorIdsSpaceNames() const;
669 
673  virtual isc::data::ElementPtr toElement() const;
674 
683  toElementWithMetadata(const bool include_metadata) const;
684 
685 private:
686 
698  void encapsulateInternal(const std::string& option_space);
699 
708  void encapsulateInternal(const OptionPtr& option);
709 
723  template <typename Selector>
724  void mergeInternal(const OptionSpaceContainer<OptionContainer,
725  OptionDescriptor, Selector>& src_container,
727  OptionDescriptor, Selector>& dest_container) const;
728 
731  std::string> OptionSpaceCollection;
733  OptionSpaceCollection options_;
734 
737  uint32_t> VendorOptionSpaceCollection;
739  VendorOptionSpaceCollection vendor_options_;
740 };
741 
743 
744 typedef boost::shared_ptr<CfgOption> CfgOptionPtr;
746 
748 typedef boost::shared_ptr<const CfgOption> ConstCfgOptionPtr;
749 
751 typedef std::list<ConstCfgOptionPtr> CfgOptionList;
752 
754 
755 }
756 }
757 
758 #endif // CFG_OPTION_H
bool equals(const OptionDescriptor &other) const
Checks if the one descriptor is equal to another.
Definition: cfg_option.cc:45
Simple container for option spaces holding various items.
OptionDescriptor(const OptionDescriptor &desc)
Copy constructor.
Definition: cfg_option.h:108
Option descriptor.
Definition: cfg_option.h:46
void setContext(const data::ConstElementPtr &ctx)
Sets user context.
Definition: user_context.h:30
OptionContainer::nth_index< 1 >::type OptionContainerTypeIndex
Type of the index #1 - option type.
Definition: cfg_option.h:278
This class represents configuration element which is associated with database identifier, modification timestamp and servers.
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:745
data::ConstElementPtr getContext() const
Returns const pointer to the user context.
Definition: user_context.h:24
boost::shared_ptr< OptionDescriptor > OptionDescriptorPtr
A pointer to option descriptor.
Definition: cfg_option.h:32
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:293
OptionDescriptor(const OptionPtr &opt, bool persist, const std::string &formatted_value="", data::ConstElementPtr user_context=data::ConstElementPtr())
Constructor.
Definition: cfg_option.h:89
Base class for user context.
Definition: user_context.h:22
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:748
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
static OptionDescriptorPtr create(const OptionPtr &opt, bool persist, const std::string &formatted_value="", data::ConstElementPtr user_context=data::ConstElementPtr())
Factory function creating an instance of the OptionDescriptor.
Definition: cfg_option.cc:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
std::list< uint32_t > getVendorIds() const
Returns a list of all configured vendor identifiers.
Definition: cfg_option.h:657
bool operator==(const CfgOption &other) const
Equality operator.
Definition: cfg_option.h:344
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
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:283
OptionDescriptor & operator=(const OptionDescriptor &other)
Assignment operator.
Definition: cfg_option.h:120
std::string formatted_value_
Option value in textual (CSV) format.
Definition: cfg_option.h:70
Represents option data configuration for the DHCP server.
Definition: cfg_option.h:318
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition: cfg_option.h:751
bool operator!=(const CfgOption &other) const
Inequality operator.
Definition: cfg_option.h:353
bool persistent_
Persistence flag.
Definition: cfg_option.h:55
Abstract class for configuration Cfg_* classes.
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:290
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
uint64_t getId() const
Returns element&#39;s database identifier.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
static data::ElementPtr toElement(data::ConstElementPtr map)
Copy an Element map.
Definition: user_context.cc:24
std::list< std::string > getOptionSpaceNames() const
Returns a list of configured option space names.
Definition: cfg_option.h:652
std::string space_name_
Option space name.
Definition: cfg_option.h:81
OptionPtr option_
Option instance.
Definition: cfg_option.h:49
Defines the logger used by the top-level component of kea-lfc.
OptionDescriptor(bool persist)
Constructor.
Definition: cfg_option.h:101
bool operator!=(const OptionDescriptor &other) const
Inequality operator.
Definition: cfg_option.h:182
void merge(ElementPtr element, ConstElementPtr other)
Merges the data from other into element.
Definition: data.cc:1139
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 > > >> OptionContainer
Multi index container for DHCP option descriptors.
Definition: cfg_option.h:273
This class represents configuration element which is associated with database identifier and the modi...
bool operator==(const OptionDescriptor &other) const
Equality operator.
Definition: cfg_option.h:173
OptionDescriptorList getList(const Selector &key, const uint16_t option_code) const
Returns options for the specified key and option code.
Definition: cfg_option.h:580
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
Definition: cfg_option.h:276
std::map< std::string, OptionSpacePtr > OptionSpaceCollection
A collection of option spaces.
Definition: option_space.h:34
OptionContainer::nth_index< 2 >::type OptionContainerPersistIndex
Type of the index #2 - option persistency flag.
Definition: cfg_option.h:285
std::vector< OptionDescriptor > OptionDescriptorList
A list of option descriptors.
Definition: cfg_option.h:38
Utility class which cascades two key extractors.
Definition: key_from_key.h:45