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