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