Kea 2.7.5
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>
11#include <dhcp/classify.h>
13#include <cc/cfg_to_element.h>
14#include <cc/stamped_element.h>
15#include <cc/user_context.h>
18#include <boost/foreach.hpp>
19#include <boost/multi_index_container.hpp>
20#include <boost/multi_index/hashed_index.hpp>
21#include <boost/multi_index/ordered_index.hpp>
22#include <boost/multi_index/sequenced_index.hpp>
23#include <boost/multi_index/mem_fun.hpp>
24#include <boost/multi_index/member.hpp>
25#include <boost/shared_ptr.hpp>
26#include <stdint.h>
27#include <list>
28#include <string>
29#include <vector>
30
31namespace isc {
32namespace dhcp {
33
34class OptionDescriptor;
35
37typedef boost::shared_ptr<OptionDescriptor> OptionDescriptorPtr;
38
40typedef std::vector<OptionDescriptor> OptionDescriptorList;
41
49public:
52
58
66
80 std::string formatted_value_;
81
91 std::string space_name_;
92
96
104 OptionDescriptor(const OptionPtr& opt, bool persist, bool cancel,
105 const std::string& formatted_value = "",
107 : data::StampedElement(), option_(opt), persistent_(persist),
108 cancelled_(cancel), formatted_value_(formatted_value),
109 space_name_() {
110 setContext(user_context);
111 }
112
117 OptionDescriptor(bool persist, bool cancel)
118 : data::StampedElement(), option_(OptionPtr()), persistent_(persist),
120
125 : data::StampedElement(desc),
126 option_(desc.option_),
132 setContext(desc.getContext());
133 }
134
139 if (this != &other) {
140 // Not self-assignment.
141 data::StampedElement::operator=(other);
142 option_ = other.option_;
143 persistent_ = other.persistent_;
144 cancelled_ = other.cancelled_;
145 formatted_value_ = other.formatted_value_;
146 space_name_ = other.space_name_;
147 client_classes_ = other.client_classes_;
148 setContext(other.getContext());
149 }
150 return (*this);
151 }
152
162 static OptionDescriptorPtr create(const OptionPtr& opt,
163 bool persist,
164 bool cancel,
165 const std::string& formatted_value = "",
166 data::ConstElementPtr user_context =
168
175 static OptionDescriptorPtr create(bool persist, bool cancel);
176
182 static OptionDescriptorPtr create(const OptionDescriptor& desc);
183
189 bool equals(const OptionDescriptor& other) const;
190
196 bool operator==(const OptionDescriptor& other) const {
197 return (equals(other));
198 }
199
205 bool operator!=(const OptionDescriptor& other) const {
206 return (!equals(other));
207 }
208
212 void addClientClass(const std::string& class_name);
213
220 bool allowedForClientClasses(const ClientClasses& cclasses) const;
221};
222
252typedef boost::multi_index_container<
253 // Container comprises elements of OptionDescriptor type.
254 OptionDescriptor,
255 // Here we start enumerating various indexes.
256 boost::multi_index::indexed_by<
257 // Sequenced index allows accessing elements in the same way
258 // as elements in std::list.
259 // Sequenced is an index #0.
260 boost::multi_index::sequenced<>,
261 // Start definition of index #1.
262 boost::multi_index::hashed_non_unique<
263 // KeyFromKeyExtractor is the index key extractor that allows
264 // accessing option type being held by the OptionPtr through
265 // OptionDescriptor structure.
266 KeyFromKeyExtractor<
267 // Use option type as the index key. The type is held
268 // in OptionPtr object so we have to call Option::getType
269 // to retrieve this key for each element.
270 boost::multi_index::const_mem_fun<
271 Option,
272 uint16_t,
274 >,
275 // Indicate that OptionPtr is a member of
276 // OptionDescriptor structure.
277 boost::multi_index::member<
278 OptionDescriptor,
279 OptionPtr,
281 >
282 >
283 >,
284 // Start definition of index #2.
285 // Use 'persistent' struct member as a key.
286 boost::multi_index::hashed_non_unique<
287 boost::multi_index::member<
288 OptionDescriptor,
289 bool,
291 >
292 >,
293 // Start definition of index #3.
294 // Use BaseStampedElement::getModificationTime as a key.
295 boost::multi_index::ordered_non_unique<
296 boost::multi_index::const_mem_fun<
298 boost::posix_time::ptime,
300 >
301 >,
302
303 // Start definition of index #4.
304 // Use BaseStampedElement::getId as a key.
305 boost::multi_index::hashed_non_unique<
306 boost::multi_index::tag<OptionIdIndexTag>,
307 boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
309 >,
310 // Start definition of index #5.
311 // Use 'cancelled' struct member as a key.
312 boost::multi_index::hashed_non_unique<
313 boost::multi_index::member<
314 OptionDescriptor,
315 bool,
317 >
318 >
319 >
321
323typedef boost::shared_ptr<OptionContainer> OptionContainerPtr;
325typedef OptionContainer::nth_index<1>::type OptionContainerTypeIndex;
329typedef std::pair<OptionContainerTypeIndex::const_iterator,
330 OptionContainerTypeIndex::const_iterator> OptionContainerTypeRange;
332typedef OptionContainer::nth_index<2>::type OptionContainerPersistIndex;
336typedef std::pair<OptionContainerPersistIndex::const_iterator,
337 OptionContainerPersistIndex::const_iterator> OptionContainerPersistRange;
339typedef OptionContainer::nth_index<5>::type OptionContainerCancelIndex;
343typedef std::pair<OptionContainerCancelIndex::const_iterator,
344 OptionContainerCancelIndex::const_iterator> OptionContainerCancelRange;
345
373public:
374
376 CfgOption();
377
381 bool empty() const;
382
385
386
391 bool equals(const CfgOption& other) const;
392
398 bool operator==(const CfgOption& other) const {
399 return (equals(other));
400 }
401
407 bool operator!=(const CfgOption& other) const {
408 return (!equals(other));
409 }
410
412
438 void add(const OptionPtr& option, const bool persistent,
439 const bool cancelled, const std::string& option_space,
440 const uint64_t id = 0);
441
450 void add(const OptionDescriptor& desc, const std::string& option_space);
451
465 void replace(const OptionDescriptor& desc, const std::string& option_space);
466
486 void merge(CfgOptionDefPtr cfg_def, CfgOption& other);
487
497 void createOptions(CfgOptionDefPtr cfg_def);
498
539 static bool createDescriptorOption(CfgOptionDefPtr cfg_def, const std::string& space,
540 OptionDescriptor& opt_desc);
541
550 void mergeTo(CfgOption& other) const;
551
557 void copyTo(CfgOption& other) const;
558
565 void encapsulate();
566
570 bool isEncapsulated() const {
571 return (encapsulated_);
572 }
573
584 OptionContainerPtr getAll(const std::string& option_space) const;
585
592 OptionContainerPtr getAll(const uint32_t vendor_id) const;
593
605 OptionContainerPtr getAllCombined(const std::string& option_space) const;
606
624 template<typename Selector>
625 OptionDescriptor get(const Selector& key,
626 const uint16_t option_code) const {
627
628 // Check for presence of options.
629 OptionContainerPtr options = getAll(key);
630 if (!options || options->empty()) {
631 return (OptionDescriptor(false, false));
632 }
633
634 // Some options present, locate the one we are interested in.
635 const OptionContainerTypeIndex& idx = options->get<1>();
636 OptionContainerTypeIndex::const_iterator od_itr = idx.find(option_code);
637 if (od_itr == idx.end()) {
638 return (OptionDescriptor(false, false));
639 }
640
641 return (*od_itr);
642 }
643
655 template<typename Selector>
656 OptionDescriptorList getList(const Selector& key,
657 const uint16_t option_code) const {
658
660 // Check for presence of options.
661 OptionContainerPtr options = getAll(key);
662 if (!options || options->empty()) {
663 return (list);
664 }
665
666 // Some options present, locate the one we are interested in.
667 const OptionContainerTypeIndex& idx = options->get<1>();
668 OptionContainerTypeRange range = idx.equal_range(option_code);
669 // This code copies descriptors and can be optimized not doing this.
670 BOOST_FOREACH(auto const& od_itr, range) {
671 list.push_back(od_itr);
672 }
673
674 return (list);
675 }
676
687 size_t del(const std::string& option_space, const uint16_t option_code);
688
695 size_t del(const uint32_t vendor_id, const uint16_t option_code);
696
718 size_t del(const uint64_t id);
719
727 std::list<std::string> getOptionSpaceNames() const {
728 return (options_.getOptionSpaceNames());
729 }
730
732 std::list<uint32_t> getVendorIds() const {
733 return (vendor_options_.getOptionSpaceNames());
734 }
735
743 std::list<std::string> getVendorIdsSpaceNames() const;
744
748 virtual isc::data::ElementPtr toElement() const;
749
754 virtual isc::data::ElementPtr toElement(CfgOptionDefPtr cfg_option_def) const;
755
765 toElementWithMetadata(const bool include_metadata,
766 CfgOptionDefPtr cfg_option_def = CfgOptionDefPtr()) const;
767
768private:
769
781 void encapsulateInternal(const std::string& option_space);
782
791 void encapsulateInternal(const OptionPtr& option);
792
806 template <typename Selector>
807 void mergeInternal(const OptionSpaceContainer<OptionContainer,
808 OptionDescriptor, Selector>& src_container,
810 OptionDescriptor, Selector>& dest_container) const;
811
813 bool encapsulated_;
814
817 std::string> OptionSpaceCollection;
819 OptionSpaceCollection options_;
820
823 uint32_t> VendorOptionSpaceCollection;
825 VendorOptionSpaceCollection vendor_options_;
826};
827
829
830
832typedef boost::shared_ptr<CfgOption> CfgOptionPtr;
833
835typedef boost::shared_ptr<const CfgOption> ConstCfgOptionPtr;
836
838typedef std::list<ConstCfgOptionPtr> CfgOptionList;
839
841
842}
843}
844
845#endif // CFG_OPTION_H
Defines elements for storing the names of client classes.
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:372
bool isEncapsulated() const
Checks if options have been encapsulated.
Definition cfg_option.h:570
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:625
isc::data::ElementPtr toElementWithMetadata(const bool include_metadata, CfgOptionDefPtr cfg_option_def=CfgOptionDefPtr()) 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:656
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:727
bool empty() const
Indicates the object is empty.
Definition cfg_option.cc:91
bool operator==(const CfgOption &other) const
Equality operator.
Definition cfg_option.h:398
bool operator!=(const CfgOption &other) const
Inequality operator.
Definition cfg_option.h:407
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:86
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:96
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.
std::list< uint32_t > getVendorIds() const
Returns a list of all configured vendor identifiers.
Definition cfg_option.h:732
Container for storing client class names.
Definition classify.h:108
Option descriptor.
Definition cfg_option.h:48
OptionPtr option_
Option instance.
Definition cfg_option.h:51
bool operator!=(const OptionDescriptor &other) const
Inequality operator.
Definition cfg_option.h:205
void addClientClass(const std::string &class_name)
Adds new client class for which the option is allowed.
Definition cfg_option.cc:55
std::string space_name_
Option space name.
Definition cfg_option.h:91
OptionDescriptor & operator=(const OptionDescriptor &other)
Assignment operator.
Definition cfg_option.h:138
OptionDescriptor(const OptionPtr &opt, bool persist, bool cancel, const std::string &formatted_value="", data::ConstElementPtr user_context=data::ConstElementPtr())
Constructor.
Definition cfg_option.h:104
bool equals(const OptionDescriptor &other) const
Checks if the one descriptor is equal to another.
Definition cfg_option.cc:46
bool allowedForClientClasses(const ClientClasses &cclasses) const
Validates an OptionDescriptor's client-classes against a list of classes.
Definition cfg_option.cc:63
bool cancelled_
Cancelled flag.
Definition cfg_option.h:65
std::string formatted_value_
Option value in textual (CSV) format.
Definition cfg_option.h:80
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
ClientClasses client_classes_
Collection of classes for the which option is allowed.
Definition cfg_option.h:95
OptionDescriptor(bool persist, bool cancel)
Constructor.
Definition cfg_option.h:117
bool operator==(const OptionDescriptor &other) const
Equality operator.
Definition cfg_option.h:196
OptionDescriptor(const OptionDescriptor &desc)
Copy constructor.
Definition cfg_option.h:124
bool persistent_
Persistence flag.
Definition cfg_option.h:57
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:300
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:832
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:330
OptionContainer::nth_index< 1 >::type OptionContainerTypeIndex
Type of the index #1 - option type.
Definition cfg_option.h:325
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:40
OptionContainer::nth_index< 5 >::type OptionContainerCancelIndex
Type of the index #5 - option cancellation flag.
Definition cfg_option.h:339
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:337
boost::shared_ptr< OptionDescriptor > OptionDescriptorPtr
A pointer to option descriptor.
Definition cfg_option.h:37
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
Definition cfg_option.h:323
OptionContainer::nth_index< 2 >::type OptionContainerPersistIndex
Type of the index #2 - option persistency flag.
Definition cfg_option.h:332
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:320
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:344
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
std::list< ConstCfgOptionPtr > CfgOptionList
Const pointer list.
Definition cfg_option.h:838
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition cfg_option.h:835
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.