LCOV - code coverage report
Current view: top level - vnsw/agent/resource_manager - resource_manager.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 45 72 62.5 %
Date: 2026-11-30 02:00:48 Functions: 9 13 69.2 %
Legend: Lines: hit not hit

          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           1 : ResourceManager::ResourceManager(Agent *agent) :
      18           1 :     agent_(agent),
      19           1 :     restore_work_queue_(agent->task_scheduler()->
      20             :             GetTaskId(kAgentResourceRestoreTask), 0,
      21             :             boost::bind(&ResourceManager::WorkQueueRestoreProcess,
      22           9 :             this, _1)) {
      23             : 
      24           7 :     for (uint8_t type = uint8_t(Resource::INVALID + 1);
      25           7 :          type < uint8_t(Resource::MAX);
      26             :          type++) {
      27           6 :         resource_table_[type].reset(Resource::Create((Resource::Type)type,
      28             :                                                       this));
      29             :     }
      30             : 
      31           1 :     agent_->set_resource_manager(this);
      32             :     //Backup resource if configured
      33           1 :     if (agent_->params()->restart_backup_enable()) {
      34           0 :         backup_mgr_.reset(new ResourceBackupManager(this));
      35             :     } else {
      36           1 :         agent_->agent_init()->SetResourceManagerReady();
      37             :     }
      38           1 : }
      39             : 
      40           8 : ResourceManager::~ResourceManager() {
      41           9 : }
      42             : 
      43           1 : void ResourceManager::Init() {
      44           1 :     if (backup_mgr_.get())
      45           0 :         backup_mgr_->Init();
      46           1 : }
      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          16 : void ResourceManager::ReserveIndex(Resource::Type type, uint32_t index) {
      77          16 :     IndexResourceTable *table = dynamic_cast<IndexResourceTable *>
      78          16 :         (resource_table(type));
      79          16 :     table->ReserveIndex(index);
      80          16 : }
      81             : 
      82          16 : void ResourceManager::ReleaseIndex(Resource::Type type, uint32_t index) {
      83          16 :     IndexResourceTable *table = dynamic_cast<IndexResourceTable *>
      84          16 :         (resource_table(type));
      85          16 :     table->ReleaseIndex(index);
      86          16 : }
      87             : 
      88             : // Allocates the the index by resource table.
      89             : // if back up is enabled it will store the data to file.
      90         111 : ResourceManager::DataPtr ResourceManager::Allocate(KeyPtr key) {
      91         111 :     assert(agent_->ResourceManagerReady());
      92         111 :     ResourceManager::DataPtr data = key.get()->resource_table()->Allocate(key);
      93         111 :     if (backup_mgr_.get()) {
      94           0 :         backup_mgr_->BackupResource(key, data, ResourceBackupReq::ADD);
      95             :     }
      96         111 :     return data;
      97           0 : }
      98             : 
      99             : // Releases the resource table index and  key.
     100             : // Delete the data from file.
     101         111 : void ResourceManager::Release(KeyPtr key) {
     102         111 :     ResourceTable *resource_table = key.get()->resource_table();
     103         111 :     ResourceManager::DataPtr data = resource_table->FindKeyPtr(key);
     104         111 :     if (data.get() == NULL) {
     105           0 :         return;
     106             :     }
     107             : 
     108         111 :     if (backup_mgr_.get()) {
     109           0 :         backup_mgr_->BackupResource(key, data, ResourceBackupReq::DEL);
     110             :     }
     111         111 :     resource_table->ReleaseKey(key, data);
     112         111 : }
     113             : 
     114             : // Releases the reserved index.
     115         111 : void ResourceManager::Release(Resource::Type type, uint32_t index) {
     116         111 :     IndexResourceTable *table = dynamic_cast<IndexResourceTable *>
     117         111 :         (resource_table(type));
     118         111 :     table->Release(index);
     119         111 : }
     120             : 
     121         254 : ResourceTable* ResourceManager::resource_table(uint8_t type) {
     122         254 :     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             : }

Generated by: LCOV version 1.14