Kea  1.5.0
cfg_option.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2018 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/key_from_key.h>
16 #include <boost/multi_index_container.hpp>
17 #include <boost/multi_index/hashed_index.hpp>
18 #include <boost/multi_index/sequenced_index.hpp>
19 #include <boost/multi_index/mem_fun.hpp>
20 #include <boost/multi_index/member.hpp>
21 #include <boost/shared_ptr.hpp>
22 #include <stdint.h>
23 #include <string>
24 #include <list>
25 
26 namespace isc {
27 namespace dhcp {
28 
36 public:
39 
45 
59  std::string formatted_value_;
60 
70  std::string space_name_;
71 
79  OptionDescriptor(const OptionPtr& opt, bool persist,
80  const std::string& formatted_value = "",
82  : option_(opt), persistent_(persist),
83  formatted_value_(formatted_value),
84  space_name_() {
85  setContext(user_context);
86  };
87 
91  OptionDescriptor(bool persist)
92  : option_(OptionPtr()), persistent_(persist),
94 
99  : option_(desc.option_), persistent_(desc.persistent_),
101  space_name_(desc.space_name_) {
102  setContext(desc.getContext());
103  };
104 
110  bool equals(const OptionDescriptor& other) const;
111 
117  bool operator==(const OptionDescriptor& other) const {
118  return (equals(other));
119  }
120 
126  bool operator!=(const OptionDescriptor& other) const {
127  return (!equals(other));
128  }
129 };
130 
132 typedef boost::shared_ptr<OptionDescriptor> OptionDescriptorPtr;
133 
161 typedef boost::multi_index_container<
162  // Container comprises elements of OptionDescriptor type.
164  // Here we start enumerating various indexes.
165  boost::multi_index::indexed_by<
166  // Sequenced index allows accessing elements in the same way
167  // as elements in std::list.
168  // Sequenced is an index #0.
169  boost::multi_index::sequenced<>,
170  // Start definition of index #1.
171  boost::multi_index::hashed_non_unique<
172  // KeyFromKeyExtractor is the index key extractor that allows
173  // accessing option type being held by the OptionPtr through
174  // OptionDescriptor structure.
176  // Use option type as the index key. The type is held
177  // in OptionPtr object so we have to call Option::getType
178  // to retrieve this key for each element.
179  boost::multi_index::const_mem_fun<
180  Option,
181  uint16_t,
183  >,
184  // Indicate that OptionPtr is a member of
185  // OptionDescriptor structure.
186  boost::multi_index::member<
188  OptionPtr,
190  >
191  >
192  >,
193  // Start definition of index #2.
194  // Use 'persistent' struct member as a key.
195  boost::multi_index::hashed_non_unique<
196  boost::multi_index::member<
198  bool,
200  >
201  >
202  >
204 
206 typedef boost::shared_ptr<OptionContainer> OptionContainerPtr;
208 typedef OptionContainer::nth_index<1>::type OptionContainerTypeIndex;
212 typedef std::pair<OptionContainerTypeIndex::const_iterator,
213  OptionContainerTypeIndex::const_iterator> OptionContainerTypeRange;
215 typedef OptionContainer::nth_index<2>::type OptionContainerPersistIndex;
219 typedef std::pair<OptionContainerPersistIndex::const_iterator,
220  OptionContainerPersistIndex::const_iterator> OptionContainerPersistRange;
221 
249 public:
250 
252  CfgOption();
253 
257  bool empty() const;
258 
261 
262  bool equals(const CfgOption& other) const;
268 
274  bool operator==(const CfgOption& other) const {
275  return (equals(other));
276  }
277 
283  bool operator!=(const CfgOption& other) const {
284  return (!equals(other));
285  }
286 
288 
311  void add(const OptionPtr& option, const bool persistent,
312  const std::string& option_space);
313 
322  void add(const OptionDescriptor& desc, const std::string& option_space);
323 
334  void mergeTo(CfgOption& other) const;
335 
341  void copyTo(CfgOption& other) const;
342 
349  void encapsulate();
350 
361  OptionContainerPtr getAll(const std::string& option_space) const;
362 
369  OptionContainerPtr getAll(const uint32_t vendor_id) const;
370 
387  template<typename Selector>
388  OptionDescriptor get(const Selector& key,
389  const uint16_t option_code) const {
390 
391  // Check for presence of options.
392  OptionContainerPtr options = getAll(key);
393  if (!options || options->empty()) {
394  return (OptionDescriptor(false));
395  }
396 
397  // Some options present, locate the one we are interested in.
398  const OptionContainerTypeIndex& idx = options->get<1>();
399  OptionContainerTypeIndex::const_iterator od_itr = idx.find(option_code);
400  if (od_itr == idx.end()) {
401  return (OptionDescriptor(false));
402  }
403 
404  return (*od_itr);
405  }
406 
414  std::list<std::string> getOptionSpaceNames() const {
415  return (options_.getOptionSpaceNames());
416  }
417 
419  std::list<uint32_t> getVendorIds() const {
420  return (vendor_options_.getOptionSpaceNames());
421  }
422 
430  std::list<std::string> getVendorIdsSpaceNames() const;
431 
435  virtual isc::data::ElementPtr toElement() const;
436 
437 private:
438 
450  void encapsulateInternal(const std::string& option_space);
451 
460  void encapsulateInternal(const OptionPtr& option);
461 
475  template <typename Selector>
476  void mergeInternal(const OptionSpaceContainer<OptionContainer,
477  OptionDescriptor, Selector>& src_container,
479  OptionDescriptor, Selector>& dest_container) const;
480 
483  std::string> OptionSpaceCollection;
485  OptionSpaceCollection options_;
486 
489  uint32_t> VendorOptionSpaceCollection;
491  VendorOptionSpaceCollection vendor_options_;
492 };
493 
495 
496 typedef boost::shared_ptr<CfgOption> CfgOptionPtr;
498 
500 typedef boost::shared_ptr<const CfgOption> ConstCfgOptionPtr;
501 
503 typedef std::list<ConstCfgOptionPtr> CfgOptionList;
504 
506 
507 }
508 }
509 
510 #endif // CFG_OPTION_H
bool equals(const OptionDescriptor &other) const
Checks if the one descriptor is equal to another.
Definition: cfg_option.cc:24
Simple container for option spaces holding various items.
OptionDescriptor(const OptionDescriptor &desc)
Constructor.
Definition: cfg_option.h:98
Option descriptor.
Definition: cfg_option.h:35
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:208
This class represents configuration element which is associated with the modification timestamp.
void copyTo(CfgOption &other) const
Copies this configuration to another configuration.
Definition: cfg_option.cc:93
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition: cfg_option.h:497
void add(const OptionPtr &option, const bool persistent, const std::string &option_space)
Adds instance of the option to the configuration.
Definition: cfg_option.cc:46
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:132
uint16_t getType() const
Returns option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition: option.h:246
OptionDescriptor(const OptionPtr &opt, bool persist, const std::string &formatted_value="", data::ConstElementPtr user_context=data::ConstElementPtr())
Constructor.
Definition: cfg_option.h:79
Base class for user context.
Definition: user_context.h:22
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:500
void mergeTo(CfgOption &other) const
Merges this configuration to another configuration.
Definition: cfg_option.cc:85
std::list< Selector > getOptionSpaceNames() const
Get a list of existing option spaces.
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
OptionContainerPtr getAll(const std::string &option_space) const
Returns all options for the specified option space.
Definition: cfg_option.cc:183
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
std::list< uint32_t > getVendorIds() const
Returns a list of all configured vendor identifiers.
Definition: cfg_option.h:419
bool operator==(const CfgOption &other) const
Equality operator.
Definition: cfg_option.h:274
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_ > > >> OptionContainer
Multi index container for DHCP option descriptors.
Definition: cfg_option.h:203
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:213
std::string formatted_value_
Option value in textual (CSV) format.
Definition: cfg_option.h:59
CfgOption()
default constructor
Definition: cfg_option.cc:31
Represents option data configuration for the DHCP server.
Definition: cfg_option.h:248
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition: cfg_option.h:503
bool operator!=(const CfgOption &other) const
Inequality operator.
Definition: cfg_option.h:283
bool persistent_
Persistence flag.
Definition: cfg_option.h:44
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:220
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: cfg_option.cc:193
std::list< std::string > getVendorIdsSpaceNames() const
Returns a list of option space names for configured vendor ids.
Definition: cfg_option.cc:70
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
std::list< std::string > getOptionSpaceNames() const
Returns a list of configured option space names.
Definition: cfg_option.h:414
std::string space_name_
Option space name.
Definition: cfg_option.h:70
OptionPtr option_
Option instance.
Definition: cfg_option.h:38
Defines the logger used by the top-level component of kea-dhcp-ddns.
OptionDescriptor(bool persist)
Constructor.
Definition: cfg_option.h:91
bool operator!=(const OptionDescriptor &other) const
Inequality operator.
Definition: cfg_option.h:126
bool empty() const
Indicates the object is empty.
Definition: cfg_option.cc:35
void encapsulate()
Appends encapsulated options to top-level options.
Definition: cfg_option.cc:101
bool operator==(const OptionDescriptor &other) const
Equality operator.
Definition: cfg_option.h:117
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
Definition: cfg_option.h:206
bool equals(const CfgOption &other) const
Check if configuration is equal to other configuration.
Definition: cfg_option.cc:40
std::map< std::string, OptionSpacePtr > OptionSpaceCollection
A collection of option spaces.
Definition: option_space.h:40
OptionDescriptor get(const Selector &key, const uint16_t option_code) const
Returns option for the specified key and option code.
Definition: cfg_option.h:388
OptionContainer::nth_index< 2 >::type OptionContainerPersistIndex
Type of the index #2 - option persistency flag.
Definition: cfg_option.h:215
Utility class which cascades two key extractors.
Definition: key_from_key.h:45