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