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