Kea  2.1.7-git
cfg_option.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2021 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 <string>
26 #include <list>
27 
28 namespace isc {
29 namespace dhcp {
30 
32 
34 typedef boost::shared_ptr<OptionDescriptor> OptionDescriptorPtr;
35 
43 public:
46 
52 
66  std::string formatted_value_;
67 
77  std::string space_name_;
78 
85  OptionDescriptor(const OptionPtr& opt, bool persist,
86  const std::string& formatted_value = "",
88  : data::StampedElement(), option_(opt), persistent_(persist),
89  formatted_value_(formatted_value),
90  space_name_() {
91  setContext(user_context);
92  };
93 
97  OptionDescriptor(bool persist)
98  : data::StampedElement(), option_(OptionPtr()), persistent_(persist),
99  formatted_value_(), space_name_() {};
100 
105  : data::StampedElement(desc),
106  option_(desc.option_),
107  persistent_(desc.persistent_),
108  formatted_value_(desc.formatted_value_),
109  space_name_(desc.space_name_) {
110  setContext(desc.getContext());
111  };
112 
117  if (this != &other) {
118  // Not self-assignment.
119  data::StampedElement::operator=(other);
120  option_ = other.option_;
121  persistent_ = other.persistent_;
122  formatted_value_ = other.formatted_value_;
123  space_name_ = other.space_name_;
124  setContext(other.getContext());
125  }
126  return (*this);
127  }
128 
137  static OptionDescriptorPtr create(const OptionPtr& opt,
138  bool persist,
139  const std::string& formatted_value = "",
140  data::ConstElementPtr user_context =
142 
148  static OptionDescriptorPtr create(bool persist);
149 
155  static OptionDescriptorPtr create(const OptionDescriptor& desc);
156 
162  bool equals(const OptionDescriptor& other) const;
163 
169  bool operator==(const OptionDescriptor& other) const {
170  return (equals(other));
171  }
172 
178  bool operator!=(const OptionDescriptor& other) const {
179  return (!equals(other));
180  }
181 };
182 
210 typedef boost::multi_index_container<
211  // Container comprises elements of OptionDescriptor type.
213  // Here we start enumerating various indexes.
214  boost::multi_index::indexed_by<
215  // Sequenced index allows accessing elements in the same way
216  // as elements in std::list.
217  // Sequenced is an index #0.
218  boost::multi_index::sequenced<>,
219  // Start definition of index #1.
220  boost::multi_index::hashed_non_unique<
221  // KeyFromKeyExtractor is the index key extractor that allows
222  // accessing option type being held by the OptionPtr through
223  // OptionDescriptor structure.
225  // Use option type as the index key. The type is held
226  // in OptionPtr object so we have to call Option::getType
227  // to retrieve this key for each element.
228  boost::multi_index::const_mem_fun<
229  Option,
230  uint16_t,
232  >,
233  // Indicate that OptionPtr is a member of
234  // OptionDescriptor structure.
235  boost::multi_index::member<
237  OptionPtr,
239  >
240  >
241  >,
242  // Start definition of index #2.
243  // Use 'persistent' struct member as a key.
244  boost::multi_index::hashed_non_unique<
245  boost::multi_index::member<
247  bool,
249  >
250  >,
251  // Start definition of index #3.
252  // Use BaseStampedElement::getModificationTime as a key.
253  boost::multi_index::ordered_non_unique<
254  boost::multi_index::const_mem_fun<
256  boost::posix_time::ptime,
258  >
259  >,
260 
261  // Start definition of index #4.
262  // Use BaseStampedElement::getId as a key.
263  boost::multi_index::hashed_non_unique<
264  boost::multi_index::tag<OptionIdIndexTag>,
265  boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
267  >
268  >
270 
272 typedef boost::shared_ptr<OptionContainer> OptionContainerPtr;
274 typedef OptionContainer::nth_index<1>::type OptionContainerTypeIndex;
278 typedef std::pair<OptionContainerTypeIndex::const_iterator,
279  OptionContainerTypeIndex::const_iterator> OptionContainerTypeRange;
281 typedef OptionContainer::nth_index<2>::type OptionContainerPersistIndex;
285 typedef std::pair<OptionContainerPersistIndex::const_iterator,
286  OptionContainerPersistIndex::const_iterator> OptionContainerPersistRange;
287 
315 public:
316 
318  CfgOption();
319 
323  bool empty() const;
324 
327 
328  bool equals(const CfgOption& other) const;
334 
340  bool operator==(const CfgOption& other) const {
341  return (equals(other));
342  }
343 
349  bool operator!=(const CfgOption& other) const {
350  return (!equals(other));
351  }
352 
354 
378  void add(const OptionPtr& option, const bool persistent,
379  const std::string& option_space,
380  const uint64_t id = 0);
381 
390  void add(const OptionDescriptor& desc, const std::string& option_space);
391 
405  void replace(const OptionDescriptor& desc, const std::string& option_space);
406 
426  void merge(CfgOptionDefPtr cfg_def, CfgOption& other);
427 
437  void createOptions(CfgOptionDefPtr cfg_def);
438 
479  static bool createDescriptorOption(CfgOptionDefPtr cfg_def, const std::string& space,
480  OptionDescriptor& opt_desc);
481 
490  void mergeTo(CfgOption& other) const;
491 
497  void copyTo(CfgOption& other) const;
498 
505  void encapsulate();
506 
517  OptionContainerPtr getAll(const std::string& option_space) const;
518 
525  OptionContainerPtr getAll(const uint32_t vendor_id) const;
526 
543  template<typename Selector>
544  OptionDescriptor get(const Selector& key,
545  const uint16_t option_code) const {
546 
547  // Check for presence of options.
548  OptionContainerPtr options = getAll(key);
549  if (!options || options->empty()) {
550  return (OptionDescriptor(false));
551  }
552 
553  // Some options present, locate the one we are interested in.
554  const OptionContainerTypeIndex& idx = options->get<1>();
555  OptionContainerTypeIndex::const_iterator od_itr = idx.find(option_code);
556  if (od_itr == idx.end()) {
557  return (OptionDescriptor(false));
558  }
559 
560  return (*od_itr);
561  }
562 
573  size_t del(const std::string& option_space, const uint16_t option_code);
574 
581  size_t del(const uint32_t vendor_id, const uint16_t option_code);
582 
604  size_t del(const uint64_t id);
605 
613  std::list<std::string> getOptionSpaceNames() const {
614  return (options_.getOptionSpaceNames());
615  }
616 
618  std::list<uint32_t> getVendorIds() const {
619  return (vendor_options_.getOptionSpaceNames());
620  }
621 
629  std::list<std::string> getVendorIdsSpaceNames() const;
630 
634  virtual isc::data::ElementPtr toElement() const;
635 
644  toElementWithMetadata(const bool include_metadata) const;
645 
646 private:
647 
659  void encapsulateInternal(const std::string& option_space);
660 
669  void encapsulateInternal(const OptionPtr& option);
670 
684  template <typename Selector>
685  void mergeInternal(const OptionSpaceContainer<OptionContainer,
686  OptionDescriptor, Selector>& src_container,
688  OptionDescriptor, Selector>& dest_container) const;
689 
692  std::string> OptionSpaceCollection;
694  OptionSpaceCollection options_;
695 
698  uint32_t> VendorOptionSpaceCollection;
700  VendorOptionSpaceCollection vendor_options_;
701 };
702 
704 
705 typedef boost::shared_ptr<CfgOption> CfgOptionPtr;
707 
709 typedef boost::shared_ptr<const CfgOption> ConstCfgOptionPtr;
710 
712 typedef std::list<ConstCfgOptionPtr> CfgOptionList;
713 
715 
716 }
717 }
718 
719 #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:104
Option descriptor.
Definition: cfg_option.h:42
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:274
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:706
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:31
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:85
Base class for user context.
Definition: user_context.h:22
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:709
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:618
bool operator==(const CfgOption &other) const
Equality operator.
Definition: cfg_option.h:340
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:279
OptionDescriptor & operator=(const OptionDescriptor &other)
Assignment operator.
Definition: cfg_option.h:116
std::string formatted_value_
Option value in textual (CSV) format.
Definition: cfg_option.h:66
Represents option data configuration for the DHCP server.
Definition: cfg_option.h:314
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition: cfg_option.h:712
bool operator!=(const CfgOption &other) const
Inequality operator.
Definition: cfg_option.h:349
bool persistent_
Persistence flag.
Definition: cfg_option.h:51
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:286
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:613
std::string space_name_
Option space name.
Definition: cfg_option.h:77
OptionPtr option_
Option instance.
Definition: cfg_option.h:45
Defines the logger used by the top-level component of kea-lfc.
OptionDescriptor(bool persist)
Constructor.
Definition: cfg_option.h:97
bool operator!=(const OptionDescriptor &other) const
Inequality operator.
Definition: cfg_option.h:178
void merge(ElementPtr element, ConstElementPtr other)
Merges the data from other into element.
Definition: data.cc:1134
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:269
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:169
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
Definition: cfg_option.h:272
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:281
Utility class which cascades two key extractors.
Definition: key_from_key.h:45