11 #include <boost/filesystem.hpp>
12 #include <boost/functional/hash.hpp>
13 #include <boost/algorithm/string.hpp>
16 #include <sandesh/sandesh_types.h>
17 #include <sandesh/sandesh.h>
31 const std::string &name,
32 const std::string &file_name) :
33 backup_manager_(manager), agent_(manager->agent()), name_(name),
108 std::stringstream temp_file_stream;
109 temp_file_stream << file_name <<
".tmp";
110 std::ofstream output;
112 output.open(temp_file_stream.str().c_str(),
113 std::ofstream::binary | std::ofstream::trunc);
114 if (!output.good()) {
115 LOG(ERROR,
"File open failed" << temp_file_stream.str());
117 return std::string();
121 return temp_file_stream.str();
126 const std::string &file_name) {
127 boost::system::error_code ec;
128 boost::filesystem::path tmp_file_path(file_tmp_name.c_str());
129 boost::filesystem::path backup_file_path(file_name.c_str());
130 boost::filesystem::rename(tmp_file_path, backup_file_path, ec);
132 LOG(ERROR,
"Resource backup mgr Rename file failed" << ec);
142 std::unique_ptr<uint8_t> buffer;
145 if (size && buffer.get()) {
146 *hashsum = (uint32_t)boost::hash_range(buffer.get(),
158 template <
typename T1,
typename T2>
160 const T2& index_map) {
161 uint32_t write_buff_size = 0;
165 if (temp_file.empty()) {
166 LOG(ERROR,
"Temp file is not created");
170 sandesh_data->set_index_map(index_map);
172 write_buff_size = sandesh_data->WriteBinaryToFile(temp_file, &error);
174 LOG(ERROR,
"Sandesh Write Binary failed " << write_buff_size);
184 std::remove(file_path.c_str());
187 std::stringstream file_path;
189 return RenameFile(temp_file, file_path.str());
200 const std::string & file_prefix) {
203 if ((dir_path = opendir(root.c_str())) == NULL) {
204 return std::string();
206 while ((dir = readdir(dir_path)) != NULL) {
207 std::string file_name = dir->d_name;
210 if (!file_name.find(file_prefix)) {
213 std::string tmpstr(file_name.c_str());
214 std::vector<string> tokens;
215 boost::split(tokens, tmpstr, boost::is_any_of(
"-"));
220 token = tokens[tokens.size() - 1];
224 for (
int i =0; token[i] !=
'\0'; i++) {
225 if (!isdigit(token[i])) {
238 return std::string();
244 template <
typename T>
246 const std::string &root) {
250 if (file_name.empty()) {
251 LOG(DEBUG,
"File name with prefix not found ");
255 std::stringstream file_path;
256 file_path << root <<
"/"<< file_name;
257 if (!boost::filesystem::exists( file_path.str().c_str())) {
258 LOG(DEBUG,
"File path not found " << file_path.str());
261 std::unique_ptr<uint8_t> buffer;
266 uint32_t hashsum = (uint32_t)boost::hash_range(buffer.get(),
271 if (std::string::npos !=
273 sandesh_data->ReadBinary(buffer.get(), size, &error);
275 LOG(ERROR,
"Sandesh Read Binary failed ");
285 "contrail_vrf_resource") {
292 VrfMplsResourceMapSandesh sandesh_data;
293 return WriteMapToFile<VrfMplsResourceMapSandesh, Map> (&sandesh_data,
map_);
297 VrfMplsResourceMapSandesh sandesh_data;
298 ReadMapFromFile<VrfMplsResourceMapSandesh>(&sandesh_data,
300 map_ = sandesh_data.get_index_map();
305 uint32_t index = it->first;
306 VrfMplsResource sandesh_key = it->second;
308 sandesh_key.get_bridge_nh());
322 "contrail_vlan_resource") {
329 VlanMplsResourceMapSandesh sandesh_data;
330 return WriteMapToFile<VlanMplsResourceMapSandesh, Map>
331 (&sandesh_data,
map_);
335 VlanMplsResourceMapSandesh sandesh_data;
336 ReadMapFromFile<VlanMplsResourceMapSandesh>(&sandesh_data,
338 map_ = sandesh_data.get_index_map();
343 uint32_t index = it->first;
344 VlanMplsResource sandesh_key = it->second;
347 sandesh_key.get_tag());
361 "contrail_route_resource") {
368 RouteMplsResourceMapSandesh sandesh_data;
369 return WriteMapToFile<RouteMplsResourceMapSandesh, Map> (&sandesh_data,
map_);
373 RouteMplsResourceMapSandesh sandesh_data;
374 ReadMapFromFile<RouteMplsResourceMapSandesh>(&sandesh_data,
backup_dir());
375 map_ = sandesh_data.get_index_map();
380 uint32_t index = it->first;
381 RouteMplsResource sandesh_key = it->second;
384 sandesh_key.get_vrf_name(),
385 sandesh_key.get_route_prefix()));
396 "contrail_interface_resource") {
403 InterfaceIndexResourceMapSandesh sandesh_data;
404 return WriteMapToFile<InterfaceIndexResourceMapSandesh, Map>
405 (&sandesh_data,
map_);
409 InterfaceIndexResourceMapSandesh sandesh_data;
410 ReadMapFromFile<InterfaceIndexResourceMapSandesh>
412 map_ = sandesh_data.get_index_map();
417 uint32_t index = it->first;
418 InterfaceIndexResource sandesh_key = it->second;
420 std::string
type = sandesh_key.get_type();
427 sandesh_key.get_name());
432 itf_nh_key =
new InterfaceNHKey(itf_key, sandesh_key.get_policy(),
433 sandesh_key.get_flags(), mac);
447 "contrail_vm_interface_resource") {
454 VmInterfaceIndexResourceMapSandesh sandesh_data;
455 return WriteMapToFile<VmInterfaceIndexResourceMapSandesh, Map>
456 (&sandesh_data,
map_);
460 VmInterfaceIndexResourceMapSandesh sandesh_data;
461 ReadMapFromFile<VmInterfaceIndexResourceMapSandesh>
463 map_ = sandesh_data.get_index_map();
468 uint32_t index = it->first;
469 VmInterfaceIndexResource sandesh_key = it->second;
474 sandesh_key.get_interface_name()));
485 "contrail_vrf_index_resource") {
492 VrfIndexResourceMapSandesh sandesh_data;
493 return WriteMapToFile<VrfIndexResourceMapSandesh, Map>
494 (&sandesh_data,
map_);
498 VrfIndexResourceMapSandesh sandesh_data;
499 ReadMapFromFile<VrfIndexResourceMapSandesh>
501 map_ = sandesh_data.get_index_map();
506 uint32_t index = it->first;
507 VrfIndexResource sandesh_key = it->second;
511 sandesh_key.get_vrf_name()));
523 "contrail_qos_resource") {
530 QosIndexResourceMapSandesh sandesh_data;
531 return WriteMapToFile<QosIndexResourceMapSandesh, Map>
532 (&sandesh_data,
map_);
536 QosIndexResourceMapSandesh sandesh_data;
537 ReadMapFromFile<QosIndexResourceMapSandesh>
539 map_ = sandesh_data.get_index_map();
544 uint32_t index = it->first;
545 QosIndexResource sandesh_key = it->second;
561 "contrail_bgp_as_service_resource") {
568 BgpAsServiceIndexResourceMapSandesh sandesh_data;
569 return WriteMapToFile<BgpAsServiceIndexResourceMapSandesh, Map>
570 (&sandesh_data,
map_);
574 BgpAsServiceIndexResourceMapSandesh sandesh_data;
575 ReadMapFromFile<BgpAsServiceIndexResourceMapSandesh>
577 map_ = sandesh_data.get_index_map();
582 uint32_t index = it->first;
583 BgpAsServiceIndexResource sandesh_key = it->second;
599 "contrail_mirror_index_resource") {
606 MirrorIndexResourceMapSandesh sandesh_data;
607 return WriteMapToFile<MirrorIndexResourceMapSandesh, Map>
608 (&sandesh_data,
map_);
612 MirrorIndexResourceMapSandesh sandesh_data;
613 ReadMapFromFile<MirrorIndexResourceMapSandesh>
615 map_ = sandesh_data.get_index_map();
620 uint32_t index = it->first;
621 MirrorIndexResource sandesh_key = it->second;
625 sandesh_key.get_analyzer_name()));
634 backup_manager_(manager), agent_(manager->agent()),
635 interface_mpls_index_table_(manager), vrf_mpls_index_table_(manager),
636 vlan_mpls_index_table_(manager), route_mpls_index_table_(manager),
637 vm_interface_index_table_(manager), vrf_index_table_ (manager),
638 qos_index_table_ (manager), bgp_as_service_index_table_(manager),
639 mirror_index_table_(manager) {
693 void VmInterfaceIndexResourceMapSandesh::Process(
SandeshContext*) {
704 void BgpAsServiceIndexResourceMapSandesh::Process(
SandeshContext*) {
713 InterfaceIndexResource data ) {
722 VrfMplsResource data) {
731 VlanMplsResource data) {
740 RouteMplsResource data) {
749 VmInterfaceIndexResource data ) {
758 VrfIndexResource data ) {
767 QosIndexResource data ) {
776 (uint32_t index, BgpAsServiceIndexResource data ) {
786 MirrorIndexResource data ) {
#define kAgentResourceBackUpTask
std::size_t hash_value(BgpAttr const &attr)
uint64_t restart_backup_idle_timeout() const
const std::string & restart_backup_dir() const
ResourceManager * resource_manager() const
AgentParam * params() const
EventManager * event_manager() const
std::string AgentGUID() const
TaskScheduler * task_scheduler() const
bool WriteMapToFile(T1 *sandesh_data, const T2 &index_map)
const std::string & file_name_str()
virtual bool WriteToFile()=0
static const std::string FindFile(const std::string &root, const std::string &file_ext)
virtual ~BackUpResourceTable()
uint32_t backup_idle_timeout_
uint64_t last_modified_time_
void ReadMapFromFile(T *sandesh_data, const std::string &root)
std::string file_name_prefix_
void EnqueueRestore(ResourceManager::KeyPtr key, ResourceManager::DataPtr data)
const std::string & backup_dir()
std::string file_name_str_
static const uint8_t kFallBackCount
static bool CalculateHashSum(const std::string &file_name, uint32_t *hashsum)
BackUpResourceTable(ResourceBackupManager *manager, const std::string &name, const std::string &file_name)
ResourceBackupManager * backup_manager()
const std::string & file_name_prefix()
BgpAsServiceBackUpResourceTable(ResourceBackupManager *manager)
virtual ~BgpAsServiceBackUpResourceTable()
boost::asio::io_context * io_service()
virtual ~InterfaceMplsBackUpResourceTable()
InterfaceMplsBackUpResourceTable(ResourceBackupManager *manager)
static MacAddress FromString(const std::string &str, boost::system::error_code *error=NULL)
MirrorBackUpResourceTable(ResourceBackupManager *manager)
virtual ~MirrorBackUpResourceTable()
virtual ~QosBackUpResourceTable()
QosBackUpResourceTable(ResourceBackupManager *manager)
static uint32_t ReadResourceDataFromFile(const std::string &file_name, std::unique_ptr< uint8_t > *buf)
ResourceManager * resource_manager()
void EnqueueRestore(KeyPtr key, DataPtr data)
boost::shared_ptr< ResourceKey > KeyPtr
boost::shared_ptr< ResourceData > DataPtr
void DeleteBgpAsServiceResourceEntry(uint32_t index)
void DeleteVrfResourceEntry(uint32_t index)
pair< uint32_t, VlanMplsResource > VlanMplsResourcePair
void AddRouteMplsResourceEntry(uint32_t index, RouteMplsResource data)
pair< uint32_t, RouteMplsResource > RouteMplsResourcePair
pair< uint32_t, InterfaceIndexResource > InterfaceMplsResourcePair
BgpAsServiceBackUpResourceTable bgp_as_service_index_table_
QosBackUpResourceTable qos_index_table_
void AddVlanMplsResourceEntry(uint32_t index, VlanMplsResource data)
virtual ~ResourceSandeshMaps()
pair< uint32_t, BgpAsServiceIndexResource > BgpAsServiceIndexResourcePair
void DeleteVrfMplsResourceEntry(uint32_t index)
void DeleteQosResourceEntry(uint32_t index)
void AddVrfMplsResourceEntry(uint32_t index, VrfMplsResource data)
void AddMirrorResourceEntry(uint32_t index, MirrorIndexResource data)
void AddBgpAsServiceResourceEntry(uint32_t index, BgpAsServiceIndexResource data)
VrfBackUpResourceTable vrf_index_table_
pair< uint32_t, VmInterfaceIndexResource > VmInterfaceIndexResourcePair
VrfMplsBackUpResourceTable vrf_mpls_index_table_
pair< uint32_t, VrfMplsResource > VrfMplsResourcePair
void AddInterfaceMplsResourceEntry(uint32_t index, InterfaceIndexResource data)
VmInterfaceBackUpResourceTable vm_interface_index_table_
VlanMplsBackUpResourceTable vlan_mpls_index_table_
MirrorBackUpResourceTable mirror_index_table_
void DeleteMirrorResourceEntry(uint32_t index)
RouteMplsBackUpResourceTable route_mpls_index_table_
void AddVmInterfaceResourceEntry(uint32_t index, VmInterfaceIndexResource data)
void DeleteVlanMplsResourceEntry(uint32_t index)
InterfaceMplsBackUpResourceTable interface_mpls_index_table_
void DeleteInterfaceMplsResourceEntry(uint32_t index)
void AddVrfResourceEntry(uint32_t index, VrfIndexResource data)
pair< uint32_t, QosIndexResource > QosIndexResourcePair
ResourceSandeshMaps(ResourceBackupManager *manager)
pair< uint32_t, VrfIndexResource > VrfIndexResourcePair
void DeleteRouteMplsResourceEntry(uint32_t index)
pair< uint32_t, MirrorIndexResource > MirrorIndexResourcePair
void DeleteVmInterfaceResourceEntry(uint32_t index)
void AddQosResourceEntry(uint32_t index, QosIndexResource data)
RouteMplsBackUpResourceTable(ResourceBackupManager *manager)
virtual ~RouteMplsBackUpResourceTable()
static bool DeleteTimer(Timer *Timer)
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)
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
VlanMplsBackUpResourceTable(ResourceBackupManager *manager)
virtual ~VlanMplsBackUpResourceTable()
VmInterfaceBackUpResourceTable(ResourceBackupManager *manager)
virtual ~VmInterfaceBackUpResourceTable()
virtual ~VrfBackUpResourceTable()
VrfBackUpResourceTable(ResourceBackupManager *manager)
VrfMplsBackUpResourceTable(ResourceBackupManager *manager)
virtual ~VrfMplsBackUpResourceTable()
#define LOG(_Level, _Msg)
static const std::string TempFilePath(const std::string &file_name)
static bool RenameFile(const std::string &file_tmp_name, const std::string &file_name)
static boost::uuids::uuid StringToUuid(const std::string &str)
static uint64_t UTCTimestampUsec()