OpenSDN source code
ifmap_xmpp_client_show.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <sandesh/request_pipeline.h>
6 
7 #include <boost/bind/bind.hpp>
8 #include <boost/assign/list_of.hpp>
9 #include "base/logging.h"
10 
11 #include "ifmap/ifmap_client.h"
13 #include "ifmap/ifmap_server.h"
14 #include "ifmap/ifmap_xmpp.h"
15 #include "ifmap/ifmap_server_show_types.h" // sandesh
16 
17 using namespace boost::assign;
18 using namespace std;
19 using namespace boost::placeholders;
20 
22 public:
23  static const int kMaxElementsPerRound = 50;
24 
26  vector<IFMapXmppClientInfo> send_buffer;
27  };
28 
30  return static_cast<RequestPipeline::InstData *>(new ShowData);
31  }
32 
34  // init as 1 indicates we need to init 'first' to begin() since there is
35  // no way to initialize an iterator here.
36  TrackerData() : init(1) { }
37  int init;
38  vector<IFMapXmppClientInfo>::const_iterator first;
39  };
40 
42  return static_cast<RequestPipeline::InstData *>(new TrackerData);
43  }
44 
45  static bool BufferStage(const Sandesh *sr,
46  const RequestPipeline::PipeSpec ps, int stage,
47  int instNum, RequestPipeline::InstData *data);
48  static void CopyNode(IFMapXmppClientInfo *dest, IFMapClient *src);
49  static bool SendStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps,
50  int stage, int instNum,
52 };
53 
54 void ShowIFMapXmppClientInfo::CopyNode(IFMapXmppClientInfo *dest,
55  IFMapClient *src) {
56  dest->set_client_name(src->identifier());
57  dest->set_client_index(src->index());
58  dest->set_msgs_sent(src->msgs_sent());
59  dest->set_msgs_blocked(src->msgs_blocked());
60  dest->set_update_nodes_sent(src->update_nodes_sent());
61  dest->set_delete_nodes_sent(src->delete_nodes_sent());
62  dest->set_update_links_sent(src->update_links_sent());
63  dest->set_delete_links_sent(src->delete_links_sent());
64  dest->set_bytes_sent(src->bytes_sent());
65  dest->set_is_blocked(src->send_is_blocked());
66 
67  VmRegInfo vm_reg_info;
68  vm_reg_info.vm_list = src->vm_list();
69  vm_reg_info.vm_count = vm_reg_info.vm_list.size();
70  dest->set_vm_reg_info(vm_reg_info);
71 }
72 
75  int stage, int instNum,
77  const IFMapXmppClientInfoShowReq *request =
78  static_cast<const IFMapXmppClientInfoShowReq *>(ps.snhRequest_.get());
79  IFMapSandeshContext *sctx =
80  static_cast<IFMapSandeshContext *>(request->module_context("IFMap"));
81  IFMapServer* server = sctx->ifmap_server();
82 
83  IFMapServer::ClientMap client_map = server->GetClientMap();
84 
85  ShowData *show_data = static_cast<ShowData *>(data);
86  show_data->send_buffer.reserve(client_map.size());
87 
88  for (IFMapServer::ClientMap::iterator iter = client_map.begin();
89  iter != client_map.end(); ++iter) {
90  IFMapXmppClientInfo dest;
91  IFMapClient *src = iter->second;
92  CopyNode(&dest, src);
93  show_data->send_buffer.push_back(dest);
94  }
95 
96  return true;
97 }
98 
99 // Can be called multiple times i.e. approx total/kMaxElementsPerRound
101  const RequestPipeline::PipeSpec ps,
102  int stage, int instNum,
104  const RequestPipeline::StageData *prev_stage_data = ps.GetStageData(0);
105  const ShowIFMapXmppClientInfo::ShowData &show_data =
106  static_cast<const ShowIFMapXmppClientInfo::ShowData &>
107  (prev_stage_data->at(0));
108 
109  // Data for this stage
110  TrackerData *tracker_data = static_cast<TrackerData *>(data);
111 
112  vector<IFMapXmppClientInfo> dest_buffer;
113  vector<IFMapXmppClientInfo>::const_iterator first, last;
114  bool more = false;
115 
116  if (tracker_data->init) {
117  first = show_data.send_buffer.begin();
118  tracker_data->init = 0;
119  } else {
120  first = tracker_data->first;
121  }
122  int rem_num = show_data.send_buffer.end() - first;
123  int send_num = (rem_num < kMaxElementsPerRound) ? rem_num :
124  kMaxElementsPerRound;
125  last = first + send_num;
126  copy(first, last, back_inserter(dest_buffer));
127  // Decide if we want to be called again.
128  if ((rem_num - send_num) > 0) {
129  more = true;
130  } else {
131  more = false;
132  }
133  const IFMapXmppClientInfoShowReq *request =
134  static_cast<const IFMapXmppClientInfoShowReq *>(ps.snhRequest_.get());
135  IFMapXmppClientInfoShowResp *response = new IFMapXmppClientInfoShowResp();
136  response->set_client_stats(dest_buffer);
137  response->set_context(request->context());
138  response->set_more(more);
139  response->Response();
140  tracker_data->first = first + send_num;
141 
142  // Return 'false' to be called again
143  return (!more);
144 }
145 
146 void IFMapXmppClientInfoShowReq::HandleRequest() const {
147 
150 
151  // 2 stages - first: gather/read, second: send
152 
153  s0.taskId_ = scheduler->GetTaskId("db::IFMapTable");
156  s0.instances_.push_back(0);
157 
158  // control-node ifmap show command task
159  s1.taskId_ = scheduler->GetTaskId("ifmap::ShowCommandSendStage");
162  s1.instances_.push_back(0);
163 
164  RequestPipeline::PipeSpec ps(this);
165  ps.stages_= list_of(s0)(s1)
166  .convert_to_container<vector<RequestPipeline::StageSpec> >();
167  RequestPipeline rp(ps);
168 }
uint64_t delete_nodes_sent() const
Definition: ifmap_client.h:36
uint64_t update_nodes_sent() const
Definition: ifmap_client.h:35
uint64_t update_links_sent() const
Definition: ifmap_client.h:37
virtual const std::string & identifier() const =0
uint64_t delete_links_sent() const
Definition: ifmap_client.h:38
std::vector< std::string > vm_list() const
Definition: ifmap_client.cc:27
int index() const
Definition: ifmap_client.h:31
uint64_t msgs_blocked() const
Definition: ifmap_client.h:33
uint64_t msgs_sent() const
Definition: ifmap_client.h:32
bool send_is_blocked() const
Definition: ifmap_client.h:39
uint64_t bytes_sent() const
Definition: ifmap_client.h:34
IFMapServer * ifmap_server()
const ClientMap & GetClientMap() const
Definition: ifmap_server.h:150
std::map< std::string, IFMapClient * > ClientMap
Definition: ifmap_server.h:58
boost::ptr_vector< InstData > StageData
static void CopyNode(IFMapXmppClientInfo *dest, IFMapClient *src)
static RequestPipeline::InstData * AllocTracker(int stage)
static RequestPipeline::InstData * AllocBuffer(int stage)
static bool SendStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *data)
static bool BufferStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *data)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:304
int GetTaskId(const std::string &name)
Definition: task.cc:861
static TaskScheduler * GetInstance()
Definition: task.cc:554
void init()
Definition: bgp_log.cc:37
boost::shared_ptr< const SandeshRequest > snhRequest_
const StageData * GetStageData(int stage) const
std::vector< int > instances_
vector< IFMapXmppClientInfo > send_buffer
vector< IFMapXmppClientInfo >::const_iterator first