OpenSDN source code
bgp_show_rtarget_group.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_internal_types.h"
10 #include "bgp/bgp_server.h"
12 
13 using contrail::regex;
16 using std::string;
17 using std::vector;
18 
19 //
20 // Fill in information for list of rtarget groups.
21 //
22 // Allows regular, summary and peer introspect to share code.
23 // Assumes that search_string is the peer name if match_peer is true.
24 //
25 static bool FillRtGroupInfoList(const BgpSandeshContext *bsc,
26  bool summary, bool match_peer, uint32_t page_limit, uint32_t iter_limit,
27  const string &start_rtarget_str, const string &search_string,
28  vector<ShowRtGroupInfo> *srtg_list, string *next_rtarget_str) {
29  RouteTarget rtarget;
30 
31  // Bail if start_rtarget_str is bad.
32  if (!start_rtarget_str.empty()) {
33  rtarget = RouteTarget::FromString(start_rtarget_str);
34  if (rtarget.IsNull())
35  return true;
36  }
37 
38  // Bail if there's no peer specified when doing a peer introspect.
39  if (match_peer && search_string.empty())
40  return true;
41 
42  regex search_expr(search_string);
43  const RTargetGroupMgr *rtgroup_mgr = bsc->bgp_server->rtarget_group_mgr();
44  RTargetGroupMgr::const_iterator it = rtgroup_mgr->lower_bound(rtarget);
45  for (uint32_t iter_count = 0; it != rtgroup_mgr->end();
46  ++it, ++iter_count) {
47  const RtGroup *rtgroup = it->second;
48  if (!match_peer && !regex_search(rtgroup->ToString(), search_expr))
49  continue;
50  ShowRtGroupInfo srtg;
51  if (match_peer) {
52  if (!rtgroup->HasInterestedPeer(search_string))
53  continue;
54  rtgroup->FillShowPeerInfo(&srtg);
55  } else if (summary) {
56  rtgroup->FillShowSummaryInfo(&srtg);
57  } else {
58  rtgroup->FillShowInfo(&srtg);
59  }
60  srtg_list->push_back(srtg);
61  if (srtg_list->size() >= page_limit)
62  break;
63  if (iter_count >= iter_limit)
64  break;
65  }
66 
67  // All done if we've looked at all rtarget groups.
68  if (it == rtgroup_mgr->end() || ++it == rtgroup_mgr->end())
69  return true;
70 
71  // Return true if we've reached the page limit, false if we've reached the
72  // iteration limit.
73  bool done = srtg_list->size() >= page_limit;
74  *next_rtarget_str = it->second->ToString();
75  return done;
76 }
77 
78 //
79 // Specialization of BgpShowHandler<>::CallbackCommon for regular introspect.
80 //
81 template <>
82 bool BgpShowHandler<ShowRtGroupReq, ShowRtGroupReqIterate,
83  ShowRtGroupResp, ShowRtGroupInfo>::CallbackCommon(
84  const BgpSandeshContext *bsc, Data *data) {
85  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
86  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
87  string next_rtarget_str;
88  bool done = FillRtGroupInfoList(bsc, false, false, page_limit, iter_limit,
89  data->next_entry, data->search_string, &data->show_list,
90  &next_rtarget_str);
91  if (!next_rtarget_str.empty())
92  SaveContextToData(next_rtarget_str, done, data);
93  return done;
94 }
95 
96 //
97 // Specialization of BgpShowHandler<>::FillShowList for regular introspect.
98 //
99 template <>
100 void BgpShowHandler<ShowRtGroupReq, ShowRtGroupReqIterate,
101  ShowRtGroupResp, ShowRtGroupInfo>::FillShowList(
102  ShowRtGroupResp *resp,
103  const vector<ShowRtGroupInfo> &show_list) {
104  resp->set_rtgroup_list(show_list);
105 }
106 
107 //
108 // Specialization of BgpShowHandler<>::CallbackCommon for summary introspect.
109 //
110 template <>
111 bool BgpShowHandler<ShowRtGroupSummaryReq, ShowRtGroupSummaryReqIterate,
112  ShowRtGroupSummaryResp, ShowRtGroupInfo>::CallbackCommon(
113  const BgpSandeshContext *bsc, Data *data) {
114  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
115  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
116  string next_rtarget_str;
117  bool done = FillRtGroupInfoList(bsc, true, false, page_limit, iter_limit,
118  data->next_entry, data->search_string, &data->show_list,
119  &next_rtarget_str);
120  if (!next_rtarget_str.empty())
121  SaveContextToData(next_rtarget_str, done, data);
122  return done;
123 }
124 
125 //
126 // Specialization of BgpShowHandler<>::FillShowList for summary introspect.
127 //
128 template <>
129 void BgpShowHandler<ShowRtGroupSummaryReq, ShowRtGroupSummaryReqIterate,
130  ShowRtGroupSummaryResp, ShowRtGroupInfo>::FillShowList(
131  ShowRtGroupSummaryResp *resp,
132  const vector<ShowRtGroupInfo> &show_list) {
133  resp->set_rtgroup_list(show_list);
134 }
135 
136 //
137 // Specialization of BgpShowHandler<>::CallbackCommon for peer introspect.
138 //
139 template <>
140 bool BgpShowHandler<ShowRtGroupPeerReq, ShowRtGroupPeerReqIterate,
141  ShowRtGroupPeerResp, ShowRtGroupInfo>::CallbackCommon(
142  const BgpSandeshContext *bsc, Data *data) {
143  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
144  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
145  string next_rtarget_str;
146  bool done = FillRtGroupInfoList(bsc, false, true, page_limit, iter_limit,
147  data->next_entry, data->search_string, &data->show_list,
148  &next_rtarget_str);
149  if (!next_rtarget_str.empty())
150  SaveContextToData(next_rtarget_str, done, data);
151  return done;
152 }
153 
154 //
155 // Specialization of BgpShowHandler<>::FillShowList for peer introspect.
156 //
157 template <>
158 void BgpShowHandler<ShowRtGroupPeerReq, ShowRtGroupPeerReqIterate,
159  ShowRtGroupPeerResp, ShowRtGroupInfo>::FillShowList(
160  ShowRtGroupPeerResp *resp,
161  const vector<ShowRtGroupInfo> &show_list) {
162  resp->set_rtgroup_list(show_list);
163 }
164 
165 //
166 // Handler for ShowRtGroupReq.
167 //
168 void ShowRtGroupReq::HandleRequest() const {
169  RequestPipeline::PipeSpec ps(this);
172 
173  s1.taskId_ = scheduler->GetTaskId("bgp::RTFilter");
174  s1.cbFn_ = boost::bind(&BgpShowHandler<
175  ShowRtGroupReq,
176  ShowRtGroupReqIterate,
177  ShowRtGroupResp,
178  ShowRtGroupInfo>::Callback, _1, _2, _3, _4, _5);
180  ShowRtGroupReq,
181  ShowRtGroupReqIterate,
182  ShowRtGroupResp,
183  ShowRtGroupInfo>::CreateData;
184  s1.instances_.push_back(0);
185  ps.stages_.push_back(s1);
186  RequestPipeline rp(ps);
187 }
188 
189 //
190 // Handler for ShowRtGroupReqIterate.
191 //
192 void ShowRtGroupReqIterate::HandleRequest() const {
193  RequestPipeline::PipeSpec ps(this);
196 
197  s1.taskId_ = scheduler->GetTaskId("bgp::RTFilter");
198  s1.cbFn_ = boost::bind(&BgpShowHandler<
199  ShowRtGroupReq,
200  ShowRtGroupReqIterate,
201  ShowRtGroupResp,
202  ShowRtGroupInfo>::CallbackIterate, _1, _2, _3, _4, _5);
204  ShowRtGroupReq,
205  ShowRtGroupReqIterate,
206  ShowRtGroupResp,
207  ShowRtGroupInfo>::CreateData;
208  s1.instances_.push_back(0);
209  ps.stages_.push_back(s1);
210  RequestPipeline rp(ps);
211 }
212 
213 //
214 // Handler for ShowRtGroupSummaryReq.
215 //
216 void ShowRtGroupSummaryReq::HandleRequest() const {
217  RequestPipeline::PipeSpec ps(this);
220 
221  s1.taskId_ = scheduler->GetTaskId("bgp::RTFilter");
222  s1.cbFn_ = boost::bind(&BgpShowHandler<
223  ShowRtGroupSummaryReq,
224  ShowRtGroupSummaryReqIterate,
225  ShowRtGroupSummaryResp,
226  ShowRtGroupInfo>::Callback, _1, _2, _3, _4, _5);
228  ShowRtGroupSummaryReq,
229  ShowRtGroupSummaryReqIterate,
230  ShowRtGroupSummaryResp,
231  ShowRtGroupInfo>::CreateData;
232  s1.instances_.push_back(0);
233  ps.stages_.push_back(s1);
234  RequestPipeline rp(ps);
235 }
236 
237 //
238 // Handler for ShowRtGroupSummaryReqIterate.
239 //
240 void ShowRtGroupSummaryReqIterate::HandleRequest() const {
241  RequestPipeline::PipeSpec ps(this);
244 
245  s1.taskId_ = scheduler->GetTaskId("bgp::RTFilter");
246  s1.cbFn_ = boost::bind(&BgpShowHandler<
247  ShowRtGroupSummaryReq,
248  ShowRtGroupSummaryReqIterate,
249  ShowRtGroupSummaryResp,
250  ShowRtGroupInfo>::CallbackIterate, _1, _2, _3, _4, _5);
252  ShowRtGroupSummaryReq,
253  ShowRtGroupSummaryReqIterate,
254  ShowRtGroupSummaryResp,
255  ShowRtGroupInfo>::CreateData;
256  s1.instances_.push_back(0);
257  ps.stages_.push_back(s1);
258  RequestPipeline rp(ps);
259 }
260 
261 //
262 // Handler for ShowRtGroupPeerReq.
263 //
264 void ShowRtGroupPeerReq::HandleRequest() const {
265  RequestPipeline::PipeSpec ps(this);
268 
269  s1.taskId_ = scheduler->GetTaskId("bgp::RTFilter");
270  s1.cbFn_ = boost::bind(&BgpShowHandler<
271  ShowRtGroupPeerReq,
272  ShowRtGroupPeerReqIterate,
273  ShowRtGroupPeerResp,
274  ShowRtGroupInfo>::Callback, _1, _2, _3, _4, _5);
276  ShowRtGroupPeerReq,
277  ShowRtGroupPeerReqIterate,
278  ShowRtGroupPeerResp,
279  ShowRtGroupInfo>::CreateData;
280  s1.instances_.push_back(0);
281  ps.stages_.push_back(s1);
282  RequestPipeline rp(ps);
283 }
284 
285 //
286 // Handler for ShowRtGroupPeerReqIterate.
287 //
288 void ShowRtGroupPeerReqIterate::HandleRequest() const {
289  RequestPipeline::PipeSpec ps(this);
292 
293  s1.taskId_ = scheduler->GetTaskId("bgp::RTFilter");
294  s1.cbFn_ = boost::bind(&BgpShowHandler<
295  ShowRtGroupPeerReq,
296  ShowRtGroupPeerReqIterate,
297  ShowRtGroupPeerResp,
298  ShowRtGroupInfo>::CallbackIterate, _1, _2, _3, _4, _5);
300  ShowRtGroupPeerReq,
301  ShowRtGroupPeerReqIterate,
302  ShowRtGroupPeerResp,
303  ShowRtGroupInfo>::CreateData;
304  s1.instances_.push_back(0);
305  ps.stages_.push_back(s1);
306  RequestPipeline rp(ps);
307 }
static bool FillRtGroupInfoList(const BgpSandeshContext *bsc, bool summary, bool match_peer, uint32_t page_limit, uint32_t iter_limit, const string &start_rtarget_str, const string &search_string, vector< ShowRtGroupInfo > *srtg_list, string *next_rtarget_str)
RTargetGroupMgr * rtarget_group_mgr()
Definition: bgp_server.h:115
const_iterator end() const
RtGroupMap::const_iterator const_iterator
const_iterator lower_bound(const RouteTarget &rt) const
bool IsNull() const
static RouteTarget FromString(const std::string &str, boost::system::error_code *error=NULL)
void FillShowInfo(ShowRtGroupInfo *info) const
bool HasInterestedPeer(const std::string &name) const
void FillShowPeerInfo(ShowRtGroupInfo *info) const
std::string ToString() const
Definition: rtarget_group.h:73
void FillShowSummaryInfo(ShowRtGroupInfo *info) const
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
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_