14#include <boost/algorithm/string/split.hpp>
15#include <boost/algorithm/string/classification.hpp>
16#include <boost/make_shared.hpp>
28 const std::string& formatted_value,
30 return (boost::make_shared<OptionDescriptor>(opt, persist, cancel,
37 return (boost::make_shared<OptionDescriptor>(persist, cancel));
42 return (boost::make_shared<OptionDescriptor>(desc));
51 option_->equals(other.option_));
55 : encapsulated_(false) {
60 return (options_.
empty() && vendor_options_.
empty());
65 return (options_.
equals(other.options_) &&
66 vendor_options_.
equals(other.vendor_options_));
71 const bool persistent,
73 const std::string& option_space,
79 add(desc, option_space);
89 << option_space <<
"'");
94 vendor_options_.
addItem(desc, vendor_id);
96 options_.
addItem(desc, option_space);
110 <<
" does not exist");
115 auto const& od_itr = idx.find(desc.option_->getType());
116 if (od_itr == idx.end()) {
118 << option_space <<
":" << desc.option_->getType()
119 <<
", it does not exist");
122 idx.replace(od_itr, desc);
125std::list<std::string>
128 std::list<std::string> names;
129 for (
auto const&
id : ids) {
130 std::ostringstream s;
133 s <<
"vendor-" << id;
134 names.push_back(s.str());
148 other.createOptions(cfg_def);
166 for (
auto opt_desc : *(
getAll(space))) {
178 if (!opt_desc.option_) {
180 "validateCreateOption: descriptor has no option instance");
184 uint16_t code = opt_desc.option_->getType();
202 def = cfg_def->get(space, code);
210 std::string& formatted_value = opt_desc.formatted_value_;
212 if (!formatted_value.empty()) {
214 <<
" has a formatted value: '" << formatted_value
215 <<
"' but no option definition");
227 if (formatted_value.empty()) {
229 opt_desc.option_ = def->optionFactory(universe, code, opt_desc.option_->getData());
232 std::vector<std::string> split_vec;
233 boost::split(split_vec, formatted_value, boost::is_any_of(
","));
234 opt_desc.option_ = def->optionFactory(universe, code, split_vec);
236 }
catch (
const std::exception& ex) {
238 <<
" from data specified, reason: " << ex.what());
248 mergeInternal(options_, other.options_);
250 mergeInternal(vendor_options_, other.vendor_options_);
256 other.options_.clearItems();
257 other.vendor_options_.clearItems();
268 encapsulated_ =
true;
272CfgOption::encapsulateInternal(
const std::string& option_space) {
277 for (
auto const& opt : *options) {
278 encapsulateInternal(opt.option_);
283CfgOption::encapsulateInternal(
const OptionPtr& option) {
285 const std::string& encap_space = option->getEncapsulatedSpace();
287 if (!encap_space.empty()) {
293 for (
auto const& encap_opt : *encap_options) {
294 if (option.get() == encap_opt.option_.get()) {
300 if (!option->getOption(encap_opt.option_->getType())) {
301 option->addOption(encap_opt.option_);
303 encapsulateInternal(encap_opt.option_);
308template <
typename Selector>
311 OptionDescriptor, Selector>& src_container,
313 OptionDescriptor, Selector>& dest_container)
const {
315 std::list<Selector> selectors = src_container.getOptionSpaceNames();
320 for (
auto const& it : selectors) {
328 for (
auto const& src_opt : *src_all) {
331 idx.equal_range(src_opt.option_->getType());
334 if (std::distance(range.first, range.second) == 0) {
335 dest_container.addItem(OptionDescriptor(src_opt), it);
343 return (options_.
getItems(option_space));
348 return (vendor_options_.
getItems(vendor_id));
355 return (
getAll(vendor_id));
357 return (
getAll(option_space));
364 if (!options || options->empty()) {
376 for (
auto const& option_space_from_list : option_space_names) {
378 auto const& options_in_space =
getAll(option_space_from_list);
379 for (
auto const& option_it : *options_in_space) {
383 if (option_it.option_ &&
384 (option_it.option_->getEncapsulatedSpace() == option_space)) {
385 option_it.option_->delOption(option_code);
391 auto& idx = options->get<1>();
392 return (idx.erase(option_code));
399 if (!vendor_options || vendor_options->empty()) {
404 auto& idx = vendor_options->get<1>();
405 return (idx.erase(option_code));
415 auto const& options =
getAll(space_name);
416 for (
auto const& option_it : *options) {
417 if (!option_it.option_) {
423 auto sub_options = option_it.option_->getOptions();
424 for (
auto const& sub : sub_options) {
426 option_it.option_->delOption(sub.second->getType());
439 return (num_deleted);
453 for (
auto const& name : names) {
455 for (
auto const& opt : *opts) {
459 opt.contextToElement(map);
463 uint16_t code = opt.option_->getType();
477 if (!opt.formatted_value_.empty()) {
485 std::vector<uint8_t> bin = opt.option_->toBinary();
486 if (!opt.cancelled_ || !bin.empty()) {
501 if (include_metadata) {
502 map->set(
"metadata", opt.getMetadata());
511 for (
auto const&
id : ids) {
513 for (
auto const& opt : *opts) {
517 opt.contextToElement(map);
519 std::ostringstream oss;
520 oss <<
"vendor-" << id;
523 uint16_t code = opt.option_->getType();
537 if (!opt.formatted_value_.empty()) {
545 std::vector<uint8_t> bin = opt.option_->toBinary();
546 if (!opt.cancelled_ || !bin.empty()) {
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a function is called in a prohibited way.
static ElementPtr create(const Position &pos=ZERO_POSITION())
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Represents option data configuration for the DHCP server.
bool isEncapsulated() const
Checks if options have been encapsulated.
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.
isc::data::ElementPtr toElementWithMetadata(const bool include_metadata) const
Unparse a configuration object with optionally including the metadata.
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.
bool empty() const
Indicates the object is empty.
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
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.
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.
static OptionDefinitionPtr getOptionDef(const std::string &space, const uint16_t code)
Return the first option definition matching a particular option code.
static OptionDefinitionPtr getVendorOptionDef(const Option::Universe u, const uint32_t vendor_id, const uint16_t code)
Returns vendor option definition for a given vendor-id and code.
static uint32_t optionSpaceToVendorId(const std::string &option_space)
Converts option space name to vendor id.
static OptionDefinitionPtr getRuntimeOptionDef(const std::string &space, const uint16_t code)
Returns runtime (non-standard) option definition by space and option code.
static OptionDefinitionPtr getLastResortOptionDef(const std::string &space, const uint16_t code)
Returns last resort option definition by space and option code.
OptionPtr option_
Option instance.
std::string space_name_
Option space name.
bool equals(const OptionDescriptor &other) const
Checks if the one descriptor is equal to another.
bool cancelled_
Cancelled flag.
std::string formatted_value_
Option value in textual (CSV) format.
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.
bool persistent_
Persistence flag.
uint64_t deleteItems(const uint64_t id)
Remove all options or option definitions with a given database identifier.
void addItem(const ItemType &item, const Selector &option_space)
Adds a new item to the option_space.
bool empty() const
Indicates the container is empty.
std::list< Selector > getOptionSpaceNames() const
Get a list of existing option spaces.
ItemsContainerPtr getItems(const Selector &option_space) const
Get all items for the particular option space.
bool equals(const OptionSpaceContainer &other) const
Check if two containers are equal.
static bool validateName(const std::string &name)
Checks that the provided option space name is valid.
Universe
defines option universe DHCPv4 or DHCPv6
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< Element > ElementPtr
std::pair< OptionContainerTypeIndex::const_iterator, OptionContainerTypeIndex::const_iterator > OptionContainerTypeRange
Pair of iterators to represent the range of options having the same option type value.
OptionContainer::nth_index< 1 >::type OptionContainerTypeIndex
Type of the index #1 - option type.
boost::shared_ptr< CfgOptionDef > CfgOptionDefPtr
Non-const pointer.
boost::shared_ptr< OptionDefinition > OptionDefinitionPtr
Pointer to option definition object.
boost::shared_ptr< OptionDescriptor > OptionDescriptorPtr
A pointer to option descriptor.
boost::shared_ptr< OptionContainer > OptionContainerPtr
Pointer to the OptionContainer object.
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.
boost::shared_ptr< Option > OptionPtr
string encodeHex(const vector< uint8_t > &binary)
Encode binary data in the base16 format.
Defines the logger used by the top-level component of kea-lfc.
#define DHCP4_OPTION_SPACE
global std option spaces
#define DHCP6_OPTION_SPACE