OpenSDN source code
httpd.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "base/logging.h"
7 #include "base/address_util.h"
8 #include "http/http_request.h"
9 #include "http/http_server.h"
10 #include "http/http_session.h"
11 #include "io/event_manager.h"
12 #include "sandesh/request_pipeline.h"
13 #include "http/http_message_test_types.h"
14 #include "http/route_test_types.h"
15 #include "config/uve/virtual_network_types.h"
16 #include <boost/bind/bind.hpp>
17 #include <boost/foreach.hpp>
18 #include <boost/tokenizer.hpp>
19 #include <boost/assign/list_of.hpp>
20 
21 using namespace boost::assign;
22 using namespace std;
23 using namespace boost::placeholders;
24 
25 class ServerInfo {
26 public:
27  void HandleRequest(HttpSession *session, const HttpRequest *request) {
28  const char response[] =
29 "HTTP/1.1 200 OK\n"
30 "Content-Type: text/html; charset=UTF-8\n"
31 "Content-Length: 45\n"
32 "\n"
33 "<html>\n"
34 "<title>Server Status</title>\n"
35 "</html>\n"
36 ;
37  session->Send(reinterpret_cast<const u_int8_t *>(response),
38  sizeof(response), NULL);
39  delete request;
40  }
41 };
42 
43 static void ServerShutdown(HttpServer *http, HttpSession *session,
44  const HttpRequest *request) {
45  const char response[] =
46 "HTTP/1.1 OK\r\n"
47  ;
48  session->Send(reinterpret_cast<const u_int8_t *>(response),
49  sizeof(response), NULL);
50  http->event_manager()->Shutdown();
51 }
52 
53 
55 public:
56  HttpRequestTestData() : ctr(0) {}
57  int ctr;
58 };
59 
60 
63  return new HttpRequestTestData;
64 }
65 
66 bool
69  int stage, int instNum,
70  RequestPipeline::InstData * dataInAndOut) {
71  HttpRequestTestData* mydata =
72  static_cast<HttpRequestTestData*>(dataInAndOut);
73 
74  if (!mydata) {
75  LOG(DEBUG, "Callback Stage " << stage << " has no data");
76  return true;
77  }
78  LOG(DEBUG, "Callback Stage " << stage << " ctr " << mydata->ctr);
79  if (instNum == 1) {
80  if ((++mydata->ctr) < 3 ) {
81  sleep(1);
82  return false;
83  }
84  HttpRequestTestResp *resp = new(HttpRequestTestResp);
85  resp->set_name("TestName");
86  resp->set_num(1234);
87  resp->set_context(sr->context());
88  resp->Response();
89  }
90  sleep(3);
91  return true;
92 }
93 
94 
95 void
96 HttpRequestTest::HandleRequest() const{
97  LOG(DEBUG, "Got HandleRequest! - i32Elem " << i32Elem << " descString " << descString << " ctx " << context());
99 
100  RequestPipeline::StageSpec s1, s2, s3;
101  s1.taskId_ = 100;
104  s1.instances_ = list_of(-1)(-1)(20);
105 
106  s2.taskId_ = 200;
108  s2.instances_ = list_of(10)(20)(30)(40);
109 
110  s3.taskId_ = 300;
113  s3.instances_ = list_of(5);
114 
115  ps.stages_= list_of(s1)(s2)(s3);
116  RequestPipeline rp(ps);
117  return;
118 }
119 
120 void
121 VNSwitchRouteReq::HandleRequest() const{
122  // TODO: context should be from derived class
123  LOG(DEBUG, "Got HandleRequest! - " <<
124  " startRoutePrefix " << startRoutePrefix <<
125  " endRoutePrefix " << endRoutePrefix <<
126  " numRoutes " << numRoutes <<
127  " ctx " << Sandesh::context());
128 
129  VNSwitchRouteResp *vsrr = new VNSwitchRouteResp();
130  vsrr->set_vnSwitchId(100);
131  vsrr->set_vnId(50);
132 
133  vector<VNSRoute> lval;
134  VNSRoute vsnr;
135  vsnr.prefix = 2; vsnr.desc = "two";
136  lval.push_back(vsnr);
137  vsnr.prefix = 3; vsnr.desc = "three";
138  lval.push_back(vsnr);
139  vsnr.prefix = 4; vsnr.desc = "four";
140  lval.push_back(vsnr);
141  vsrr->set_vnRoutes(lval);
142 
143  vector<string> vs;
144  vs.push_back("XXX");
145  vs.push_back("YYY");
146  vsrr->set_names(vs);
147 
148  vsnr.prefix = 0; vsnr.desc = "zero";
149  vsrr->set_vnMarkerRoute(vsnr);
150  vsrr->set_context(context());
151  if (numRoutes > 5) vsrr->set_more(true);
152  vsrr->Response();
153 
154  if (numRoutes > 5) {
155  VNSContainTestResp *vctr = new VNSContainTestResp();
156  vctr->set_desc("The Contained");
157 
158  VNSContained vcd1, vcd2;
159  vcd1.set_magic(888);
160  vcd1.set_magic1(887);
161  vcd2.set_magic(999);
162  vcd2.set_magic1(998);
163 
164  vector<VNSContained> vvcd;
165  vvcd.push_back(vcd1);
166  vvcd.push_back(vcd2);
167 
168  VNSContainer vcr1,vcr2;
169  vcr1.set_magic2(8);
170  vcr1.set_vnCon(vcd1);
171  vcr1.set_vnLc(vvcd);
172 
173  vvcd.push_back(vcd1);
174  vcr2.set_magic2(9);
175  vcr2.set_vnCon(vcd2);
176  vcr2.set_vnLc(vvcd);
177 
178  vctr->set_vnC(vcr1);
179  vector<VNSContainer> vvc;
180  vvc.push_back(vcr2);
181  vvc.push_back(vcr1);
182  vvc.push_back(vcr1);
183  vvc.push_back(vcr2);
184  vctr->set_vnL(vvc);
185  vctr->set_context(context());
186  vctr->set_more(true);
187  vctr->Response();
188 
189  VNSwitchRouteResp *vsrr2 = new VNSwitchRouteResp();
190  vsrr2->set_vnSwitchId(155);
191  vsrr2->set_vnId(66);
192 
193  vsnr.prefix = 8; vsnr.desc = "ate";
194  lval.push_back(vsnr);
195  vsrr2->set_vnRoutes(lval);
196  vsrr2->set_vnMarkerRoute(vsnr);
197 
198  vsrr2->set_context(context());
199  vsrr2->Response();
200  }
201 
202  return;
203 }
204 
205 int
206 main(int argc, char *argv[]) {
207  LoggingInit();
208 
209  ServerInfo info;
211  HttpServer http(&evm);
212  http.RegisterHandler("/server-status",
213  boost::bind(&ServerInfo::HandleRequest, &info, _1, _2));
214  http.RegisterHandler("quitquitquit",
215  boost::bind(&ServerShutdown, &http, _1, _2));
216  http.Initialize(8090);
217  Sandesh::InitGeneratorTest("httpd", ResolveCanonicalName(), "httpd", "httpd", &evm, 8080);
218  Sandesh::SetLoggingParams(true, "HttpSession", SandeshLevel::UT_INFO);
219  evm.Run();
220  http.Shutdown();
221 }
std::string ResolveCanonicalName()
Definition: address_util.cc:41
std::string context() const
Definition: cpp/sandesh.h:314
static bool InitGeneratorTest(const std::string &module, const std::string &source, const std::string &node_type, const std::string &instance_id, EventManager *evm, unsigned short http_port, SandeshContext *client_context=NULL, const SandeshConfig &config=SandeshConfig())
Definition: sandesh.cc:312
static void SetLoggingParams(bool enable_local_log, std::string category, std::string level, bool enable_trace_print=false, bool enable_flow_log=false, bool enable_session_syslog=false)
Definition: sandesh.cc:371
void HandleRequest(HttpSession *session, const HttpRequest *request)
Definition: httpd.cc:27
EventManager * event_manager()
Definition: tcp_server.h:77
virtual bool Send(const uint8_t *data, size_t size, size_t *sent)
Definition: tcp_session.cc:430
static EventManager evm
bool HttpRequestTestCallback(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *dataInAndOut)
Definition: httpd.cc:67
HttpRequestTestData * HttpRequestTestFactory(int stage)
Definition: httpd.cc:62
static void ServerShutdown(HttpServer *http, HttpSession *session, const HttpRequest *request)
Definition: httpd.cc:43
void LoggingInit()
Definition: logging.cc:83
#define LOG(_Level, _Msg)
Definition: logging.h:35
std::vector< int > instances_