Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 1 | /* -*- 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 | |
| 25 | namespace nlsr { |
| 26 | namespace update { |
| 27 | |
| 28 | INIT_LOGGER("NfdRibProcessor"); |
| 29 | |
| 30 | const ndn::PartialName REGISTER_VERB = ndn::PartialName("rib/register"); |
| 31 | const ndn::PartialName UNREGISTER_VERB = ndn::PartialName("rib/unregister"); |
| 32 | const ndn::Name COMMAND_PREFIX = ndn::Name("/localhost/nlsr"); |
| 33 | |
| 34 | NfdRibCommandProcessor::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 | |
| 45 | void |
| 46 | NfdRibCommandProcessor::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 | |
| 72 | template<typename T> |
| 73 | bool |
| 74 | NfdRibCommandProcessor::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 | |
| 89 | void |
| 90 | NfdRibCommandProcessor::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 | |
| 103 | void |
| 104 | NfdRibCommandProcessor::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 |