10 #include <boost/filesystem.hpp>
11 #include <boost/functional/hash.hpp>
12 #include <boost/algorithm/string.hpp>
15 #include <sandesh/sandesh_types.h>
16 #include <sandesh/sandesh.h>
30 const std::string &name,
31 const std::string &file_name) :
32 backup_manager_(manager), agent_(manager->agent()), name_(name),
107 std::stringstream temp_file_stream;
108 temp_file_stream << file_name <<
".tmp";
109 std::ofstream output;
111 output.open(temp_file_stream.str().c_str(),
112 std::ofstream::binary | std::ofstream::trunc);
113 if (!output.good()) {
114 LOG(ERROR,
"File open failed" << temp_file_stream.str());
116 return std::string();
120 return temp_file_stream.str();
125 const std::string &file_name) {
126 boost::system::error_code ec;
127 boost::filesystem::path tmp_file_path(file_tmp_name.c_str());
128 boost::filesystem::path backup_file_path(file_name.c_str());
129 boost::filesystem::rename(tmp_file_path, backup_file_path, ec);
131 LOG(ERROR,
"Resource backup mgr Rename file failed" << ec);
141 std::unique_ptr<uint8_t> buffer;
144 if (size && buffer.get()) {
145 *hashsum = (uint32_t)boost::hash_range(buffer.get(),
157 template <
typename T1,
typename T2>
159 const T2& index_map) {
160 uint32_t write_buff_size = 0;
164 if (temp_file.empty()) {
165 LOG(ERROR,
"Temp file is not created");
169 sandesh_data->set_index_map(index_map);
171 write_buff_size = sandesh_data->WriteBinaryToFile(temp_file, &error);
173 LOG(ERROR,
"Sandesh Write Binary failed " << write_buff_size);
183 std::remove(file_path.c_str());
186 std::stringstream file_path;
188 return RenameFile(temp_file, file_path.str());
199 const std::string & file_prefix) {
202 if ((dir_path = opendir(root.c_str())) == NULL) {
203 return std::string();
205 while ((dir = readdir(dir_path)) != NULL) {
206 std::string file_name = dir->d_name;
209 if (!file_name.find(file_prefix)) {
212 std::string tmpstr(file_name.c_str());
213 std::vector<string> tokens;
214 boost::split(tokens, tmpstr, boost::is_any_of(
"-"));
219 token = tokens[tokens.size() - 1];
223 for (
int i =0; token[i] !=
'\0'; i++) {
224 if (!isdigit(token[i])) {
237 return std::string();
243 template <
typename T>
245 const std::string &root) {
249 if (file_name.empty()) {
250 LOG(DEBUG,
"File name with prefix not found ");
254 std::stringstream file_path;
255 file_path << root <<
"/"<< file_name;
256 if (!boost::filesystem::exists( file_path.str().c_str())) {
257 LOG(DEBUG,
"File path not found " << file_path.str());
260 std::unique_ptr<uint8_t> buffer;
265 uint32_t hashsum = (uint32_t)boost::hash_range(buffer.get(),
268 hash_value << hashsum;
270 if (std::string::npos !=
271 file_name.find(hash_value.str())) {
272 sandesh_data->ReadBinary(buffer.get(), size, &error);
274 LOG(ERROR,
"Sandesh Read Binary failed ");
284 "contrail_vrf_resource") {
291 VrfMplsResourceMapSandesh sandesh_data;
292 return WriteMapToFile<VrfMplsResourceMapSandesh, Map> (&sandesh_data,
map_);
296 VrfMplsResourceMapSandesh sandesh_data;
297 ReadMapFromFile<VrfMplsResourceMapSandesh>(&sandesh_data,
299 map_ = sandesh_data.get_index_map();
304 uint32_t index = it->first;
305 VrfMplsResource sandesh_key = it->second;
307 sandesh_key.get_bridge_nh());
321 "contrail_vlan_resource") {
328 VlanMplsResourceMapSandesh sandesh_data;
329 return WriteMapToFile<VlanMplsResourceMapSandesh, Map>
330 (&sandesh_data,
map_);
334 VlanMplsResourceMapSandesh sandesh_data;
335 ReadMapFromFile<VlanMplsResourceMapSandesh>(&sandesh_data,
337 map_ = sandesh_data.get_index_map();
342 uint32_t index = it->first;
343 VlanMplsResource sandesh_key = it->second;
346 sandesh_key.get_tag());
360 "contrail_route_resource") {
367 RouteMplsResourceMapSandesh sandesh_data;
368 return WriteMapToFile<RouteMplsResourceMapSandesh, Map> (&sandesh_data,
map_);
372 RouteMplsResourceMapSandesh sandesh_data;
373 ReadMapFromFile<RouteMplsResourceMapSandesh>(&sandesh_data,
backup_dir());
374 map_ = sandesh_data.get_index_map();
379 uint32_t index = it->first;
380 RouteMplsResource sandesh_key = it->second;
383 sandesh_key.get_vrf_name(),
384 sandesh_key.get_route_prefix()));
395 "contrail_interface_resource") {
402 InterfaceIndexResourceMapSandesh sandesh_data;
403 return WriteMapToFile<InterfaceIndexResourceMapSandesh, Map>
404 (&sandesh_data,
map_);
408 InterfaceIndexResourceMapSandesh sandesh_data;
409 ReadMapFromFile<InterfaceIndexResourceMapSandesh>
411 map_ = sandesh_data.get_index_map();
416 uint32_t index = it->first;
417 InterfaceIndexResource sandesh_key = it->second;
419 std::string
type = sandesh_key.get_type();
426 sandesh_key.get_name());
431 itf_nh_key =
new InterfaceNHKey(itf_key, sandesh_key.get_policy(),
432 sandesh_key.get_flags(), mac);
446 "contrail_vm_interface_resource") {
453 VmInterfaceIndexResourceMapSandesh sandesh_data;
454 return WriteMapToFile<VmInterfaceIndexResourceMapSandesh, Map>
455 (&sandesh_data,
map_);
459 VmInterfaceIndexResourceMapSandesh sandesh_data;
460 ReadMapFromFile<VmInterfaceIndexResourceMapSandesh>
462 map_ = sandesh_data.get_index_map();
467 uint32_t index = it->first;
468 VmInterfaceIndexResource sandesh_key = it->second;
473 sandesh_key.get_interface_name()));
484 "contrail_vrf_index_resource") {
491 VrfIndexResourceMapSandesh sandesh_data;
492 return WriteMapToFile<VrfIndexResourceMapSandesh, Map>
493 (&sandesh_data,
map_);
497 VrfIndexResourceMapSandesh sandesh_data;
498 ReadMapFromFile<VrfIndexResourceMapSandesh>
500 map_ = sandesh_data.get_index_map();
505 uint32_t index = it->first;
506 VrfIndexResource sandesh_key = it->second;
510 sandesh_key.get_vrf_name()));
522 "contrail_qos_resource") {
529 QosIndexResourceMapSandesh sandesh_data;
530 return WriteMapToFile<QosIndexResourceMapSandesh, Map>
531 (&sandesh_data,
map_);
535 QosIndexResourceMapSandesh sandesh_data;
536 ReadMapFromFile<QosIndexResourceMapSandesh>
538 map_ = sandesh_data.get_index_map();
543 uint32_t index = it->first;
544 QosIndexResource sandesh_key = it->second;
560 "contrail_bgp_as_service_resource") {
567 BgpAsServiceIndexResourceMapSandesh sandesh_data;
568 return WriteMapToFile<BgpAsServiceIndexResourceMapSandesh, Map>
569 (&sandesh_data,
map_);
573 BgpAsServiceIndexResourceMapSandesh sandesh_data;
574 ReadMapFromFile<BgpAsServiceIndexResourceMapSandesh>
576 map_ = sandesh_data.get_index_map();
581 uint32_t index = it->first;
582 BgpAsServiceIndexResource sandesh_key = it->second;
598 "contrail_mirror_index_resource") {
605 MirrorIndexResourceMapSandesh sandesh_data;
606 return WriteMapToFile<MirrorIndexResourceMapSandesh, Map>
607 (&sandesh_data,
map_);
611 MirrorIndexResourceMapSandesh sandesh_data;
612 ReadMapFromFile<MirrorIndexResourceMapSandesh>
614 map_ = sandesh_data.get_index_map();
619 uint32_t index = it->first;
620 MirrorIndexResource sandesh_key = it->second;
624 sandesh_key.get_analyzer_name()));
633 backup_manager_(manager), agent_(manager->agent()),
634 interface_mpls_index_table_(manager), vrf_mpls_index_table_(manager),
635 vlan_mpls_index_table_(manager), route_mpls_index_table_(manager),
636 vm_interface_index_table_(manager), vrf_index_table_ (manager),
637 qos_index_table_ (manager), bgp_as_service_index_table_(manager),
638 mirror_index_table_(manager) {
692 void VmInterfaceIndexResourceMapSandesh::Process(
SandeshContext*) {
703 void BgpAsServiceIndexResourceMapSandesh::Process(
SandeshContext*) {
712 InterfaceIndexResource data ) {
721 VrfMplsResource data) {
730 VlanMplsResource data) {
739 RouteMplsResource data) {
748 VmInterfaceIndexResource data ) {
757 VrfIndexResource data ) {
766 QosIndexResource data ) {
775 (uint32_t index, BgpAsServiceIndexResource data ) {
785 MirrorIndexResource data ) {
virtual bool WriteToFile()=0
void EnqueueRestore(KeyPtr key, DataPtr data)
pair< uint32_t, QosIndexResource > QosIndexResourcePair
VrfMplsBackUpResourceTable(ResourceBackupManager *manager)
void AddVrfResourceEntry(uint32_t index, VrfIndexResource data)
static boost::uuids::uuid StringToUuid(const std::string &str)
pair< uint32_t, VlanMplsResource > VlanMplsResourcePair
ResourceSandeshMaps(ResourceBackupManager *manager)
std::string file_name_prefix_
void AddRouteMplsResourceEntry(uint32_t index, RouteMplsResource data)
void DeleteInterfaceMplsResourceEntry(uint32_t index)
void AddVlanMplsResourceEntry(uint32_t index, VlanMplsResource data)
virtual ~QosBackUpResourceTable()
void DeleteVmInterfaceResourceEntry(uint32_t index)
void AddVrfMplsResourceEntry(uint32_t index, VrfMplsResource data)
static bool RenameFile(const std::string &file_tmp_name, const std::string &file_name)
virtual ~VmInterfaceBackUpResourceTable()
boost::shared_ptr< ResourceKey > KeyPtr
boost::asio::io_context * io_service()
virtual ~MirrorBackUpResourceTable()
void DeleteVrfResourceEntry(uint32_t index)
BgpAsServiceBackUpResourceTable bgp_as_service_index_table_
boost::shared_ptr< ResourceData > DataPtr
uint32_t backup_idle_timeout_
pair< uint32_t, RouteMplsResource > RouteMplsResourcePair
virtual ~RouteMplsBackUpResourceTable()
void AddQosResourceEntry(uint32_t index, QosIndexResource data)
virtual ~VrfMplsBackUpResourceTable()
const std::string & file_name_str()
uint64_t last_modified_time_
static const std::string FindFile(const std::string &root, const std::string &file_ext)
pair< uint32_t, BgpAsServiceIndexResource > BgpAsServiceIndexResourcePair
virtual ~ResourceSandeshMaps()
InterfaceMplsBackUpResourceTable(ResourceBackupManager *manager)
virtual ~VlanMplsBackUpResourceTable()
static const uint8_t kFallBackCount
VrfMplsBackUpResourceTable vrf_mpls_index_table_
BackUpResourceTable(ResourceBackupManager *manager, const std::string &name, const std::string &file_name)
QosBackUpResourceTable(ResourceBackupManager *manager)
pair< uint32_t, VmInterfaceIndexResource > VmInterfaceIndexResourcePair
VlanMplsBackUpResourceTable vlan_mpls_index_table_
TaskScheduler * task_scheduler() const
void DeleteVlanMplsResourceEntry(uint32_t index)
pair< uint32_t, MirrorIndexResource > MirrorIndexResourcePair
virtual ~BackUpResourceTable()
std::string file_name_str_
ResourceBackupManager * backup_manager()
VmInterfaceBackUpResourceTable(ResourceBackupManager *manager)
pair< uint32_t, VrfIndexResource > VrfIndexResourcePair
EventManager * event_manager() const
void EnqueueRestore(ResourceManager::KeyPtr key, ResourceManager::DataPtr data)
uint64_t restart_backup_idle_timeout() const
VrfBackUpResourceTable(ResourceBackupManager *manager)
void DeleteRouteMplsResourceEntry(uint32_t index)
VrfBackUpResourceTable vrf_index_table_
AgentParam * params() const
const std::string & backup_dir()
void DeleteQosResourceEntry(uint32_t index)
static Timer * CreateTimer(boost::asio::io_context &service, const std::string &name, int task_id=Timer::GetTimerTaskId(), int task_instance=Timer::GetTimerInstanceId(), bool delete_on_completion=false)
RouteMplsBackUpResourceTable(ResourceBackupManager *manager)
static const std::string TempFilePath(const std::string &file_name)
static uint32_t ReadResourceDataFromFile(const std::string &file_name, std::unique_ptr< uint8_t > *buf)
#define kAgentResourceBackUpTask
void DeleteMirrorResourceEntry(uint32_t index)
pair< uint32_t, VrfMplsResource > VrfMplsResourcePair
std::string AgentGUID() const
pair< uint32_t, InterfaceIndexResource > InterfaceMplsResourcePair
void AddBgpAsServiceResourceEntry(uint32_t index, BgpAsServiceIndexResource data)
InterfaceMplsBackUpResourceTable interface_mpls_index_table_
static uint64_t UTCTimestampUsec()
const std::string & restart_backup_dir() const
const std::string & file_name_prefix()
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
MirrorBackUpResourceTable(ResourceBackupManager *manager)
#define LOG(_Level, _Msg)
VmInterfaceBackUpResourceTable vm_interface_index_table_
void AddVmInterfaceResourceEntry(uint32_t index, VmInterfaceIndexResource data)
std::size_t hash_value(BgpAttr const &attr)
static MacAddress FromString(const std::string &str, boost::system::error_code *error=NULL)
void DeleteVrfMplsResourceEntry(uint32_t index)
RouteMplsBackUpResourceTable route_mpls_index_table_
QosBackUpResourceTable qos_index_table_
VlanMplsBackUpResourceTable(ResourceBackupManager *manager)
MirrorBackUpResourceTable mirror_index_table_
virtual ~VrfBackUpResourceTable()
void AddInterfaceMplsResourceEntry(uint32_t index, InterfaceIndexResource data)
virtual ~BgpAsServiceBackUpResourceTable()
void AddMirrorResourceEntry(uint32_t index, MirrorIndexResource data)
BgpAsServiceBackUpResourceTable(ResourceBackupManager *manager)
ResourceManager * resource_manager()
void ReadMapFromFile(T *sandesh_data, const std::string &root)
static bool CalculateHashSum(const std::string &file_name, uint32_t *hashsum)
bool WriteMapToFile(T1 *sandesh_data, const T2 &index_map)
ResourceManager * resource_manager() const
void DeleteBgpAsServiceResourceEntry(uint32_t index)
virtual ~InterfaceMplsBackUpResourceTable()
static bool DeleteTimer(Timer *Timer)