OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sandesh_trace.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 //
6 // sandesh_trace.cc
7 //
8 // Sandesh Trace Request Implementation
9 //
10 
11 #include <boost/bind.hpp>
12 #include <ctime>
13 #include <base/trace.h>
14 #include <sandesh/sandesh_types.h>
15 #include <sandesh/sandesh.h>
16 #include <sandesh/sandesh_trace_types.h>
17 #include "sandesh_trace.h"
18 
19 using std::string;
20 using std::vector;
21 
23 
25 
27 public:
29  const std::string& req_context,
30  const std::string& read_context,
31  uint32_t count) :
32  req_buf_(trace_buf),
33  req_context_(req_context),
34  read_context_(read_context),
35  sttr_(new SandeshTraceTextResponse),
36  req_count_(count),
37  read_count_(0) {
38  if (!req_count_) {
39  // Fill the read_count_ with the size of the trace buffer
41  }
42  }
43 
45 
46  void Run() {
49  this, _1, _2));
50  if ("Collector" != read_context_) {
51  sttr_->set_context(req_context_);
52  sttr_->Response();
54  }
55  }
56 
57 private:
58  void SandeshTraceRead(SandeshTrace *tsnh, bool more) {
59  read_count_++;
60  assert(tsnh);
61  vector<string>& tracebuf = const_cast<vector<string>&>(sttr_->get_traces());
62 
63  if ("Collector" != read_context_) {
64  tracebuf.push_back(tsnh->ToString());
65  return;
66  }
67 
68  if (!more || (read_count_ == req_count_)) {
69  tsnh->SendTrace(req_context_, false);
70  } else {
71  tsnh->SendTrace(req_context_, true);
72  }
73  }
74 
76  std::string req_context_;
77  std::string read_context_;
78  SandeshTraceTextResponse *sttr_;
79  uint32_t req_count_;
80  uint32_t read_count_;
81 };
82 
83 void SandeshTraceRequest::HandleRequest() const {
84  std::string read_context;
85 
86  // TODO:
87  // We should get the Sandesh Header to differentiate
88  // trace requests from non-http modules.
89  if ((0 == context().find("http%")) || (0 == context().find("https%"))) {
90  read_context = "Http";
91  } else {
92  read_context = "Collector";
93  }
94 
95  SandeshTraceBufferPtr trace_buf(SandeshTraceBufferGet(get_buf_name()));
96  if (!trace_buf) {
97  if (read_context != "Collector") {
98  SandeshTraceTextResponse *sttr = new SandeshTraceTextResponse;
99  sttr->set_context(context());
100  sttr->Response();
101  }
102  return;
103  }
104  std::unique_ptr<SandeshTraceRequestRunner> trace_req_runner(new
105  SandeshTraceRequestRunner(trace_buf, context(),
106  read_context, get_count()));
107  trace_req_runner->Run();
108 }
109 
110 void SandeshTraceSend(const std::string& buf_name, uint32_t trace_count) {
111  // TODO:
112  // Handle this in a separate task, so that the generator can resume its execution.
113  // Need to ensure that we don't run parallel tasks that send trace messages
114  // [in the same trace buffer] to the collector. It is possible that when the
115  // generator invoked this api, there may be another task running that handles
116  // the trace request (for the same trace buffer) from the Collector.
117  SandeshTraceBufferPtr trace_buf(SandeshTraceBufferGet(buf_name));
118  if (!trace_buf) {
119  return;
120  }
121  std::unique_ptr<SandeshTraceRequestRunner> trace_req_runner(new
122  SandeshTraceRequestRunner(trace_buf, "", "Collector", trace_count));
123  trace_req_runner->Run();
124 }
125 
126 void SandeshTraceBufferListRequest::HandleRequest() const {
127  std::vector<std::string> trace_buf_list;
128  SandeshTraceBufferListGet(trace_buf_list);
129  std::vector<SandeshTraceBufInfo> trace_buf_info_list;
130  for (std::vector<std::string>::const_iterator it = trace_buf_list.begin();
131  it != trace_buf_list.end(); ++it) {
132  SandeshTraceBufInfo trace_buf_info;
133  trace_buf_info.set_trace_buf_name(*it);
134  trace_buf_info_list.push_back(trace_buf_info);
135  }
136  SandeshTraceBufferListResponse *resp = new SandeshTraceBufferListResponse;
137  resp->set_trace_buffer_list(trace_buf_info_list);
138  resp->set_context(context());
139  resp->set_more(false);
140  resp->Response();
141 }
142 
143 void SandeshTraceEnableDisableReq::HandleRequest() const {
144  std::string status;
145  if (__isset.enable) {
146  if (get_enable()) {
148  status = "Sandesh Trace Enabled";
149  } else {
151  status = "Sandesh Trace Disabled";
152  }
153  } else {
154  // If the user has not specified whether to enable or disable
155  // trace, then toggle the trace status.
156  if (IsSandeshTraceEnabled()) {
158  status = "Sandesh Trace Disabled";
159  } else {
161  status = "Sandesh Trace Enabled";
162  }
163  }
164  SandeshTraceEnableDisableRes *resp = new
165  SandeshTraceEnableDisableRes;
166  resp->set_enable_disable_status(status);
167  resp->set_context(context());
168  resp->set_more(false);
169  resp->Response();
170 }
171 
172 void SandeshTraceBufStatusReq::HandleRequest() const {
173  std::vector<std::string> trace_buf_list;
174  SandeshTraceBufferListGet(trace_buf_list);
175  std::vector<SandeshTraceBufStatusInfo> trace_buf_status_list;
176  for (std::vector<std::string>::const_iterator it = trace_buf_list.begin();
177  it != trace_buf_list.end(); ++it) {
178  SandeshTraceBufStatusInfo trace_buf_status;
179  trace_buf_status.set_trace_buf_name(*it);
181  assert(tbp);
182  if (IsSandeshTraceBufferEnabled(tbp)) {
183  trace_buf_status.set_enable_disable("Enabled");
184  } else {
185  trace_buf_status.set_enable_disable("Disabled");
186  }
187  trace_buf_status_list.push_back(trace_buf_status);
188  }
189  SandeshTraceBufStatusRes *resp = new SandeshTraceBufStatusRes;
190  resp->set_trace_buf_status_list(trace_buf_status_list);
191  resp->set_context(context());
192  resp->set_more(false);
193  resp->Response();
194 }
195 
196 void SandeshTraceBufferEnableDisableReq::HandleRequest() const {
197  SandeshTraceBufferEnableDisableRes *resp = new
198  SandeshTraceBufferEnableDisableRes;
199  std::string status;
200  SandeshTraceBufferPtr trace_buf(
201  SandeshTraceBufferGet(get_trace_buf_name()));
202  if (trace_buf) {
203  if (__isset.enable) {
204  if (get_enable()) {
205  SandeshTraceBufferEnable(trace_buf);
206  status = "Trace buffer Enabled";
207  } else {
208  SandeshTraceBufferDisable(trace_buf);
209  status = "Trace buffer Disabled";
210  }
211  } else {
212  // If the user has not specified whether to enable or disable
213  // trace, then toggle the trace status.
214  if (IsSandeshTraceBufferEnabled(trace_buf)) {
215  SandeshTraceBufferDisable(trace_buf);
216  status = "Trace buffer Disabled";
217  } else {
218  SandeshTraceBufferEnable(trace_buf);
219  status = "Trace buffer Enabled";
220  }
221  }
222  } else {
223  status = "Invalid Trace buffer";
224  }
225  resp->set_enable_disable_status(status);
226  resp->set_context(context());
227  resp->set_more(false);
228  resp->Response();
229 }
void SandeshTraceBufferReadDone(SandeshTraceBufferPtr trace_buf, const std::string &read_context)
Definition: sandesh_trace.h:80
virtual void SendTrace(const std::string &context, bool more)=0
void SandeshTraceBufferListGet(std::vector< std::string > &trace_buf_list)
Definition: sandesh_trace.h:85
int PullSandeshTraceReq
bool IsSandeshTraceEnabled()
Definition: sandesh_trace.h:31
SandeshTraceBufferPtr req_buf_
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:18
void SandeshTraceBufferRead(SandeshTraceBufferPtr trace_buf, const std::string &read_context, const int count, boost::function< void(SandeshTrace *, bool)> cb)
Definition: sandesh_trace.h:74
bool IsSandeshTraceBufferEnabled(SandeshTraceBufferPtr trace_buf)
Definition: sandesh_trace.h:66
void SandeshTraceDisable()
Definition: sandesh_trace.h:27
Definition: trace.h:220
size_t SandeshTraceBufferSizeGet(SandeshTraceBufferPtr trace_buf)
Definition: sandesh_trace.h:70
SandeshTraceTextResponse * sttr_
void SandeshTraceRead(SandeshTrace *tsnh, bool more)
void SandeshTraceSend(const std::string &buf_name, uint32_t trace_count)
virtual std::string ToString() const =0
void SandeshTraceBufferDisable(SandeshTraceBufferPtr trace_buf)
Definition: sandesh_trace.h:62
SandeshTraceRequestRunner(SandeshTraceBufferPtr trace_buf, const std::string &req_context, const std::string &read_context, uint32_t count)
void SandeshTraceEnable()
Definition: sandesh_trace.h:23
void SandeshTraceBufferEnable(SandeshTraceBufferPtr trace_buf)
Definition: sandesh_trace.h:58
static Trace * trace_
Definition: trace.h:318
SandeshTraceBufferPtr SandeshTraceBufferGet(const std::string &buf_name)
Definition: sandesh_trace.h:54