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