Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : //
7 : // sandesh.h
8 : //
9 : // Sandesh means message in Hindi. Sandeshs are used to store
10 : // structured information from various software modules of the
11 : // Virtual Network System (VNS) into the analytics database.
12 : // Sandeshs are also used to exchange information for the purpose of
13 : // debugging the VNS.
14 : // The different types of Sandeshs are:
15 : // 1. Async - Used to send event-triggered messages to the analytics
16 : // database
17 : // 2. Request and Response - Used for debugging and displaying
18 : // information from various software modules of the VNS like
19 : // vnswa and bgpd.
20 : // 3. Trace - Used for triggering internal trace messages
21 : //
22 : // Sandesh module consists of 2 parts:
23 : // 1. The first part is a code generator tool that reads in .sandesh files
24 : // and generates appropriate C++ code for encoding the Sandeshs using
25 : // XML.
26 : // 2. The second part is a library that software modules like vnswa and
27 : // bgpd will link with and use to:
28 : // a. Send Sandeshs to the analytics database
29 : // b. Provide operational command implementation
30 : // c. Enable/disable tracing
31 : //
32 : // For example, vnswa developer wants to generate a structured error message to
33 : // report some error condition. The developer will add an async sandesh to the
34 : // file VNSwitch.sandesh as below:
35 : //
36 : // struct VNSRoute {
37 : // 1: i32 prefix;
38 : // }
39 : //
40 : // async sandesh VNSwitchError {
41 : // 1: "VNSwitchId = ";
42 : // 2: i32 vnSwitchId;
43 : // 3: "VNetworkId = ";
44 : // 4: i32 vnId;
45 : // 5: list<VNSRoute> vnRoutes;
46 : // 6: VNSRoute vnMarkerRoute;
47 : // }
48 : //
49 : // To use Sandesh, the following needs to be added to the module SConscript:
50 : ///
51 : // # Generate the source files
52 : // SandeshGenFiles = env.SandeshGenCpp('VNS.sandesh')
53 : // SandeshGenFiles += env.SandeshGenCpp('VNSwitch.sandesh')
54 : //
55 : // # The above returns VNS_types.h, VNS_types.cpp, VNS_constants.h
56 : // # VNS_constants.cpp, VNSwitch_types.h, VNSwitch_types.cpp,
57 : // # VNSwitch_constants.h, VNSwitch_constants.cpp
58 : //
59 : // # To include the header files above from your module's sources
60 : // env.Append(CPPPATH = env['TOP'])
61 : //
62 : // # Extract the .cpp files to be used as sources
63 : // SandeshGenSrcs = env.ExtractCpp(SandeshGenFiles)
64 : //
65 : // Add SandeshGenSrcs to the module source files
66 : //
67 : // Add libsandesh, and libbase to the module libraries.
68 : //
69 : // The code to send the Sandesh to the analytics database will be:
70 : //
71 : // VNSwitchError::Send(int32_t vnSwitchId, int32_t vnId, std::vector<VNSRoute> vnRoutes,
72 : // VNSRoute vnMarkerRoute)
73 : //
74 : // The developer will have to call Sandesh::InitGenerator from the module's
75 : // initialization before calling the above function.
76 : //
77 :
78 : #ifndef __SANDESH_H__
79 : #define __SANDESH_H__
80 :
81 : #include <time.h>
82 :
83 : #include <atomic>
84 : #include <map>
85 : #include <mutex>
86 :
87 : #include <boost/asio/ip/tcp.hpp>
88 : #include <boost/ptr_container/ptr_map.hpp>
89 : #include <boost/enable_shared_from_this.hpp>
90 : #include <boost/tuple/tuple.hpp>
91 : #include <base/contrail_ports.h>
92 : #include <base/logging.h>
93 : #include <base/queue_task.h>
94 : #include <base/string_util.h>
95 : #include <base/time_util.h>
96 : #include <sandesh/sandesh_util.h>
97 : #include <sandesh/sandesh_types.h>
98 : #include <sandesh/protocol/TProtocol.h>
99 : #include <sandesh/transport/TBufferTransports.h>
100 : #include <sandesh/sandesh_trace.h>
101 : #include <sandesh/sandesh_options.h>
102 :
103 : // Forward declaration
104 : class EventManager;
105 : class SandeshClient;
106 : class SandeshSession;
107 :
108 : // Sandesh Context
109 : class SandeshContext {
110 : // Abstract base class for users of sandesh library to
111 : // pass in information to used when handling sandesh
112 : // request and response
113 : protected:
114 : SandeshContext() { }
115 : public:
116 : virtual ~SandeshContext() { }
117 : };
118 :
119 : //
120 : // Sandesh
121 : //
122 : class SandeshMessageStatistics;
123 : class SandeshMessageTypeStats;
124 : class SandeshMessageStats;
125 : class SandeshMessageTypeBasicStats;
126 : class SandeshMessageBasicStats;
127 : class SandeshConnection;
128 : class SandeshRequest;
129 :
130 :
131 : struct SandeshElement;
132 :
133 : class Sandesh {
134 : public:
135 : typedef WorkQueue<SandeshRequest *> SandeshRxQueue;
136 : typedef WorkQueue<SandeshElement> SandeshQueue;
137 : typedef WorkQueue<
138 : boost::shared_ptr<contrail::sandesh::transport::TMemoryBuffer> >
139 : SandeshBufferQueue;
140 : typedef boost::asio::ip::tcp::endpoint Endpoint;
141 : typedef boost::function<void (Sandesh *)> SandeshCallback;
142 : struct SandeshRole {
143 : enum type {
144 : Invalid,
145 : Generator,
146 : Collector,
147 : Test,
148 : };
149 : };
150 : typedef boost::tuple<size_t, SandeshLevel::type, bool, bool> QueueWaterMarkInfo;
151 : typedef std::map<std::string, std::map<std::string,std::string> > DerivedStats;
152 :
153 : // Initialization APIs
154 : static bool InitGenerator(const std::string &module,
155 : const std::string &source,
156 : const std::string &node_type,
157 : const std::string &instance_id,
158 : EventManager *evm,
159 : unsigned short http_port,
160 : const std::vector<std::string> &collectors,
161 : SandeshContext *client_context = NULL,
162 : DerivedStats ds = DerivedStats(),
163 : const SandeshConfig &config = SandeshConfig());
164 : static bool InitGenerator(const std::string &module,
165 : const std::string &source,
166 : const std::string &node_type,
167 : const std::string &instance_id,
168 : EventManager *evm,
169 : unsigned short http_port,
170 : SandeshContext *client_context = NULL,
171 : DerivedStats ds = DerivedStats(),
172 : const SandeshConfig &config = SandeshConfig());
173 : static void RecordPort(const std::string& name, const std::string& module,
174 : unsigned short port);
175 : // Collector
176 : static bool InitCollector(const std::string &module,
177 : const std::string &source,
178 : const std::string &node_type,
179 : const std::string &instance_id,
180 : EventManager *evm,
181 : const std::string &collector_ip, int collector_port,
182 : unsigned short http_port,
183 : SandeshContext *client_context = NULL,
184 : const SandeshConfig &config = SandeshConfig());
185 : // Test
186 : static bool InitGeneratorTest(const std::string &module,
187 : const std::string &source,
188 : const std::string &node_type,
189 : const std::string &instance_id,
190 : EventManager *evm,
191 : unsigned short http_port,
192 : SandeshContext *client_context = NULL,
193 : const SandeshConfig &config = SandeshConfig());
194 : static bool ConnectToCollector(const std::string &collector_ip,
195 : int collector_port, bool periodicuve = false);
196 : static void ReConfigCollectors(const std::vector<std::string>& collector_list);
197 : static void Uninit();
198 : static void SetDscpValue(uint8_t value);
199 :
200 : // Disable flow collection
201 : static void DisableFlowCollection(bool disable);
202 : static bool IsFlowCollectionDisabled() { return disable_flow_collection_; }
203 :
204 : // Flags to control sending of sandesh from generators
205 : static void DisableSendingAllMessages(bool disable);
206 : static bool IsSendingAllMessagesDisabled();
207 : static void DisableSendingObjectLogs(bool disable);
208 : static bool IsSendingObjectLogsDisabled();
209 : static bool IsSendingSystemLogsDisabled();
210 : static void DisableSendingFlows(bool disable);
211 : static bool IsSendingFlowsDisabled();
212 : static void set_send_rate_limit(int rate_limit);
213 : static uint32_t get_send_rate_limit();
214 :
215 : // Logging and category APIs
216 : static void SetLoggingParams(bool enable_local_log, std::string category,
217 : std::string level, bool enable_trace_print = false,
218 : bool enable_flow_log = false, bool enable_session_syslog = false);
219 : static void SetLoggingParams(bool enable_local_log, std::string category,
220 : SandeshLevel::type level, bool enable_trace_print = false,
221 : bool enable_flow_log = false);
222 : static void SetLoggingLevel(std::string level);
223 : static void SetLoggingLevel(SandeshLevel::type level);
224 : static SandeshLevel::type LoggingLevel() { return logging_level_; }
225 : static SandeshLevel::type LoggingUtLevel() { return logging_ut_level_; }
226 : static bool IsLocalLoggingEnabled() { return enable_local_log_; }
227 : static void SetLocalLogging(bool enable);
228 : static bool IsFlowLoggingEnabled() { return enable_flow_log_; }
229 : static bool IsSloSyslogEnabled() { return enable_session_syslog_; }
230 : static void SetFlowLogging(bool enable);
231 : static void SetSessionSyslogging(bool enable_session_syslog);
232 : static bool IsTracePrintEnabled() { return enable_trace_print_; }
233 : static void SetTracePrint(bool enable);
234 : static void SetLoggingCategory(std::string category);
235 : static std::string LoggingCategory() { return logging_category_; }
236 :
237 : //GetSize method to report the size
238 : virtual size_t GetSize() const = 0;
239 :
240 : // Send queue processing
241 : static void SetSendQueue(bool enable);
242 : static inline bool IsSendQueueEnabled() {
243 : return send_queue_enabled_;
244 : }
245 : static inline bool IsConnectToCollectorEnabled() {
246 : return connect_to_collector_;
247 : }
248 : static SandeshLevel::type SendingLevel();
249 :
250 : static int32_t ReceiveBinaryMsgOne(u_int8_t *buf, u_int32_t buf_len,
251 : int *error, SandeshContext *client_context);
252 : static int32_t ReceiveBinaryMsg(u_int8_t *buf, u_int32_t buf_len,
253 : int *error, SandeshContext *client_context);
254 : static bool SendReady(SandeshConnection * sconn = NULL);
255 : static void UpdateRxMsgStats(const std::string &msg_name, uint64_t bytes);
256 : static void UpdateRxMsgFailStats(const std::string &msg_name,
257 : uint64_t bytes, SandeshRxDropReason::type dreason);
258 : static void UpdateTxMsgStats(const std::string &msg_name, uint64_t bytes);
259 : static void UpdateTxMsgFailStats(const std::string &msg_name,
260 : uint64_t bytes, SandeshTxDropReason::type dreason);
261 : static void GetMsgStats(
262 : std::vector<SandeshMessageTypeStats> *mtype_stats,
263 : SandeshMessageStats *magg_stats);
264 : static void GetMsgStats(
265 : boost::ptr_map<std::string, SandeshMessageTypeStats> *mtype_stats,
266 : SandeshMessageStats *magg_stats);
267 : static const char * SandeshRoleToString(SandeshRole::type role);
268 :
269 : virtual void Release() { delete this; }
270 : virtual void Log() const = 0;
271 : virtual void ForcedLog() const = 0;
272 : virtual std::string ToString() const = 0;
273 : virtual std::string ModuleName() const = 0;
274 : virtual int32_t Read(
275 : boost::shared_ptr<contrail::sandesh::protocol::TProtocol> iprot) = 0;
276 : virtual int32_t Write(
277 : boost::shared_ptr<contrail::sandesh::protocol::TProtocol> oprot) const = 0;
278 : virtual const uint32_t seqnum() { return seqnum_; }
279 : virtual const int32_t versionsig() const = 0;
280 : virtual const char *Name() const { return name_.c_str(); }
281 : bool Enqueue(SandeshQueue* queue);
282 : virtual int32_t WriteBinary(u_int8_t *buf, u_int32_t buf_len, int *error);
283 : virtual int32_t ReadBinary(u_int8_t *buf, u_int32_t buf_len, int *error);
284 : virtual int32_t WriteBinaryToFile(const std::string& path, int *error);
285 : virtual int32_t ReadBinaryFromFile(const std::string& path, int *error);
286 :
287 : bool IsLoggingAllowed() const;
288 : static bool IsLoggingDroppedAllowed(SandeshType::type);
289 :
290 : // Accessors
291 : static void set_source(std::string &source) { source_ = source; }
292 : static std::string source() { return source_; }
293 : static void set_module(std::string &module) { module_ = module; }
294 : static std::string module() { return module_; }
295 : static void set_instance_id(std::string &instance_id) { instance_id_ = instance_id; }
296 : static std::string instance_id() { return instance_id_; }
297 : static void set_node_type(std::string &node_type) { node_type_ = node_type; }
298 : static std::string node_type() { return node_type_; }
299 : static SandeshRole::type role() { return role_; }
300 : static int http_port() { return http_port_; }
301 : static SandeshRxQueue* recv_queue() { return recv_queue_.get(); }
302 : static SandeshContext* client_context() { return client_context_; }
303 : static void set_client_context(SandeshContext *context) { client_context_ = context; }
304 : static SandeshContext *module_context(const std::string &module_name);
305 : static void set_module_context(const std::string &module_name,
306 : SandeshContext *context);
307 : static void set_response_callback(SandeshCallback response_cb) { response_callback_ = response_cb; }
308 : static SandeshCallback response_callback() { return response_callback_; }
309 : static SandeshClient* client() { return client_; }
310 : static SandeshConfig& config() { return config_; }
311 :
312 : time_t timestamp() const { return timestamp_; }
313 : void set_context(std::string context) { context_ = context; }
314 : std::string context() const { return context_; }
315 : void set_scope(std::string scope) { scope_ = scope; }
316 : std::string scope() const { return scope_; }
317 : SandeshType::type type() const { return type_; }
318 : void set_hints(int32_t hints) { hints_ = hints; }
319 : int32_t hints() const { return hints_; }
320 : void set_level(SandeshLevel::type level) { level_ = level; }
321 : SandeshLevel::type level() const { return level_; }
322 : void set_category(std::string category) { category_ = category; }
323 : std::string category() const { return category_; }
324 : static const char* LevelToString(SandeshLevel::type level);
325 : static SandeshLevel::type StringToLevel(std::string level);
326 : static log4cplus::Logger& logger() { return logger_; }
327 : static log4cplus::Logger& slo_logger() { return slo_logger_; }
328 : static log4cplus::Logger& sampled_logger() { return sampled_logger_; }
329 : static void set_logger_appender(const std::string &file_name,
330 : long max_file_size,
331 : int max_backup_index,
332 : const std::string &syslog_facility,
333 : const std::vector<std::string> &destn,
334 : const std::string &ident,
335 : bool is_sampled_logger);
336 : static void set_send_to_collector_flags(
337 : const std::vector<std::string> &sampled_destination,
338 : const std::vector<std::string> &slo_destination);
339 : static bool is_send_slo_to_collector_enabled() { return slo_to_collector_; }
340 : static bool is_send_sampled_to_collector_enabled() { return sampled_to_collector_; }
341 : static bool is_send_slo_to_logger_enabled() { return slo_to_logger_; }
342 : static bool is_send_sampled_to_logger_enabled() { return sampled_to_logger_; }
343 :
344 : protected:
345 : void set_timestamp(time_t timestamp) { timestamp_ = timestamp; }
346 : void set_type(SandeshType::type type) { type_ = type; }
347 : void set_name(const char *name) { name_ = name; }
348 :
349 : Sandesh(SandeshType::type type, const std::string& name, uint32_t seqno) :
350 : seqnum_(seqno),
351 : context_(""),
352 : timestamp_(UTCTimestampUsec()),
353 : scope_(""),
354 : type_(type),
355 : hints_(0),
356 : level_(SandeshLevel::INVALID),
357 : category_(""),
358 : name_(name) {
359 : }
360 : Sandesh(SandeshType::type type, const std::string& name, uint32_t seqno, bool no_time_stamp) :
361 : seqnum_(seqno),
362 : context_(""),
363 : scope_(""),
364 : type_(type),
365 : hints_(0),
366 : level_(SandeshLevel::INVALID),
367 : category_(""),
368 : name_(name) {
369 : timestamp_ = no_time_stamp ? 0 : UTCTimestampUsec();
370 : }
371 : virtual ~Sandesh() {}
372 : virtual bool Dispatch(SandeshConnection * sconn = NULL);
373 : virtual bool SendEnqueue();
374 :
375 : static bool HandleTest(SandeshLevel::type level, const
376 : std::string& category);
377 :
378 : static bool IsUnitTest() {
379 : return role_ == SandeshRole::Invalid || role_ == SandeshRole::Test;
380 : }
381 : static SandeshCallback response_callback_;
382 : static SandeshClient *client_;
383 : static bool IsLevelUT(SandeshLevel::type level);
384 : static bool IsLevelCategoryLoggingAllowed(SandeshType::type type,
385 : SandeshLevel::type level,
386 : const std::string& category);
387 :
388 : private:
389 : friend class SandeshTracePerfTest;
390 :
391 : typedef std::map<std::string, SandeshContext *> ModuleContextMap;
392 :
393 : static void InitReceive(int recv_task_inst = -1);
394 : static void InitClient(EventManager *evm, Endpoint server,
395 : const SandeshConfig &config, bool periodicuve);
396 : static bool InitClient(EventManager *evm,
397 : const std::vector<std::string> &collectors,
398 : const SandeshConfig &config);
399 : static bool ProcessRecv(SandeshRequest *);
400 : static bool Initialize(SandeshRole::type role, const std::string &module,
401 : const std::string &source,
402 : const std::string &node_type,
403 : const std::string &instance_id,
404 : EventManager *evm,
405 : unsigned short http_port,
406 : SandeshContext *client_context = NULL,
407 : const SandeshConfig &config = SandeshConfig());
408 :
409 : static SandeshRole::type role_;
410 : static std::string module_;
411 : static std::string source_;
412 : static std::string node_type_;
413 : static std::string instance_id_;
414 : static int http_port_;
415 : static std::unique_ptr<SandeshRxQueue> recv_queue_;
416 : static int recv_task_id_;
417 : static SandeshContext *client_context_;
418 : static ModuleContextMap module_context_;
419 : static bool enable_local_log_; // whether to just enable local logging
420 : static bool enable_flow_log_; // whether to enable flow sandesh message logging
421 : static bool enable_session_syslog_; // whether to enable syslog for SLO session messages
422 : static SandeshLevel::type logging_level_; // current logging level
423 : static SandeshLevel::type logging_ut_level_; // ut_debug logging level
424 : static std::string logging_category_; // current logging category
425 : static bool enable_trace_print_; // whether to print traces locally
426 : static bool connect_to_collector_; // whether to connect to collector
427 : static EventManager *event_manager_;
428 : static bool send_queue_enabled_;
429 : static SandeshMessageStatistics msg_stats_;
430 : static std::mutex stats_mutex_;
431 : static log4cplus::Logger logger_;
432 : static log4cplus::Logger slo_logger_;
433 : static log4cplus::Logger sampled_logger_;
434 : static bool disable_flow_collection_; // disable flow collection
435 : static SandeshConfig config_;
436 : static bool disable_sending_all_;
437 : static bool disable_sending_object_logs_;
438 : static bool disable_sending_flows_;
439 :
440 : const uint32_t seqnum_;
441 : std::string context_;
442 : time_t timestamp_;
443 : std::string scope_;
444 : SandeshType::type type_;
445 : int32_t hints_;
446 : SandeshLevel::type level_;
447 : std::string category_;
448 : std::string name_;
449 : static std::atomic<uint32_t> sandesh_send_ratelimit_;
450 : static bool slo_to_collector_;
451 : static bool sampled_to_collector_;
452 : static bool slo_to_logger_;
453 : static bool sampled_to_logger_;
454 : };
455 :
456 : struct SandeshElement {
457 : Sandesh *snh_;
458 : //Explicit constructor creating only if Sandesh is passed as arg
459 : explicit SandeshElement(Sandesh *snh):snh_(snh),size_(snh->GetSize()) {
460 : }
461 : SandeshElement():size_(0) { }
462 : size_t GetSize() const {
463 : return size_;
464 : }
465 : private:
466 : size_t size_;
467 : };
468 :
469 : template<>
470 : size_t Sandesh::SandeshQueue::AtomicIncrementQueueCount(
471 : SandeshElement *element);
472 :
473 : template<>
474 : size_t Sandesh::SandeshQueue::AtomicDecrementQueueCount(
475 : SandeshElement *element);
476 :
477 : #define SANDESH_LOG(_Level, _Msg) \
478 : do { \
479 : if (LoggingDisabled()) break; \
480 : LOG4CPLUS_##_Level(Sandesh::logger(), _Msg); \
481 : } while (0)
482 :
483 : class SandeshRequest : public Sandesh,
484 : public boost::enable_shared_from_this<SandeshRequest> {
485 : public:
486 : virtual void HandleRequest() const = 0;
487 : virtual bool RequestFromHttp(const std::string& ctx,
488 : const std::string& snh_query) = 0;
489 : void Release();
490 1928 : boost::shared_ptr<const SandeshRequest> SharedPtr() const { return shared_from_this(); }
491 : bool Enqueue(SandeshRxQueue* queue);
492 : protected:
493 : SandeshRequest(const std::string& name, uint32_t seqno) :
494 : Sandesh(SandeshType::REQUEST, name, seqno), self_(this) {}
495 :
496 : template <typename T>
497 : friend void boost::checked_delete(T *x);
498 : boost::shared_ptr<SandeshRequest> self_;
499 : };
500 :
501 : class SandeshResponse : public Sandesh {
502 : public:
503 : virtual const bool get_more() const = 0;
504 : virtual void set_more(const bool val) = 0;
505 : virtual void Response() {}
506 : protected:
507 : SandeshResponse(const std::string& name, uint32_t seqno) :
508 : Sandesh(SandeshType::RESPONSE, name, seqno) {}
509 : bool Dispatch(SandeshConnection * sconn = NULL);
510 : };
511 :
512 : class SandeshBuffer : public Sandesh {
513 : public:
514 : virtual void Process(SandeshContext *context) = 0;
515 : SandeshBuffer& operator=(const SandeshBuffer& r) {
516 : set_context(r.context());
517 : set_timestamp(r.timestamp());
518 : set_scope(r.scope());
519 : set_hints(r.hints());
520 : set_type(r.type());
521 : set_level(r.level());
522 : set_category(r.category());
523 : set_name(r.Name());
524 : return(*this);
525 : }
526 : protected:
527 : SandeshBuffer(const std::string& name, uint32_t seqno) :
528 : Sandesh(SandeshType::BUFFER, name, seqno, true) {}
529 : };
530 :
531 : class SandeshTrace : public Sandesh {
532 : public:
533 : const uint32_t seqnum() { return xseqnum_; }
534 : virtual void SendTrace(const std::string& context, bool more) = 0;
535 : const bool get_more() const { return more_; }
536 : virtual ~SandeshTrace() {}
537 : protected:
538 : SandeshTrace(const std::string& name, uint32_t seqno) :
539 : Sandesh(SandeshType::TRACE, name, 0), xseqnum_(0), more_(true) {}
540 : bool Dispatch(SandeshConnection * sconn = NULL);
541 : void set_seqnum(const uint32_t seqnum) { xseqnum_= seqnum; }
542 : void set_more(bool more) { more_ = more; }
543 : private:
544 : uint32_t xseqnum_;
545 : bool more_;
546 : };
547 :
548 : class SandeshSystem : public Sandesh {
549 : protected:
550 : SandeshSystem(const std::string& name, uint32_t seqno) :
551 : Sandesh(SandeshType::SYSTEM, name, seqno) {}
552 : };
553 :
554 : class SandeshObject : public Sandesh {
555 : protected:
556 : SandeshObject(const std::string& name, uint32_t seqno) :
557 : Sandesh(SandeshType::OBJECT, name, seqno) {}
558 : };
559 :
560 : class SandeshFlow : public Sandesh {
561 : protected:
562 : SandeshFlow(const std::string& name, uint32_t seqno) :
563 : Sandesh(SandeshType::FLOW, name, seqno) {}
564 : };
565 :
566 : class SandeshFlowSession : public Sandesh {
567 : protected:
568 : SandeshFlowSession(const std::string& name, uint32_t seqno) :
569 : Sandesh(SandeshType::SESSION, name, seqno) {}
570 : };
571 :
572 : class SandeshUVE : public Sandesh {
573 : public:
574 : const bool get_more() const { return more_; }
575 : typedef enum {
576 : ST_SYNC = 0,
577 : ST_INTROSPECT = 1,
578 : ST_PERIODIC = 2,
579 : ST_MAX = 3
580 : } SendType;
581 : virtual std::string DataLog(void) { return std::string(); }
582 : protected:
583 : SandeshUVE(const std::string& name, uint32_t seqno,
584 : SandeshType::type t = SandeshType::UVE) :
585 : Sandesh(t, name, seqno), more_(false) {}
586 : bool Dispatch(SandeshConnection * sconn = NULL);
587 : void set_more(const bool val) { more_=val; }
588 :
589 : private:
590 : bool more_;
591 : };
592 :
593 : class SandeshAlarm : public SandeshUVE {
594 : protected:
595 : SandeshAlarm(const std::string& name, uint32_t seqno) :
596 : SandeshUVE(name, seqno, SandeshType::ALARM) {}
597 : };
598 :
599 : template<>
600 : struct WorkQueueDelete<SandeshElement> {
601 : template <typename QueueT>
602 : void operator()(QueueT &q, bool delete_entry) {
603 : SandeshElement element;
604 : while (q.try_pop(element)) {
605 : Sandesh *sandesh(element.snh_);
606 : sandesh->Release();
607 : }
608 : }
609 : };
610 :
611 : template<>
612 : struct WorkQueueDelete<SandeshRequest *> {
613 : template <typename QueueT>
614 : void operator()(QueueT &q, bool delete_entry) {
615 : SandeshRequest *rsandesh;
616 : while (q.try_pop(rsandesh)) {
617 : rsandesh->Release();
618 : }
619 : }
620 : };
621 :
622 : template<typename T> Sandesh* createT() { return new T; }
623 :
624 : class SandeshBaseFactory {
625 : public:
626 : static Sandesh* CreateInstance(std::string const& s) {
627 : map_type::iterator it = GetMap()->find(s);
628 : if (it == GetMap()->end()) {
629 : return 0;
630 : }
631 : return it->second();
632 : }
633 :
634 : typedef std::map<std::string, Sandesh*(*)()> map_type;
635 :
636 : static void Update(map_type &map) {
637 : map_type::const_iterator m_iter = map.begin();
638 : for (; m_iter != map.end(); m_iter++) {
639 : map_type::iterator b_iter = GetMap()->find((*m_iter).first);
640 : if (b_iter != End()) {
641 : GetMap()->erase(b_iter);
642 : }
643 : GetMap()->insert(*m_iter);
644 : }
645 : }
646 : static map_type::const_iterator Begin() { return GetMap()->begin(); }
647 : static map_type::const_iterator End() { return GetMap()->end(); }
648 :
649 : protected:
650 : static map_type* GetMap() {
651 : static map_type map_;
652 : return &map_;
653 : }
654 : };
655 :
656 : template<typename T>
657 : struct SandeshDerivedRegister : public SandeshBaseFactory {
658 : SandeshDerivedRegister(std::string const& s) :
659 : name_(s) {
660 : GetMap()->insert(std::make_pair(s, &createT<T>));
661 : }
662 : ~SandeshDerivedRegister() {
663 : map_type::iterator iter = GetMap()->find(name_);
664 : GetMap()->erase(iter);
665 : }
666 : private:
667 : std::string name_;
668 : };
669 :
670 : #define SANDESH_REGISTER_DEC_TYPE(NAME) \
671 : static SandeshDerivedRegister<NAME> reg
672 :
673 : #define SANDESH_REGISTER_DEF_TYPE(NAME) \
674 : SandeshDerivedRegister<NAME> NAME::reg(#NAME)
675 :
676 : bool DoDropSandeshMessage(const SandeshHeader &header,
677 : SandeshLevel::type drop_level);
678 :
679 : log4cplus::LogLevel SandeshLevelTolog4Level(
680 : SandeshLevel::type slevel);
681 :
682 : template <typename T>
683 : struct SandeshStructDeleteTrait {
684 : static bool get(const T& s) { return false; }
685 : };
686 : template <typename T>
687 : struct SandeshStructProxyTrait {
688 : static std::string get(const T& s) { return std::string(""); }
689 : };
690 : #endif // __SANDESH_H__
|