blob: 962349df857c73801a4697f5a2896c5ab09e3219 [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 }
75
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070076 if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
77 commandNComps < COMMAND_SIGNED_NCOMPS)
78 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070079 NFD_LOG_DEBUG("command result: unsigned verb: " << command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070080 sendResponse(command, 401, "Signature required");
81
82 return;
83 }
84 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
85 !COMMAND_PREFIX.isPrefixOf(command))
86 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070087 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070088 sendResponse(command, 400, "Malformed command");
89 return;
90 }
91
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070092 validate(request,
93 bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1),
94 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070095}
96
97void
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060098StrategyChoiceManager::listStrategies(const Interest& request)
99{
100 m_listPublisher.publish();
101}
102
103void
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700104StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request)
105{
106 static const Name::Component VERB_SET("set");
107 static const Name::Component VERB_UNSET("unset");
108
109 const Name& command = request->getName();
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600110 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700111
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600112 ControlParameters parameters;
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600113 if (!extractParameters(parameterComponent, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700114 {
115 sendResponse(command, 400, "Malformed command");
116 return;
117 }
118
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600119 const Name::Component& verb = command[COMMAND_PREFIX.size()];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700120 ControlResponse response;
121 if (verb == VERB_SET)
122 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600123 setStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700124 }
125 else if (verb == VERB_UNSET)
126 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600127 unsetStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700128 }
129 else
130 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700131 NFD_LOG_DEBUG("command result: unsupported verb: " << verb);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700132 setResponse(response, 501, "Unsupported command");
133 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600134
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700135 sendResponse(command, response);
136}
137
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700138void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600139StrategyChoiceManager::setStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700140 ControlResponse& response)
141{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600142 ndn::nfd::StrategyChoiceSetCommand command;
143
144 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600145 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700146 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600147 setResponse(response, 400, "Malformed command");
148 return;
149 }
150
151 const Name& prefix = parameters.getName();
152 const Name& selectedStrategy = parameters.getStrategy();
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700153
154 if (!m_strategyChoice.hasStrategy(selectedStrategy))
155 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700156 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: "
157 << parameters.getStrategy());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700158 setResponse(response, 504, "Unsupported strategy");
159 return;
160 }
161
162 if (m_strategyChoice.insert(prefix, selectedStrategy))
163 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700164 NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
Steve DiBenedetto77c87512014-10-06 14:18:22 -0600165 parameters.setStrategy(*m_strategyChoice.get(prefix));
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600166 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700167 }
168 else
169 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700170 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: not-installed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700171 setResponse(response, 405, "Strategy not installed");
172 }
173}
174
175void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600176StrategyChoiceManager::unsetStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700177 ControlResponse& response)
178{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600179 ndn::nfd::StrategyChoiceUnsetCommand command;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700180
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600181 if (!validateParameters(command, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700182 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600183 static const Name ROOT_PREFIX;
184 if (parameters.hasName() && parameters.getName() == ROOT_PREFIX)
185 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700186 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unset-root");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600187 setResponse(response, 403, "Cannot unset root prefix strategy");
188 }
189 else
190 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700191 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600192 setResponse(response, 400, "Malformed command");
193 }
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700194 return;
195 }
196
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600197 m_strategyChoice.erase(parameters.getName());
198
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700199 NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600200 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700201}
202
203
204
205} // namespace nfd