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 ctrlplane_ifmap_agent_table_h 7 : #define ctrlplane_ifmap_agent_table_h 8 : 9 : #include "db/db_graph_base.h" 10 : #include "ifmap/ifmap_table.h" 11 : #include "ifmap/ifmap_link_table.h" 12 : #include "ifmap/ifmap_object.h" 13 : #include "boost/asio/io_service.hpp" 14 : #include "base/timer.h" 15 : #include "base/task.h" 16 : #include "sandesh/sandesh_trace.h" 17 : 18 : #define IFMAP_AGENT_LINK_DB_NAME "__ifmap_agentlinkdata__.0" 19 : extern SandeshTraceBufferPtr IFMapAgentTraceBuf; 20 : 21 : #define IFMAP_AGENT_TRACE(obj, ...) do { \ 22 : if (LoggingDisabled()) break; \ 23 : IFMapAgent##obj::TraceMsg(IFMapAgentTraceBuf, __FILE__, __LINE__, ##__VA_ARGS__); \ 24 : } while (false) 25 : 26 : 27 : class DBGraph; 28 : class IFMapNode; 29 : 30 : 31 : class IFMapAgentTable : public IFMapTable { 32 : public: 33 : 34 : struct IFMapAgentData : DBRequestData { 35 : std::unique_ptr<IFMapObject>content; 36 : }; 37 : typedef boost::function<bool(DBTable *table, IFMapNode *node, DBRequest *req)> PreFilterFn; 38 : 39 : IFMapAgentTable(DB *db, const std::string &name, DBGraph *graph); 40 : 41 : virtual void Input(DBTablePartition *partition, DBClient *client, 42 : DBRequest *req); 43 : virtual void Clear(); 44 : 45 : // Allocate an IFMapNode. 46 : virtual std::unique_ptr<DBEntry> AllocEntry(const DBRequestKey *key) const; 47 : 48 : static IFMapNode *TableEntryLookup(DB *db, RequestKey *key); 49 : void NotifyNode(IFMapNode *node); 50 : const DBGraph *GetGraph() const {return graph();}; 51 35118 : DBGraph *GetGraph() {return graph();}; 52 : void DeleteNode(IFMapNode *node); 53 84 : void RegisterPreFilter(PreFilterFn fn) {pre_filter_ = fn;}; 54 : 55 : private: 56 : IFMapNode *EntryLocate(IFMapNode *node, RequestKey *key); 57 : IFMapNode *EntryLookup(RequestKey *key); 58 : IFMapAgentTable* TableFind(const std::string &node_name); 59 : void HandlePendingLinks(IFMapNode *); 60 : PreFilterFn pre_filter_; 61 : }; 62 : 63 : class IFMapAgentLinkTable : public IFMapLinkTable { 64 : public: 65 : struct RequestKey : DBRequestKey { 66 : IFMapTable::RequestKey left_key; 67 : IFMapTable::RequestKey right_key; 68 : std::string metadata; 69 : }; 70 : 71 : struct DeferredNode { 72 : IFMapTable::RequestKey node_key; 73 : std::string link_metadata; 74 : }; 75 : 76 : class comp { 77 : public: 78 3191 : bool operator()(const IFMapTable::RequestKey &left, 79 : const IFMapTable::RequestKey &right) const { 80 3191 : if (left.id_type != right.id_type) 81 1587 : return left.id_type < right.id_type; 82 : 83 1604 : return left.id_name < right.id_name; 84 : } 85 : }; 86 : 87 : IFMapAgentLinkTable(DB *db, const std::string &name, DBGraph *graph); 88 : typedef std::map<IFMapTable::RequestKey, std::list<DeferredNode> *, comp> LinkDefMap; 89 : virtual void Input(DBTablePartition *partition, DBClient *client, 90 : DBRequest *req); 91 : void IFMapAgentLinkTable_Init(DB *db, DBGraph *graph); 92 : static DBTable *CreateTable(DB *db, const std::string &name, 93 : DBGraph *graph); 94 : void EvalDefLink(IFMapTable::RequestKey *key); 95 : bool RemoveDefListEntry(LinkDefMap *map, LinkDefMap::iterator &map_it, 96 : std::list<DeferredNode>::iterator *list_it); 97 : void DestroyDefLink(uint64_t); 98 1 : const LinkDefMap &GetLinkDefMap() const { 99 1 : return link_def_map_; 100 : } 101 : void DelLink(IFMapNode *first, IFMapNode *second, DBGraphEdge *edge); 102 : void LinkDefAdd(DBRequest *request); 103 : private: 104 : void AddLink(IFMapNode *left, IFMapNode *right, 105 : const std::string &metadata, uint64_t seq); 106 : 107 : IFMapLink *FindLink(IFMapNode *left, IFMapNode *right, const std::string &metadata); 108 : 109 : LinkDefMap link_def_map_; 110 : }; 111 : 112 : 113 : class IFMapAgentStaleCleaner { 114 : public: 115 : IFMapAgentStaleCleaner(DB *db, DBGraph *graph); 116 : ~IFMapAgentStaleCleaner(); 117 : class IFMapAgentStaleCleanerWorker; 118 : void Clear(); 119 : bool StaleTimeout(uint64_t); 120 : 121 : private: 122 : DB *db_; 123 : DBGraph *graph_; 124 : uint64_t seq_; 125 : }; 126 : 127 : extern void IFMapAgentLinkTable_Init(DB *db, DBGraph *graph); 128 : #endif