blob: 6ca1c4aeabe61b7de50b58572a389154f21fc4c6 [file] [log] [blame]
Laqin Fan54a43f02017-03-08 12:31:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, 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/>.
20 **/
21
22#include "manager-base.hpp"
Laqin Fan54a43f02017-03-08 12:31:30 -060023
24namespace nlsr {
25namespace update {
26
dmcoomescf8d0ed2017-02-21 11:39:01 -060027INIT_LOGGER(update.PrefixCommandProcessor);
Laqin Fan54a43f02017-03-08 12:31:30 -060028
29ManagerBase::ManagerBase(ndn::mgmt::Dispatcher& dispatcher,
30 const std::string& module)
31 : m_dispatcher(dispatcher)
32 , m_module(module)
33{
34}
35
36ndn::PartialName
37ManagerBase::makeRelPrefix(const std::string& verb) const
38{
39 return ndn::PartialName(m_module).append(verb);
40}
41
42CommandManagerBase::CommandManagerBase(ndn::mgmt::Dispatcher& dispatcher,
43 NamePrefixList& namePrefixList,
44 Lsdb& lsdb,
45 const std::string& module)
46 : ManagerBase(dispatcher, module)
47 , m_namePrefixList(namePrefixList)
48 , m_lsdb(lsdb)
49{
50}
51
52void
53CommandManagerBase::advertiseAndInsertPrefix(const ndn::Name& prefix,
54 const ndn::Interest& interest,
55 const ndn::mgmt::ControlParameters& parameters,
56 const ndn::mgmt::CommandContinuation& done)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050057{
Laqin Fan54a43f02017-03-08 12:31:30 -060058 const ndn::nfd::ControlParameters& castParams =
59 static_cast<const ndn::nfd::ControlParameters&>(parameters);
60
61 // Only build a Name LSA if the added name is new
62 if (m_namePrefixList.insert(castParams.getName())) {
Saurab Dulal7526cee2018-01-31 18:14:10 +000063 NLSR_LOG_INFO("Advertising name: " << castParams.getName() << "\n");
Laqin Fan54a43f02017-03-08 12:31:30 -060064 m_lsdb.buildAndInstallOwnNameLsa();
Saurab Dulal7526cee2018-01-31 18:14:10 +000065 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060066 NLSR_LOG_INFO("Saving name to the configuration file ");
Saurab Dulal7526cee2018-01-31 18:14:10 +000067 if (afterAdvertise(castParams.getName()) == true) {
68 return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
69 }
70 else {
71 return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.")
72 .setBody(parameters.wireEncode()));
73 }
74 }
Laqin Fan54a43f02017-03-08 12:31:30 -060075 return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode()));
76 }
Saurab Dulal7526cee2018-01-31 18:14:10 +000077 else {
78 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
79 // Save an already advertised prefix
80 NLSR_LOG_INFO("Saving an already advertised name: " << castParams.getName() << "\n");
81 if (afterAdvertise(castParams.getName()) == true) {
82 return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
83 }
84 else {
85 return done(ndn::nfd::ControlResponse(406, "Prefix is already Saved/Failed to open configuration file.")
86 .setBody(parameters.wireEncode()));
87 }
88 }
89 return done(ndn::nfd::ControlResponse(204, "Prefix is already advertised/inserted.")
90 .setBody(parameters.wireEncode()));
91 }
Laqin Fan54a43f02017-03-08 12:31:30 -060092}
93
94void
95CommandManagerBase::withdrawAndRemovePrefix(const ndn::Name& prefix,
96 const ndn::Interest& interest,
97 const ndn::mgmt::ControlParameters& parameters,
98 const ndn::mgmt::CommandContinuation& done)
99{
100 const ndn::nfd::ControlParameters& castParams =
101 static_cast<const ndn::nfd::ControlParameters&>(parameters);
102
103 // Only build a Name LSA if the added name is new
104 if (m_namePrefixList.remove(castParams.getName())) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500105 NLSR_LOG_INFO("Withdrawing/Removing name: " << castParams.getName() << "\n");
Laqin Fan54a43f02017-03-08 12:31:30 -0600106 m_lsdb.buildAndInstallOwnNameLsa();
Saurab Dulal7526cee2018-01-31 18:14:10 +0000107 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
108 if (afterWithdraw(castParams.getName()) == true) {
109 return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
110 }
111 else {
112 return done(ndn::nfd::ControlResponse(406, "Failed to open configuration file.")
113 .setBody(parameters.wireEncode()));
114 }
115 }
Laqin Fan54a43f02017-03-08 12:31:30 -0600116 return done(ndn::nfd::ControlResponse(200, "OK").setBody(parameters.wireEncode()));
117 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000118 else {
119 if (castParams.hasFlags() && castParams.getFlags() == PREFIX_FLAG) {
120 // Delete an already withdrawn prefix
121 NLSR_LOG_INFO("Deleting an already withdrawn name: " << castParams.getName() << "\n");
122 if (afterWithdraw(castParams.getName()) == true) {
123 return done(ndn::nfd::ControlResponse(205, "OK").setBody(parameters.wireEncode()));
124 }
125 else {
126 return done(ndn::nfd::ControlResponse(406, "Prefix is already deleted/Failed to open configuration file.")
127 .setBody(parameters.wireEncode()));
128 }
129 }
130 return done(ndn::nfd::ControlResponse(204, "Prefix is already withdrawn/removed.")
131 .setBody(parameters.wireEncode()));
132 }
Laqin Fan54a43f02017-03-08 12:31:30 -0600133}
134
135} // namespace update
136} // namespace nlsr