blob: 4f2728c7d69d0d8b057bc2b04d4ceb43ade7b8b6 [file] [log] [blame]
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman5144f822014-07-23 15:12:56 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Vince Lehman5144f822014-07-23 15:12:56 -070024 */
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070025
26#include "strategy-choice-manager.hpp"
27#include "table/strategy-choice.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070029#include "mgmt/app-face.hpp"
30
31namespace nfd {
32
33NFD_LOG_INIT("StrategyChoiceManager");
34
35const Name StrategyChoiceManager::COMMAND_PREFIX = "/localhost/nfd/strategy-choice";
36
37const size_t StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS =
38 StrategyChoiceManager::COMMAND_PREFIX.size() +
39 1 + // verb
Steve DiBenedetto7564d972014-03-24 14:28:46 -060040 1; // verb parameters
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070041
42const size_t StrategyChoiceManager::COMMAND_SIGNED_NCOMPS =
43 StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS +
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070044 4; // (timestamp, nonce, signed info tlv, signature tlv)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070045
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060046const Name StrategyChoiceManager::LIST_DATASET_PREFIX("/localhost/nfd/strategy-choice/list");
47
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070048StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice,
Vince Lehman5144f822014-07-23 15:12:56 -070049 shared_ptr<InternalFace> face,
50 ndn::KeyChain& keyChain)
51 : ManagerBase(face, STRATEGY_CHOICE_PRIVILEGE, keyChain)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070052 , m_strategyChoice(strategyChoice)
Vince Lehman5144f822014-07-23 15:12:56 -070053 , m_listPublisher(strategyChoice, *m_face, LIST_DATASET_PREFIX, keyChain)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070054{
55 face->setInterestFilter("/localhost/nfd/strategy-choice",
56 bind(&StrategyChoiceManager::onStrategyChoiceRequest, this, _2));
57}
58
59StrategyChoiceManager::~StrategyChoiceManager()
60{
61
62}
63
64void
65StrategyChoiceManager::onStrategyChoiceRequest(const Interest& request)
66{
67 const Name& command = request.getName();
68 const size_t commandNComps = command.size();
69
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060070 if (command == LIST_DATASET_PREFIX)
71 {
72 listStrategies(request);
73 return;
74 }
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -070075 else if (commandNComps <= COMMAND_PREFIX.size())
76 {
77 // command is too short to have a verb
78 NFD_LOG_DEBUG("command result: malformed");
79 sendResponse(command, 400, "Malformed command");
80 return;
81 }
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060082
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070083 if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
84 commandNComps < COMMAND_SIGNED_NCOMPS)
85 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070086 NFD_LOG_DEBUG("command result: unsigned verb: " << command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070087 sendResponse(command, 401, "Signature required");
88
89 return;
90 }
91 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
92 !COMMAND_PREFIX.isPrefixOf(command))
93 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070094 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070095 sendResponse(command, 400, "Malformed command");
96 return;
97 }
98
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070099 validate(request,
100 bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1),
101 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700102}
103
104void
Steve DiBenedetto3fff5612014-05-30 15:52:56 -0600105StrategyChoiceManager::listStrategies(const Interest& request)
106{
107 m_listPublisher.publish();
108}
109
110void
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700111StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request)
112{
113 static const Name::Component VERB_SET("set");
114 static const Name::Component VERB_UNSET("unset");
115
116 const Name& command = request->getName();
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600117 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700118
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600119 ControlParameters parameters;
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600120 if (!extractParameters(parameterComponent, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700121 {
122 sendResponse(command, 400, "Malformed command");
123 return;
124 }
125
Steve DiBenedettocd4ee5f2014-12-08 16:09:11 -0700126 const Name::Component& verb = command.at(COMMAND_PREFIX.size());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700127 ControlResponse response;
128 if (verb == VERB_SET)
129 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600130 setStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700131 }
132 else if (verb == VERB_UNSET)
133 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600134 unsetStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700135 }
136 else
137 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700138 NFD_LOG_DEBUG("command result: unsupported verb: " << verb);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700139 setResponse(response, 501, "Unsupported command");
140 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600141
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700142 sendResponse(command, response);
143}
144
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700145void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600146StrategyChoiceManager::setStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700147 ControlResponse& response)
148{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600149 ndn::nfd::StrategyChoiceSetCommand command;
150
151 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600152 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700153 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600154 setResponse(response, 400, "Malformed command");
155 return;
156 }
157
158 const Name& prefix = parameters.getName();
159 const Name& selectedStrategy = parameters.getStrategy();
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700160
161 if (!m_strategyChoice.hasStrategy(selectedStrategy))
162 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700163 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: "
164 << parameters.getStrategy());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700165 setResponse(response, 504, "Unsupported strategy");
166 return;
167 }
168
169 if (m_strategyChoice.insert(prefix, selectedStrategy))
170 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700171 NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
Junxiao Shi838c4f12014-11-03 18:55:24 -0700172 auto currentStrategyChoice = m_strategyChoice.get(prefix);
173 BOOST_ASSERT(currentStrategyChoice.first);
174 parameters.setStrategy(currentStrategyChoice.second);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600175 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700176 }
177 else
178 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700179 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: not-installed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700180 setResponse(response, 405, "Strategy not installed");
181 }
182}
183
184void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600185StrategyChoiceManager::unsetStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700186 ControlResponse& response)
187{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600188 ndn::nfd::StrategyChoiceUnsetCommand command;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700189
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600190 if (!validateParameters(command, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700191 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600192 static const Name ROOT_PREFIX;
193 if (parameters.hasName() && parameters.getName() == ROOT_PREFIX)
194 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700195 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unset-root");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600196 setResponse(response, 403, "Cannot unset root prefix strategy");
197 }
198 else
199 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700200 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600201 setResponse(response, 400, "Malformed command");
202 }
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700203 return;
204 }
205
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600206 m_strategyChoice.erase(parameters.getName());
207
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700208 NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600209 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700210}
211
212
213
214} // namespace nfd