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