blob: d13d9b6a043bae1618d09d5ff5cd161beb7bd2bd [file] [log] [blame]
Laqin Fan54a43f02017-03-08 12:31:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04002/*
Davide Pesaventod2610dc2025-01-03 13:20:06 -05003 * Copyright (c) 2014-2025, The University of Memphis,
Laqin Fan54a43f02017-03-08 12:31:30 -06004 * 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/>.
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040020 */
Laqin Fan54a43f02017-03-08 12:31:30 -060021
Davide Pesavento20e60a22025-01-07 01:50:10 -050022#include "command-processor.hpp"
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040023#include "logger.hpp"
Laqin Fan54a43f02017-03-08 12:31:30 -060024
Davide Pesavento20e60a22025-01-07 01:50:10 -050025#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Laqin Fan54a43f02017-03-08 12:31:30 -060026
Davide Pesavento20e60a22025-01-07 01:50:10 -050027namespace nlsr::update {
Laqin Fan54a43f02017-03-08 12:31:30 -060028
Davide Pesavento20e60a22025-01-07 01:50:10 -050029INIT_LOGGER(update.CommandProcessor);
30
31CommandProcessor::CommandProcessor(ndn::mgmt::Dispatcher& dispatcher,
32 NamePrefixList& namePrefixList,
33 Lsdb& lsdb)
Laqin Fan54a43f02017-03-08 12:31:30 -060034 : m_dispatcher(dispatcher)
Laqin Fan54a43f02017-03-08 12:31:30 -060035 , m_namePrefixList(namePrefixList)
36 , m_lsdb(lsdb)
37{
38}
39
Davide Pesavento20e60a22025-01-07 01:50:10 -050040CommandProcessor::~CommandProcessor() = default;
41
Laqin Fan54a43f02017-03-08 12:31:30 -060042void
Davide Pesavento20e60a22025-01-07 01:50:10 -050043CommandProcessor::advertiseAndInsertPrefix(const ndn::mgmt::ControlParametersBase& parameters,
44 const ndn::mgmt::CommandContinuation& done)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050045{
Davide Pesaventod2610dc2025-01-03 13:20:06 -050046 const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters);
Laqin Fan54a43f02017-03-08 12:31:30 -060047
48 // Only build a Name LSA if the added name is new
49 if (m_namePrefixList.insert(castParams.getName())) {
Davide Pesavento968eb1a2025-01-07 00:46:05 -050050 NLSR_LOG_INFO("Advertising name: " << castParams.getName());
Laqin Fan54a43f02017-03-08 12:31:30 -060051 m_lsdb.buildAndInstallOwnNameLsa();
Saurab Dulal7526cee2018-01-31 18:14:10 +000052 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060053 NLSR_LOG_INFO("Saving name to the configuration file ");
Saurab Dulal7526cee2018-01-31 18:14:10 +000054 if (afterAdvertise(castParams.getName()) == true) {
55 return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
56 }
57 else {
58 return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.")
59 .setBody(parameters.wireEncode()));
60 }
61 }
Laqin Fan54a43f02017-03-08 12:31:30 -060062 return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode()));
63 }
Saurab Dulal7526cee2018-01-31 18:14:10 +000064 else {
65 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
66 // Save an already advertised prefix
Davide Pesavento968eb1a2025-01-07 00:46:05 -050067 NLSR_LOG_INFO("Saving an already advertised name: " << castParams.getName());
Saurab Dulal7526cee2018-01-31 18:14:10 +000068 if (afterAdvertise(castParams.getName()) == true) {
69 return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
70 }
71 else {
72 return done(ndn::nfd::ControlResponse(406, "Prefix is already Saved/Failed to open configuration file.")
73 .setBody(parameters.wireEncode()));
74 }
75 }
76 return done(ndn::nfd::ControlResponse(204, "Prefix is already advertised/inserted.")
77 .setBody(parameters.wireEncode()));
78 }
Laqin Fan54a43f02017-03-08 12:31:30 -060079}
80
81void
Davide Pesavento20e60a22025-01-07 01:50:10 -050082CommandProcessor::withdrawAndRemovePrefix(const ndn::mgmt::ControlParametersBase& parameters,
83 const ndn::mgmt::CommandContinuation& done)
Laqin Fan54a43f02017-03-08 12:31:30 -060084{
Davide Pesaventod2610dc2025-01-03 13:20:06 -050085 const auto& castParams = static_cast<const ndn::nfd::ControlParameters&>(parameters);
Laqin Fan54a43f02017-03-08 12:31:30 -060086
87 // Only build a Name LSA if the added name is new
Junxiao Shi52f16642023-08-15 00:18:35 +000088 if (m_namePrefixList.erase(castParams.getName())) {
Davide Pesavento968eb1a2025-01-07 00:46:05 -050089 NLSR_LOG_INFO("Withdrawing/Removing name: " << castParams.getName());
Laqin Fan54a43f02017-03-08 12:31:30 -060090 m_lsdb.buildAndInstallOwnNameLsa();
Saurab Dulal7526cee2018-01-31 18:14:10 +000091 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
92 if (afterWithdraw(castParams.getName()) == true) {
93 return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
94 }
95 else {
96 return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.")
97 .setBody(parameters.wireEncode()));
98 }
99 }
Laqin Fan54a43f02017-03-08 12:31:30 -0600100 return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode()));
101 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000102 else {
103 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
104 // Delete an already withdrawn prefix
Davide Pesavento968eb1a2025-01-07 00:46:05 -0500105 NLSR_LOG_INFO("Deleting an already withdrawn name: " << castParams.getName());
Saurab Dulal7526cee2018-01-31 18:14:10 +0000106 if (afterWithdraw(castParams.getName()) == true) {
107 return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
108 }
109 else {
110 return done(ndn::nfd::ControlResponse(406, "Prefix is already deleted/Failed to open configuration file.")
111 .setBody(parameters.wireEncode()));
112 }
113 }
114 return done(ndn::nfd::ControlResponse(204, "Prefix is already withdrawn/removed.")
115 .setBody(parameters.wireEncode()));
116 }
Laqin Fan54a43f02017-03-08 12:31:30 -0600117}
118
Davide Pesavento20e60a22025-01-07 01:50:10 -0500119} // namespace nlsr::update