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 : #ifndef _GENERATOR_H_ 7 : #define _GENERATOR_H_ 8 : 9 : #include <atomic> 10 : #include <mutex> 11 : #include <boost/shared_ptr.hpp> 12 : #include <string> 13 : #include <boost/tuple/tuple.hpp> 14 : #include "viz_message.h" 15 : #include "base/queue_task.h" 16 : 17 : #include <sandesh/sandesh_state_machine.h> 18 : #include "collector_uve_types.h" 19 : #include "syslog_collector.h" 20 : 21 : class DbHandler; 22 : class Sandesh; 23 : class VizSession; 24 : class Collector; 25 : class SandeshStateMachineStats; 26 : 27 : int GetDeferTimeMSec(uint64_t event_time_usec, 28 : uint64_t last_expiry_time_usec, uint64_t last_defer_time_usec); 29 : 30 : class Generator { 31 : public: 32 0 : virtual ~Generator() {} 33 : 34 : virtual const std::string ToString() const = 0; 35 : virtual bool ProcessRules(const VizMsg *vmsg, bool rsc) = 0; 36 : virtual DbHandler * GetDbHandler() const = 0; // db_handler_; 37 : 38 : bool ReceiveSandeshMsg(const VizMsg *vmsg, bool rsc); 39 : void SendSandeshMessageStatistics(); 40 : void GetStatistics(std::vector<SandeshStats> *ssv) const; 41 : void GetStatistics(std::vector<SandeshLogLevelStats> *lsv) const; 42 : 43 : private: 44 : void UpdateStatistics(const VizMsg *vmsg); 45 : 46 : VizMsgStatistics statistics_; 47 : mutable std::mutex smutex_; 48 : }; 49 : 50 : class SandeshGenerator : public Generator { 51 : public: 52 : const static int kInitialSmDeferTimeMSec = 300; 53 : const static int kMaxSmDeferTimeMSec = 30000; 54 : 55 : typedef boost::tuple<std::string /* Source */, std::string /* Module */, 56 : std::string /* Instance id */, std::string /* Node type */> GeneratorId; 57 : 58 : SandeshGenerator(Collector * const collector, VizSession *session, 59 : SandeshStateMachine *state_machine, 60 : const std::string &source, const std::string &module, 61 : const std::string &instance_id, const std::string &node_type, 62 : DbHandlerPtr global_db_handler); 63 : ~SandeshGenerator(); 64 : 65 : void ReceiveSandeshCtrlMsg(uint32_t connects); 66 : void DisconnectSession(VizSession *vsession); 67 : void ConnectSession(VizSession *vsession, SandeshStateMachine *state_machine); 68 : 69 : bool GetSandeshStateMachineQueueCount(uint64_t &queue_count) const; 70 : bool GetSandeshStateMachineDropLevel(std::string &drop_level) const; 71 : bool GetSandeshStateMachineStats(SandeshStateMachineStats &sm_stats, 72 : SandeshGeneratorBasicStats &sm_msg_stats) const; 73 : bool IsStateMachineDeferTimerRunning() const; 74 : int GetStateMachineDeferTimeMSec() const; 75 : 76 0 : const std::string &instance_id() const { return instance_id_; } 77 0 : const std::string &node_type() const { return node_type_; } 78 0 : VizSession * session() const { 79 0 : std::scoped_lock lock(mutex_); 80 0 : return viz_session_; 81 0 : } 82 0 : const std::string &module() const { return module_; } 83 0 : const std::string &source() const { return source_; } 84 0 : virtual const std::string ToString() const { return name_; } 85 0 : SandeshStateMachine * get_state_machine(void) { 86 0 : return state_machine_; 87 : } 88 : const std::string State() const; 89 : 90 : void GetGeneratorInfo(ModuleServerState &genlist) const; 91 : void SetDbQueueWaterMarkInfo(Sandesh::QueueWaterMarkInfo &wm); 92 : void ResetDbQueueWaterMarkInfo(); 93 : void SetSmQueueWaterMarkInfo(Sandesh::QueueWaterMarkInfo &wm); 94 : void ResetSmQueueWaterMarkInfo(); 95 0 : virtual DbHandler * GetDbHandler() const { return db_handler_.get(); } 96 : 97 : private: 98 : virtual bool ProcessRules(const VizMsg *vmsg, bool rsc); 99 : void set_session(VizSession *session); 100 : 101 0 : void set_state_machine(SandeshStateMachine *state_machine) { 102 0 : state_machine_ = state_machine; 103 : // Update state machine 104 0 : state_machine_->SetGeneratorKey(name_); 105 0 : } 106 : void HandleSeqRedisReply(const std::map<std::string,int32_t> &typeMap); 107 : void HandleDelRedisReply(bool res); 108 : void TimerErrorHandler(std::string name, std::string error); 109 : 110 : void ProcessRulesCb(GenDb::DbOpResult::type dresult); 111 : bool StateMachineDeferTimerExpired(); 112 : void CreateStateMachineDeferTimer(); 113 : void DeleteStateMachineDeferTimer(); 114 : void StartStateMachineDeferTimer(int time_msec); 115 : void StopStateMachineDeferTimer(); 116 : bool IsStateMachineDeferTimerRunningUnlocked() const; 117 : int GetStateMachineDeferMSec() const; 118 : 119 : Collector * const collector_; 120 : SandeshStateMachine *state_machine_; 121 : VizSession *viz_session_; 122 : GeneratorInfoAttr gen_attr_; 123 : 124 : const std::string instance_id_; 125 : const std::string node_type_; 126 : const std::string source_; 127 : const std::string module_; 128 : const std::string name_; 129 : int instance_; 130 : 131 : std::atomic<bool> disconnected_; 132 : DbHandlerPtr db_handler_; 133 : GenDb::GenDbIf::DbAddColumnCb process_rules_cb_; 134 : Timer *sm_defer_timer_; 135 : uint64_t sm_defer_timer_expiry_time_usec_; 136 : int sm_defer_time_msec_; 137 : mutable std::mutex mutex_; 138 : }; 139 : 140 : class SyslogGenerator : public Generator { 141 : public: 142 : SyslogGenerator(SyslogListeners *const listeners, 143 : const std::string &source, const std::string &module); 144 : const std::string &module() const { return module_; } 145 : const std::string &source() const { return source_; } 146 0 : virtual const std::string ToString() const { return name_; } 147 0 : virtual DbHandler * GetDbHandler() const { return db_handler_.get(); } 148 : 149 : private: 150 : virtual bool ProcessRules(const VizMsg *vmsg, bool rsc); 151 : 152 : SyslogListeners * const syslog_; 153 : const std::string source_; 154 : const std::string module_; 155 : const std::string name_; 156 : DbHandlerPtr db_handler_; 157 : }; 158 : 159 : #endif