OpenSDN source code
show_route_aggregate.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <boost/foreach.hpp>
7 #include <boost/assign/list_of.hpp>
8 
9 #include "base/regex.h"
10 #include "bgp/bgp_server.h"
11 #include "bgp/bgp_show_handler.h"
14 #include "bgp/routing-instance/route_aggregate_internal_types.h"
15 #include "bgp/routing-instance/route_aggregate_types.h"
16 
17 using boost::assign::list_of;
18 using contrail::regex;
21 using std::string;
22 using std::vector;
23 
25  bool summary, uint32_t page_limit, uint32_t iter_limit,
26  const string &start_instance, const string &search_string,
27  vector<AggregateRouteEntriesInfo> *are_list, string *next_instance) {
28  regex search_expr(search_string);
31  rim->name_clower_bound(start_instance);
32  for (uint32_t iter_count = 0; it != rim->name_cend(); ++it, ++iter_count) {
33  RoutingInstance *rtinstance = it->second;
34 
35  vector<Address::Family> families =
36  list_of(Address::INET)(Address::INET6);
37  BOOST_FOREACH(Address::Family family, families) {
38  const BgpTable *table =
39  static_cast<const BgpTable *>(rtinstance->GetTable(family));
40  if (!table)
41  continue;
42  if ((!regex_search(table->name(), search_expr)) &&
43  (search_string != "deleted" || !table->IsDeleted())) {
44  continue;
45  }
46 
47  IRouteAggregator *iroute_aggregator =
48  rtinstance->route_aggregator(family);
49  if (!iroute_aggregator)
50  continue;
51  AggregateRouteEntriesInfo info;
52  if (!iroute_aggregator->FillAggregateRouteInfo(&info, summary))
53  continue;
54  are_list->push_back(info);
55  }
56 
57  if (are_list->size() >= page_limit)
58  break;
59  if (iter_count >= iter_limit)
60  break;
61  }
62 
63  // All done if we've looked at all instances.
64  if (it == rim->name_cend() || ++it == rim->name_cend())
65  return true;
66 
67  // Return true if we've reached the page limit, false if we've reached the
68  // iteration limit.
69  bool done = are_list->size() >= page_limit;
70  *next_instance = it->second->name();
71  return done;
72 }
73 
74 //
75 // Specialization of BgpShowHandler<>::CallbackCommon for regular introspect.
76 //
77 template <>
78 bool BgpShowHandler<ShowRouteAggregateReq,
79  ShowRouteAggregateReqIterate, ShowRouteAggregateResp,
80  AggregateRouteEntriesInfo>::CallbackCommon(
81  const BgpSandeshContext *bsc, Data *data) {
82  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
83  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
84  string next_instance;
85  bool done = FillRouteAggregateInfoList(bsc, false, page_limit, iter_limit,
86  data->next_entry, data->search_string, &data->show_list,
87  &next_instance);
88  if (!next_instance.empty())
89  SaveContextToData(next_instance, done, data);
90  return done;
91 }
92 
93 //
94 // Specialization of BgpShowHandler<>::FillShowList for regular introspect.
95 //
96 template <>
97 void BgpShowHandler<ShowRouteAggregateReq,
98  ShowRouteAggregateReqIterate, ShowRouteAggregateResp,
99  AggregateRouteEntriesInfo>::FillShowList(
100  ShowRouteAggregateResp *resp,
101  const vector<AggregateRouteEntriesInfo> &show_list) {
102  resp->set_aggregate_route_entries(show_list);
103 }
104 
105 //
106 // Specialization of BgpShowHandler<>::CallbackCommon for summary introspect.
107 //
108 template <>
109 bool BgpShowHandler<ShowRouteAggregateSummaryReq,
110  ShowRouteAggregateSummaryReqIterate, ShowRouteAggregateSummaryResp,
111  AggregateRouteEntriesInfo>::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_instance;
116  bool done = FillRouteAggregateInfoList(bsc, true, page_limit, iter_limit,
117  data->next_entry, data->search_string, &data->show_list,
118  &next_instance);
119  if (!next_instance.empty())
120  SaveContextToData(next_instance, done, data);
121  return done;
122 }
123 
124 //
125 // Specialization of BgpShowHandler<>::FillShowList for summary introspect.
126 //
127 template <>
128 void BgpShowHandler<ShowRouteAggregateSummaryReq,
129  ShowRouteAggregateSummaryReqIterate, ShowRouteAggregateSummaryResp,
130  AggregateRouteEntriesInfo>::FillShowList(
131  ShowRouteAggregateSummaryResp *resp,
132  const vector<AggregateRouteEntriesInfo> &show_list) {
133  resp->set_aggregate_route_entries(show_list);
134 }
135 
136 //
137 // Handler for ShowRouteAggregateReq.
138 //
139 void ShowRouteAggregateReq::HandleRequest() const {
140  RequestPipeline::PipeSpec ps(this);
143 
144  s1.taskId_ = scheduler->GetTaskId("bgp::RouteAggregate");
145  s1.cbFn_ = boost::bind(&BgpShowHandler<
146  ShowRouteAggregateReq,
147  ShowRouteAggregateReqIterate,
148  ShowRouteAggregateResp,
149  AggregateRouteEntriesInfo>::Callback, _1, _2, _3, _4, _5);
151  ShowRouteAggregateReq,
152  ShowRouteAggregateReqIterate,
153  ShowRouteAggregateResp,
154  AggregateRouteEntriesInfo>::CreateData;
155  s1.instances_.push_back(0);
156  ps.stages_.push_back(s1);
157  RequestPipeline rp(ps);
158 }
159 
160 //
161 // Handler for ShowRouteAggregateReqIterate.
162 //
163 void ShowRouteAggregateReqIterate::HandleRequest() const {
164  RequestPipeline::PipeSpec ps(this);
167 
168  s1.taskId_ = scheduler->GetTaskId("bgp::RouteAggregate");
169  s1.cbFn_ = boost::bind(&BgpShowHandler<
170  ShowRouteAggregateReq,
171  ShowRouteAggregateReqIterate,
172  ShowRouteAggregateResp,
173  AggregateRouteEntriesInfo>::CallbackIterate, _1, _2, _3, _4, _5);
175  ShowRouteAggregateReq,
176  ShowRouteAggregateReqIterate,
177  ShowRouteAggregateResp,
178  AggregateRouteEntriesInfo>::CreateData;
179  s1.instances_.push_back(0);
180  ps.stages_.push_back(s1);
181  RequestPipeline rp(ps);
182 }
183 
184 //
185 // Handler for ShowRouteAggregateSummaryReq.
186 //
187 void ShowRouteAggregateSummaryReq::HandleRequest() const {
188  RequestPipeline::PipeSpec ps(this);
191 
192  s1.taskId_ = scheduler->GetTaskId("bgp::RouteAggregate");
193  s1.cbFn_ = boost::bind(&BgpShowHandler<
194  ShowRouteAggregateSummaryReq,
195  ShowRouteAggregateSummaryReqIterate,
196  ShowRouteAggregateSummaryResp,
197  AggregateRouteEntriesInfo>::Callback, _1, _2, _3, _4, _5);
199  ShowRouteAggregateSummaryReq,
200  ShowRouteAggregateSummaryReqIterate,
201  ShowRouteAggregateSummaryResp,
202  AggregateRouteEntriesInfo>::CreateData;
203  s1.instances_.push_back(0);
204  ps.stages_.push_back(s1);
205  RequestPipeline rp(ps);
206 }
207 
208 //
209 // Handler for ShowRouteAggregateSummaryReqIterate.
210 //
211 void ShowRouteAggregateSummaryReqIterate::HandleRequest() const {
212  RequestPipeline::PipeSpec ps(this);
215 
216  s1.taskId_ = scheduler->GetTaskId("bgp::RouteAggregate");
217  s1.cbFn_ = boost::bind(&BgpShowHandler<
218  ShowRouteAggregateSummaryReq,
219  ShowRouteAggregateSummaryReqIterate,
220  ShowRouteAggregateSummaryResp,
221  AggregateRouteEntriesInfo>::CallbackIterate, _1, _2, _3, _4, _5);
223  ShowRouteAggregateSummaryReq,
224  ShowRouteAggregateSummaryReqIterate,
225  ShowRouteAggregateSummaryResp,
226  AggregateRouteEntriesInfo>::CreateData;
227  s1.instances_.push_back(0);
228  ps.stages_.push_back(s1);
229  RequestPipeline rp(ps);
230 }
Family
Definition: address.h:25
@ INET
Definition: address.h:27
@ INET6
Definition: address.h:28
RoutingInstanceMgr * routing_instance_mgr()
Definition: bgp_server.h:107
bool IsDeleted() const
Definition: bgp_table.h:143
const std::string & name() const
Definition: db_table.h:111
virtual bool FillAggregateRouteInfo(AggregateRouteEntriesInfo *info, bool summary) const =0
const_name_iterator name_cend()
const_name_iterator name_clower_bound(const std::string &name)
RoutingInstanceList::const_iterator const_name_iterator
IRouteAggregator * route_aggregator(Address::Family family) const
BgpTable * GetTable(Address::Family fmly)
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
static bool FillRouteAggregateInfoList(const BgpSandeshContext *bsc, bool summary, uint32_t page_limit, uint32_t iter_limit, const string &start_instance, const string &search_string, vector< AggregateRouteEntriesInfo > *are_list, string *next_instance)
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_