blob: 569a3be8dd0417d12187b7442c1e3be4e0f37e1b [file] [log] [blame]
Steve DiBenedetto042bfe92014-01-30 15:05:08 -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 "fib-manager.hpp"
8
9#include "table/fib.hpp"
10#include "fw/forwarder.hpp"
11#include "mgmt/internal-face.hpp"
12#include "mgmt/app-face.hpp"
13
14#include <ndn-cpp-dev/management/fib-management-options.hpp>
15
16namespace nfd {
17
18NFD_LOG_INIT("FibManager");
19
20const Name FibManager::FIB_MANAGER_REQUEST_PREFIX = "/localhost/nfd/fib";
21
22// In the future this should be 3 (Signature TLV, Signature Info TLV, Timestamp)
23const size_t FibManager::FIB_MANAGER_REQUEST_SIGNED_INTEREST_NCOMPS = 0;
24
25const size_t FibManager::FIB_MANAGER_REQUEST_COMMAND_MIN_NCOMPS =
26 FibManager::FIB_MANAGER_REQUEST_PREFIX.size() +
27 1 + // verb
28 1 + // verb options
29 FibManager::FIB_MANAGER_REQUEST_SIGNED_INTEREST_NCOMPS;
30
31const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_INSERT = "insert";
32const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_DELETE = "delete";
33const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_ADD_NEXTHOP = "add-nexthop";
34const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_REMOVE_NEXTHOP = "remove-nexthop";
35const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_STRATEGY = "strategy";
36
37
38const FibManager::VerbAndProcessor FibManager::FIB_MANAGER_REQUEST_VERBS[] =
39 {
40 // Unsupported
41
42 // VerbAndProcessor(
43 // FibManager::FIB_MANAGER_REQUEST_VERB_INSERT,
44 // &FibManager::fibInsert
45 // ),
46
47 // VerbAndProcessor(
48 // FibManager::FIB_MANAGER_REQUEST_VERB_DELETE,
49 // &FibManager::fibDelete
50 // ),
51
52 VerbAndProcessor(
53 FibManager::FIB_MANAGER_REQUEST_VERB_ADD_NEXTHOP,
54 &FibManager::fibAddNextHop
55 ),
56
57 // Unsupported
58
59 // VerbAndProcessor(
60 // FibManager::FIB_MANAGER_REQUEST_VERB_REMOVE_NEXTHOP,
61 // &FibManager::fibRemoveNextHop
62 // ),
63
64 // VerbAndProcessor(
65 // FibManager::FIB_MANAGER_REQUEST_VERB_STRATEGY,
66 // &FibManager::fibStrategy
67 // )
68
69 };
70
71FibManager::FibManager(Fib& fib,
Steve DiBenedetto3970c892014-01-31 23:31:13 -070072 function<shared_ptr<Face>(FaceId)> getFace,
73 shared_ptr<AppFace> face)
74 : ManagerBase(face),
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070075 m_managedFib(fib),
76 m_getFace(getFace),
77 m_verbDispatch(FIB_MANAGER_REQUEST_VERBS,
78 FIB_MANAGER_REQUEST_VERBS +
79 (sizeof(FIB_MANAGER_REQUEST_VERBS) / sizeof(VerbAndProcessor)))
80{
81
82}
83
84void
85FibManager::onFibRequest(const Interest& request)
86{
87 const Name& requestCommand = request.getName();
88
89 /// \todo Separate out response status codes 400 and 401
90
91 if (requestCommand.size() < FIB_MANAGER_REQUEST_COMMAND_MIN_NCOMPS ||
92 !FIB_MANAGER_REQUEST_PREFIX.isPrefixOf(requestCommand))
93 {
94 NFD_LOG_INFO("Malformed command: " << requestCommand);
95 sendResponse(request.getName(), 404, "MALFORMED");
96 return;
97 }
98
99 const Name::Component& requestVerb = requestCommand.get(FIB_MANAGER_REQUEST_PREFIX.size());
100
101 VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find (requestVerb);
102 if (verbProcessor == m_verbDispatch.end())
103 {
104 NFD_LOG_INFO("Unsupported command verb: " << requestVerb);
105 sendResponse(request.getName(), 404, "UNSUPPORTED");
106
107 }
108 else
109 {
110 NFD_LOG_INFO("Processing command verb: " << requestVerb);
111 (verbProcessor->second)(this, request);
112 }
113
114}
115
116void
117FibManager::fibInsert(const Interest& request)
118{
119
120}
121
122void
123FibManager::fibDelete(const Interest& request)
124{
125
126}
127
128void
129FibManager::fibAddNextHop(const Interest& request)
130{
131 ndn::FibManagementOptions options;
132 const size_t optionCompIndex =
133 FIB_MANAGER_REQUEST_PREFIX.size() + 1;
134
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700135 const ndn::Buffer& optionBuffer =
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700136 request.getName()[optionCompIndex].getValue();
137 shared_ptr<const ndn::Buffer> tmpOptionBuffer(new ndn::Buffer(optionBuffer));
138 Block rawOptions(tmpOptionBuffer);
139
140 options.wireDecode(rawOptions);
141
142 /// \todo authorize command
143 if (false)
144 {
145 NFD_LOG_INFO("Unauthorized command attempt");
146
147 sendResponse(request.getName(), 403, "");
148 return;
149 }
150
151 NFD_LOG_INFO("add-nexthop Name: " << options.getName()
152 << " FaceId: " << options.getFaceId()
153 << " Cost: " << options.getCost());
154
155 shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId());
156 if (nextHopFace)
157 {
158 std::pair<shared_ptr<fib::Entry>, bool> insertResult = m_managedFib.insert(options.getName());
159 insertResult.first->addNextHop(nextHopFace, options.getCost());
160 sendResponse(request.getName(), 200, "OK");
161 }
162 else
163 {
164 sendResponse(request.getName(), 400, "INCORRECT");
165 }
166}
167
168void
169FibManager::fibRemoveNextHop(const Interest& request)
170{
171
172}
173
174void
175FibManager::fibStrategy(const Interest& request)
176{
177
178}
179
180// void
181// FibManager::onConfig(ConfigFile::Node section, bool isDryRun)
182// {
183
184// }
185
186} // namespace nfd