Loading...
Searching...
No Matches
command-processor.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2025, 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 "command-processor.hpp"
23#include "logger.hpp"
24
25#include <ndn-cxx/mgmt/nfd/control-response.hpp>
26
27namespace nlsr::update {
28
29INIT_LOGGER(update.CommandProcessor);
30
31CommandProcessor::CommandProcessor(ndn::mgmt::Dispatcher& dispatcher,
32 NamePrefixList& namePrefixList,
33 Lsdb& lsdb)
34 : m_dispatcher(dispatcher)
35 , m_namePrefixList(namePrefixList)
36 , m_lsdb(lsdb)
37{
38}
39
41
42void
43CommandProcessor::advertiseAndInsertPrefix(const ndn::mgmt::ControlParametersBase& parameters,
44 const ndn::mgmt::CommandContinuation& done)
45{
46 const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters);
47 // This is a bit of a hack, waiting on the work in #5348 to complete for full refactoring
48 ndn::nfd::ControlParameters responseParams(castParams.wireEncode());
49 uint64_t responseFaceId = (castParams.hasFaceId() && castParams.getFaceId() > 0)
50 ? responseParams.getFaceId() : m_defaultResponseFaceId;
51 responseParams.setFaceId(responseFaceId);
52 // Only build a Name LSA if the added name is new
53 double castParamCost = (castParams.hasCost() ? castParams.getCost() : 0);
54 if (m_namePrefixList.insert(castParams.getName(), "", castParamCost)) {
55 NLSR_LOG_INFO("Advertising name: " << castParams.getName());
57 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
58 NLSR_LOG_INFO("Saving name to the configuration file ");
59 auto [afterAdvertiseReturn, afterAdvertiseMessage] = afterAdvertise(castParams.getName(), castParamCost);
60 if (afterAdvertiseReturn) {
61 return done(ndn::nfd::ControlResponse(205, afterAdvertiseMessage).setBody(responseParams.wireEncode()));
62 }
63 else {
64 return done(ndn::nfd::ControlResponse(500, afterAdvertiseMessage)
65 .setBody(responseParams.wireEncode()));
66 }
67 }
68 return done(ndn::nfd::ControlResponse(200, "OK").setBody(responseParams.wireEncode()));
69 }
70 else {
71 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
72 // Save an already advertised prefix
73 NLSR_LOG_INFO("Saving an already advertised name: " << castParams.getName());
74 auto [afterAdvertiseReturn, afterAdvertiseMessage] = afterAdvertise(castParams.getName(), castParamCost);
75 if (afterAdvertiseReturn) {
76 return done(ndn::nfd::ControlResponse(205, afterAdvertiseMessage).setBody(responseParams.wireEncode()));
77 }
78 else {
79 return done(ndn::nfd::ControlResponse(500, afterAdvertiseMessage)
80 .setBody(responseParams.wireEncode()));
81 }
82 }
83 return done(ndn::nfd::ControlResponse(204, "Prefix is already advertised/inserted.")
84 .setBody(responseParams.wireEncode()));
85 }
86}
87
88void
89CommandProcessor::withdrawAndRemovePrefix(const ndn::mgmt::ControlParametersBase& parameters,
90 const ndn::mgmt::CommandContinuation& done)
91{
92 const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters);
93 // This is a bit of a hack, waiting on the work in #5348 to complete for full refactoring
94 ndn::nfd::ControlParameters responseParams(castParams.wireEncode());
95 uint64_t responseFaceId = (castParams.hasFaceId() && castParams.getFaceId() > 0)
96 ? responseParams.getFaceId() : m_defaultResponseFaceId;
97 responseParams.setFaceId(responseFaceId);
98 // Only build a Name LSA if the added name is new
99 if (m_namePrefixList.erase(castParams.getName())) {
100 NLSR_LOG_INFO("Withdrawing/Removing name: " << castParams.getName());
102 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
103 auto [afterWithdrawReturn, afterWithdrawMessage] = afterWithdraw(castParams.getName());
104 if (afterWithdrawReturn) {
105 return done(ndn::nfd::ControlResponse(205, afterWithdrawMessage).setBody(responseParams.wireEncode()));
106 }
107 else {
108 return done(ndn::nfd::ControlResponse(500, afterWithdrawMessage)
109 .setBody(responseParams.wireEncode()));
110 }
111 }
112 return done(ndn::nfd::ControlResponse(200, "OK").setBody(responseParams.wireEncode()));
113 }
114 else {
115 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
116 // Delete an already withdrawn prefix
117 NLSR_LOG_INFO("Deleting an already withdrawn name: " << castParams.getName());
118 auto [afterWithdrawReturn, afterWithdrawMessage] = afterWithdraw(castParams.getName());
119 if (afterWithdrawReturn) {
120 return done(ndn::nfd::ControlResponse(205, afterWithdrawMessage).setBody(responseParams.wireEncode()));
121 }
122 else {
123 return done(ndn::nfd::ControlResponse(500, afterWithdrawMessage)
124 .setBody(responseParams.wireEncode()));
125 }
126 }
127 return done(ndn::nfd::ControlResponse(204, "Prefix is already withdrawn/removed.")
128 .setBody(responseParams.wireEncode()));
129 }
130}
131
132} // namespace nlsr::update
void buildAndInstallOwnNameLsa()
Builds a name LSA for this router and then installs it into the LSDB.
Definition lsdb.cpp:93
bool erase(const ndn::Name &name, const std::string &source="")
Deletes name and source combination.
bool insert(const ndn::Name &name, const std::string &source="", double cost=0)
Inserts name and source combination.
virtual std::tuple< bool, std::string > afterWithdraw(const ndn::Name &prefix)
Processing after withdraw command delegated to subclass. This is always treated as successful if not ...
void advertiseAndInsertPrefix(const ndn::mgmt::ControlParametersBase &parameters, const ndn::mgmt::CommandContinuation &done)
Add desired name prefix to the advertised name prefix list or insert a prefix into the FIB if paramet...
void withdrawAndRemovePrefix(const ndn::mgmt::ControlParametersBase &parameters, const ndn::mgmt::CommandContinuation &done)
Remove desired name prefix from the advertised name prefix list or remove a prefix from the FIB if pa...
CommandProcessor(ndn::mgmt::Dispatcher &m_dispatcher, NamePrefixList &m_namePrefixList, Lsdb &lsdb)
virtual std::tuple< bool, std::string > afterAdvertise(const ndn::Name &prefix, uint64_t cost)
Processing after advertise command delegated to subclass. This is always treated as successful if not...
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition logger.hpp:35
#define NLSR_LOG_INFO(x)
Definition logger.hpp:39