OpenSDN source code
rest_server.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <vector>
7 #include <string>
8 
9 #include <boost/bind/bind.hpp>
10 #include <boost/assign.hpp>
11 #include <boost/foreach.hpp>
12 
13 #include "base/regex.h"
14 #include "http/http_server.h"
15 #include "http/http_request.h"
16 #include "http/http_session.h"
17 #include "base/contrail_ports.h"
19 #include "cmn/agent.h"
20 #include "rest_server.h"
21 #include "rest_common.h"
22 #include "init/agent_param.h"
23 
24 using namespace boost::placeholders;
25 
26 using contrail::regex;
29 
30 class RestServerGetVmCfgTask : public Task {
31 public:
33  const struct RestServer::RESTData& data):
34  Task((TaskScheduler::GetInstance()->GetTaskId("Agent::RestApi")), 0),
35  pih_(pih),
36  vm_name_((*data.match)[1]),
37  data_(data),
38  context_(data_.session->get_context()) {
39  }
41  virtual bool Run() {
42  std::string info;
43  const std::string client_ctx = data_.session->get_client_context(context_);
44  // check session is not deleted
45  if ((!data_.session->set_client_context(context_, client_ctx)) ||
46  (context_.empty()))
47  return true;
48  if (pih_->GetVmVnCfgPort(vm_name_, info)) {
49  REST::SendResponse(data_.session, info, 200, context_);
50  } else {
51  REST::SendErrorResponse(data_.session, "{ Not Found }", 404, context_);
52  }
53  return true;
54  }
55  std::string Description() const { return "RestServerGetVmCfgTask"; }
56 private:
58  std::string vm_name_;
59  const struct RestServer::RESTData& data_;
60  const std::string context_;
62 };
63 
64 void RestServer::VmPortPostHandler(const struct RESTData& data) {
66  if (pih) {
67  std::string err_msg;
68  if (pih->AddPortFromJson(data.request->Body(), false, err_msg, true)) {
69  REST::SendResponse(data.session, "{}");
70  } else {
71  REST::SendErrorResponse(data.session, "{ " + err_msg + " }");
72  }
73  } else {
74  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
75  }
76 }
77 
78 void RestServer::VmPortSyncHandler(const struct RESTData& data) {
80  if (pih) {
81  pih->SyncHandler();
82  REST::SendResponse(data.session, "{}");
83  } else {
84  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
85  }
86 }
87 
88 void RestServer::VmPortDeleteHandler(const struct RESTData& data) {
89  std::string error;
90  const std::string& port_id = (*data.match)[1];
92  if (pih) {
93  if (pih->DeletePort(port_id, error)) {
94  REST::SendResponse(data.session, "{}");
95  } else {
96  REST::SendErrorResponse(data.session, "{" + error + "}");
97  }
98  } else {
99  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
100  }
101 }
102 
103 void RestServer::VmPortGetHandler(const struct RESTData& data) {
104  const std::string& port_id = (*data.match)[1];
106  if (pih) {
107  std::string info;
108  if (pih->GetPortInfo(port_id, info)) {
109  REST::SendResponse(data.session, info);
110  } else {
111  REST::SendErrorResponse(data.session, "{ Not Found }", 404);
112  }
113  } else {
114  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
115  }
116 }
117 
118 void RestServer::GatewayPostHandler(const struct RESTData& data) {
120  if (pih) {
121  std::string err_msg;
122  if (pih->AddVgwFromJson(data.request->Body(), err_msg)) {
123  REST::SendResponse(data.session, "{}");
124  } else {
125  REST::SendErrorResponse(data.session, "{ " + err_msg + " }");
126  }
127  } else {
128  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
129  }
130 }
131 
132 void RestServer::GatewayDeleteHandler(const struct RESTData& data) {
133  std::string error;
135  if (pih) {
136  std::string err_msg;
137  if (pih->DelVgwFromJson(data.request->Body(), err_msg)) {
138  REST::SendResponse(data.session, "{}");
139  } else {
140  REST::SendErrorResponse(data.session, "{" + error + "}");
141  }
142  } else {
143  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
144  }
145 }
146 
147 void RestServer::VmVnPortPostHandler(const struct RESTData& data) {
149  if (pih) {
150  std::string err_msg;
151  if (pih->AddVmVnPort(data.request->Body(), false, err_msg, true)) {
152  REST::SendResponse(data.session, "{}");
153  } else {
154  REST::SendErrorResponse(data.session, "{ " + err_msg + " }");
155  }
156  } else {
157  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
158  }
159 }
160 
161 void RestServer::VmVnPortDeleteHandler(const struct RESTData& data) {
162  std::string error;
163  const std::string &vm_uuid = (*data.match)[1];
165  if (pih) {
166  if (pih->DeleteVmVnPort(data.request->Body(), vm_uuid, error)) {
167  REST::SendResponse(data.session, "{}");
168  } else {
169  REST::SendErrorResponse(data.session, "{" + error + "}");
170  }
171  } else {
172  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
173  }
174 }
175 
176 void RestServer::VmVnPortGetHandler(const struct RESTData& data) {
177  std::string url = (*data.match)[0];
178  bool vmi_uuid_presence = false;
179  std::size_t pos = 4; // skip "/vm/"
180  std::size_t vmi_uuid_pos = url.find("/", pos);
181  if (vmi_uuid_pos != std::string::npos) {
182  vmi_uuid_presence = true;
183  }
184  std::string vm_uuid = url.substr(pos, vmi_uuid_pos);
185  std::string vmi_uuid = "";
186  if (vmi_uuid_presence) {
187  vmi_uuid = url.substr(vmi_uuid_pos+1, std::string::npos);
188  }
189 
191  if (pih) {
192  std::string info;
193  if (pih->GetVmVnPort(vm_uuid, vmi_uuid, info)) {
194  REST::SendResponse(data.session, info);
195  } else {
196  REST::SendErrorResponse(data.session, "{ Not Found }", 404);
197  }
198  } else {
199  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
200  }
201 }
202 
203 void RestServer::VmVnPortCfgGetHandler(const struct RESTData& data) {
205  if (pih) {
207  new RestServerGetVmCfgTask(pih, data);
209  scheduler->Enqueue(t);
210  } else {
211  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
212  }
213 }
214 
215 void RestServer::VmPortEnableHandler(const struct RESTData& data) {
216  std::string error;
217  const std::string& port_id = (*data.match)[1];
219  if (pih) {
220  if (pih->EnablePort(port_id, error)) {
221  REST::SendResponse(data.session, "{}");
222  } else {
223  REST::SendErrorResponse(data.session, "{" + error + "}");
224  }
225  } else {
226  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
227  }
228 }
229 
230 void RestServer::VmPortDisableHandler(const struct RESTData& data) {
231  std::string error;
232  const std::string& port_id = (*data.match)[1];
234  if (pih) {
235  if (pih->DisablePort(port_id, error)) {
236  REST::SendResponse(data.session, "{}");
237  } else {
238  REST::SendErrorResponse(data.session, "{" + error + "}");
239  }
240  } else {
241  REST::SendErrorResponse(data.session, "{ Operation Not Supported }");
242  }
243 }
244 
245 const std::vector<RestServer::HandlerSpecifier> RestServer::RESTHandlers_ =
246  boost::assign::list_of
247  (HandlerSpecifier(
248  regex("/port"),
249  HTTP_POST,
251  (HandlerSpecifier(
252  regex("/syncports"),
253  HTTP_POST,
255  (HandlerSpecifier(
256  regex("/port/"
257  "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"),
258  HTTP_DELETE,
260  (HandlerSpecifier(
261  regex("/port/"
262  "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"),
263  HTTP_GET,
265  (HandlerSpecifier(
266  regex("/gateway"),
267  HTTP_POST,
269  (HandlerSpecifier(
270  regex("/gateway"),
271  HTTP_DELETE,
273  (HandlerSpecifier(
274  regex("/vm"),
275  HTTP_POST,
277  (HandlerSpecifier(
278  regex("/vm/"
279  "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"),
280  HTTP_DELETE,
282  (HandlerSpecifier(
283  regex("/vm/"
284  "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"),
285  HTTP_GET,
287  (HandlerSpecifier(
288  regex("/vm/"
289  "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/"
290  "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"),
291  HTTP_GET,
293  (HandlerSpecifier(
294  regex("/vm-cfg/(.*)"),
295  HTTP_GET,
297  (HandlerSpecifier(
298  regex("/enable-port/"
299  "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"),
300  HTTP_PUT,
302  (HandlerSpecifier(
303  regex("/disable-port/"
304  "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"),
305  HTTP_PUT,
307 
309  : agent_(agent), http_server_(new HttpServer(agent->event_manager())) {
311  boost::bind(&RestServer::HandleRequest, this, _1, _2));
312 }
313 
316  //only if RestServer is used via mocked agent
318  else //otherwise , use the hardcoded value
320 }
321 
323  const HttpRequest* request) {
324  std::string path = request->UrlPath();
325  BOOST_FOREACH(const RestServer::HandlerSpecifier& hs, RESTHandlers_) {
326  boost::smatch match;
327  if (regex_match(path, match, hs.request_regex) &&
328  request->GetMethod() == hs.method) {
329  struct RESTData data = {
330  &match,
331  request->GetMethod(),
332  session,
333  request
334  };
335  (this->*hs.handler_func)(data);
336  // Though IMHO it contradicts most programmers' intuition,
337  // currently handlers are responsible for freeing handled
338  // requests.
339  delete request;
340  return;
341  }
342  }
343  REST::SendErrorResponse(session, "Unknown Request", 404);
344 }
345 
347  if (http_server_) {
350  http_server_ = NULL;
351  }
352 }
353 
355 }
bool cat_is_agent_mocked() const
Definition: agent_param.h:568
uint16_t rest_port() const
Definition: agent_param.h:312
Definition: agent.h:361
PortIpcHandler * port_ipc_handler() const
Definition: agent.cc:1009
AgentParam * params() const
Definition: agent.h:1227
static const uint16_t PortIpcVrouterAgentPort()
const std::string & Body() const
Definition: http_request.h:40
http_method GetMethod() const
Definition: http_request.h:22
std::string UrlPath() const
Definition: http_request.cc:26
void RegisterHandler(const std::string &path, HttpHandlerFn handler)
Definition: http_server.cc:103
void Shutdown()
Definition: http_server.cc:72
bool DisablePort(const string &url, string &err_msg)
bool EnablePort(const string &url, string &err_msg)
bool DeletePort(const string &url, string &err_msg)
bool GetVmVnPort(const std::string &vm_uuid, const std::string &vmi_uuid, std::string &info) const
bool AddVgwFromJson(const std::string &json, std::string &err_msg) const
bool GetPortInfo(const std::string &uuid_str, std::string &info) const
bool AddPortFromJson(const string &json, bool check_port, string &err_msg, bool write_file)
bool DeleteVmVnPort(const boost::uuids::uuid &vmi_uuid, string &err_msg)
bool AddVmVnPort(const std::string &json, bool check_port, std::string &err_msg, bool write_file)
bool DelVgwFromJson(const std::string &json, std::string &err_msg) const
const PortIpcHandler * pih_
Definition: rest_server.cc:57
DISALLOW_COPY_AND_ASSIGN(RestServerGetVmCfgTask)
const struct RestServer::RESTData & data_
Definition: rest_server.cc:59
std::string Description() const
Gives a description of the task.
Definition: rest_server.cc:55
const std::string context_
Definition: rest_server.cc:60
RestServerGetVmCfgTask(PortIpcHandler *pih, const struct RestServer::RESTData &data)
Definition: rest_server.cc:32
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
Definition: rest_server.cc:41
virtual ~RestServerGetVmCfgTask()
Definition: rest_server.cc:40
contrail::regex request_regex
Definition: rest_server.h:45
enum http_method method
Definition: rest_server.h:46
HttpServer * http_server_
Definition: rest_server.h:71
void VmVnPortGetHandler(const struct RESTData &)
Definition: rest_server.cc:176
static const std::vector< HandlerSpecifier > RESTHandlers_
Definition: rest_server.h:50
void Shutdown()
Definition: rest_server.cc:346
void VmPortEnableHandler(const struct RESTData &data)
Definition: rest_server.cc:215
friend class RestServerGetVmCfgTask
Definition: rest_server.h:68
void VmPortGetHandler(const struct RESTData &)
Definition: rest_server.cc:103
void GatewayDeleteHandler(const struct RESTData &data)
Definition: rest_server.cc:132
RestServer(Agent *agent)
Definition: rest_server.cc:308
void VmVnPortPostHandler(const struct RESTData &)
Definition: rest_server.cc:147
void VmVnPortDeleteHandler(const struct RESTData &)
Definition: rest_server.cc:161
Agent * agent_
Definition: rest_server.h:70
void VmPortDeleteHandler(const struct RESTData &)
Definition: rest_server.cc:88
void VmPortDisableHandler(const struct RESTData &data)
Definition: rest_server.cc:230
void GatewayPostHandler(const struct RESTData &data)
Definition: rest_server.cc:118
virtual ~RestServer()
Definition: rest_server.cc:354
void VmVnPortCfgGetHandler(const struct RESTData &data)
Definition: rest_server.cc:203
void VmPortPostHandler(const struct RESTData &)
Definition: rest_server.cc:64
void InitDone()
Definition: rest_server.cc:314
void VmPortSyncHandler(const struct RESTData &data)
Definition: rest_server.cc:78
void HandleRequest(HttpSession *session, const HttpRequest *request)
Definition: rest_server.cc:322
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq....
Definition: task.cc:662
static TaskScheduler * GetInstance()
Definition: task.cc:574
Task is a class to describe a computational task within OpenSDN control plane applications....
Definition: task.h:82
static void DeleteServer(TcpServer *server)
Definition: tcp_server.cc:659
virtual bool Initialize(unsigned short port)
Definition: tcp_server.cc:61
#define HTTP_WILDCARD_ENTRY
Definition: http_server.h:17
void SendErrorResponse(HttpSession *session, const std::string &error_msg, int status_code, std::string context)
Definition: rest_common.cc:32
void SendResponse(HttpSession *session, const std::string &msg, int status_code, std::string context)
Definition: rest_common.cc:16
static bool regex_search(const std::string &input, const regex &regex)
Definition: regex.h:26
static bool regex_match(const std::string &input, const regex &regex)
Definition: regex.h:35
const boost::smatch * match
Definition: rest_server.h:28
const HttpRequest * request
Definition: rest_server.h:31
HttpSession * session
Definition: rest_server.h:30