Commit 835aeee9 authored by Marcin Siodelski's avatar Marcin Siodelski Committed by Francis Dupont

[#676] Modified internal representation of the tags in ServerSelector.

parent 29eef3fb
......@@ -143,10 +143,10 @@ public:
const std::string& name) {
StampedValueCollection parameters;
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createString(name)
};
......@@ -529,10 +529,10 @@ public:
const SubnetID& subnet_id) {
Subnet4Collection subnets;
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createInteger<uint32_t>(subnet_id)
};
......@@ -555,10 +555,10 @@ public:
const std::string& subnet_prefix) {
Subnet4Collection subnets;
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createString(subnet_prefix)
};
......@@ -575,11 +575,11 @@ public:
/// subnets should be inserted.
void getAllSubnets4(const ServerSelector& server_selector,
Subnet4Collection& subnets) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag)
MySqlBinding::createString(tag.get())
};
getSubnets4(GET_ALL_SUBNETS4, in_bindings, subnets);
......@@ -595,11 +595,11 @@ public:
void getModifiedSubnets4(const ServerSelector& server_selector,
const boost::posix_time::ptime& modification_ts,
Subnet4Collection& subnets) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createTimestamp(modification_ts)
};
......@@ -617,11 +617,11 @@ public:
void getSharedNetworkSubnets4(const ServerSelector& server_selector,
const std::string& shared_network_name,
Subnet4Collection& subnets) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createString(shared_network_name)
};
......@@ -1252,11 +1252,11 @@ public:
/// structure where shared networks should be inserted.
void getAllSharedNetworks4(const ServerSelector& server_selector,
SharedNetwork4Collection& shared_networks) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag)
MySqlBinding::createString(tag.get())
};
getSharedNetworks4(GET_ALL_SHARED_NETWORKS4, in_bindings, shared_networks);
......@@ -1272,11 +1272,11 @@ public:
void getModifiedSharedNetworks4(const ServerSelector& server_selector,
const boost::posix_time::ptime& modification_ts,
SharedNetwork4Collection& shared_networks) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createTimestamp(modification_ts)
};
......@@ -1407,7 +1407,7 @@ public:
// a server into the dhcp4_options_server table.
MySqlBindingCollection in_server_bindings = {
MySqlBinding::createInteger<uint64_t>(id), // option_id
MySqlBinding::createString(*getServerTags(server_selector).begin()), // server_tag
MySqlBinding::createString(server_selector.getTags().begin()->get()), // server_tag
in_bindings[11] // copy modification timestamp from option
};
......@@ -2609,9 +2609,9 @@ StampedValueCollection
MySqlConfigBackendDHCPv4::getAllGlobalParameters4(const ServerSelector& server_selector) const {
LOG_DEBUG(mysql_cb_logger, DBGLVL_TRACE_BASIC, MYSQL_CB_GET_ALL_GLOBAL_PARAMETERS4);
StampedValueCollection parameters;
auto tags = impl_->getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = { MySqlBinding::createString(tag) };
MySqlBindingCollection in_bindings = { MySqlBinding::createString(tag.get()) };
impl_->getGlobalParameters(MySqlConfigBackendDHCPv4Impl::GET_ALL_GLOBAL_PARAMETERS4,
in_bindings, parameters);
}
......@@ -2626,10 +2626,10 @@ MySqlConfigBackendDHCPv4::getModifiedGlobalParameters4(const db::ServerSelector&
LOG_DEBUG(mysql_cb_logger, DBGLVL_TRACE_BASIC, MYSQL_CB_GET_MODIFIED_GLOBAL_PARAMETERS4)
.arg(util::ptimeToText(modification_time));
StampedValueCollection parameters;
auto tags = impl_->getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createTimestamp(modification_time)
};
impl_->getGlobalParameters(MySqlConfigBackendDHCPv4Impl::GET_MODIFIED_GLOBAL_PARAMETERS4,
......
......@@ -150,10 +150,10 @@ public:
const std::string& name) {
StampedValueCollection parameters;
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createString(name)
};
......@@ -585,10 +585,10 @@ public:
const SubnetID& subnet_id) {
Subnet6Collection subnets;
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createInteger<uint32_t>(subnet_id)
};
......@@ -611,10 +611,10 @@ public:
const std::string& subnet_prefix) {
Subnet6Collection subnets;
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createString(subnet_prefix)
};
......@@ -631,11 +631,11 @@ public:
/// subnets should be inserted.
void getAllSubnets6(const ServerSelector& server_selector,
Subnet6Collection& subnets) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag)
MySqlBinding::createString(tag.get())
};
getSubnets6(GET_ALL_SUBNETS6, in_bindings, subnets);
......@@ -651,11 +651,11 @@ public:
void getModifiedSubnets6(const ServerSelector& server_selector,
const boost::posix_time::ptime& modification_ts,
Subnet6Collection& subnets) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createTimestamp(modification_ts)
};
......@@ -673,11 +673,11 @@ public:
void getSharedNetworkSubnets6(const ServerSelector& server_selector,
const std::string& shared_network_name,
Subnet6Collection& subnets) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createString(shared_network_name)
};
......@@ -1452,11 +1452,11 @@ public:
/// structure where shared networks should be inserted.
void getAllSharedNetworks6(const ServerSelector& server_selector,
SharedNetwork6Collection& shared_networks) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag)
MySqlBinding::createString(tag.get())
};
getSharedNetworks6(GET_ALL_SHARED_NETWORKS6, in_bindings, shared_networks);
......@@ -1472,11 +1472,11 @@ public:
void getModifiedSharedNetworks6(const ServerSelector& server_selector,
const boost::posix_time::ptime& modification_ts,
SharedNetwork6Collection& shared_networks) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createTimestamp(modification_ts)
};
......@@ -1618,7 +1618,7 @@ public:
// a server into the dhcp6_options_server table.
MySqlBindingCollection in_server_bindings = {
MySqlBinding::createInteger<uint64_t>(id), // option_id
MySqlBinding::createString(*getServerTags(server_selector).begin()), // server_tag
MySqlBinding::createString(server_selector.getTags().begin()->get()), // server_tag
in_bindings[11] // copy modification timestamp from option
};
......@@ -2971,9 +2971,9 @@ StampedValueCollection
MySqlConfigBackendDHCPv6::getAllGlobalParameters6(const ServerSelector& server_selector) const {
LOG_DEBUG(mysql_cb_logger, DBGLVL_TRACE_BASIC, MYSQL_CB_GET_ALL_GLOBAL_PARAMETERS6);
StampedValueCollection parameters;
auto tags = impl_->getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = { MySqlBinding::createString(tag) };
MySqlBindingCollection in_bindings = { MySqlBinding::createString(tag.get()) };
impl_->getGlobalParameters(MySqlConfigBackendDHCPv6Impl::GET_ALL_GLOBAL_PARAMETERS6,
in_bindings, parameters);
}
......@@ -2988,10 +2988,10 @@ MySqlConfigBackendDHCPv6::getModifiedGlobalParameters6(const db::ServerSelector&
LOG_DEBUG(mysql_cb_logger, DBGLVL_TRACE_BASIC, MYSQL_CB_GET_MODIFIED_GLOBAL_PARAMETERS6)
.arg(util::ptimeToText(modification_time));
StampedValueCollection parameters;
auto tags = impl_->getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createTimestamp(modification_time)
};
impl_->getGlobalParameters(MySqlConfigBackendDHCPv6Impl::GET_MODIFIED_GLOBAL_PARAMETERS6,
......
......@@ -193,13 +193,13 @@ MySqlConfigBackendImpl::getRecentAuditEntries(const int index,
MySqlBinding::createString(AUDIT_ENTRY_LOG_MESSAGE_BUF_LENGTH) // log_message
};
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
// There is only one input binding, modification time.
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createTimestamp(modification_time)
};
......@@ -341,10 +341,10 @@ void
MySqlConfigBackendImpl::getAllOptionDefs(const int index,
const ServerSelector& server_selector,
OptionDefContainer& option_defs) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag)
MySqlBinding::createString(tag.get())
};
getOptionDefs(index, in_bindings, option_defs);
}
......@@ -355,10 +355,10 @@ MySqlConfigBackendImpl::getModifiedOptionDefs(const int index,
const ServerSelector& server_selector,
const boost::posix_time::ptime& modification_time,
OptionDefContainer& option_defs) {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createTimestamp(modification_time)
};
getOptionDefs(index, in_bindings, option_defs);
......@@ -587,10 +587,10 @@ MySqlConfigBackendImpl::getAllOptions(const int index,
const ServerSelector& server_selector) {
OptionContainer options;
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag)
MySqlBinding::createString(tag.get())
};
getOptions(index, in_bindings, universe, options);
}
......@@ -605,10 +605,10 @@ MySqlConfigBackendImpl::getModifiedOptions(const int index,
const boost::posix_time::ptime& modification_time) {
OptionContainer options;
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
for (auto tag : tags) {
MySqlBindingCollection in_bindings = {
MySqlBinding::createString(tag),
MySqlBinding::createString(tag.get()),
MySqlBinding::createTimestamp(modification_time)
};
getOptions(index, in_bindings, universe, options);
......
......@@ -164,25 +164,6 @@ public:
const db::MySqlBindingPtr& min_binding,
const db::MySqlBindingPtr& max_binding);
/// @brief Returns server tags associated with the particular selector.
///
/// @param server_selector Server selector.
/// @return Set of server tags.
std::set<std::string> getServerTags(const db::ServerSelector& server_selector) const {
std::set<std::string> tags;
switch (server_selector.getType()) {
case db::ServerSelector::Type::ALL:
tags.insert("all");
return (tags);
default:
return (server_selector.getTags());
}
// Unassigned server case.
return (tags);
}
/// @brief Returns server tag associated with the particular selector.
///
/// This method expects that there is exactly one server tag associated with
......@@ -196,14 +177,14 @@ public:
/// is more than one server tag associated with the selector.
std::string getServerTag(const db::ServerSelector& server_selector,
const std::string& operation) const {
auto tags = getServerTags(server_selector);
auto tags = server_selector.getTags();
if (tags.size() != 1) {
isc_throw(InvalidOperation, "expected exactly one server tag to be specified"
" while " << operation << ". Got: "
<< getServerTagsAsText(server_selector));
}
return (*tags.begin());
return (tags.begin()->get());
}
/// @brief Returns server tags associated with the particular selector
......@@ -212,12 +193,12 @@ public:
/// This method is useful for logging purposes.
std::string getServerTagsAsText(const db::ServerSelector& server_selector) const {
std::ostringstream s;
auto server_tags = getServerTags(server_selector);
auto server_tags = server_selector.getTags();
for (auto tag : server_tags) {
if (s.tellp() != 0) {
s << ", ";
}
s << tag;
s << tag.get();
}
return (s.str());
......
......@@ -392,7 +392,7 @@ public:
} else if (tags.size() == 1) {
// Get the server tag for which we run the current test.
tag = *tags.begin();
tag = tags.begin()->get();
}
}
......
......@@ -432,7 +432,7 @@ public:
} else if (tags.size() == 1) {
// Get the server tag for which we run the current test.
tag = *tags.begin();
tag = tags.begin()->get();
}
}
......
......@@ -35,5 +35,11 @@ ServerTag::amAll() const {
return (tag_ == ALL);
}
std::ostream&
operator<<(std::ostream& os, const ServerTag& server_tag) {
os << server_tag.get();
return (os);
}
} // end of namespace isc::data
} // end of namespace isc
......@@ -50,12 +50,30 @@ public:
return (tag_);
}
/// @brief Overload of the less operator for using @c ServerTag in sets.
///
/// @param other other server tag to compare to.
/// @return true if this server tag is less than the other server tag.
bool operator<(const ServerTag& other) const {
return (tag_ < other.tag_);
}
private:
/// @brief Holds server tag as string.
std::string tag_;
};
/// @brief Insert the @c ServerTag as a string into stream.
///
/// @param os stream to insert server tag into.
/// @param server_tag server tag to be converted to text and
/// inserted into a stream.
/// @return Reference to the stream object with inserted server
/// tag.
std::ostream&
operator<<(std::ostream& os, const ServerTag& server_tag);
} // end of namespace isc::data
} // end of namespace isc
......
......@@ -20,7 +20,7 @@ libkea_database_la_SOURCES += db_log.cc db_log.h
libkea_database_la_SOURCES += db_messages.cc db_messages.h
libkea_database_la_SOURCES += server.cc server.h
libkea_database_la_SOURCES += server_collection.cc server_collection.h
libkea_database_la_SOURCES += server_selector.h
libkea_database_la_SOURCES += server_selector.cc server_selector.h
libkea_database_la_LIBADD = $(top_builddir)/src/lib/cc/libkea-cc.la
libkea_database_la_LIBADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la
......
// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <database/server_selector.h>
#include <exceptions/exceptions.h>
using namespace isc::data;
namespace isc {
namespace db {
ServerSelector
ServerSelector::MULTIPLE(const std::set<std::string>& server_tags) {
if (server_tags.empty()) {
isc_throw(InvalidOperation, "ServerSelector: expecting at least one"
" server tag");
}
std::set<ServerTag> tags;
// Create a set of tags from strings.
for (auto tag : server_tags) {
tags.insert(ServerTag(tag));
}
ServerSelector selector(tags);
return (selector);
}
ServerSelector::ServerSelector(const Type& type)
: type_(type), tags_() {
if (type_ == Type::ALL) {
tags_.insert(ServerTag());
}
}
ServerSelector::ServerSelector(const ServerTag& server_tag)
: type_(server_tag.amAll() ? Type::ALL : Type::SUBSET), tags_({server_tag}) {
}
ServerSelector::ServerSelector(const std::set<ServerTag>& server_tags)
: type_(Type::SUBSET), tags_(server_tags) {
}
} // end of namespace isc::db
} // end of namespace isc
// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
......@@ -7,6 +7,7 @@
#ifndef SERVER_SELECTOR_H
#define SERVER_SELECTOR_H
#include <cc/server_tag.h>
#include <set>
#include <string>
......@@ -68,17 +69,15 @@ public:
///
/// @param server_tag tag of the single server to be selected.
static ServerSelector ONE(const std::string& server_tag) {
ServerSelector selector(server_tag);
ServerSelector selector((data::ServerTag(server_tag)));
return (selector);
}
/// @brief Factory returning "multiple servers" selector.
///
/// @param server_tags set of server tags to be selected.
static ServerSelector MULTIPLE(const std::set<std::string>& server_tags) {
ServerSelector selector(server_tags);
return (selector);
}
/// @throw InvalidOperation if no server tags provided.
static ServerSelector MULTIPLE(const std::set<std::string>& server_tags);
/// @brief Returns type of the selector.
Type getType() const {
......@@ -89,7 +88,7 @@ public:
///
/// @return server tags for mutliple selections and for one server,
/// empty set for all servers and and unassigned.
std::set<std::string> getTags() const {
std::set<data::ServerTag> getTags() const {
return (tags_);
}
......@@ -100,35 +99,35 @@ public:
return (getType() == Type::UNASSIGNED);
}
/// @brief Convenience method checking if the server selector has multiple tags.
///
/// @return true if it has multiple tags, false otherwise.
bool hasMultipleTags() const {
return (tags_.size() > 1);
}
private:
/// @brief Constructor used for "unassigned" and "all" slection types.
///
/// @param type selector type.
explicit ServerSelector(const Type& type)
: type_(type), tags_() {
}
explicit ServerSelector(const Type& type);
/// @brief Constructor used for selecting a single server.
///
/// @param server_tag tag of the server to be selected.
explicit ServerSelector(const std::string& server_tag)
: type_(Type::SUBSET), tags_() {
tags_.insert(server_tag);
}
explicit ServerSelector(const data::ServerTag& server_tag);
/// @brief Constructor used for selecting multiple servers.
///
/// @param server_tags set of server tags.
explicit ServerSelector(const std::set<std::string>& server_tags)
: type_(Type::SUBSET), tags_(server_tags) {
}