OpenSDN source code
bgp_show_ifmap_peering_config.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 "bgp/bgp_show_handler.h"
7 
8 #include <boost/foreach.hpp>
9 
10 #include "base/regex.h"
11 #include "bgp/bgp_config_ifmap.h"
12 #include "bgp/bgp_peer_types.h"
13 #include "bgp/bgp_peer_internal_types.h"
14 #include "bgp/bgp_server.h"
15 #include "schema/bgp_schema_types.h"
16 
17 using contrail::regex;
20 using std::string;
21 using std::vector;
22 
23 //
24 // Fill in information for a peering.
25 //
26 static void FillBgpPeeringConfigInfo(ShowBgpPeeringConfig *sbpc,
27  const BgpSandeshContext *bsc, const BgpIfmapPeeringConfig *peering) {
28  sbpc->set_instance_name(peering->instance()->name());
29  sbpc->set_name(peering->name());
30  sbpc->set_neighbor_count(peering->size());
31  if (peering->bgp_peering()) {
32  vector<ShowBgpSessionConfig> sbsc_list;
33  for (vector<autogen::BgpSession>::const_iterator sit =
34  peering->bgp_peering()->data().begin();
35  sit != peering->bgp_peering()->data().end(); ++sit) {
36  ShowBgpSessionConfig sbsc;
37  sbsc.set_uuid(sit->uuid);
38  vector<ShowBgpSessionAttributesConfig> sbsac_list;
39  for (vector<autogen::BgpSessionAttributes>::const_iterator ait =
40  sit->attributes.begin(); ait != sit->attributes.end(); ++ait) {
41  ShowBgpSessionAttributesConfig sbsac;
42  sbsac.set_bgp_router(ait->bgp_router);
43  sbsac.set_address_families(ait->address_families.family);
44  sbsac_list.push_back(sbsac);
45  }
46  sbsc.set_attributes(sbsac_list);
47  sbsc_list.push_back(sbsc);
48  }
49  sbpc->set_sessions(sbsc_list);
50  }
51 }
52 
53 //
54 // Specialization of BgpShowHandler<>::CallbackCommon.
55 //
56 template <>
57 bool BgpShowHandler<ShowBgpPeeringConfigReq, ShowBgpPeeringConfigReqIterate,
58  ShowBgpPeeringConfigResp, ShowBgpPeeringConfig>::CallbackCommon(
59  const BgpSandeshContext *bsc, Data *data) {
60  uint32_t page_limit = bsc->page_limit() ? bsc->page_limit() : kPageLimit;
61  uint32_t iter_limit = bsc->iter_limit() ? bsc->iter_limit() : kIterLimit;
62  const BgpIfmapConfigManager *bcm = dynamic_cast<BgpIfmapConfigManager *>(
63  bsc->bgp_server->config_manager());
64  if (!bcm)
65  return true;
66 
67  regex search_expr(data->search_string);
69  bcm->InstanceMapItems(data->next_entry);
70  BgpConfigManager::InstanceMap::const_iterator it = range.first;
71  BgpConfigManager::InstanceMap::const_iterator it_end = range.second;
72  for (uint32_t iter_count = 0; it != it_end; ++it, ++iter_count) {
73  const BgpIfmapInstanceConfig *instance =
74  bcm->config()->FindInstance(it->first);
75  if (!instance)
76  continue;
77  BOOST_FOREACH(BgpIfmapInstanceConfig::PeeringMap::value_type value,
78  instance->peerings()) {
79  const BgpIfmapPeeringConfig *peering = value.second;
80  if (!regex_search(peering->name(), search_expr))
81  continue;
82  ShowBgpPeeringConfig sbpc;
83  FillBgpPeeringConfigInfo(&sbpc, bsc, peering);
84  data->show_list.push_back(sbpc);
85  }
86  if (data->show_list.size() >= page_limit)
87  break;
88  if (iter_count >= iter_limit)
89  break;
90  }
91 
92  // All done if we've looked at all instances.
93  if (it == it_end || ++it == it_end)
94  return true;
95 
96  // Return true if we've reached the page limit, false if we've reached the
97  // iteration limit.
98  bool done = data->show_list.size() >= page_limit;
99  SaveContextToData(it->second->name(), done, data);
100  return done;
101 }
102 
103 //
104 // Specialization of BgpShowHandler<>::FillShowList.
105 //
106 template <>
107 void BgpShowHandler<ShowBgpPeeringConfigReq, ShowBgpPeeringConfigReqIterate,
108  ShowBgpPeeringConfigResp, ShowBgpPeeringConfig>::FillShowList(
109  ShowBgpPeeringConfigResp *resp,
110  const vector<ShowBgpPeeringConfig> &show_list) {
111  resp->set_peerings(show_list);
112 }
113 
114 //
115 // Handler for ShowBgpPeeringConfigReq.
116 // Called via BgpSandeshContext::PeeringShowReqHandler.
117 //
119  const ShowBgpPeeringConfigReq *req) {
123 
124  s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
125  s1.cbFn_ = boost::bind(&BgpShowHandler<
126  ShowBgpPeeringConfigReq,
127  ShowBgpPeeringConfigReqIterate,
128  ShowBgpPeeringConfigResp,
129  ShowBgpPeeringConfig>::Callback, _1, _2, _3, _4, _5);
131  ShowBgpPeeringConfigReq,
132  ShowBgpPeeringConfigReqIterate,
133  ShowBgpPeeringConfigResp,
134  ShowBgpPeeringConfig>::CreateData;
135  s1.instances_.push_back(0);
136  ps.stages_.push_back(s1);
137  RequestPipeline rp(ps);
138 }
139 
140 //
141 // Handler for ShowBgpPeeringConfigReqIterate.
142 // Called via BgpSandeshContext::PeeringShowReqIterateHandler.
143 //
145  const ShowBgpPeeringConfigReqIterate *req_iterate) {
146  RequestPipeline::PipeSpec ps(req_iterate);
149 
150  s1.taskId_ = scheduler->GetTaskId("bgp::ShowCommand");
151  s1.cbFn_ = boost::bind(&BgpShowHandler<
152  ShowBgpPeeringConfigReq,
153  ShowBgpPeeringConfigReqIterate,
154  ShowBgpPeeringConfigResp,
155  ShowBgpPeeringConfig>::CallbackIterate, _1, _2, _3, _4, _5);
157  ShowBgpPeeringConfigReq,
158  ShowBgpPeeringConfigReqIterate,
159  ShowBgpPeeringConfigResp,
160  ShowBgpPeeringConfig>::CreateData;
161  s1.instances_.push_back(0);
162  ps.stages_.push_back(s1);
163  RequestPipeline rp(ps);
164 }
void ShowBgpIfmapPeeringConfigReqHandler(const BgpSandeshContext *bsc, const ShowBgpPeeringConfigReq *req)
static void FillBgpPeeringConfigInfo(ShowBgpPeeringConfig *sbpc, const BgpSandeshContext *bsc, const BgpIfmapPeeringConfig *peering)
void ShowBgpIfmapPeeringConfigReqIterateHandler(const BgpSandeshContext *bsc, const ShowBgpPeeringConfigReqIterate *req_iterate)
std::pair< InstanceMap::const_iterator, InstanceMap::const_iterator > InstanceMapRange
Definition: bgp_config.h:773
BgpIfmapInstanceConfig * FindInstance(const std::string &name)
virtual InstanceMapRange InstanceMapItems(const std::string &start_name=std::string()) const
const BgpIfmapConfigData * config() const
const PeeringMap & peerings() const
const std::string & name() const
const autogen::BgpPeering * bgp_peering() const
std::string name() const
BgpIfmapInstanceConfig * instance()
BgpConfigManager * config_manager()
Definition: bgp_server.h:105
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
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< StageSpec > stages_
std::vector< int > instances_