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