OpenSDN source code
show_static_route.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "base/regex.h"
7 #include "bgp/bgp_config.h"
8 #include "bgp/bgp_server.h"
9 #include "bgp/bgp_show_handler.h"
10 #include "bgp/bgp_table.h"
13 #include "bgp/routing-instance/static_route_types.h"
14 
15 using contrail::regex;
18 using std::string;
19 using std::vector;
20 
22  const string &search_string,
23  const regex &search_expr,
24  StaticRouteEntriesInfo &info,
25  RoutingInstance *rtinstance) {
26  const BgpTable *table =
27  static_cast<const BgpTable *>(rtinstance->GetTable(family));
28  if (!table)
29  return false;
30  if ((!regex_search(table->name(), search_expr)) &&
31  (search_string != "deleted" || !table->IsDeleted())) {
32  return false;
33  }
34 
35  IStaticRouteMgr *imanager = rtinstance->static_route_mgr(family);
36  if (!imanager)
37  return false;
38  return imanager->FillStaticRouteInfo(rtinstance, &info);
39 }
40 
41 // Specialization of BgpShowHandler<>::CallbackCommon.
42 template <>
43 bool BgpShowHandler<ShowStaticRouteReq, ShowStaticRouteReqIterate,
44  ShowStaticRouteResp, StaticRouteEntriesInfo>::CallbackCommon(
45  const BgpSandeshContext *bsc, Data *data) {
46  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
47  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
49 
50  regex search_expr(data->search_string);
52  rim->name_clower_bound(data->next_entry);
53  for (uint32_t iter_count = 0; it != rim->name_cend(); ++it, ++iter_count) {
54  RoutingInstance *rinstance = it->second;
55  StaticRouteEntriesInfo info;
57  search_expr, info, rinstance)) {
58  data->show_list.push_back(info);
59  }
61  search_expr, info, rinstance)) {
62  data->show_list.push_back(info);
63  }
64 
65  if (data->show_list.size() >= page_limit)
66  break;
67  if (iter_count >= iter_limit)
68  break;
69  }
70 
71  // All done if we've looked at all instances.
72  if (it == rim->name_cend() || ++it == rim->name_cend())
73  return true;
74 
75  // Return true if we've reached the page limit, false if we've reached the
76  // iteration limit.
77  bool done = data->show_list.size() >= page_limit;
78  SaveContextToData(it->second->name(), done, data);
79  return done;
80 }
81 
82 // Specialization of BgpShowHandler<>::FillShowList.
83 template <>
84 void BgpShowHandler<ShowStaticRouteReq, ShowStaticRouteReqIterate,
85  ShowStaticRouteResp, StaticRouteEntriesInfo>::FillShowList(
86  ShowStaticRouteResp *resp,
87  const vector<StaticRouteEntriesInfo> &show_list) {
88  resp->set_static_route_entries(show_list);
89 }
90 
91 // Handler for ShowStaticRouteReq.
92 void ShowStaticRouteReq::HandleRequest() const {
96 
97  s1.taskId_ = scheduler->GetTaskId("bgp::StaticRoute");
98  s1.cbFn_ = boost::bind(&BgpShowHandler<
99  ShowStaticRouteReq,
100  ShowStaticRouteReqIterate,
101  ShowStaticRouteResp,
102  StaticRouteEntriesInfo>::Callback, _1, _2, _3, _4, _5);
104  ShowStaticRouteReq,
105  ShowStaticRouteReqIterate,
106  ShowStaticRouteResp,
107  StaticRouteEntriesInfo>::CreateData;
108  s1.instances_.push_back(0);
109  ps.stages_.push_back(s1);
110  RequestPipeline rp(ps);
111 }
112 
113 //
114 // Handler for ShowStaticRouteReqIterate.
115 //
116 void ShowStaticRouteReqIterate::HandleRequest() const {
117  RequestPipeline::PipeSpec ps(this);
120 
121  s1.taskId_ = scheduler->GetTaskId("bgp::StaticRoute");
122  s1.cbFn_ = boost::bind(&BgpShowHandler<
123  ShowStaticRouteReq,
124  ShowStaticRouteReqIterate,
125  ShowStaticRouteResp,
126  StaticRouteEntriesInfo>::CallbackIterate, _1, _2, _3, _4, _5);
128  ShowStaticRouteReq,
129  ShowStaticRouteReqIterate,
130  ShowStaticRouteResp,
131  StaticRouteEntriesInfo>::CreateData;
132  s1.instances_.push_back(0);
133  ps.stages_.push_back(s1);
134  RequestPipeline rp(ps);
135 }
Family
Definition: address.h:25
@ INET
Definition: address.h:27
@ INET6
Definition: address.h:28
RoutingInstanceMgr * routing_instance_mgr()
Definition: bgp_server.h:107
bool IsDeleted() const
Definition: bgp_table.h:143
const std::string & name() const
Definition: db_table.h:111
virtual bool FillStaticRouteInfo(RoutingInstance *rtinstance, StaticRouteEntriesInfo *info) const =0
const_name_iterator name_cend()
const_name_iterator name_clower_bound(const std::string &name)
RoutingInstanceList::const_iterator const_name_iterator
IStaticRouteMgr * static_route_mgr(Address::Family family)
BgpTable * GetTable(Address::Family fmly)
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 FillStaticRouteInfo(Address::Family family, const string &search_string, const regex &search_expr, StaticRouteEntriesInfo &info, RoutingInstance *rtinstance)
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_