Kea 2.2.0
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>
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
28namespace isc {
29namespace dhcp {
30
31class OptionDescriptor;
32
34typedef boost::shared_ptr<OptionDescriptor> OptionDescriptorPtr;
35
43public:
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),
100
105 : data::StampedElement(desc),
106 option_(desc.option_),
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_;
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
210typedef boost::multi_index_container<
211 // Container comprises elements of OptionDescriptor type.
212 OptionDescriptor,
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.
224 KeyFromKeyExtractor<
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<
236 OptionDescriptor,
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<
246 OptionDescriptor,
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
272typedef boost::shared_ptr<OptionContainer> OptionContainerPtr;
274typedef OptionContainer::nth_index<1>::type OptionContainerTypeIndex;
278typedef std::pair<OptionContainerTypeIndex::const_iterator,
279 OptionContainerTypeIndex::const_iterator> OptionContainerTypeRange;
281typedef OptionContainer::nth_index<2>::type OptionContainerPersistIndex;
285typedef std::pair<OptionContainerPersistIndex::const_iterator,
286 OptionContainerPersistIndex::const_iterator> OptionContainerPersistRange;
287
315public:
316
318 CfgOption();
319
323 bool empty() const;
324
327
328
333 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
646private:
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
706typedef boost::shared_ptr<CfgOption> CfgOptionPtr;
707
709typedef boost::shared_ptr<const CfgOption> ConstCfgOptionPtr;
710
712typedef std::list<ConstCfgOptionPtr> CfgOptionList;
713
715
716}
717}
718
719#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:314
void encapsulate()
Appends encapsulated options to top-level options.
Definition: cfg_option.cc:246
void replace(const OptionDescriptor &desc, const std::string &option_space)
Replaces the instance of an option within this collection.
Definition: cfg_option.cc:96
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:165
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: cfg_option.cc:418
void createOptions(CfgOptionDefPtr cfg_def)
Re-create the option in each descriptor based on given definitions.
Definition: cfg_option.cc:150
OptionDescriptor get(const Selector &key, const uint16_t option_code) const
Returns option for the specified key and option code.
Definition: cfg_option.h:544
isc::data::ElementPtr toElementWithMetadata(const bool include_metadata) const
Unparse a configuration object with optionally including the metadata.
Definition: cfg_option.cc:423
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:332
std::list< std::string > getOptionSpaceNames() const
Returns a list of configured option space names.
Definition: cfg_option.h:613
bool empty() const
Indicates the object is empty.
Definition: cfg_option.cc:56
bool operator==(const CfgOption &other) const
Equality operator.
Definition: cfg_option.h:340
bool operator!=(const CfgOption &other) const
Inequality operator.
Definition: cfg_option.h:349
void mergeTo(CfgOption &other) const
Merges this configuration to another configuration.
Definition: cfg_option.cc:230
void copyTo(CfgOption &other) const
Copies this configuration to another configuration.
Definition: cfg_option.cc:238
CfgOption()
default constructor
Definition: cfg_option.cc:52
std::list< std::string > getVendorIdsSpaceNames() const
Returns a list of option space names for configured vendor ids.
Definition: cfg_option.cc:121
OptionContainerPtr getAll(const std::string &option_space) const
Returns all options for the specified option space.
Definition: cfg_option.cc:322
void merge(CfgOptionDefPtr cfg_def, CfgOption &other)
Merges another option configuration into this one.
Definition: cfg_option.cc:135
void add(const OptionPtr &option, const bool persistent, const std::string &option_space, const uint64_t id=0)
Adds instance of the option to the configuration.
Definition: cfg_option.cc:67
bool equals(const CfgOption &other) const
Check if configuration is equal to other configuration.
Definition: cfg_option.cc:61
std::list< uint32_t > getVendorIds() const
Returns a list of all configured vendor identifiers.
Definition: cfg_option.h:618
Option descriptor.
Definition: cfg_option.h:42
OptionPtr option_
Option instance.
Definition: cfg_option.h:45
bool operator!=(const OptionDescriptor &other) const
Inequality operator.
Definition: cfg_option.h:178
std::string space_name_
Option space name.
Definition: cfg_option.h:77
OptionDescriptor & operator=(const OptionDescriptor &other)
Assignment operator.
Definition: cfg_option.h:116
OptionDescriptor(bool persist)
Constructor.
Definition: cfg_option.h:97
bool equals(const OptionDescriptor &other) const
Checks if the one descriptor is equal to another.
Definition: cfg_option.cc:45
OptionDescriptor(const OptionPtr &opt, bool persist, const std::string &formatted_value="", data::ConstElementPtr user_context=data::ConstElementPtr())
Constructor.
Definition: cfg_option.h:85
std::string formatted_value_
Option value in textual (CSV) format.
Definition: cfg_option.h:66
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
bool operator==(const OptionDescriptor &other) const
Equality operator.
Definition: cfg_option.h:169
OptionDescriptor(const OptionDescriptor &desc)
Copy constructor.
Definition: cfg_option.h:104
bool persistent_
Persistence flag.
Definition: cfg_option.h:51
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:706
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
OptionContainer::nth_index< 1 >::type OptionContainerTypeIndex
Type of the index #1 - option type.
Definition: cfg_option.h:274
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::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::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
boost::shared_ptr< OptionDescriptor > OptionDescriptorPtr
A pointer to option descriptor.
Definition: cfg_option.h:31
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
Definition: cfg_option.h:272
OptionContainer::nth_index< 2 >::type OptionContainerPersistIndex
Type of the index #2 - option persistency flag.
Definition: cfg_option.h:281
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition: cfg_option.h:712
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:709
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