Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef vnsw_agent_resource_table_hpp 6 : #define vnsw_agent_resource_table_hpp 7 : #include "resource_cmn.h" 8 : class ResourceManager; 9 : class ResourceTable; 10 : class ResourceData; 11 : class ResourceKey { 12 : public: 13 : ResourceKey(ResourceManager *rm, Resource::Type type); 14 : virtual ~ResourceKey(); 15 : 16 0 : virtual const std::string ToString() {return "";} 17 : virtual bool IsLess(const ResourceKey &rhs) const = 0; 18 : virtual void Backup(ResourceData *data, uint16_t op) = 0; 19 : bool operator<(const ResourceKey &rhs) const; 20 0 : void set_dirty() {dirty_ = true;} 21 124 : void reset_dirty() {dirty_ = false;} 22 0 : bool dirty() const {return dirty_;} 23 248 : ResourceTable * resource_table() {return resource_table_;} 24 124 : ResourceManager* rm() {return rm_;} 25 : private: 26 : ResourceManager *rm_; 27 : bool dirty_; 28 : ResourceTable *resource_table_; 29 : DISALLOW_COPY_AND_ASSIGN(ResourceKey); 30 : }; 31 : 32 : class ResourceData { 33 : public: 34 : typedef boost::shared_ptr<ResourceData> Ptr; 35 : ResourceData(ResourceManager *rm); 36 : virtual ~ResourceData(); 37 : virtual const std::string ToString() = 0; 38 : private: 39 : ResourceManager *rm_; 40 : }; 41 : 42 : struct KeyDataMapComparator { 43 2162 : bool operator()( const boost::shared_ptr<ResourceKey> lhs, 44 : const boost::shared_ptr<ResourceKey> rhs) const { 45 2162 : ResourceKey *left = lhs.get(); 46 2162 : ResourceKey *right = rhs.get(); 47 2162 : return (*left).IsLess(*right); 48 : } 49 : }; 50 : 51 : class ResourceBackupEndKey : public ResourceKey { 52 : public: 53 : ResourceBackupEndKey(ResourceManager *rm); 54 : virtual ~ResourceBackupEndKey(); 55 : 56 0 : virtual const std::string ToString() {return "ResourceBackupEndKey";} 57 0 : virtual bool IsLess(const ResourceKey &rhs) const { 58 0 : assert(0); 59 : return false; 60 : } 61 0 : virtual void Backup(ResourceData *data, uint16_t op) { 62 0 : assert(0); 63 : } 64 : }; 65 : // This class is resposible for allocating & restoring the the resources. 66 : // maintains the Resource Key and resource data value pairs. 67 : class ResourceTable { 68 : public: 69 : typedef boost::shared_ptr<ResourceKey> KeyPtr; 70 : typedef boost::shared_ptr<ResourceData> DataPtr; 71 : typedef ResourceTable::KeyPtr ResourceKeyPtr; 72 : typedef std::map<KeyPtr, DataPtr, KeyDataMapComparator> KeyDataMap; 73 : typedef KeyDataMap::iterator KeyDataMapIter; 74 : ResourceTable(ResourceManager *rm); 75 : virtual ~ResourceTable(); 76 : virtual const std::string ToString() = 0; 77 : virtual DataPtr AllocateData(KeyPtr key) = 0; 78 : virtual void RestoreKey(KeyPtr key, DataPtr data) = 0; 79 : virtual void ReleaseKey(KeyPtr key, DataPtr data) = 0; 80 : void InsertKey(KeyPtr key, DataPtr data); 81 : void DeleteKey(KeyPtr key); 82 : ResourceData *FindKey(KeyPtr key); 83 : DataPtr FindKeyPtr(KeyPtr key); 84 : void FlushStale(); 85 : ResourceTable::DataPtr Allocate(KeyPtr key); 86 124 : ResourceManager *resource_manager() const { return rm_; } 87 : private: 88 : ResourceManager *rm_; 89 : KeyDataMap key_data_map_; 90 : DISALLOW_COPY_AND_ASSIGN(ResourceTable); 91 : }; 92 : 93 : #endif //resource_table