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