blob: e90503a4b8f256f23f5f50c5dc6b026e88c5bdc0 [file] [log] [blame]
Nick Gordon4d2c6c02017-01-20 13:18:46 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22#include "nfd-rib-command-processor.hpp"
23#include "logger.hpp"
24
25namespace nlsr {
26namespace update {
27
28INIT_LOGGER("NfdRibProcessor");
29
30const ndn::PartialName REGISTER_VERB = ndn::PartialName("rib/register");
31const ndn::PartialName UNREGISTER_VERB = ndn::PartialName("rib/unregister");
32const ndn::Name COMMAND_PREFIX = ndn::Name("/localhost/nlsr");
33
34NfdRibCommandProcessor::NfdRibCommandProcessor(ndn::mgmt::Dispatcher& dispatcher,
35 NamePrefixList& namePrefixes,
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050036 Lsdb& lsdb)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060037 : m_dispatcher(dispatcher)
38 , m_namePrefixList(namePrefixes)
39 , m_lsdb(lsdb)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060040{
41}
42
43void
44NfdRibCommandProcessor::startListening()
45{
46 _LOG_DEBUG("Registering control command prefixes for: " << REGISTER_VERB <<
47 " and " << UNREGISTER_VERB);
48 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(REGISTER_VERB,
49 ndn::mgmt::makeAcceptAllAuthorization(),
50 std::bind(&NfdRibCommandProcessor::validate<NfdRibRegisterCommand>,
51 this, _1, REGISTER_VERB),
52 [this] (const ndn::Name& prefix, const ndn::Interest& interest,
53 const ndn::mgmt::ControlParameters& parameters,
54 const ndn::mgmt::CommandContinuation& done) {
55 _LOG_DEBUG("Params verified, calling insertPrefix()");
56 this->insertPrefix(parameters);
57 });
58 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(UNREGISTER_VERB,
59 ndn::mgmt::makeAcceptAllAuthorization(),
60 std::bind(&NfdRibCommandProcessor::validate<NfdRibUnregisterCommand>,
61 this, _1, UNREGISTER_VERB),
62 [this] (const ndn::Name& prefix, const ndn::Interest& interest,
63 const ndn::mgmt::ControlParameters& parameters,
64 const ndn::mgmt::CommandContinuation& done) {
65 _LOG_DEBUG("Params verified, calling removePrefix()");
66 this->removePrefix(parameters);
67 });
68}
69
70template<typename T>
71bool
72NfdRibCommandProcessor::validate(const ndn::mgmt::ControlParameters& parameters,
73 const ndn::PartialName& command)
74{
75 _LOG_DEBUG("Attempting to verify incoming params for " << command <<
76 " command...");
77 bool wasValidated = true;
78 try {
79 wasValidated = this->validateParameters<T>(parameters);
80 } catch (const ndn::nfd::ControlCommand::ArgumentError& ae) {
81 _LOG_DEBUG("Could not parse params. Message: " << ae.what());
82 wasValidated = false;
83 }
84 return wasValidated;
85}
86
87void
88NfdRibCommandProcessor::insertPrefix(const ndn::mgmt::ControlParameters& parameters)
89{
90 const ndn::nfd::ControlParameters& castParams =
91 static_cast<const ndn::nfd::ControlParameters&>(parameters);
92
93 _LOG_DEBUG("Inserting prefix into the FIB: " << castParams.getName() << "\n");
94
95 if (m_namePrefixList.insert(castParams.getName())) {
96 m_lsdb.buildAndInstallOwnNameLsa();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060097 }
98}
99
100void
101NfdRibCommandProcessor::removePrefix(const ndn::mgmt::ControlParameters& parameters)
102{
103 const ndn::nfd::ControlParameters& castParams =
104 static_cast<const ndn::nfd::ControlParameters&>(parameters);
105
106 _LOG_DEBUG("Removing prefix from the FIB: " << castParams.getName() << "\n");
107
108 if (m_namePrefixList.remove(castParams.getName())) {
109 m_lsdb.buildAndInstallOwnNameLsa();
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600110 }
111}
112
113} // namespace update
114} // namespace nlsr