blob: f4baeb58c5d34f3044ba5161f4f2b62d7f3af482 [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,
36 Lsdb& lsdb,
37 SyncLogicHandler& sync)
38 : m_dispatcher(dispatcher)
39 , m_namePrefixList(namePrefixes)
40 , m_lsdb(lsdb)
41 , m_sync(sync)
42{
43}
44
45void
46NfdRibCommandProcessor::startListening()
47{
48 _LOG_DEBUG("Registering control command prefixes for: " << REGISTER_VERB <<
49 " and " << UNREGISTER_VERB);
50 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(REGISTER_VERB,
51 ndn::mgmt::makeAcceptAllAuthorization(),
52 std::bind(&NfdRibCommandProcessor::validate<NfdRibRegisterCommand>,
53 this, _1, REGISTER_VERB),
54 [this] (const ndn::Name& prefix, const ndn::Interest& interest,
55 const ndn::mgmt::ControlParameters& parameters,
56 const ndn::mgmt::CommandContinuation& done) {
57 _LOG_DEBUG("Params verified, calling insertPrefix()");
58 this->insertPrefix(parameters);
59 });
60 m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(UNREGISTER_VERB,
61 ndn::mgmt::makeAcceptAllAuthorization(),
62 std::bind(&NfdRibCommandProcessor::validate<NfdRibUnregisterCommand>,
63 this, _1, UNREGISTER_VERB),
64 [this] (const ndn::Name& prefix, const ndn::Interest& interest,
65 const ndn::mgmt::ControlParameters& parameters,
66 const ndn::mgmt::CommandContinuation& done) {
67 _LOG_DEBUG("Params verified, calling removePrefix()");
68 this->removePrefix(parameters);
69 });
70}
71
72template<typename T>
73bool
74NfdRibCommandProcessor::validate(const ndn::mgmt::ControlParameters& parameters,
75 const ndn::PartialName& command)
76{
77 _LOG_DEBUG("Attempting to verify incoming params for " << command <<
78 " command...");
79 bool wasValidated = true;
80 try {
81 wasValidated = this->validateParameters<T>(parameters);
82 } catch (const ndn::nfd::ControlCommand::ArgumentError& ae) {
83 _LOG_DEBUG("Could not parse params. Message: " << ae.what());
84 wasValidated = false;
85 }
86 return wasValidated;
87}
88
89void
90NfdRibCommandProcessor::insertPrefix(const ndn::mgmt::ControlParameters& parameters)
91{
92 const ndn::nfd::ControlParameters& castParams =
93 static_cast<const ndn::nfd::ControlParameters&>(parameters);
94
95 _LOG_DEBUG("Inserting prefix into the FIB: " << castParams.getName() << "\n");
96
97 if (m_namePrefixList.insert(castParams.getName())) {
98 m_lsdb.buildAndInstallOwnNameLsa();
99 m_sync.publishRoutingUpdate();
100 }
101}
102
103void
104NfdRibCommandProcessor::removePrefix(const ndn::mgmt::ControlParameters& parameters)
105{
106 const ndn::nfd::ControlParameters& castParams =
107 static_cast<const ndn::nfd::ControlParameters&>(parameters);
108
109 _LOG_DEBUG("Removing prefix from the FIB: " << castParams.getName() << "\n");
110
111 if (m_namePrefixList.remove(castParams.getName())) {
112 m_lsdb.buildAndInstallOwnNameLsa();
113 m_sync.publishRoutingUpdate();
114 }
115}
116
117} // namespace update
118} // namespace nlsr