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