Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include <fstream> 6 : #include <cmn/agent.h> 7 : #include <cmn/event_notifier.h> 8 : #include <base/timer.h> 9 : #include "resource_manager/resource_backup.h" 10 : #include "resource_manager/resource_table.h" 11 : #include "resource_manager/sandesh_map.h" 12 : #include "resource_manager/resource_manager.h" 13 : #include "resource_manager/resource_manager_types.h" 14 : #include <sys/stat.h> 15 : 16 0 : ResourceBackupManager::ResourceBackupManager(ResourceManager *mgr) : 17 0 : resource_manager_(mgr), agent_(mgr->agent()), sandesh_maps_(this), 18 0 : backup_work_queue_(agent_->task_scheduler()-> 19 : GetTaskId(kAgentResourceBackUpTask), 0, 20 : boost::bind(&ResourceBackupManager::WorkQueueBackUpProcess, 21 0 : this, _1)) { 22 0 : audit_handle_ = agent_->event_notifier()->RegisterSubscriber 23 0 : ((new EventNotifyKey(EventNotifyKey::END_OF_RIB)), 24 0 : boost::bind(&ResourceManager::Audit, mgr)); 25 0 : } 26 : 27 0 : ResourceBackupManager::~ResourceBackupManager() { 28 0 : agent_->event_notifier()->DeregisterSubscriber(audit_handle_); 29 0 : } 30 : 31 0 : void ResourceBackupManager::Init() { 32 : //Read stored resource file and restore resources 33 0 : sandesh_maps_.ReadFromFile(); 34 0 : sandesh_maps_.RestoreResource(); 35 0 : } 36 : 37 0 : void ResourceBackupManager::BackupResource(ResourceManager::KeyPtr key, 38 : ResourceManager::DataPtr data, 39 : ResourceBackupReq::Op op) { 40 0 : ResourceBackupReqPtr backup_data(new ResourceBackupReq (key, data, op)); 41 0 : backup_work_queue_.Enqueue(backup_data); 42 0 : } 43 : 44 0 : bool ResourceBackupManager::WorkQueueBackUpProcess( 45 : ResourceBackupReqPtr backup_data) { 46 0 : backup_data->Process(); 47 0 : return true; 48 : } 49 : 50 0 : uint32_t ResourceBackupManager::ReadResourceDataFromFile 51 : (const std::string &file_name, std::unique_ptr<uint8_t> *buf) { 52 0 : std::stringstream error_str; 53 : uint32_t size; 54 0 : std::ifstream input(file_name.c_str(), std::ofstream::binary); 55 0 : if (!input.good()) { 56 0 : error_str << "Resource backup mgr Open failed to read file"; 57 0 : goto error; 58 : } 59 : struct stat st; 60 0 : if (stat(file_name.c_str(), &st) == -1) { 61 0 : error_str << "Resource backup mgr Get size from file failed"; 62 0 : goto error; 63 : } 64 0 : size = (uint32_t) st.st_size; 65 0 : *buf = std::unique_ptr<uint8_t> (new uint8_t [size]()); 66 0 : input.read((char *)((*buf).get()), size); 67 : 68 0 : if (!input.good()) { 69 0 : error_str << "Resource backup mgr reading file failed"; 70 0 : goto error; 71 : } 72 : 73 0 : input.close(); 74 0 : return size; 75 : 76 0 : error: 77 : { 78 0 : input.close(); 79 0 : error_str << file_name; 80 0 : LOG(ERROR, error_str.str()); 81 0 : return 0; 82 : } 83 0 : } 84 : 85 0 : ResourceSandeshMaps& ResourceBackupManager::sandesh_maps() { 86 0 : return sandesh_maps_; 87 : } 88 : 89 0 : void ResourceBackupManager::AuditDone() { 90 0 : agent_->event_notifier()->DeregisterSubscriber(audit_handle_); 91 0 : audit_handle_.reset(); 92 0 : } 93 : 94 0 : ResourceBackupReq::ResourceBackupReq(ResourceManager::KeyPtr key, 95 : ResourceManager::DataPtr data, 96 0 : Op op) : 97 0 : key_(key), data_(data), op_(op) { 98 0 : } 99 : 100 0 : ResourceBackupReq::~ResourceBackupReq() { 101 0 : } 102 : 103 0 : void ResourceBackupReq::Process() { 104 0 : key_.get()->Backup(data_.get(), op_); 105 0 : } 106 : 107 0 : ResourceRestoreReq::ResourceRestoreReq(ResourceManager::KeyPtr key, 108 0 : ResourceManager::DataPtr data) : 109 0 : key_(key), data_(data) { 110 0 : } 111 : 112 0 : ResourceRestoreReq::~ResourceRestoreReq() { 113 0 : }