Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef __DB_IFMAP_EXPORTER_H__ 6 : #define __DB_IFMAP_EXPORTER_H__ 7 : 8 : #include <list> 9 : #include <memory> 10 : #include <map> 11 : #include <string> 12 : #include <boost/crc.hpp> // for boost::crc_32_type 13 : #include <boost/scoped_ptr.hpp> 14 : #include <boost/unordered_set.hpp> 15 : 16 : #include "db/db_table.h" 17 : 18 : class BitSet; 19 : 20 : class IFMapClient; 21 : class IFMapGraphWalker; 22 : class IFMapLink; 23 : class IFMapLinkState; 24 : class IFMapNode; 25 : class IFMapNodeState; 26 : class IFMapServer; 27 : class IFMapState; 28 : class IFMapTable; 29 : class IFMapUpdate; 30 : class IFMapUpdateQueue; 31 : class IFMapUpdateSender; 32 : 33 : struct IFMapTypenameWhiteList; 34 : 35 : // The IFMapExporter makes sure that the right entries are added to the update 36 : // queue. It uses the GraphWalker to calculate the 'interest' set for each node 37 : // and then ensures that all clients see the necessary information. It also 38 : // enforces ordering. All link add/change operations must come after the nodes 39 : // they refer to have been advertised to the client. Add node delete operations 40 : // must come after the links that refer to the node have been deleted. 41 : class IFMapExporter { 42 : public: 43 : enum TrackerType { 44 : INTEREST, 45 : ADVERTISED, 46 : TT_END, 47 : }; 48 : typedef boost::unordered_set<IFMapState *> ConfigSet; 49 : typedef ConfigSet::size_type CsSz_t; 50 : typedef ConfigSet::const_iterator Cs_citer; 51 : typedef std::vector<ConfigSet *> ClientConfigTracker; 52 : typedef boost::crc_32_type::value_type crc32type; 53 : explicit IFMapExporter(IFMapServer *server); 54 : ~IFMapExporter(); 55 : 56 : void Initialize(DB *db); 57 : void Shutdown(); 58 : 59 : void StateUpdateOnDequeue(IFMapUpdate *update, const BitSet &dequeue_set, 60 : bool is_delete); 61 : 62 : // GraphWalker API 63 : DBTable::ListenerId TableListenerId(const DBTable *table) const; 64 : 65 : IFMapNodeState *NodeStateLocate(IFMapNode *node); 66 : IFMapNodeState *NodeStateLookup(IFMapNode *node); 67 : IFMapLinkState *LinkStateLookup(IFMapLink *link); 68 : 69 677 : DBTable *link_table() { return link_table_; } 70 21 : IFMapServer *server() { return server_; } 71 : 72 : bool FilterNeighbor(IFMapNode *lnode, IFMapLink *link); 73 : 74 : void AddClientConfigTracker(int index); 75 : void DeleteClientConfigTracker(int index); 76 : void UpdateClientConfigTracker(IFMapState *state, const BitSet& client_bits, 77 : bool add, TrackerType tracker_type); 78 : void CleanupClientConfigTrackedEntries(int index); 79 : bool ClientHasConfigTracker(TrackerType tracker_type, int index); 80 : bool ClientConfigTrackerHasState(TrackerType tracker_type, int index, 81 : IFMapState *state); 82 : bool ClientConfigTrackerEmpty(TrackerType tracker_type, int index); 83 : size_t ClientConfigTrackerSize(TrackerType tracker_type, int index); 84 : Cs_citer ClientConfigTrackerBegin(TrackerType tracker_type, int index) const; 85 : Cs_citer ClientConfigTrackerEnd(TrackerType tracker_type, int index) const; 86 : 87 : void StateInterestSet(IFMapState *state, const BitSet& interest_bits); 88 : void StateInterestOr(IFMapState *state, const BitSet& interest_bits); 89 : void StateInterestReset(IFMapState *state, const BitSet& interest_bits); 90 : void StateAdvertisedOr(IFMapState *state, const BitSet& interest_bits); 91 : void StateAdvertisedReset(IFMapState *state, const BitSet& interest_bits); 92 : 93 : const IFMapTypenameWhiteList &get_traversal_white_list() const; 94 : void ResetLinkDeleteClients(const BitSet &bset); 95 : 96 : private: 97 : friend class XmppIfmapTest; 98 : class TableInfo; 99 : typedef std::map<DBTable *, TableInfo *> TableMap; 100 : 101 : // Database listener for IFMap identifier (and link attr) tables. 102 : void NodeTableExport(DBTablePartBase *partition, DBEntryBase *entry); 103 : // Database listener for the IFMapLink DB Table. 104 : void LinkTableExport(DBTablePartBase *partition, DBEntryBase *entry); 105 : 106 : template <class ObjectType> 107 : bool UpdateAddChange(ObjectType *obj, IFMapState *state, 108 : const BitSet &add_set, const BitSet &rm_set, 109 : bool change); 110 : template <class ObjectType> 111 : bool UpdateRemove(ObjectType *obj, IFMapState *state, 112 : const BitSet &rm_set); 113 : template <class ObjectType> 114 : void EnqueueDelete(ObjectType *obj, IFMapState *state); 115 : 116 : void MoveDependentLinks(IFMapNodeState *state); 117 : void RemoveDependentLinks(IFMapNodeState *state, const BitSet &rm_set); 118 : void MoveAdjacentNode(IFMapNodeState *state); 119 : void ProcessAdjacentNode(IFMapNode *node, const BitSet &add_set, 120 : IFMapNodeState *state, bool force_process=false); 121 : 122 : bool IsFeasible(const IFMapNode *node); 123 : 124 : const BitSet *MergeClientInterest(IFMapNode *node, IFMapNodeState *state, 125 : std::unique_ptr<BitSet> *ptr); 126 : 127 : const TableInfo *Find(const DBTable *table) const; 128 : 129 : void TableStateClear(DBTable *table, DBTable::ListenerId tsid); 130 : bool ConfigChanged(IFMapNode *node); 131 : void DeleteStateIfAppropriate(DBTable *table, DBEntryBase *entry, 132 : IFMapState *state); 133 : 134 : IFMapUpdateQueue *queue(); 135 : IFMapUpdateSender *sender(); 136 : 137 : IFMapServer *server_; 138 : boost::scoped_ptr<IFMapGraphWalker> walker_; 139 : TableMap table_map_; 140 : 141 : DBTable *link_table_; 142 : ClientConfigTracker client_config_tracker_[TT_END]; 143 : }; 144 : 145 : #endif