blob: c226b419195732d48ce3b04e88da7f226af7975a [file] [log] [blame]
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "strategy-choice-manager.hpp"
8#include "table/strategy-choice.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -06009#include "core/logger.hpp"
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070010#include "mgmt/app-face.hpp"
11
12namespace nfd {
13
14NFD_LOG_INIT("StrategyChoiceManager");
15
16const Name StrategyChoiceManager::COMMAND_PREFIX = "/localhost/nfd/strategy-choice";
17
18const size_t StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS =
19 StrategyChoiceManager::COMMAND_PREFIX.size() +
20 1 + // verb
Steve DiBenedetto7564d972014-03-24 14:28:46 -060021 1; // verb parameters
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070022
23const size_t StrategyChoiceManager::COMMAND_SIGNED_NCOMPS =
24 StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS +
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070025 4; // (timestamp, nonce, signed info tlv, signature tlv)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070026
27StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070028 shared_ptr<InternalFace> face)
29 : ManagerBase(face, STRATEGY_CHOICE_PRIVILEGE)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070030 , m_strategyChoice(strategyChoice)
31{
32 face->setInterestFilter("/localhost/nfd/strategy-choice",
33 bind(&StrategyChoiceManager::onStrategyChoiceRequest, this, _2));
34}
35
36StrategyChoiceManager::~StrategyChoiceManager()
37{
38
39}
40
41void
42StrategyChoiceManager::onStrategyChoiceRequest(const Interest& request)
43{
44 const Name& command = request.getName();
45 const size_t commandNComps = command.size();
46
47 if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
48 commandNComps < COMMAND_SIGNED_NCOMPS)
49 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070050 NFD_LOG_DEBUG("command result: unsigned verb: " << command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070051 sendResponse(command, 401, "Signature required");
52
53 return;
54 }
55 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
56 !COMMAND_PREFIX.isPrefixOf(command))
57 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070058 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070059 sendResponse(command, 400, "Malformed command");
60 return;
61 }
62
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070063 validate(request,
64 bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1),
65 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070066}
67
68void
69StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request)
70{
71 static const Name::Component VERB_SET("set");
72 static const Name::Component VERB_UNSET("unset");
73
74 const Name& command = request->getName();
Steve DiBenedetto7564d972014-03-24 14:28:46 -060075 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070076
Steve DiBenedetto7564d972014-03-24 14:28:46 -060077 ControlParameters parameters;
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060078 if (!extractParameters(parameterComponent, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070079 {
80 sendResponse(command, 400, "Malformed command");
81 return;
82 }
83
Steve DiBenedetto7564d972014-03-24 14:28:46 -060084 const Name::Component& verb = command[COMMAND_PREFIX.size()];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070085 ControlResponse response;
86 if (verb == VERB_SET)
87 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -060088 setStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070089 }
90 else if (verb == VERB_UNSET)
91 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -060092 unsetStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070093 }
94 else
95 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070096 NFD_LOG_DEBUG("command result: unsupported verb: " << verb);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070097 setResponse(response, 501, "Unsupported command");
98 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060099
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700100 sendResponse(command, response);
101}
102
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700103void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600104StrategyChoiceManager::setStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700105 ControlResponse& response)
106{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600107 ndn::nfd::StrategyChoiceSetCommand command;
108
109 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600110 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700111 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600112 setResponse(response, 400, "Malformed command");
113 return;
114 }
115
116 const Name& prefix = parameters.getName();
117 const Name& selectedStrategy = parameters.getStrategy();
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700118
119 if (!m_strategyChoice.hasStrategy(selectedStrategy))
120 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700121 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: "
122 << parameters.getStrategy());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700123 setResponse(response, 504, "Unsupported strategy");
124 return;
125 }
126
127 if (m_strategyChoice.insert(prefix, selectedStrategy))
128 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700129 NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600130 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700131 }
132 else
133 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700134 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: not-installed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700135 setResponse(response, 405, "Strategy not installed");
136 }
137}
138
139void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600140StrategyChoiceManager::unsetStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700141 ControlResponse& response)
142{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600143 ndn::nfd::StrategyChoiceUnsetCommand command;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700144
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600145 if (!validateParameters(command, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700146 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600147 static const Name ROOT_PREFIX;
148 if (parameters.hasName() && parameters.getName() == ROOT_PREFIX)
149 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700150 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unset-root");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600151 setResponse(response, 403, "Cannot unset root prefix strategy");
152 }
153 else
154 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700155 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600156 setResponse(response, 400, "Malformed command");
157 }
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700158 return;
159 }
160
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600161 m_strategyChoice.erase(parameters.getName());
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
167
168
169} // namespace nfd