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