OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bgp_show_neighbor.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include "bgp/bgp_show_handler.h"
6 
7 #include "base/regex.h"
8 #include "bgp/bgp_peer.h"
9 #include "bgp/bgp_peer_internal_types.h"
10 #include "bgp/bgp_server.h"
13 
14 using contrail::regex;
17 using std::string;
18 using std::vector;
19 
20 //
21 // Build the list of BgpPeers in one shot for now.
22 //
24  bool summary, uint32_t page_limit, uint32_t iter_limit,
25  const string &start_neighbor, const string &search_string,
26  vector<BgpNeighborResp> *show_list, string *next_instance) {
27  regex search_expr(search_string);
28 
29  for (const BgpPeer *peer = bsc->bgp_server->FindNextPeer(); peer != NULL;
30  peer = bsc->bgp_server->FindNextPeer(peer->peer_name())) {
31  if ((!regex_search(peer->peer_basename(), search_expr)) &&
32  (!regex_search(peer->peer_address_string(), search_expr)) &&
33  (search_string != "deleted" || !peer->IsDeleted())) {
34  continue;
35  }
36 
37  BgpNeighborResp bnr;
38  peer->FillNeighborInfo(bsc, &bnr, summary);
39  show_list->push_back(bnr);
40  }
41  return true;
42 }
43 
44 //
45 // Specialization of BgpShowHandler<>::CallbackCommon for regular introspect.
46 //
47 // Note that we don't both paginating bgp neighbors since the list should be
48 // pretty small. We always add at least one xmpp neighbor to the list and then
49 // paginate the xmpp neighbors as needed.
50 //
51 template <>
52 bool BgpShowHandler<BgpNeighborReq, BgpNeighborReqIterate,
53  BgpNeighborListResp, BgpNeighborResp>::CallbackCommon(
54  const BgpSandeshContext *bsc, Data *data) {
55  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
56  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
57 
58  // Build the list of BgpPeers in one shot for now.
59  if (data->next_entry.empty()) {
60  FillBgpNeighborInfoList(bsc, false, page_limit, iter_limit, string(),
61  data->search_string, &data->show_list, NULL);
62  }
63 
64  // Add xmpp neighbors.
65  string next_neighbor;
66  bool done = bsc->ShowNeighborExtension(bsc, false, page_limit, iter_limit,
67  data->next_entry, data->search_string, &data->show_list,
68  &next_neighbor);
69  if (!next_neighbor.empty())
70  SaveContextToData(next_neighbor, done, data);
71  return done;
72 }
73 
74 //
75 // Specialization of BgpShowHandler<>::FillShowList for regular introspect.
76 //
77 template <>
78 void BgpShowHandler<BgpNeighborReq, BgpNeighborReqIterate,
79  BgpNeighborListResp, BgpNeighborResp>::FillShowList(
80  BgpNeighborListResp *resp, const vector<BgpNeighborResp> &show_list) {
81  resp->set_neighbors(show_list);
82 }
83 
84 //
85 // Specialization of BgpShowHandler<>::CallbackCommon for summary introspect.
86 //
87 // Note that we don't both paginating bgp neighbors since the list should be
88 // pretty small. We always add at least one xmpp neighbor to the list and then
89 // paginate the xmpp neighbors as needed.
90 //
91 template <>
92 bool BgpShowHandler<ShowBgpNeighborSummaryReq, ShowBgpNeighborSummaryReqIterate,
93  ShowBgpNeighborSummaryResp, BgpNeighborResp>::CallbackCommon(
94  const BgpSandeshContext *bsc, Data *data) {
95  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
96  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
97 
98  // Build the list of BgpPeers in one shot for now.
99  if (data->next_entry.empty()) {
100  FillBgpNeighborInfoList(bsc, true, page_limit, iter_limit, string(),
101  data->search_string, &data->show_list, NULL);
102  }
103 
104  // Add xmpp neighbors.
105  string next_neighbor;
106  bool done = bsc->ShowNeighborExtension(bsc, true, page_limit, iter_limit,
107  data->next_entry, data->search_string, &data->show_list,
108  &next_neighbor);
109  if (!next_neighbor.empty())
110  SaveContextToData(next_neighbor, done, data);
111  return done;
112 }
113 
114 //
115 // Specialization of BgpShowHandler<>::FillShowList for summary introspect.
116 //
117 template <>
118 void BgpShowHandler<ShowBgpNeighborSummaryReq, ShowBgpNeighborSummaryReqIterate,
119  ShowBgpNeighborSummaryResp, BgpNeighborResp>::FillShowList(
120  ShowBgpNeighborSummaryResp *resp,
121  const vector<BgpNeighborResp> &show_list) {
122  resp->set_neighbors(show_list);
123 }
124 
125 //
126 // Handler for BgpNeighborReq.
127 //
128 void BgpNeighborReq::HandleRequest() const {
129  RequestPipeline::PipeSpec ps(this);
132 
133  s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
134  s1.cbFn_ = boost::bind(&BgpShowHandler<
135  BgpNeighborReq,
136  BgpNeighborReqIterate,
137  BgpNeighborListResp,
138  BgpNeighborResp>::Callback, _1, _2, _3, _4, _5);
140  BgpNeighborReq,
141  BgpNeighborReqIterate,
142  BgpNeighborListResp,
143  BgpNeighborResp>::CreateData;
144  s1.instances_.push_back(0);
145  ps.stages_.push_back(s1);
146  RequestPipeline rp(ps);
147 }
148 
149 //
150 // Handler for BgpNeighborReqIterate.
151 //
152 void BgpNeighborReqIterate::HandleRequest() const {
153  RequestPipeline::PipeSpec ps(this);
156 
157  s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
158  s1.cbFn_ = boost::bind(&BgpShowHandler<
159  BgpNeighborReq,
160  BgpNeighborReqIterate,
161  BgpNeighborListResp,
162  BgpNeighborResp>::CallbackIterate, _1, _2, _3, _4, _5);
164  BgpNeighborReq,
165  BgpNeighborReqIterate,
166  BgpNeighborListResp,
167  BgpNeighborResp>::CreateData;
168  s1.instances_.push_back(0);
169  ps.stages_.push_back(s1);
170  RequestPipeline rp(ps);
171 }
172 
173 //
174 // Handler for ShowBgpNeighborSummaryReq.
175 //
176 void ShowBgpNeighborSummaryReq::HandleRequest() const {
177  RequestPipeline::PipeSpec ps(this);
180 
181  s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
182  s1.cbFn_ = boost::bind(&BgpShowHandler<
183  ShowBgpNeighborSummaryReq,
184  ShowBgpNeighborSummaryReqIterate,
185  ShowBgpNeighborSummaryResp,
186  BgpNeighborResp>::Callback, _1, _2, _3, _4, _5);
188  ShowBgpNeighborSummaryReq,
189  ShowBgpNeighborSummaryReqIterate,
190  ShowBgpNeighborSummaryResp,
191  BgpNeighborResp>::CreateData;
192  s1.instances_.push_back(0);
193  ps.stages_.push_back(s1);
194  RequestPipeline rp(ps);
195 }
196 
197 //
198 // Handler for ShowBgpNeighborSummaryReqIterate.
199 //
200 void ShowBgpNeighborSummaryReqIterate::HandleRequest() const {
201  RequestPipeline::PipeSpec ps(this);
204 
205  s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
206  s1.cbFn_ = boost::bind(&BgpShowHandler<
207  ShowBgpNeighborSummaryReq,
208  ShowBgpNeighborSummaryReqIterate,
209  ShowBgpNeighborSummaryResp,
210  BgpNeighborResp>::CallbackIterate, _1, _2, _3, _4, _5);
212  ShowBgpNeighborSummaryReq,
213  ShowBgpNeighborSummaryReqIterate,
214  ShowBgpNeighborSummaryResp,
215  BgpNeighborResp>::CreateData;
216  s1.instances_.push_back(0);
217  ps.stages_.push_back(s1);
218  RequestPipeline rp(ps);
219 }
std::vector< int > instances_
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:178
BgpServer * bgp_server
Definition: bgp_sandesh.h:46
int GetTaskId(const std::string &name)
Definition: task.cc:856
static bool regex_match(const std::string &input, const regex &regex)
Definition: regex.h:34
bool ShowNeighborExtension(const BgpSandeshContext *bsc, bool summary, uint32_t page_limit, uint32_t iter_limit, const std::string &start_neighbor, const std::string &search_string, std::vector< BgpNeighborResp > *list, std::string *next_neighbor) const
Definition: bgp_sandesh.cc:669
static TaskScheduler * GetInstance()
Definition: task.cc:547
uint32_t iter_limit() const
Definition: bgp_sandesh.h:65
static bool FillBgpNeighborInfoList(const BgpSandeshContext *bsc, bool summary, uint32_t page_limit, uint32_t iter_limit, const string &start_neighbor, const string &search_string, vector< BgpNeighborResp > *show_list, string *next_instance)
std::vector< ShowT > show_list
static bool regex_search(const std::string &input, const regex &regex)
Definition: regex.h:25
uint32_t page_limit() const
Definition: bgp_sandesh.h:63
BgpPeer * FindNextPeer(const std::string &name=std::string())
Definition: bgp_server.cc:634