-
Notifications
You must be signed in to change notification settings - Fork 390
/
show_routing_policy.cc
154 lines (140 loc) · 5.37 KB
/
show_routing_policy.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_show_handler.h"
#include <boost/foreach.hpp>
#include "bgp/bgp_server.h"
#include "bgp/bgp_show_handler.h"
#include "bgp/routing-policy/routing_policy.h"
#include "bgp/routing-policy/routing_policy_action.h"
#include "bgp/routing-policy/routing_policy_match.h"
#include "bgp/routing-policy/routing_policy_types.h"
using std::string;
using std::vector;
//
// Fill in information for a policy.
//
static void FillRoutingPolicyInfo(ShowRoutingPolicyInfo *srpi,
const BgpSandeshContext *bsc, const RoutingPolicy *policy,
bool summary) {
srpi->set_name(policy->name());
srpi->set_generation(policy->generation());
srpi->set_ref_count(policy->refcount());
srpi->set_deleted(policy->deleted());
vector<PolicyTermInfo> term_list;
BOOST_FOREACH(RoutingPolicy::PolicyTermPtr term, policy->terms()) {
PolicyTermInfo show_term;
show_term.set_terminal(term->terminal());
vector<string> match_list;
BOOST_FOREACH(RoutingPolicyMatch *match, term->matches()) {
match_list.push_back(match->ToString());
}
show_term.set_matches(match_list);
vector<string> action_list;
BOOST_FOREACH(RoutingPolicyAction *action, term->actions()) {
action_list.push_back(action->ToString());
}
show_term.set_actions(action_list);
term_list.push_back(show_term);
}
srpi->set_terms(term_list);
}
//
// Fill in information for list of policies.
//
static bool FillRoutingPolicyInfoList(const BgpSandeshContext *bsc,
bool summary, uint32_t page_limit, uint32_t iter_limit,
const string &start_policy, const string &search_string,
vector<ShowRoutingPolicyInfo> *srpi_list, string *next_policy) {
RoutingPolicyMgr *rpm = bsc->bgp_server->routing_policy_mgr();
RoutingPolicyMgr::const_name_iterator it =
rpm->name_clower_bound(start_policy);
for (uint32_t iter_count = 0; it != rpm->name_cend(); ++it, ++iter_count) {
const RoutingPolicy *policy = it->second;
if (!search_string.empty() &&
(policy->name().find(search_string) == string::npos) &&
(search_string != "deleted" || !policy->deleted())) {
continue;
}
ShowRoutingPolicyInfo srpi;
FillRoutingPolicyInfo(&srpi, bsc, policy, summary);
srpi_list->push_back(srpi);
if (srpi_list->size() >= page_limit)
break;
if (iter_count >= iter_limit)
break;
}
// All done if we've looked at all policies.
if (it == rpm->name_cend() || ++it == rpm->name_end())
return true;
// Return true if we've reached the page limit, false if we've reached the
// iteration limit.
bool done = srpi_list->size() >= page_limit;
*next_policy = it->second->name();
return done;
}
// Specialization of BgpShowHandler<>::CallbackCommon.
template <>
bool BgpShowHandler<ShowRoutingPolicyReq, ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp, ShowRoutingPolicyInfo>::CallbackCommon(
const BgpSandeshContext *bsc, Data *data) {
uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
string next_policy;
bool done = FillRoutingPolicyInfoList(bsc, false, page_limit, iter_limit,
data->next_entry, data->search_string, &data->show_list,
&next_policy);
if (!next_policy.empty())
SaveContextToData(next_policy, done, data);
return done;
}
// Specialization of BgpShowHandler<>::FillShowList.
template <>
void BgpShowHandler<ShowRoutingPolicyReq, ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp, ShowRoutingPolicyInfo>::FillShowList(
ShowRoutingPolicyResp *resp,
const vector<ShowRoutingPolicyInfo> &show_list) {
resp->set_routing_policies(show_list);
}
// Handler for ShowRoutingPolicyReq.
void ShowRoutingPolicyReq::HandleRequest() const {
RequestPipeline::PipeSpec ps(this);
RequestPipeline::StageSpec s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();
s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
s1.cbFn_ = boost::bind(&BgpShowHandler<
ShowRoutingPolicyReq,
ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp,
ShowRoutingPolicyInfo>::Callback, _1, _2, _3, _4, _5);
s1.allocFn_ = BgpShowHandler<
ShowRoutingPolicyReq,
ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp,
ShowRoutingPolicyInfo>::CreateData;
s1.instances_.push_back(0);
ps.stages_.push_back(s1);
RequestPipeline rp(ps);
}
//
// Handler for ShowRoutingPolicyReqIterate.
//
void ShowRoutingPolicyReqIterate::HandleRequest() const {
RequestPipeline::PipeSpec ps(this);
RequestPipeline::StageSpec s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();
s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
s1.cbFn_ = boost::bind(&BgpShowHandler<
ShowRoutingPolicyReq,
ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp,
ShowRoutingPolicyInfo>::CallbackIterate, _1, _2, _3, _4, _5);
s1.allocFn_ = BgpShowHandler<
ShowRoutingPolicyReq,
ShowRoutingPolicyReqIterate,
ShowRoutingPolicyResp,
ShowRoutingPolicyInfo>::CreateData;
s1.instances_.push_back(0);
ps.stages_.push_back(s1);
RequestPipeline rp(ps);
}