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