OpenSDN source code
show_path_resolver.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 "bgp/bgp_show_handler.h"
7 
8 #include "base/regex.h"
9 #include "bgp/bgp_peer_internal_types.h"
10 #include "bgp/bgp_server.h"
11 #include "bgp/bgp_table.h"
14 
15 using contrail::regex;
18 using std::string;
19 using std::vector;
20 
21 //
22 // Fill in information for list of path resolvers.
23 //
24 // Allows regular and summary introspect to share code.
25 //
27  bool summary, uint32_t page_limit, uint32_t iter_limit,
28  const string &start_instance, const string &search_string,
29  vector<ShowPathResolver> *spr_list, string *next_instance) {
30  regex search_expr(search_string);
33  rim->name_clower_bound(start_instance);
34  for (uint32_t iter_count = 0; it1 != rim->name_cend();
35  ++it1, ++iter_count) {
36  const RoutingInstance *rtinstance = it1->second;
37  for (RoutingInstance::RouteTableList::const_iterator it2 =
38  rtinstance->GetTables().begin();
39  it2 != rtinstance->GetTables().end(); ++it2, ++iter_count) {
40  const BgpTable *table = it2->second;
41  if ((!regex_search(table->name(), search_expr)) &&
42  (search_string != "deleted" || !table->IsDeleted())) {
43  continue;
44  }
45  const PathResolver *path_resolver = table->path_resolver();
46  if (!path_resolver)
47  continue;
48  ShowPathResolver spr;
49  path_resolver->FillShowInfo(&spr, summary);
50  if (spr.get_path_count() == 0 &&
51  spr.get_modified_path_count() == 0 &&
52  spr.get_nexthop_count() == 0 &&
53  spr.get_modified_nexthop_count() == 0)
54  continue;
55  spr_list->push_back(spr);
56  }
57  if (spr_list->size() >= page_limit)
58  break;
59  if (iter_count >= iter_limit)
60  break;
61  }
62 
63  // All done if we've looked at all instances.
64  if (it1 == rim->name_cend() || ++it1 == rim->name_end())
65  return true;
66 
67  // Return true if we've reached the page limit, false if we've reached the
68  // iteration limit.
69  bool done = spr_list->size() >= page_limit;
70  *next_instance = it1->second->name();
71  return done;
72 }
73 
74 //
75 // Specialization of BgpShowHandler<>::CallbackCommon for regular introspect.
76 //
77 template <>
78 bool BgpShowHandler<ShowPathResolverReq, ShowPathResolverReqIterate,
79  ShowPathResolverResp, ShowPathResolver>::CallbackCommon(
80  const BgpSandeshContext *bsc, Data *data) {
81  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
82  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
83  string next_instance;
84  bool done = FillPathResolverInfoList(bsc, false, page_limit, iter_limit,
85  data->next_entry, data->search_string, &data->show_list,
86  &next_instance);
87  if (!next_instance.empty())
88  SaveContextToData(next_instance, done, data);
89  return done;
90 }
91 
92 //
93 // Specialization of BgpShowHandler<>::FillShowList for regular introspect.
94 //
95 template <>
96 void BgpShowHandler<ShowPathResolverReq,
97  ShowPathResolverReqIterate, ShowPathResolverResp,
98  ShowPathResolver>::FillShowList(ShowPathResolverResp *resp,
99  const vector<ShowPathResolver> &show_list) {
100  resp->set_resolvers(show_list);
101 }
102 
103 //
104 // Specialization of BgpShowHandler<>::CallbackCommon for summary introspect.
105 //
106 template <>
107 bool BgpShowHandler<ShowPathResolverSummaryReq,
108  ShowPathResolverSummaryReqIterate, ShowPathResolverSummaryResp,
109  ShowPathResolver>::CallbackCommon(
110  const BgpSandeshContext *bsc, Data *data) {
111  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
112  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
113  string next_instance;
114  bool done = FillPathResolverInfoList(bsc, true, page_limit, iter_limit,
115  data->next_entry, data->search_string, &data->show_list,
116  &next_instance);
117  if (!next_instance.empty())
118  SaveContextToData(next_instance, done, data);
119  return done;
120 }
121 
122 //
123 // Specialization of BgpShowHandler<>::FillShowList for summary introspect.
124 //
125 template <>
126 void BgpShowHandler<ShowPathResolverSummaryReq,
127  ShowPathResolverSummaryReqIterate, ShowPathResolverSummaryResp,
128  ShowPathResolver>::FillShowList(ShowPathResolverSummaryResp *resp,
129  const vector<ShowPathResolver> &show_list) {
130  resp->set_resolvers(show_list);
131 }
132 
133 //
134 // Handler for ShowPathResolverReq.
135 //
136 void ShowPathResolverReq::HandleRequest() const {
137  RequestPipeline::PipeSpec ps(this);
140 
141  s1.taskId_ = scheduler->GetTaskId("bgp::ResolverNexthop");
142  s1.cbFn_ = boost::bind(&BgpShowHandler<
143  ShowPathResolverReq,
144  ShowPathResolverReqIterate,
145  ShowPathResolverResp,
146  ShowPathResolver>::Callback, _1, _2, _3, _4, _5);
148  ShowPathResolverReq,
149  ShowPathResolverReqIterate,
150  ShowPathResolverResp,
151  ShowPathResolver>::CreateData;
152  s1.instances_.push_back(0);
153  ps.stages_.push_back(s1);
154  RequestPipeline rp(ps);
155 }
156 
157 //
158 // Handler for ShowPathResolverReqIterate.
159 //
160 void ShowPathResolverReqIterate::HandleRequest() const {
161  RequestPipeline::PipeSpec ps(this);
164 
165  s1.taskId_ = scheduler->GetTaskId("bgp::ResolverNexthop");
166  s1.cbFn_ = boost::bind(&BgpShowHandler<
167  ShowPathResolverReq,
168  ShowPathResolverReqIterate,
169  ShowPathResolverResp,
170  ShowPathResolver>::CallbackIterate, _1, _2, _3, _4, _5);
172  ShowPathResolverReq,
173  ShowPathResolverReqIterate,
174  ShowPathResolverResp,
175  ShowPathResolver>::CreateData;
176  s1.instances_.push_back(0);
177  ps.stages_.push_back(s1);
178  RequestPipeline rp(ps);
179 }
180 
181 //
182 // Handler for ShowPathResolverSummaryReq.
183 //
184 void ShowPathResolverSummaryReq::HandleRequest() const {
185  RequestPipeline::PipeSpec ps(this);
188 
189  s1.taskId_ = scheduler->GetTaskId("bgp::ResolverNexthop");
190  s1.cbFn_ = boost::bind(&BgpShowHandler<
191  ShowPathResolverSummaryReq,
192  ShowPathResolverSummaryReqIterate,
193  ShowPathResolverSummaryResp,
194  ShowPathResolver>::Callback, _1, _2, _3, _4, _5);
196  ShowPathResolverSummaryReq,
197  ShowPathResolverSummaryReqIterate,
198  ShowPathResolverSummaryResp,
199  ShowPathResolver>::CreateData;
200  s1.instances_.push_back(0);
201  ps.stages_.push_back(s1);
202  RequestPipeline rp(ps);
203 }
204 
205 //
206 // Handler for ShowPathResolverSummaryReqIterate.
207 //
208 void ShowPathResolverSummaryReqIterate::HandleRequest() const {
209  RequestPipeline::PipeSpec ps(this);
212 
213  s1.taskId_ = scheduler->GetTaskId("bgp::ResolverNexthop");
214  s1.cbFn_ = boost::bind(&BgpShowHandler<
215  ShowPathResolverSummaryReq,
216  ShowPathResolverSummaryReqIterate,
217  ShowPathResolverSummaryResp,
218  ShowPathResolver>::CallbackIterate, _1, _2, _3, _4, _5);
220  ShowPathResolverSummaryReq,
221  ShowPathResolverSummaryReqIterate,
222  ShowPathResolverSummaryResp,
223  ShowPathResolver>::CreateData;
224  s1.instances_.push_back(0);
225  ps.stages_.push_back(s1);
226  RequestPipeline rp(ps);
227 }
RoutingInstanceMgr * routing_instance_mgr()
Definition: bgp_server.h:107
PathResolver * path_resolver()
Definition: bgp_table.h:153
bool IsDeleted() const
Definition: bgp_table.h:143
const std::string & name() const
Definition: db_table.h:111
void FillShowInfo(ShowPathResolver *spr, bool summary) const
const_name_iterator name_cend()
name_iterator name_end()
const_name_iterator name_clower_bound(const std::string &name)
RoutingInstanceList::const_iterator const_name_iterator
RouteTableList & GetTables()
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 regex_search(const std::string &input, const regex &regex)
Definition: regex.h:26
static bool regex_match(const std::string &input, const regex &regex)
Definition: regex.h:35
static bool FillPathResolverInfoList(const BgpSandeshContext *bsc, bool summary, uint32_t page_limit, uint32_t iter_limit, const string &start_instance, const string &search_string, vector< ShowPathResolver > *spr_list, string *next_instance)
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_