OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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.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 
21 public:
22  static const int kMaxElementsPerRound = 50;
23 
25  vector<IFMapXmppClientInfo> send_buffer;
26  };
27 
29  return static_cast<RequestPipeline::InstData *>(new ShowData);
30  }
31 
33  // init as 1 indicates we need to init 'first' to begin() since there is
34  // no way to initialize an iterator here.
35  TrackerData() : init(1) { }
36  int init;
37  vector<IFMapXmppClientInfo>::const_iterator first;
38  };
39 
41  return static_cast<RequestPipeline::InstData *>(new TrackerData);
42  }
43 
44  static bool BufferStage(const Sandesh *sr,
45  const RequestPipeline::PipeSpec ps, int stage,
46  int instNum, RequestPipeline::InstData *data);
47  static void CopyNode(IFMapXmppClientInfo *dest, IFMapClient *src);
48  static bool SendStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps,
49  int stage, int instNum,
51 };
52 
53 void ShowIFMapXmppClientInfo::CopyNode(IFMapXmppClientInfo *dest,
54  IFMapClient *src) {
55  dest->set_client_name(src->identifier());
56  dest->set_client_index(src->index());
57  dest->set_msgs_sent(src->msgs_sent());
58  dest->set_msgs_blocked(src->msgs_blocked());
59  dest->set_update_nodes_sent(src->update_nodes_sent());
60  dest->set_delete_nodes_sent(src->delete_nodes_sent());
61  dest->set_update_links_sent(src->update_links_sent());
62  dest->set_delete_links_sent(src->delete_links_sent());
63  dest->set_bytes_sent(src->bytes_sent());
64  dest->set_is_blocked(src->send_is_blocked());
65 
66  VmRegInfo vm_reg_info;
67  vm_reg_info.vm_list = src->vm_list();
68  vm_reg_info.vm_count = vm_reg_info.vm_list.size();
69  dest->set_vm_reg_info(vm_reg_info);
70 }
71 
74  int stage, int instNum,
76  const IFMapXmppClientInfoShowReq *request =
77  static_cast<const IFMapXmppClientInfoShowReq *>(ps.snhRequest_.get());
78  IFMapSandeshContext *sctx =
79  static_cast<IFMapSandeshContext *>(request->module_context("IFMap"));
80  IFMapServer* server = sctx->ifmap_server();
81 
82  IFMapServer::ClientMap client_map = server->GetClientMap();
83 
84  ShowData *show_data = static_cast<ShowData *>(data);
85  show_data->send_buffer.reserve(client_map.size());
86 
87  for (IFMapServer::ClientMap::iterator iter = client_map.begin();
88  iter != client_map.end(); ++iter) {
89  IFMapXmppClientInfo dest;
90  IFMapClient *src = iter->second;
91  CopyNode(&dest, src);
92  show_data->send_buffer.push_back(dest);
93  }
94 
95  return true;
96 }
97 
98 // Can be called multiple times i.e. approx total/kMaxElementsPerRound
100  const RequestPipeline::PipeSpec ps,
101  int stage, int instNum,
103  const RequestPipeline::StageData *prev_stage_data = ps.GetStageData(0);
104  const ShowIFMapXmppClientInfo::ShowData &show_data =
105  static_cast<const ShowIFMapXmppClientInfo::ShowData &>
106  (prev_stage_data->at(0));
107 
108  // Data for this stage
109  TrackerData *tracker_data = static_cast<TrackerData *>(data);
110 
111  vector<IFMapXmppClientInfo> dest_buffer;
112  vector<IFMapXmppClientInfo>::const_iterator first, last;
113  bool more = false;
114 
115  if (tracker_data->init) {
116  first = show_data.send_buffer.begin();
117  tracker_data->init = 0;
118  } else {
119  first = tracker_data->first;
120  }
121  int rem_num = show_data.send_buffer.end() - first;
122  int send_num = (rem_num < kMaxElementsPerRound) ? rem_num :
123  kMaxElementsPerRound;
124  last = first + send_num;
125  copy(first, last, back_inserter(dest_buffer));
126  // Decide if we want to be called again.
127  if ((rem_num - send_num) > 0) {
128  more = true;
129  } else {
130  more = false;
131  }
132  const IFMapXmppClientInfoShowReq *request =
133  static_cast<const IFMapXmppClientInfoShowReq *>(ps.snhRequest_.get());
134  IFMapXmppClientInfoShowResp *response = new IFMapXmppClientInfoShowResp();
135  response->set_client_stats(dest_buffer);
136  response->set_context(request->context());
137  response->set_more(more);
138  response->Response();
139  tracker_data->first = first + send_num;
140 
141  // Return 'false' to be called again
142  return (!more);
143 }
144 
145 void IFMapXmppClientInfoShowReq::HandleRequest() const {
146 
149 
150  // 2 stages - first: gather/read, second: send
151 
152  s0.taskId_ = scheduler->GetTaskId("db::IFMapTable");
155  s0.instances_.push_back(0);
156 
157  // control-node ifmap show command task
158  s1.taskId_ = scheduler->GetTaskId("ifmap::ShowCommandSendStage");
161  s1.instances_.push_back(0);
162 
163  RequestPipeline::PipeSpec ps(this);
164  ps.stages_= list_of(s0)(s1)
165  .convert_to_container<vector<RequestPipeline::StageSpec> >();
166  RequestPipeline rp(ps);
167 }
static RequestPipeline::InstData * AllocTracker(int stage)
static RequestPipeline::InstData * AllocBuffer(int stage)
std::vector< int > instances_
static bool BufferStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *data)
virtual const std::string & identifier() const =0
void init()
Definition: bgp_log.cc:37
IFMapServer * ifmap_server()
std::vector< std::string > vm_list() const
Definition: ifmap_client.cc:27
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:178
uint64_t delete_nodes_sent() const
Definition: ifmap_client.h:36
boost::ptr_vector< InstData > StageData
int index() const
Definition: ifmap_client.h:31
uint64_t update_nodes_sent() const
Definition: ifmap_client.h:35
uint64_t bytes_sent() const
Definition: ifmap_client.h:34
uint64_t msgs_sent() const
Definition: ifmap_client.h:32
vector< IFMapXmppClientInfo >::const_iterator first
int GetTaskId(const std::string &name)
Definition: task.cc:856
uint64_t msgs_blocked() const
Definition: ifmap_client.h:33
static TaskScheduler * GetInstance()
Definition: task.cc:547
vector< IFMapXmppClientInfo > send_buffer
uint64_t update_links_sent() const
Definition: ifmap_client.h:37
static bool SendStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps, int stage, int instNum, RequestPipeline::InstData *data)
const ClientMap & GetClientMap() const
Definition: ifmap_server.h:150
uint64_t delete_links_sent() const
Definition: ifmap_client.h:38
boost::shared_ptr< const SandeshRequest > snhRequest_
bool send_is_blocked() const
Definition: ifmap_client.h:39
static void CopyNode(IFMapXmppClientInfo *dest, IFMapClient *src)
const StageData * GetStageData(int stage) const
std::map< std::string, IFMapClient * > ClientMap
Definition: ifmap_server.h:58