Kea 2.7.0
cfg_option.h
Go to the documentation of this file.
1// Copyright (C) 2014-2024 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/foreach.hpp>
18#include <boost/multi_index_container.hpp>
19#include <boost/multi_index/hashed_index.hpp>
20#include <boost/multi_index/ordered_index.hpp>
21#include <boost/multi_index/sequenced_index.hpp>
22#include <boost/multi_index/mem_fun.hpp>
23#include <boost/multi_index/member.hpp>
24#include <boost/shared_ptr.hpp>
25#include <stdint.h>
26#include <list>
27#include <string>
28#include <vector>
29
30namespace isc {
31namespace dhcp {
32
33class OptionDescriptor;
34
36typedef boost::shared_ptr<OptionDescriptor> OptionDescriptorPtr;
37
39typedef std::vector<OptionDescriptor> OptionDescriptorList;
40
48public:
51
57
65
79 std::string formatted_value_;
80
90 std::string space_name_;
91
99 OptionDescriptor(const OptionPtr& opt, bool persist, bool cancel,
100 const std::string& formatted_value = "",
102 : data::StampedElement(), option_(opt), persistent_(persist),
103 cancelled_(cancel), formatted_value_(formatted_value),
104 space_name_() {
105 setContext(user_context);
106 };
107
112 OptionDescriptor(bool persist, bool cancel)
113 : data::StampedElement(), option_(OptionPtr()), persistent_(persist),
114 cancelled_(cancel), formatted_value_(), space_name_() {};
115
120 : data::StampedElement(desc),
121 option_(desc.option_),
126 setContext(desc.getContext());
127 };
128
133 if (this != &other) {
134 // Not self-assignment.
135 data::StampedElement::operator=(other);
136 option_ = other.option_;
137 persistent_ = other.persistent_;
138 cancelled_ = other.cancelled_;
139 formatted_value_ = other.formatted_value_;
140 space_name_ = other.space_name_;
141 setContext(other.getContext());
142 }
143 return (*this);
144 }
145
155 static OptionDescriptorPtr create(const OptionPtr& opt,
156 bool persist,
157 bool cancel,
158 const std::string& formatted_value = "",
159 data::ConstElementPtr user_context =
161
168 static OptionDescriptorPtr create(bool persist, bool cancel);
169
175 static OptionDescriptorPtr create(const OptionDescriptor& desc);
176
182 bool equals(const OptionDescriptor& other) const;
183
189 bool operator==(const OptionDescriptor& other) const {
190 return (equals(other));
191 }
192
198 bool operator!=(const OptionDescriptor& other) const {
199 return (!equals(other));
200 }
201};
202
232typedef boost::multi_index_container<
233 // Container comprises elements of OptionDescriptor type.
234 OptionDescriptor,
235 // Here we start enumerating various indexes.
236 boost::multi_index::indexed_by<
237 // Sequenced index allows accessing elements in the same way
238 // as elements in std::list.
239 // Sequenced is an index #0.
240 boost::multi_index::sequenced<>,
241 // Start definition of index #1.
242 boost::multi_index::hashed_non_unique<
243 // KeyFromKeyExtractor is the index key extractor that allows
244 // accessing option type being held by the OptionPtr through
245 // OptionDescriptor structure.
246 KeyFromKeyExtractor<
247 // Use option type as the index key. The type is held
248 // in OptionPtr object so we have to call Option::getType
249 // to retrieve this key for each element.
250 boost::multi_index::const_mem_fun<
251 Option,
252 uint16_t,
254 >,
255 // Indicate that OptionPtr is a member of
256 // OptionDescriptor structure.
257 boost::multi_index::member<
258 OptionDescriptor,
259 OptionPtr,
261 >
262 >
263 >,
264 // Start definition of index #2.
265 // Use 'persistent' struct member as a key.
266 boost::multi_index::hashed_non_unique<
267 boost::multi_index::member<
268 OptionDescriptor,
269 bool,
271 >
272 >,
273 // Start definition of index #3.
274 // Use BaseStampedElement::getModificationTime as a key.
275 boost::multi_index::ordered_non_unique<
276 boost::multi_index::const_mem_fun<
278 boost::posix_time::ptime,
280 >
281 >,
282
283 // Start definition of index #4.
284 // Use BaseStampedElement::getId as a key.
285 boost::multi_index::hashed_non_unique<
286 boost::multi_index::tag<OptionIdIndexTag>,
287 boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
289 >,
290 // Start definition of index #5.
291 // Use 'cancelled' struct member as a key.
292 boost::multi_index::hashed_non_unique<
293 boost::multi_index::member<
294 OptionDescriptor,
295 bool,
297 >
298 >
299 >
301
303typedef boost::shared_ptr<OptionContainer> OptionContainerPtr;
305typedef OptionContainer::nth_index<1>::type OptionContainerTypeIndex;
309typedef std::pair<OptionContainerTypeIndex::const_iterator,
310 OptionContainerTypeIndex::const_iterator> OptionContainerTypeRange;
312typedef OptionContainer::nth_index<2>::type OptionContainerPersistIndex;
316typedef std::pair<OptionContainerPersistIndex::const_iterator,
317 OptionContainerPersistIndex::const_iterator> OptionContainerPersistRange;
319typedef OptionContainer::nth_index<5>::type OptionContainerCancelIndex;
323typedef std::pair<OptionContainerCancelIndex::const_iterator,
324 OptionContainerCancelIndex::const_iterator> OptionContainerCancelRange;
325
353public:
354
356 CfgOption();
357
361 bool empty() const;
362
365
366
371 bool equals(const CfgOption& other) const;
372
378 bool operator==(const CfgOption& other) const {
379 return (equals(other));
380 }
381
387 bool operator!=(const CfgOption& other) const {
388 return (!equals(other));
389 }
390
392
418 void add(const OptionPtr& option, const bool persistent,
419 const bool cancelled, const std::string& option_space,
420 const uint64_t id = 0);
421
430 void add(const OptionDescriptor& desc, const std::string& option_space);
431
445 void replace(const OptionDescriptor& desc, const std::string& option_space);
446
466 void merge(CfgOptionDefPtr cfg_def, CfgOption& other);
467
477 void createOptions(CfgOptionDefPtr cfg_def);
478
519 static bool createDescriptorOption(CfgOptionDefPtr cfg_def, const std::string& space,
520 OptionDescriptor& opt_desc);
521
530 void mergeTo(CfgOption& other) const;
531
537 void copyTo(CfgOption& other) const;
538
545 void encapsulate();
546
550 bool isEncapsulated() const {
551 return (encapsulated_);
552 }
553
564 OptionContainerPtr getAll(const std::string& option_space) const;
565
572 OptionContainerPtr getAll(const uint32_t vendor_id) const;
573
585 OptionContainerPtr getAllCombined(const std::string& option_space) const;
586
604 template<typename Selector>
605 OptionDescriptor get(const Selector& key,
606 const uint16_t option_code) const {
607
608 // Check for presence of options.
609 OptionContainerPtr options = getAll(key);
610 if (!options || options->empty()) {
611 return (OptionDescriptor(false, false));
612 }
613
614 // Some options present, locate the one we are interested in.
615 const OptionContainerTypeIndex& idx = options->get<1>();
616 OptionContainerTypeIndex::const_iterator od_itr = idx.find(option_code);
617 if (od_itr == idx.end()) {
618 return (OptionDescriptor(false, false));
619 }
620
621 return (*od_itr);
622 }
623
635 template<typename Selector>
636 OptionDescriptorList getList(const Selector& key,
637 const uint16_t option_code) const {
638
640 // Check for presence of options.
641 OptionContainerPtr options = getAll(key);
642 if (!options || options->empty()) {
643 return (list);
644 }
645
646 // Some options present, locate the one we are interested in.
647 const OptionContainerTypeIndex& idx = options->get<1>();
648 OptionContainerTypeRange range = idx.equal_range(option_code);
649 // This code copies descriptors and can be optimized not doing this.
650 BOOST_FOREACH(auto const& od_itr, range) {
651 list.push_back(od_itr);
652 }
653
654 return (list);
655 }
656
667 size_t del(const std::string& option_space, const uint16_t option_code);
668
675 size_t del(const uint32_t vendor_id, const uint16_t option_code);
676
698 size_t del(const uint64_t id);
699
707 std::list<std::string> getOptionSpaceNames() const {
708 return (options_.getOptionSpaceNames());
709 }
710
712 std::list<uint32_t> getVendorIds() const {
713 return (vendor_options_.getOptionSpaceNames());
714 }
715
723 std::list<std::string> getVendorIdsSpaceNames() const;
724
728 virtual isc::data::ElementPtr toElement() const;
729
738 toElementWithMetadata(const bool include_metadata) const;
739
740private:
741
753 void encapsulateInternal(const std::string& option_space);
754
763 void encapsulateInternal(const OptionPtr& option);
764
778 template <typename Selector>
779 void mergeInternal(const OptionSpaceContainer<OptionContainer,
780 OptionDescriptor, Selector>& src_container,
782 OptionDescriptor, Selector>& dest_container) const;
783
785 bool encapsulated_;
786
789 std::string> OptionSpaceCollection;
791 OptionSpaceCollection options_;
792
795 uint32_t> VendorOptionSpaceCollection;
797 VendorOptionSpaceCollection vendor_options_;
798};
799
801
802
803typedef boost::shared_ptr<CfgOption> CfgOptionPtr;
804
806typedef boost::shared_ptr<const CfgOption> ConstCfgOptionPtr;
807
809typedef std::list<ConstCfgOptionPtr> CfgOptionList;
810
812
813}
814}
815
816#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:352
bool isEncapsulated() const
Checks if options have been encapsulated.
Definition cfg_option.h:550
OptionContainerPtr getAllCombined(const std::string &option_space) const
Returns all non-vendor or vendor options for the specified option space.
void encapsulate()
Appends encapsulated options to top-level options.
void replace(const OptionDescriptor &desc, const std::string &option_space)
Replaces the instance of an option within this collection.
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.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
void createOptions(CfgOptionDefPtr cfg_def)
Re-create the option in each descriptor based on given definitions.
OptionDescriptor get(const Selector &key, const uint16_t option_code) const
Returns option for the specified key and option code.
Definition cfg_option.h:605
isc::data::ElementPtr toElementWithMetadata(const bool include_metadata) const
Unparse a configuration object with optionally including the metadata.
OptionDescriptorList getList(const Selector &key, const uint16_t option_code) const
Returns options for the specified key and option code.
Definition cfg_option.h:636
size_t del(const std::string &option_space, const uint16_t option_code)
Deletes option for the specified option space and option code.
std::list< std::string > getOptionSpaceNames() const
Returns a list of configured option space names.
Definition cfg_option.h:707
bool empty() const
Indicates the object is empty.
Definition cfg_option.cc:59
bool operator==(const CfgOption &other) const
Equality operator.
Definition cfg_option.h:378
bool operator!=(const CfgOption &other) const
Inequality operator.
Definition cfg_option.h:387
void mergeTo(CfgOption &other) const
Merges this configuration to another configuration.
void copyTo(CfgOption &other) const
Copies this configuration to another configuration.
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.
OptionContainerPtr getAll(const std::string &option_space) const
Returns all options for the specified option space.
void merge(CfgOptionDefPtr cfg_def, CfgOption &other)
Merges another option configuration into this one.
bool equals(const CfgOption &other) const
Check if configuration is equal to other configuration.
Definition cfg_option.cc:64
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:70
std::list< uint32_t > getVendorIds() const
Returns a list of all configured vendor identifiers.
Definition cfg_option.h:712
Option descriptor.
Definition cfg_option.h:47
OptionPtr option_
Option instance.
Definition cfg_option.h:50
bool operator!=(const OptionDescriptor &other) const
Inequality operator.
Definition cfg_option.h:198
std::string space_name_
Option space name.
Definition cfg_option.h:90
OptionDescriptor & operator=(const OptionDescriptor &other)
Assignment operator.
Definition cfg_option.h:132
OptionDescriptor(const OptionPtr &opt, bool persist, bool cancel, const std::string &formatted_value="", data::ConstElementPtr user_context=data::ConstElementPtr())
Constructor.
Definition cfg_option.h:99
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:64
std::string formatted_value_
Option value in textual (CSV) format.
Definition cfg_option.h:79
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:112
bool operator==(const OptionDescriptor &other) const
Equality operator.
Definition cfg_option.h:189
OptionDescriptor(const OptionDescriptor &desc)
Copy constructor.
Definition cfg_option.h:119
bool persistent_
Persistence flag.
Definition cfg_option.h:56
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:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
boost::shared_ptr< CfgOption > CfgOptionPtr
Non-const pointer.
Definition cfg_option.h:803
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:310
OptionContainer::nth_index< 1 >::type OptionContainerTypeIndex
Type of the index #1 - option type.
Definition cfg_option.h:305
std::map< std::string, OptionSpacePtr > OptionSpaceCollection
A collection of option spaces.
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
std::vector< OptionDescriptor > OptionDescriptorList
A list of option descriptors.
Definition cfg_option.h:39
OptionContainer::nth_index< 5 >::type OptionContainerCancelIndex
Type of the index #5 - option cancellation flag.
Definition cfg_option.h:319
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:317
boost::shared_ptr< OptionDescriptor > OptionDescriptorPtr
A pointer to option descriptor.
Definition cfg_option.h:36
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
Definition cfg_option.h:303
OptionContainer::nth_index< 2 >::type OptionContainerPersistIndex
Type of the index #2 - option persistency flag.
Definition cfg_option.h:312
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:300
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:324
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition cfg_option.h:809
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition cfg_option.h:806
Defines the logger used by the top-level component of kea-lfc.
Abstract class for configuration Cfg_* classes.
Base class for user context.
void setContext(const data::ConstElementPtr &ctx)
Sets user context.