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