blob: 5a7e3975e29aa9d68bd37b73f0c6eeb705c48b08 [file] [log] [blame]
Alexander Afanasyeve289b532014-02-09 22:14:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * See COPYING for copyright and distribution information.
5 */
6
Alexander Afanasyeve289b532014-02-09 22:14:44 -08007#include "nfd-controller.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -08008#include "nfd-control-response.hpp"
9
10namespace ndn {
11namespace nfd {
12
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080013Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080014 : m_face(face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080015{
16}
17
18void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070019Controller::processCommandResponse(const Data& data,
20 const shared_ptr<ControlCommand>& command,
21 const CommandSucceedCallback& onSuccess,
22 const CommandFailCallback& onFailure)
23{
24 /// \todo verify Data signature
25
26 const uint32_t serverErrorCode = 500;
27
28 ControlResponse response;
29 try {
30 response.wireDecode(data.getContent().blockFromValue());
31 }
32 catch (ndn::Tlv::Error& e) {
33 if (static_cast<bool>(onFailure))
34 onFailure(serverErrorCode, e.what());
35 return;
36 }
37
38 uint32_t code = response.getCode();
39 const uint32_t errorCodeLowerBound = 400;
40 if (code >= errorCodeLowerBound) {
41 if (static_cast<bool>(onFailure))
42 onFailure(code, response.getText());
43 return;
44 }
45
46 ControlParameters parameters;
47 try {
48 parameters.wireDecode(response.getBody());
49 }
50 catch (ndn::Tlv::Error& e) {
51 if (static_cast<bool>(onFailure))
52 onFailure(serverErrorCode, e.what());
53 return;
54 }
55
56 try {
57 command->validateResponse(parameters);
58 }
59 catch (ControlCommand::ArgumentError& e) {
60 if (static_cast<bool>(onFailure))
61 onFailure(serverErrorCode, e.what());
62 return;
63 }
64
65 onSuccess(parameters);
66}
67
68
69void
Alexander Afanasyeve289b532014-02-09 22:14:44 -080070Controller::selfRegisterPrefix(const Name& prefixToRegister,
71 const SuccessCallback& onSuccess,
72 const FailCallback& onFail)
73{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070074 const uint32_t selfFaceId = 0;
75
76 ControlParameters parameters;
77 parameters.setName(prefixToRegister)
78 .setFaceId(selfFaceId);
79
80 this->start<FibAddNextHopCommand>(parameters,
81 bind(onSuccess),
82 bind(onFail, _2));
Alexander Afanasyev21abc102014-02-18 18:59:02 -080083}
84
85void
Alexander Afanasyev884280c2014-03-07 09:40:39 +000086Controller::selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080087 const SuccessCallback& onSuccess,
88 const FailCallback& onFail)
89{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070090 const uint32_t selfFaceId = 0;
91
92 ControlParameters parameters;
93 parameters.setName(prefixToDeRegister)
94 .setFaceId(selfFaceId);
95
96 this->start<FibRemoveNextHopCommand>(parameters,
97 bind(onSuccess),
98 bind(onFail, _2));
Alexander Afanasyev884280c2014-03-07 09:40:39 +000099}
hilataa99e37e2014-02-15 23:52:46 -0600100
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000101void
102Controller::fibAddNextHop(const Name& prefix, uint64_t faceId, int cost,
Obaid6e7f5f12014-03-11 14:46:10 -0500103 const FibCommandSucceedCallback& onSuccess,
104 const FailCallback& onFail)
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000105{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700106 BOOST_ASSERT(cost >= 0);
107
108 ControlParameters parameters;
109 parameters.setName(prefix)
110 .setFaceId(faceId)
111 .setCost(static_cast<uint64_t>(cost));
112
113 this->start<FibAddNextHopCommand>(parameters,
114 onSuccess,
115 bind(onFail, _2));
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000116}
117
118void
119Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId,
Obaid6e7f5f12014-03-11 14:46:10 -0500120 const FibCommandSucceedCallback& onSuccess,
121 const FailCallback& onFail)
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000122{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700123 ControlParameters parameters;
124 parameters.setName(prefix)
125 .setFaceId(faceId);
126
127 this->start<FibRemoveNextHopCommand>(parameters,
128 onSuccess,
129 bind(onFail, _2));
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800130}
131
132void
133Controller::startFibCommand(const std::string& command,
134 const FibManagementOptions& options,
135 const FibCommandSucceedCallback& onSuccess,
136 const FailCallback& onFail)
137{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700138 if (command == "add-nexthop") {
139 this->start<FibAddNextHopCommand>(options,
140 onSuccess,
141 bind(onFail, _2));
142 }
143 else if (command == "remove-nexthop") {
144 this->start<FibRemoveNextHopCommand>(options,
145 onSuccess,
146 bind(onFail, _2));
147 }
148 else {
149 onFail("unknown command");
150 }
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800151}
152
hilataa99e37e2014-02-15 23:52:46 -0600153void
154Controller::startFaceCommand(const std::string& command,
155 const FaceManagementOptions& options,
156 const FaceCommandSucceedCallback& onSuccess,
157 const FailCallback& onFail)
158{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700159 if (command == "create") {
160 this->start<FaceCreateCommand>(options,
161 onSuccess,
162 bind(onFail, _2));
hilata7d160f22014-03-13 19:51:42 -0500163 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700164 else if (command == "destroy") {
165 this->start<FaceDestroyCommand>(options,
166 onSuccess,
167 bind(onFail, _2));
168 }
169 // enable-local-control and disable-local-control are not in legacy API.
170 else {
171 onFail("unknown command");
hilata7d160f22014-03-13 19:51:42 -0500172 }
hilataa99e37e2014-02-15 23:52:46 -0600173}
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000174
hilata7d160f22014-03-13 19:51:42 -0500175void
176Controller::startStrategyChoiceCommand(const std::string& command,
177 const StrategyChoiceOptions& options,
178 const StrategyChoiceCommandSucceedCallback& onSuccess,
179 const FailCallback& onFail)
180{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700181 if (command == "set") {
182 this->start<StrategyChoiceSetCommand>(options,
183 onSuccess,
184 bind(onFail, _2));
hilata7d160f22014-03-13 19:51:42 -0500185 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700186 else if (command == "unset") {
187 this->start<StrategyChoiceUnsetCommand>(options,
188 onSuccess,
189 bind(onFail, _2));
190 }
191 else {
192 onFail("unknown command");
hilata7d160f22014-03-13 19:51:42 -0500193 }
194}
195
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700196
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800197} // namespace nfd
198} // namespace ndn