blob: b51badce026865b8d8edc959d4d3207b5749be13 [file] [log] [blame]
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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,
Steve DiBenedetto3fff5612014-05-30 15:52:56 -06008 * 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/>.
24 **/
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,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070049 shared_ptr<InternalFace> face)
50 : ManagerBase(face, STRATEGY_CHOICE_PRIVILEGE)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070051 , m_strategyChoice(strategyChoice)
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060052 , m_listPublisher(strategyChoice, m_face, LIST_DATASET_PREFIX)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070053{
54 face->setInterestFilter("/localhost/nfd/strategy-choice",
55 bind(&StrategyChoiceManager::onStrategyChoiceRequest, this, _2));
56}
57
58StrategyChoiceManager::~StrategyChoiceManager()
59{
60
61}
62
63void
64StrategyChoiceManager::onStrategyChoiceRequest(const Interest& request)
65{
66 const Name& command = request.getName();
67 const size_t commandNComps = command.size();
68
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060069 if (command == LIST_DATASET_PREFIX)
70 {
71 listStrategies(request);
72 return;
73 }
74
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070075 if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
76 commandNComps < COMMAND_SIGNED_NCOMPS)
77 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070078 NFD_LOG_DEBUG("command result: unsigned verb: " << command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070079 sendResponse(command, 401, "Signature required");
80
81 return;
82 }
83 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
84 !COMMAND_PREFIX.isPrefixOf(command))
85 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070086 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070087 sendResponse(command, 400, "Malformed command");
88 return;
89 }
90
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070091 validate(request,
92 bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1),
93 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070094}
95
96void
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060097StrategyChoiceManager::listStrategies(const Interest& request)
98{
99 m_listPublisher.publish();
100}
101
102void
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700103StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request)
104{
105 static const Name::Component VERB_SET("set");
106 static const Name::Component VERB_UNSET("unset");
107
108 const Name& command = request->getName();
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600109 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700110
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600111 ControlParameters parameters;
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600112 if (!extractParameters(parameterComponent, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700113 {
114 sendResponse(command, 400, "Malformed command");
115 return;
116 }
117
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600118 const Name::Component& verb = command[COMMAND_PREFIX.size()];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700119 ControlResponse response;
120 if (verb == VERB_SET)
121 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600122 setStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700123 }
124 else if (verb == VERB_UNSET)
125 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600126 unsetStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700127 }
128 else
129 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700130 NFD_LOG_DEBUG("command result: unsupported verb: " << verb);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700131 setResponse(response, 501, "Unsupported command");
132 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600133
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700134 sendResponse(command, response);
135}
136
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700137void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600138StrategyChoiceManager::setStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700139 ControlResponse& response)
140{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600141 ndn::nfd::StrategyChoiceSetCommand command;
142
143 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600144 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700145 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600146 setResponse(response, 400, "Malformed command");
147 return;
148 }
149
150 const Name& prefix = parameters.getName();
151 const Name& selectedStrategy = parameters.getStrategy();
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700152
153 if (!m_strategyChoice.hasStrategy(selectedStrategy))
154 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700155 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: "
156 << parameters.getStrategy());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700157 setResponse(response, 504, "Unsupported strategy");
158 return;
159 }
160
161 if (m_strategyChoice.insert(prefix, selectedStrategy))
162 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700163 NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600164 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700165 }
166 else
167 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700168 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: not-installed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700169 setResponse(response, 405, "Strategy not installed");
170 }
171}
172
173void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600174StrategyChoiceManager::unsetStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700175 ControlResponse& response)
176{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600177 ndn::nfd::StrategyChoiceUnsetCommand command;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700178
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600179 if (!validateParameters(command, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700180 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600181 static const Name ROOT_PREFIX;
182 if (parameters.hasName() && parameters.getName() == ROOT_PREFIX)
183 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700184 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unset-root");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600185 setResponse(response, 403, "Cannot unset root prefix strategy");
186 }
187 else
188 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700189 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600190 setResponse(response, 400, "Malformed command");
191 }
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700192 return;
193 }
194
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600195 m_strategyChoice.erase(parameters.getName());
196
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700197 NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600198 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700199}
200
201
202
203} // namespace nfd