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