Commit b8eb5a85 authored by Francis Dupont's avatar Francis Dupont

[128-netconf-config] Addressed comments

parent 73cafb6a
......@@ -21,7 +21,7 @@ namespace isc {
namespace netconf {
NetconfConfig::NetconfConfig()
: servers_map_(new ServersMap()) {
: servers_map_(new CfgServersMap()) {
}
NetconfConfig::NetconfConfig(const NetconfConfig& orig)
......@@ -45,7 +45,7 @@ NetconfCfgMgr::getConfigSummary(const uint32_t /*selection*/) {
std::ostringstream s;
// Then print managed servers.
for (auto serv : *ctx->getServersMap()) {
for (auto serv : *ctx->getCfgServersMap()) {
if (s.tellp() != 0) {
s << " ";
}
......
......@@ -38,14 +38,14 @@ public:
/// @brief Returns non-const reference to the managed servers map.
///
/// @return non-const reference to the managed servers map.
ServersMapPtr& getServersMap() {
CfgServersMapPtr& getCfgServersMap() {
return (servers_map_);
}
/// @brief Returns const reference to the managed servers map.
///
/// @return const reference to the managed servers map.
const ServersMapPtr& getServersMap() const {
const CfgServersMapPtr& getCfgServersMap() const {
return (servers_map_);
}
......@@ -88,8 +88,8 @@ private:
/// @param rhs Context to be assigned.
NetconfConfig& operator=(const NetconfConfig& rhs);
/// @brief Servers map.
ServersMapPtr servers_map_;
/// @brief CfgServers map.
CfgServersMapPtr servers_map_;
/// @brief Configured hooks libraries.
isc::hooks::HooksConfig hooks_config_;
......
......@@ -26,43 +26,45 @@ using namespace isc::http;
namespace isc {
namespace netconf {
// *********************** ControlSocket *************************
// *********************** CfgControlSocket *************************
ControlSocket::ControlSocket(Type type, const string& name, const Url& url)
CfgControlSocket::CfgControlSocket(Type type, const string& name,
const Url& url)
: type_(type), name_(name), url_(url) {
}
ControlSocket::~ControlSocket() {
CfgControlSocket::~CfgControlSocket() {
}
ControlSocket::Type
ControlSocket::stringToType(const string& type) {
CfgControlSocket::Type
CfgControlSocket::stringToType(const string& type) {
if (type == "unix") {
return (ControlSocket::Type::UNIX);
return (CfgControlSocket::Type::UNIX);
} else if (type == "http") {
return (ControlSocket::Type::HTTP);
return (CfgControlSocket::Type::HTTP);
} else if (type == "stdout") {
return (ControlSocket::Type::STDOUT);
return (CfgControlSocket::Type::STDOUT);
}
isc_throw(BadValue, "Unknown control socket type: " << type);
}
const string
ControlSocket::typeToString(ControlSocket::Type type) {
CfgControlSocket::typeToString(CfgControlSocket::Type type) {
switch (type) {
case ControlSocket::Type::UNIX:
case CfgControlSocket::Type::UNIX:
return ("unix");
case ControlSocket::Type::HTTP:
case CfgControlSocket::Type::HTTP:
return ("http");
case ControlSocket::Type::STDOUT:
case CfgControlSocket::Type::STDOUT:
return ("stdout");
default:
isc_throw(BadValue, "Unknown control socket type: " << type);
}
/*UNREACHED*/
}
ElementPtr
ControlSocket::toElement() const {
CfgControlSocket::toElement() const {
ElementPtr result = Element::createMap();
// Set user-context
contextToElement(result);
......@@ -75,29 +77,29 @@ ControlSocket::toElement() const {
return (result);
}
// *********************** Server *************************
Server::Server(const string& model, ControlSocketPtr ctrl_sock)
// *********************** CfgServer *************************
CfgServer::CfgServer(const string& model, CfgControlSocketPtr ctrl_sock)
: model_(model), control_socket_(ctrl_sock) {
}
Server::~Server() {
CfgServer::~CfgServer() {
}
string
Server::toText() const {
CfgServer::toText() const {
ostringstream s;
s << "model: " << model_ << ", control socker: ";
if (!control_socket_) {
s << "none";
} else {
switch (control_socket_->getType()) {
case ControlSocket::Type::UNIX:
case CfgControlSocket::Type::UNIX:
s << "UNIX:'" << control_socket_->getName() << "'";
break;
case ControlSocket::Type::HTTP:
case CfgControlSocket::Type::HTTP:
s << "HTTP:'" << control_socket_->getUrl().toText() << "'";
break;
case ControlSocket::Type::STDOUT:
case CfgControlSocket::Type::STDOUT:
s << "STDOUT";
break;
}
......@@ -106,7 +108,7 @@ Server::toText() const {
}
ElementPtr
Server::toElement() const {
CfgServer::toElement() const {
ElementPtr result = Element::createMap();
// Set user-context
contextToElement(result);
......@@ -120,45 +122,45 @@ Server::toElement() const {
}
ostream&
operator<<(ostream& os, const Server& server) {
operator<<(ostream& os, const CfgServer& server) {
os << server.toText();
return (os);
}
// *************************** PARSERS ***********************************
// *********************** ControlSocketParser *************************
// *********************** ControlSocketConfigParser *************************
ControlSocketPtr
ControlSocketParser::parse(ConstElementPtr ctrl_sock_config) {
ControlSocketPtr result;
CfgControlSocketPtr
ControlSocketConfigParser::parse(ConstElementPtr ctrl_sock_config) {
CfgControlSocketPtr result;
string type_str = getString(ctrl_sock_config, "socket-type");
string name = getString(ctrl_sock_config, "socket-name");
string url_str = getString(ctrl_sock_config, "socket-url");
ConstElementPtr user_context = ctrl_sock_config->get("user-context");
// Type must be valid.
ControlSocket::Type type;
CfgControlSocket::Type type;
try {
type = ControlSocket::stringToType(type_str);
type = CfgControlSocket::stringToType(type_str);
} catch (const std::exception& ex) {
isc_throw(NetconfCfgError, ex.what() << " '" << type_str << "' ("
isc_throw(ConfigError, ex.what() << " '" << type_str << "' ("
<< getPosition("socket-type", ctrl_sock_config) << ")");
}
// Url must be valid.
Url url(url_str);
if (!url.isValid()) {
isc_throw(NetconfCfgError, "invalid control socket url: "
isc_throw(ConfigError, "invalid control socket url: "
<< url.getErrorMessage() << " '" << url_str << "' ("
<< getPosition("socket-url", ctrl_sock_config) << ")");
}
// Create the control socket.
try {
result.reset(new ControlSocket(type, name, url));
result.reset(new CfgControlSocket(type, name, url));
} catch (const std::exception& ex) {
isc_throw(NetconfCfgError, ex.what() << " ("
isc_throw(ConfigError, ex.what() << " ("
<< ctrl_sock_config->getPosition() << ")");
}
......@@ -170,23 +172,23 @@ ControlSocketParser::parse(ConstElementPtr ctrl_sock_config) {
return (result);
}
// *********************** ServerParser *************************
// *********************** ServerConfigParser *************************
ServerPtr
ServerParser::parse(ConstElementPtr server_config) {
ServerPtr result;
CfgServerPtr
ServerConfigParser::parse(ConstElementPtr server_config) {
CfgServerPtr result;
string model = getString(server_config, "model");
ConstElementPtr user_context = server_config->get("user-context");
ConstElementPtr ctrl_sock_config = server_config->get("control-socket");
ControlSocketPtr ctrl_sock;
CfgControlSocketPtr ctrl_sock;
if (ctrl_sock_config) {
ControlSocketParser parser;
ControlSocketConfigParser parser;
ctrl_sock = parser.parse(ctrl_sock_config);
}
try {
result.reset(new Server(model, ctrl_sock));
result.reset(new CfgServer(model, ctrl_sock));
} catch (const std::exception& ex) {
isc_throw(NetconfCfgError, ex.what() << " ("
isc_throw(ConfigError, ex.what() << " ("
<< server_config->getPosition() << ")");
}
......
......@@ -35,11 +35,11 @@ namespace netconf {
///
/// The parsing class hierarchy reflects this same scheme. Working top down:
///
/// A ServerMapParser handles the managed servers map invoking a ServerParser
/// to parse each server.
/// A ServerMapParser handles the managed servers map invoking a
/// ServerConfigParser to parse each server.
///
/// A ServerParser handles the scalars which belong to the server as well as
/// creating and invoking a CtrlSocketParser to parse its control socket.
/// A ServerConfigParser handles the scalars which belong to the server as well
/// as creating and invoking a CtrlSocketParser to parse its control socket.
///
/// A CtrlSocketParser handles the scalars which belong to the control socket.
///
......@@ -63,19 +63,11 @@ namespace netconf {
/// }
/// @endcode
/// @brief Exception thrown when the error during configuration handling
/// occurs.
class NetconfCfgError : public isc::Exception {
public:
NetconfCfgError(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) { };
};
/// @brief Represents a Control Socket.
///
/// Acts as a storage class containing the basic attributes which
/// describe a Control Socket.
class ControlSocket : public isc::data::UserContext,
class CfgControlSocket : public isc::data::UserContext,
public isc::data::CfgToElement {
public:
/// @brief Defines the list of possible constrol socket types.
......@@ -90,15 +82,15 @@ public:
/// @param type The socket type.
/// @param name The Unix socket name.
/// @param url The HTTP server URL.
ControlSocket(Type type, const std::string& name,
const isc::http::Url& url);
CfgControlSocket(Type type, const std::string& name,
const isc::http::Url& url);
/// @brief Destructor (doing nothing).
virtual ~ControlSocket();
virtual ~CfgControlSocket();
/// @brief Getter which returns the socket type.
///
/// @return returns the socket type as a ControlSocket::Type.
/// @return returns the socket type as a CfgControlSocket::Type.
Type getType() const {
return (type_);
}
......@@ -117,20 +109,20 @@ public:
return (url_);
}
/// @brief Converts socket type name to ControlSocket::Type.
/// @brief Converts socket type name to CfgControlSocket::Type.
///
/// @param type The type name.
/// Currently supported values are "unix", "http" and "stdout".
///
/// @return The ControlSocket::Type corresponding to the type name.
/// @return The CfgControlSocket::Type corresponding to the type name.
/// @throw BadValue if the type name isn't recognized.
static Type stringToType(const std::string& type);
/// @brief Converts ControlSocket::Type to string.
/// @brief Converts CfgControlSocket::Type to string.
///
/// @param type The ControlSocket::Type type.
/// @param type The CfgControlSocket::Type type.
/// @return The type name corresponding to the enumeration element.
static const std::string typeToString(ControlSocket::Type type);
static const std::string typeToString(CfgControlSocket::Type type);
/// @brief Unparse a configuration object
///
......@@ -148,23 +140,23 @@ private:
const isc::http::Url url_;
};
/// @brief Defines a pointer for ControlSocket instances.
typedef boost::shared_ptr<ControlSocket> ControlSocketPtr;
/// @brief Defines a pointer for CfgControlSocket instances.
typedef boost::shared_ptr<CfgControlSocket> CfgControlSocketPtr;
/// @brief Represents a Managed Server.
/// @brief Represents a Managed CfgServer.
///
/// Acts as a storage class containing the basic attributes and
/// the Control Socket which describe a Managed Server.
class Server : public isc::data::UserContext, public isc::data::CfgToElement {
/// the Control Socket which describe a Managed CfgServer.
class CfgServer : public isc::data::UserContext, public isc::data::CfgToElement {
public:
/// @brief Constructor.
///
/// @param model The model name.
/// @param ctrl_sock The control socket.
Server(const std::string& model, ControlSocketPtr ctrl_sock);
CfgServer(const std::string& model, CfgControlSocketPtr ctrl_sock);
/// @brief Destructor (doing nothing).
virtual ~Server();
virtual ~CfgServer();
/// @brief Getter which returns the model name.
///
......@@ -175,8 +167,8 @@ public:
/// @brief Getter which returns the control socket.
///
/// @return returns the control socket as a ControlSocketPtr.
const ControlSocketPtr& getControlSocket() const {
/// @return returns the control socket as a CfgControlSocketPtr.
const CfgControlSocketPtr& getCfgControlSocket() const {
return (control_socket_);
}
......@@ -193,60 +185,60 @@ private:
const std::string model_;
/// @brief The control socket.
ControlSocketPtr control_socket_;
CfgControlSocketPtr control_socket_;
};
/// @brief Defines a pointer for Server instances.
typedef boost::shared_ptr<Server> ServerPtr;
/// @brief Defines a pointer for CfgServer instances.
typedef boost::shared_ptr<CfgServer> CfgServerPtr;
/// @brief Defines a map of Servers, keyed by the name.
typedef std::map<std::string, ServerPtr> ServersMap;
/// @brief Defines a map of CfgServers, keyed by the name.
typedef std::map<std::string, CfgServerPtr> CfgServersMap;
/// @brief Defines a iterator pairing of name and Server
typedef std::pair<std::string, ServerPtr> ServersMapPair;
/// @brief Defines a iterator pairing of name and CfgServer
typedef std::pair<std::string, CfgServerPtr> CfgServersMapPair;
/// @brief Defines a pointer to map of Servers.
typedef boost::shared_ptr<ServersMap> ServersMapPtr;
/// @brief Defines a pointer to map of CfgServers.
typedef boost::shared_ptr<CfgServersMap> CfgServersMapPtr;
/// @brief Dumps the contents of a Server as text to a output stream.
/// @brief Dumps the contents of a CfgServer as text to a output stream.
///
/// @param os The output stream to which text should be sent.
/// @param server The Server instance to dump.
std::ostream& operator<<(std::ostream& os, const Server& server);
/// @param server The CfgServer instance to dump.
std::ostream& operator<<(std::ostream& os, const CfgServer& server);
/// @brief Parser for ControlSocket.
/// @brief Parser for CfgControlSocket.
///
/// This class parses the configuration element "control-socket"
/// and creates an instance of a ControlSocket.
class ControlSocketParser : public data::SimpleParser {
/// and creates an instance of a CfgControlSocket.
class ControlSocketConfigParser : public data::SimpleParser {
public:
/// @brief Performs the actual parsing of the given "control-socket" element.
///
/// Parses a configuration for the elements needed to instantiate a
/// ControlSocket, validates those entries, creates a ControlSocket
/// CfgControlSocket, validates those entries, creates a CfgControlSocket
/// instance.
///
/// @param ctrl_sock_config is the "control-socket" configuration to parse.
///
/// @return pointer to the new ControlSocket instance.
ControlSocketPtr parse(data::ConstElementPtr ctrl_sock_config);
/// @return pointer to the new CfgControlSocket instance.
CfgControlSocketPtr parse(data::ConstElementPtr ctrl_sock_config);
};
/// @brief Parser for Server.
/// @brief Parser for CfgServer.
///
/// This class parses the configuration value from the "managed-servers" map
/// and creates an instance of a Server.
class ServerParser : public data::SimpleParser {
/// and creates an instance of a CfgServer.
class ServerConfigParser : public data::SimpleParser {
public:
/// @brief Performs the actual parsing of the given value from
/// the "managed-servers" map.
///
/// Parses a configuration for the elements needed to instantiate a
/// Server, validates those entries, creates a Server instance.
/// CfgServer, validates those entries, creates a CfgServer instance.
///
/// @param server_config is the value from the "managed-servers" map to parse.
/// @return pointer to the new Server instance.
ServerPtr parse(data::ConstElementPtr server_config);
/// @return pointer to the new CfgServer instance.
CfgServerPtr parse(data::ConstElementPtr server_config);
};
}; // end of isc::netconf namespace
......
......@@ -71,7 +71,7 @@ const SimpleDefaults NetconfSimpleParser::CA_DEFAULTS = {
/// --- end of default values -------------------------------------------------
/// ---------------------------------------------------------------------------
size_t NetconfSimpleParser::setAllDefaults(const isc::data::ElementPtr& global) {
size_t NetconfSimpleParser::setAllDefaults(const ElementPtr& global) {
size_t cnt = 0;
// Set global defaults first.
......@@ -89,10 +89,10 @@ size_t NetconfSimpleParser::setAllDefaults(const isc::data::ElementPtr& global)
size_t
NetconfSimpleParser::setServerDefaults(const std::string name,
isc::data::ConstElementPtr server) {
ConstElementPtr server) {
size_t cnt = 0;
isc::data::ElementPtr mutable_server =
ElementPtr mutable_server =
boost::const_pointer_cast<Element>(server);
if (name == "dhcp4") {
cnt += setDefaults(mutable_server, DHCP4_DEFAULTS);
......@@ -104,11 +104,11 @@ NetconfSimpleParser::setServerDefaults(const std::string name,
cnt += setDefaults(mutable_server, CA_DEFAULTS);
}
isc::data::ConstElementPtr ctrl_sock = server->get("control-socket");
ConstElementPtr ctrl_sock = server->get("control-socket");
if (!ctrl_sock) {
return (cnt);
}
isc::data::ElementPtr mutable_ctrl_sock =
ElementPtr mutable_ctrl_sock =
boost::const_pointer_cast<Element>(ctrl_sock);
cnt += setDefaults(mutable_ctrl_sock, CTRL_SOCK_DEFAULTS);
......@@ -117,7 +117,7 @@ NetconfSimpleParser::setServerDefaults(const std::string name,
void
NetconfSimpleParser::parse(const NetconfConfigPtr& ctx,
const isc::data::ConstElementPtr& config,
const ConstElementPtr& config,
bool check_only) {
// User context can be done at anytime.
......@@ -130,9 +130,9 @@ NetconfSimpleParser::parse(const NetconfConfigPtr& ctx,
ConstElementPtr servers = config->get("managed-servers");
if (servers) {
for (auto it : servers->mapValue()) {
ServerParser server_parser;
ServerPtr server = server_parser.parse(it.second);
ctx->getServersMap()->insert(make_pair(it.first, server));
ServerConfigParser server_parser;
CfgServerPtr server = server_parser.parse(it.second);
ctx->getCfgServersMap()->insert(make_pair(it.first, server));
}
}
......
/netconf_tests.sh
netconf_unittests
test_data_files_config.h
test_libraries.h
......@@ -221,7 +221,7 @@ public:
EXPECT_TRUE(executeConfiguration(config, "reset config"));
}
boost::scoped_ptr<NakedNetconfCfgMgr> srv_; ///< CA server under test
boost::scoped_ptr<NakedNetconfCfgMgr> srv_; ///< Netconf server under test
int rcode_; ///< Return code from element parsing
ConstElementPtr comment_; ///< Reason for parse fail
};
......
......@@ -11,6 +11,7 @@
#include <cc/command_interpreter.h>
#include <process/testutils/d_test_stubs.h>
#include <process/d_cfg_mgr.h>
#include <yang/yang_models.h>
#include <netconf/tests/test_libraries.h>
#include <boost/scoped_ptr.hpp>
#include <gtest/gtest.h>
......@@ -22,6 +23,7 @@ using namespace isc::data;
using namespace isc::hooks;
using namespace isc::http;
using namespace isc::process;
using namespace isc::yang;
namespace {
......@@ -63,52 +65,56 @@ TEST(NetconfCfgMgr, contextServer) {
// Check managed server parameters.
// By default, there are no server stored.
ASSERT_TRUE(ctx.getServersMap());
EXPECT_EQ(0, ctx.getServersMap()->size());
ControlSocketPtr socket1(new ControlSocket(ControlSocket::Type::UNIX,
"socket1",
Url("http://127.0.0.1:8000/")));
ServerPtr server1(new Server("model1", socket1));
ControlSocketPtr socket2(new ControlSocket(ControlSocket::Type::UNIX,
"socket2",
Url("http://127.0.0.1:8000/")));
ServerPtr server2(new Server("model2", socket2));
ControlSocketPtr socket3(new ControlSocket(ControlSocket::Type::UNIX,
"socket3",
Url("http://127.0.0.1:8000/")));
ServerPtr server3(new Server("model3", socket3));
ControlSocketPtr socket4(new ControlSocket(ControlSocket::Type::UNIX,
"socket4",
Url("http://127.0.0.1:8000/")));
ServerPtr server4(new Server("model4", socket4));
ASSERT_TRUE(ctx.getCfgServersMap());
EXPECT_EQ(0, ctx.getCfgServersMap()->size());
CfgControlSocketPtr
socket1(new CfgControlSocket(CfgControlSocket::Type::UNIX,
"socket1",
Url("http://127.0.0.1:8000/")));
CfgServerPtr server1(new CfgServer("model1", socket1));
CfgControlSocketPtr
socket2(new CfgControlSocket(CfgControlSocket::Type::UNIX,
"socket2",
Url("http://127.0.0.1:8000/")));
CfgServerPtr server2(new CfgServer("model2", socket2));
CfgControlSocketPtr
socket3(new CfgControlSocket(CfgControlSocket::Type::UNIX,
"socket3",
Url("http://127.0.0.1:8000/")));
CfgServerPtr server3(new CfgServer("model3", socket3));
CfgControlSocketPtr
socket4(new CfgControlSocket(CfgControlSocket::Type::UNIX,
"socket4",
Url("http://127.0.0.1:8000/")));
CfgServerPtr server4(new CfgServer("model4", socket4));
// Ok, now set the server for D2
EXPECT_NO_THROW(ctx.getServersMap()->insert(make_pair("d2", server1)));
EXPECT_NO_THROW(ctx.getCfgServersMap()->insert(make_pair("d2", server1)));
// Now check the values returned
EXPECT_EQ(1, ctx.getServersMap()->size());
ASSERT_NO_THROW(ctx.getServersMap()->at("d2"));
EXPECT_EQ(server1, ctx.getServersMap()->at("d2"));
EXPECT_THROW(ctx.getServersMap()->at("dhcp4"), std::out_of_range);
EXPECT_EQ(1, ctx.getCfgServersMap()->size());
ASSERT_NO_THROW(ctx.getCfgServersMap()->at("d2"));
EXPECT_EQ(server1, ctx.getCfgServersMap()->at("d2"));
EXPECT_THROW(ctx.getCfgServersMap()->at("dhcp4"), std::out_of_range);
// Now set the v6 server and sanity check again
EXPECT_NO_THROW(ctx.getServersMap()->insert(make_pair("dhcp6", server2)));
EXPECT_NO_THROW(ctx.getCfgServersMap()->insert(make_pair("dhcp6", server2)));
// Should be possible to retrieve two servers
EXPECT_EQ(2, ctx.getServersMap()->size());
ASSERT_NO_THROW(ctx.getServersMap()->at("dhcp6"));
EXPECT_EQ(server1, ctx.getServersMap()->at("d2"));
EXPECT_EQ(server2, ctx.getServersMap()->at("dhcp6"));
EXPECT_EQ(2, ctx.getCfgServersMap()->size());
ASSERT_NO_THROW(ctx.getCfgServersMap()->at("dhcp6"));
EXPECT_EQ(server1, ctx.getCfgServersMap()->at("d2"));
EXPECT_EQ(server2, ctx.getCfgServersMap()->at("dhcp6"));
// Finally, set all servers.
EXPECT_NO_THROW(ctx.getServersMap()->insert(make_pair("dhcp4", server3)));
EXPECT_NO_THROW(ctx.getServersMap()->insert(make_pair("ca", server4)));
EXPECT_EQ(4, ctx.getServersMap()->size());
ASSERT_NO_THROW(ctx.getServersMap()->at("dhcp4"));
ASSERT_NO_THROW(ctx.getServersMap()->at("ca"));
EXPECT_EQ(server3, ctx.getServersMap()->at("dhcp4"));
EXPECT_EQ(server4, ctx.getServersMap()->at("ca"));
EXPECT_NO_THROW(ctx.getCfgServersMap()->insert(make_pair("dhcp4", server3)));
EXPECT_NO_THROW(ctx.getCfgServersMap()->insert(make_pair("ca", server4)));
EXPECT_EQ(4, ctx.getCfgServersMap()->size());
ASSERT_NO_THROW(ctx.getCfgServersMap()->at("dhcp4"));
ASSERT_NO_THROW(ctx.getCfgServersMap()->at("ca"));
EXPECT_EQ(server3, ctx.getCfgServersMap()->at("dhcp4"));
EXPECT_EQ(server4, ctx.getCfgServersMap()->at("ca"));
}
// Tests if the context can store and retrieve hook libs information.
......@@ -336,8 +342,8 @@ TEST_F(NetconfParserTest, configParseEmpty) {
NetconfConfigPtr ctx = cfg_mgr_.getNetconfConfig();
ASSERT_TRUE(ctx);
ASSERT_TRUE(ctx->getServersMap());
EXPECT_EQ(0, ctx->getServersMap()->size());
ASSERT_TRUE(ctx->getCfgServersMap());
EXPECT_EQ(0, ctx->getCfgServersMap()->size());
}