Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include <boost/uuid/uuid_io.hpp> 6 : #include <cmn/agent_cmn.h> 7 : #include <cmn/agent.h> 8 : #include <init/agent_param.h> 9 : #include <init/agent_init.h> 10 : #include <resource_manager/resource_manager.h> 11 : #include <resource_manager/resource_table.h> 12 : #include <resource_manager/resource_backup.h> 13 : #include <resource_manager/resource_cmn.h> 14 : #include <resource_manager/index_resource.h> 15 : 16 3 : ResourceManager::ResourceManager(Agent *agent) : 17 3 : agent_(agent), 18 3 : restore_work_queue_(agent->task_scheduler()-> 19 : GetTaskId(kAgentResourceRestoreTask), 0, 20 : boost::bind(&ResourceManager::WorkQueueRestoreProcess, 21 27 : this, _1)) { 22 : 23 21 : for (uint8_t type = uint8_t(Resource::INVALID + 1); 24 21 : type < uint8_t(Resource::MAX); 25 : type++) { 26 18 : resource_table_[type].reset(Resource::Create((Resource::Type)type, 27 : this)); 28 : } 29 : 30 3 : agent_->set_resource_manager(this); 31 : //Backup resource if configured 32 3 : if (agent_->params()->restart_backup_enable()) { 33 0 : backup_mgr_.reset(new ResourceBackupManager(this)); 34 : } else { 35 3 : agent_->agent_init()->SetResourceManagerReady(); 36 : } 37 3 : } 38 : 39 24 : ResourceManager::~ResourceManager() { 40 27 : } 41 : 42 3 : void ResourceManager::Init() { 43 3 : if (backup_mgr_.get()) 44 0 : backup_mgr_->Init(); 45 3 : } 46 : // Read the Data from Resore Queue and process it. 47 0 : bool ResourceManager::WorkQueueRestoreProcess( 48 : ResourceRestoreReqPtr restore_data) { 49 0 : RestoreResource(restore_data.get()->key(), restore_data.get()->data()); 50 0 : return true; 51 : } 52 : 53 : 54 0 : void ResourceManager::EnqueueRestore(KeyPtr key, DataPtr data) { 55 0 : ResourceRestoreReqPtr restore_data(new ResourceRestoreReq(key, data)); 56 0 : restore_work_queue_.Enqueue(restore_data); 57 0 : } 58 : 59 : // Check for ResouceBackupEndkey if it is set mark it resource manager ready. 60 : // Mark the resource key diry until we process the audit for validity of key. 61 0 : void ResourceManager::RestoreResource(KeyPtr key, DataPtr data) { 62 0 : if (dynamic_cast<ResourceBackupEndKey *>(key.get())) { 63 0 : agent_->agent_init()->SetResourceManagerReady(); 64 0 : return; 65 : } 66 : 67 0 : ResourceTable *resource_table = key->resource_table(); 68 0 : assert(resource_table->FindKey(key) == NULL); 69 : //If its not used then candidate for deletion so mark dirty 70 0 : key->set_dirty(); 71 0 : resource_table->RestoreKey(key, data); 72 : } 73 : 74 : // Reserve the index for certain resource types. 75 48 : void ResourceManager::ReserveIndex(Resource::Type type, uint32_t index) { 76 48 : IndexResourceTable *table = dynamic_cast<IndexResourceTable *> 77 48 : (resource_table(type)); 78 48 : table->ReserveIndex(index); 79 48 : } 80 : 81 48 : void ResourceManager::ReleaseIndex(Resource::Type type, uint32_t index) { 82 48 : IndexResourceTable *table = dynamic_cast<IndexResourceTable *> 83 48 : (resource_table(type)); 84 48 : table->ReleaseIndex(index); 85 48 : } 86 : 87 : // Allocates the the index by resource table. 88 : // if back up is enabled it will store the data to file. 89 124 : ResourceManager::DataPtr ResourceManager::Allocate(KeyPtr key) { 90 124 : assert(agent_->ResourceManagerReady()); 91 124 : ResourceManager::DataPtr data = key.get()->resource_table()->Allocate(key); 92 124 : if (backup_mgr_.get()) { 93 0 : backup_mgr_->BackupResource(key, data, ResourceBackupReq::ADD); 94 : } 95 124 : return data; 96 0 : } 97 : 98 : // Releases the resource table index and key. 99 : // Delete the data from file. 100 124 : void ResourceManager::Release(KeyPtr key) { 101 124 : ResourceTable *resource_table = key.get()->resource_table(); 102 124 : ResourceManager::DataPtr data = resource_table->FindKeyPtr(key); 103 124 : if (data.get() == NULL) { 104 0 : return; 105 : } 106 : 107 124 : if (backup_mgr_.get()) { 108 0 : backup_mgr_->BackupResource(key, data, ResourceBackupReq::DEL); 109 : } 110 124 : resource_table->ReleaseKey(key, data); 111 124 : } 112 : 113 : // Releases the reserved index. 114 124 : void ResourceManager::Release(Resource::Type type, uint32_t index) { 115 124 : IndexResourceTable *table = dynamic_cast<IndexResourceTable *> 116 124 : (resource_table(type)); 117 124 : table->Release(index); 118 124 : } 119 : 120 344 : ResourceTable* ResourceManager::resource_table(uint8_t type) { 121 344 : return resource_table_[type].get(); 122 : } 123 : 124 : // Audit the records per resource table. 125 0 : bool ResourceManager::Audit() { 126 0 : for (uint8_t type = uint8_t(Resource::INVALID + 1); 127 0 : type < uint8_t(Resource::MAX); 128 : type++) { 129 0 : resource_table_[type].get()->FlushStale(); 130 : } 131 0 : backup_mgr_->AuditDone(); 132 0 : return true; 133 : }