Line data Source code
1 : /*
2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3 : */
4 :
5 : #include <fstream>
6 : #include <dirent.h>
7 : #include <cctype>
8 : #include <stdlib.h>
9 : #include <cmn/agent.h>
10 : #include <boost/filesystem.hpp>
11 : #include <boost/functional/hash.hpp>
12 : #include <boost/algorithm/string.hpp>
13 : #include "init/agent_param.h"
14 : #include <base/timer.h>
15 : #include <sandesh/sandesh_types.h>
16 : #include <sandesh/sandesh.h>
17 : #include "resource_manager/resource_backup.h"
18 : #include "resource_manager/resource_table.h"
19 : #include "resource_manager/sandesh_map.h"
20 : #include "resource_manager/resource_manager.h"
21 : #include "resource_manager/mpls_index.h"
22 : #include "resource_manager/vm_interface_index.h"
23 : #include "resource_manager/vrf_index.h"
24 : #include "resource_manager/qos_index.h"
25 : #include "resource_manager/bgp_as_service_index.h"
26 : #include "resource_manager/mirror_index.h"
27 : #include <oper/nexthop.h>
28 :
29 0 : BackUpResourceTable::BackUpResourceTable(ResourceBackupManager *manager,
30 : const std::string &name,
31 0 : const std::string &file_name) :
32 0 : backup_manager_(manager), agent_(manager->agent()), name_(name),
33 0 : last_modified_time_(UTCTimestampUsec()) {
34 :
35 0 : if (!agent_->isMockMode()) {
36 0 : backup_dir_ = agent_->params()->restart_backup_dir();
37 : }
38 : else {
39 0 : backup_dir_ = "/tmp/" + agent_->AgentGUID() +
40 0 : agent_->params()->restart_backup_dir();
41 : }
42 :
43 0 : backup_idle_timeout_ = agent_->params()
44 0 : ->restart_backup_idle_timeout();
45 0 : file_name_str_ = backup_dir_ + "/" + file_name;
46 0 : file_name_prefix_ = file_name + "-";
47 0 : boost::filesystem::path dir(backup_dir_.c_str());
48 0 : if (!boost::filesystem::exists(backup_dir_))
49 0 : boost::filesystem::create_directory(backup_dir_);
50 0 : timer_ = TimerManager::CreateTimer(*(agent_->event_manager()->io_service()),
51 : name,
52 0 : agent_->task_scheduler()->
53 : GetTaskId(kAgentResourceBackUpTask));
54 0 : }
55 :
56 0 : BackUpResourceTable::~BackUpResourceTable() {
57 0 : timer_->Cancel();
58 0 : TimerManager::DeleteTimer(timer_);
59 0 : }
60 :
61 0 : bool BackUpResourceTable::TimerExpiry() {
62 : // Check for Update required otherwise wait for fallback time.
63 0 : if (UpdateRequired() || fall_back_count_ >= kFallBackCount) {
64 0 : if (WriteToFile()) {
65 0 : last_modified_time_ = UTCTimestampUsec();
66 0 : fall_back_count_ = 0;
67 0 : return false;
68 : }
69 : }
70 0 : fall_back_count_++;
71 0 : return true;
72 : }
73 :
74 0 : void BackUpResourceTable::StartTimer() {
75 0 : timer_->Start(backup_idle_timeout_,
76 : boost::bind(&BackUpResourceTable::TimerExpiry, this));
77 0 : }
78 :
79 : // Don't Update the file if there is any activity seen with in
80 : // backup_idle_timeout_ and start the fallback count so that
81 : // after 6th itteration file can be updated.
82 : // if write to file fails trigger the timer.
83 0 : void BackUpResourceTable::TriggerBackup() {
84 0 : if (timer_->running() == false)
85 : //Start Fallback timer.
86 0 : StartTimer();
87 0 : last_modified_time_ = UTCTimestampUsec();
88 0 : }
89 :
90 0 : bool BackUpResourceTable::UpdateRequired() {
91 0 : uint64_t current_time_stamp = UTCTimestampUsec();
92 : // dont update the file if the frequent updates are seen with in 10sec.
93 0 : if (current_time_stamp - last_modified_time_ <
94 0 : backup_idle_timeout_) {
95 0 : return false;
96 : }
97 0 : return true;
98 : }
99 :
100 0 : void BackUpResourceTable::EnqueueRestore(ResourceManager::KeyPtr key,
101 : ResourceManager::DataPtr data) {
102 0 : backup_manager()->resource_manager()->EnqueueRestore(key, data);
103 0 : }
104 :
105 : // Write the content to file temprory file and rename the file
106 0 : static const std::string TempFilePath(const std::string &file_name) {
107 0 : std::stringstream temp_file_stream;
108 0 : temp_file_stream << file_name << ".tmp";
109 0 : std::ofstream output;
110 : // Trunctate the file as it is a fresh write for the modified Map
111 0 : output.open(temp_file_stream.str().c_str(),
112 : std::ofstream::binary | std::ofstream::trunc);
113 0 : if (!output.good()) {
114 0 : LOG(ERROR, "File open failed" << temp_file_stream.str());
115 0 : output.close();
116 0 : return std::string();
117 : }
118 :
119 0 : output.close();
120 0 : return temp_file_stream.str();
121 0 : }
122 :
123 : // Rename the Temp file
124 0 : static bool RenameFile(const std::string &file_tmp_name,
125 : const std::string &file_name) {
126 0 : boost::system::error_code ec;
127 0 : boost::filesystem::path tmp_file_path(file_tmp_name.c_str());
128 0 : boost::filesystem::path backup_file_path(file_name.c_str());
129 0 : boost::filesystem::rename(tmp_file_path, backup_file_path, ec);
130 0 : if (ec.failed()) {
131 0 : LOG(ERROR, "Resource backup mgr Rename file failed" << ec);
132 0 : return false;
133 : }
134 0 : return true;
135 0 : }
136 :
137 : // Calulate the Hash value for the stored file
138 : // This hash sum will be validated while reading the content.
139 0 : bool BackUpResourceTable::CalculateHashSum(const std::string &file_name,
140 : uint32_t *hashsum) {
141 0 : std::unique_ptr<uint8_t> buffer;
142 0 : uint32_t size = ResourceBackupManager::ReadResourceDataFromFile(file_name,
143 : &(buffer));
144 0 : if (size && buffer.get()) {
145 0 : *hashsum = (uint32_t)boost::hash_range(buffer.get(),
146 0 : buffer.get()+size);
147 0 : return true;
148 : }
149 0 : return false;
150 0 : }
151 :
152 : // Type T1 is Final output sandesh structure writes in to file
153 : // Type T2 index map for the specific table
154 : // Write the Map to file
155 : // Calculate the hashsum and append that to file name
156 : // so that validated while reading file
157 : template <typename T1, typename T2>
158 0 : bool BackUpResourceTable::WriteMapToFile(T1* sandesh_data,
159 : const T2& index_map) {
160 0 : uint32_t write_buff_size = 0;
161 0 : int error = 0;
162 :
163 0 : const std::string temp_file = TempFilePath(file_name_str());
164 0 : if (temp_file.empty()) {
165 0 : LOG(ERROR, "Temp file is not created");
166 0 : return false;
167 : }
168 :
169 0 : sandesh_data->set_index_map(index_map);
170 0 : sandesh_data->set_time_stamp(UTCTimestampUsec());
171 0 : write_buff_size = sandesh_data->WriteBinaryToFile(temp_file, &error);
172 0 : if (error != 0) {
173 0 : LOG(ERROR, "Sandesh Write Binary failed " << write_buff_size);
174 0 : return false;
175 : }
176 :
177 : uint32_t hashsum;
178 0 : if (CalculateHashSum(temp_file, &hashsum)) {
179 : // remove the file with existing hashsum extention.
180 0 : std::string file = FindFile(backup_dir(), file_name_prefix());
181 0 : if (!file.empty()) {
182 0 : std::string file_path = backup_dir_ + "/" + file;
183 0 : std::remove(file_path.c_str());
184 0 : }
185 : // rename the tmp file to new file by appending hashsum
186 0 : std::stringstream file_path;
187 0 : file_path << file_name_str() << "-" << hashsum;
188 0 : return RenameFile(temp_file, file_path.str());
189 0 : }
190 :
191 0 : return false;
192 0 : }
193 :
194 : // TODO final file format needs to be defined along with 3rd backup file.
195 : // function needs to be enhanced with 3rd backup file.
196 : // Find the file with the prefix.
197 : const std::string
198 0 : BackUpResourceTable::FindFile(const std::string &root,
199 : const std::string & file_prefix) {
200 : DIR *dir_path;
201 : struct dirent *dir;
202 0 : if ((dir_path = opendir(root.c_str())) == NULL) {
203 0 : return std::string();
204 : }
205 0 : while ((dir = readdir(dir_path)) != NULL) {
206 0 : std::string file_name = dir->d_name;
207 : // Match with the file prefix name if is not there return empty string
208 : // Check Start of the file_name matches with prefix name.
209 0 : if (!file_name.find(file_prefix)) {
210 : // check for file format filename-hashvalue(digits)
211 : // example name contrail_interface_resource-12345678
212 0 : std::string tmpstr(file_name.c_str());
213 0 : std::vector<string> tokens;
214 0 : boost::split(tokens, tmpstr, boost::is_any_of("-"));
215 : // split the string check after file prefix hashsum is number.
216 0 : std::string token;
217 0 : if (tokens.size()) {
218 : // TODO file format changes this needs to be revisited.
219 0 : token = tokens[tokens.size() - 1];
220 : }
221 :
222 0 : bool found = true;
223 0 : for (int i =0; token[i] != '\0'; i++) {
224 0 : if (!isdigit(token[i])) {
225 0 : found = false;
226 0 : break;
227 : }
228 : }
229 :
230 0 : if (found) {
231 0 : closedir(dir_path);
232 0 : return file_name;
233 : }
234 0 : }
235 0 : }
236 0 : closedir(dir_path);
237 0 : return std::string();
238 : }
239 :
240 : // Read Map from the file.
241 : // First read the file to a buffer
242 : // verify that hash sum matches
243 : template <typename T>
244 0 : void BackUpResourceTable::ReadMapFromFile(T* sandesh_data,
245 : const std::string &root) {
246 : // Find the File with prefix name
247 0 : const std::string file_name = FindFile(root, file_name_prefix());
248 0 : int error = 0;
249 0 : if (file_name.empty()) {
250 0 : LOG(DEBUG, "File name with prefix not found ");
251 0 : return;
252 : }
253 : // Make the complete file path with hash value
254 0 : std::stringstream file_path;
255 0 : file_path << root << "/"<< file_name;
256 0 : if (!boost::filesystem::exists( file_path.str().c_str())) {
257 0 : LOG(DEBUG, "File path not found " << file_path.str());
258 0 : return;
259 : }
260 0 : std::unique_ptr<uint8_t> buffer;
261 0 : uint32_t size = ResourceBackupManager::ReadResourceDataFromFile(file_path.str(),
262 : &(buffer));
263 0 : if (buffer.get()) {
264 0 : if (size) {
265 0 : uint32_t hashsum = (uint32_t)boost::hash_range(buffer.get(),
266 0 : buffer.get()+size);
267 0 : std::stringstream hash_value;
268 0 : hash_value << hashsum;
269 : // Check for hashsum present.
270 0 : if (std::string::npos !=
271 0 : file_name.find(hash_value.str())) {
272 0 : sandesh_data->ReadBinary(buffer.get(), size, &error);
273 0 : if (error != 0) {
274 0 : LOG(ERROR, "Sandesh Read Binary failed ");
275 : }
276 : }
277 0 : }
278 : }
279 0 : }
280 :
281 0 : VrfMplsBackUpResourceTable::VrfMplsBackUpResourceTable
282 0 : (ResourceBackupManager *manager) :
283 : BackUpResourceTable(manager, "VrfMplsBackUpResourceTable",
284 0 : "contrail_vrf_resource") {
285 0 : }
286 :
287 0 : VrfMplsBackUpResourceTable::~VrfMplsBackUpResourceTable() {
288 0 : }
289 :
290 0 : bool VrfMplsBackUpResourceTable::WriteToFile() {
291 0 : VrfMplsResourceMapSandesh sandesh_data;
292 0 : return WriteMapToFile<VrfMplsResourceMapSandesh, Map> (&sandesh_data, map_);
293 0 : }
294 :
295 0 : void VrfMplsBackUpResourceTable::ReadFromFile() {
296 0 : VrfMplsResourceMapSandesh sandesh_data;
297 0 : ReadMapFromFile<VrfMplsResourceMapSandesh>(&sandesh_data,
298 : backup_dir());
299 0 : map_ = sandesh_data.get_index_map();
300 0 : }
301 :
302 0 : void VrfMplsBackUpResourceTable::RestoreResource() {
303 0 : for (MapIter it = map_.begin(); it != map_.end(); it++) {
304 0 : uint32_t index = it->first;
305 0 : VrfMplsResource sandesh_key = it->second;
306 0 : VrfNHKey *vrf_nh_key = new VrfNHKey(sandesh_key.get_name(), false,
307 0 : sandesh_key.get_bridge_nh());
308 : ResourceManager::KeyPtr key(new NexthopIndexResourceKey(
309 0 : backup_manager()->resource_manager(),
310 0 : vrf_nh_key));
311 : ResourceManager::DataPtr data(new IndexResourceData
312 0 : (backup_manager()->resource_manager(),
313 0 : index));
314 0 : EnqueueRestore(key, data);
315 0 : }
316 0 : }
317 :
318 0 : VlanMplsBackUpResourceTable::VlanMplsBackUpResourceTable
319 0 : (ResourceBackupManager *manager) :
320 : BackUpResourceTable(manager, "VlanMplsBackUpResourceTable",
321 0 : "contrail_vlan_resource") {
322 0 : }
323 :
324 0 : VlanMplsBackUpResourceTable::~VlanMplsBackUpResourceTable() {
325 0 : }
326 :
327 0 : bool VlanMplsBackUpResourceTable::WriteToFile() {
328 0 : VlanMplsResourceMapSandesh sandesh_data;
329 : return WriteMapToFile<VlanMplsResourceMapSandesh, Map>
330 0 : (&sandesh_data, map_);
331 0 : }
332 :
333 0 : void VlanMplsBackUpResourceTable::ReadFromFile() {
334 0 : VlanMplsResourceMapSandesh sandesh_data;
335 0 : ReadMapFromFile<VlanMplsResourceMapSandesh>(&sandesh_data,
336 : backup_dir());
337 0 : map_ = sandesh_data.get_index_map();
338 0 : }
339 :
340 0 : void VlanMplsBackUpResourceTable::RestoreResource() {
341 0 : for (MapIter it = map_.begin(); it != map_.end(); it++) {
342 0 : uint32_t index = it->first;
343 0 : VlanMplsResource sandesh_key = it->second;
344 : VlanNHKey *vlan_nh_key = new VlanNHKey(
345 0 : StringToUuid(sandesh_key.get_uuid()),
346 0 : sandesh_key.get_tag());
347 : ResourceManager::KeyPtr key(new NexthopIndexResourceKey(
348 0 : backup_manager()->resource_manager(),
349 0 : vlan_nh_key));
350 : ResourceManager::DataPtr data(new IndexResourceData
351 0 : (backup_manager()->resource_manager(),
352 0 : index));
353 0 : EnqueueRestore(key, data);
354 0 : }
355 0 : }
356 :
357 0 : RouteMplsBackUpResourceTable::RouteMplsBackUpResourceTable
358 0 : (ResourceBackupManager *manager) :
359 : BackUpResourceTable(manager, "RouteMplsBackUpResourceTable",
360 0 : "contrail_route_resource") {
361 0 : }
362 :
363 0 : RouteMplsBackUpResourceTable::~RouteMplsBackUpResourceTable() {
364 0 : }
365 :
366 0 : bool RouteMplsBackUpResourceTable::WriteToFile() {
367 0 : RouteMplsResourceMapSandesh sandesh_data;
368 0 : return WriteMapToFile<RouteMplsResourceMapSandesh, Map> (&sandesh_data, map_);
369 0 : }
370 :
371 0 : void RouteMplsBackUpResourceTable::ReadFromFile() {
372 0 : RouteMplsResourceMapSandesh sandesh_data;
373 0 : ReadMapFromFile<RouteMplsResourceMapSandesh>(&sandesh_data, backup_dir());
374 0 : map_ = sandesh_data.get_index_map();
375 0 : }
376 :
377 0 : void RouteMplsBackUpResourceTable::RestoreResource() {
378 0 : for (MapIter it = map_.begin(); it != map_.end(); it++) {
379 0 : uint32_t index = it->first;
380 0 : RouteMplsResource sandesh_key = it->second;
381 : ResourceManager::KeyPtr key(new RouteMplsResourceKey
382 0 : (backup_manager()->resource_manager(),
383 0 : sandesh_key.get_vrf_name(),
384 0 : sandesh_key.get_route_prefix()));
385 : ResourceManager::DataPtr data(new IndexResourceData
386 0 : (backup_manager()->resource_manager(),
387 0 : index));
388 0 : EnqueueRestore(key, data);
389 0 : }
390 0 : }
391 :
392 0 : InterfaceMplsBackUpResourceTable::InterfaceMplsBackUpResourceTable
393 0 : (ResourceBackupManager *manager) :
394 : BackUpResourceTable(manager, "InterfaceMplsBackUpResourceTable",
395 0 : "contrail_interface_resource") {
396 0 : }
397 :
398 0 : InterfaceMplsBackUpResourceTable::~InterfaceMplsBackUpResourceTable() {
399 0 : }
400 :
401 0 : bool InterfaceMplsBackUpResourceTable::WriteToFile() {
402 0 : InterfaceIndexResourceMapSandesh sandesh_data;
403 : return WriteMapToFile<InterfaceIndexResourceMapSandesh, Map>
404 0 : (&sandesh_data, map_);
405 0 : }
406 :
407 0 : void InterfaceMplsBackUpResourceTable::ReadFromFile() {
408 0 : InterfaceIndexResourceMapSandesh sandesh_data;
409 : ReadMapFromFile<InterfaceIndexResourceMapSandesh>
410 0 : (&sandesh_data, backup_dir());
411 0 : map_ = sandesh_data.get_index_map();
412 0 : }
413 :
414 0 : void InterfaceMplsBackUpResourceTable::RestoreResource() {
415 0 : for (MapIter it = map_.begin(); it != map_.end(); it++) {
416 0 : uint32_t index = it->first;
417 0 : InterfaceIndexResource sandesh_key = it->second;
418 0 : MacAddress mac = MacAddress::FromString(sandesh_key.get_mac());
419 0 : std::string type = sandesh_key.get_type();
420 0 : InterfaceNHKey *itf_nh_key = NULL;
421 0 : InterfaceKey *itf_key = NULL;
422 0 : if (type == "vmi") {
423 : //Vm interface
424 0 : itf_key = new VmInterfaceKey(AgentKey::ADD_DEL_CHANGE,
425 0 : StringToUuid(sandesh_key.get_uuid()),
426 0 : sandesh_key.get_name());
427 : } else {
428 : //Inet interface
429 0 : itf_key = new InetInterfaceKey(sandesh_key.get_name());
430 : }
431 0 : itf_nh_key = new InterfaceNHKey(itf_key, sandesh_key.get_policy(),
432 0 : sandesh_key.get_flags(), mac);
433 : ResourceManager::KeyPtr key(new NexthopIndexResourceKey(
434 0 : backup_manager()->resource_manager(),
435 0 : itf_nh_key));
436 : ResourceManager::DataPtr data(new IndexResourceData
437 0 : (backup_manager()->resource_manager(),
438 0 : index));
439 0 : EnqueueRestore(key, data);
440 0 : }
441 0 : }
442 : // Vm interface backup.
443 0 : VmInterfaceBackUpResourceTable::VmInterfaceBackUpResourceTable
444 0 : (ResourceBackupManager *manager) :
445 : BackUpResourceTable(manager, "VmInterfaceBackUpResourceTable",
446 0 : "contrail_vm_interface_resource") {
447 0 : }
448 :
449 0 : VmInterfaceBackUpResourceTable::~VmInterfaceBackUpResourceTable() {
450 0 : }
451 :
452 0 : bool VmInterfaceBackUpResourceTable::WriteToFile() {
453 0 : VmInterfaceIndexResourceMapSandesh sandesh_data;
454 : return WriteMapToFile<VmInterfaceIndexResourceMapSandesh, Map>
455 0 : (&sandesh_data, map_);
456 0 : }
457 :
458 0 : void VmInterfaceBackUpResourceTable::ReadFromFile() {
459 0 : VmInterfaceIndexResourceMapSandesh sandesh_data;
460 : ReadMapFromFile<VmInterfaceIndexResourceMapSandesh>
461 0 : (&sandesh_data, backup_dir());
462 0 : map_ = sandesh_data.get_index_map();
463 0 : }
464 :
465 0 : void VmInterfaceBackUpResourceTable::RestoreResource() {
466 0 : for (MapIter it = map_.begin(); it != map_.end(); it++) {
467 0 : uint32_t index = it->first;
468 0 : VmInterfaceIndexResource sandesh_key = it->second;
469 :
470 : ResourceManager::KeyPtr key(new VmInterfaceIndexResourceKey(
471 0 : backup_manager()->resource_manager(),
472 0 : StringToUuid(sandesh_key.get_uuid()),
473 0 : sandesh_key.get_interface_name()));
474 : ResourceManager::DataPtr data(new IndexResourceData
475 0 : (backup_manager()->resource_manager(),
476 0 : index));
477 0 : EnqueueRestore(key, data);
478 0 : }
479 0 : }
480 : // Vrf backup.
481 0 : VrfBackUpResourceTable::VrfBackUpResourceTable
482 0 : (ResourceBackupManager *manager) :
483 : BackUpResourceTable(manager, "VrfBackUpResourceTable",
484 0 : "contrail_vrf_index_resource") {
485 0 : }
486 :
487 0 : VrfBackUpResourceTable::~VrfBackUpResourceTable() {
488 0 : }
489 :
490 0 : bool VrfBackUpResourceTable::WriteToFile() {
491 0 : VrfIndexResourceMapSandesh sandesh_data;
492 : return WriteMapToFile<VrfIndexResourceMapSandesh, Map>
493 0 : (&sandesh_data, map_);
494 0 : }
495 :
496 0 : void VrfBackUpResourceTable::ReadFromFile() {
497 0 : VrfIndexResourceMapSandesh sandesh_data;
498 : ReadMapFromFile<VrfIndexResourceMapSandesh>
499 0 : (&sandesh_data, backup_dir());
500 0 : map_ = sandesh_data.get_index_map();
501 0 : }
502 :
503 0 : void VrfBackUpResourceTable::RestoreResource() {
504 0 : for (MapIter it = map_.begin(); it != map_.end(); it++) {
505 0 : uint32_t index = it->first;
506 0 : VrfIndexResource sandesh_key = it->second;
507 :
508 : ResourceManager::KeyPtr key(new VrfIndexResourceKey(
509 0 : backup_manager()->resource_manager(),
510 0 : sandesh_key.get_vrf_name()));
511 : ResourceManager::DataPtr data(new IndexResourceData
512 0 : (backup_manager()->resource_manager(),
513 0 : index));
514 0 : EnqueueRestore(key, data);
515 0 : }
516 0 : }
517 :
518 : // Qos id backup.
519 0 : QosBackUpResourceTable::QosBackUpResourceTable
520 0 : (ResourceBackupManager *manager) :
521 : BackUpResourceTable(manager, "QosBackUpResourceTable",
522 0 : "contrail_qos_resource") {
523 0 : }
524 :
525 0 : QosBackUpResourceTable::~QosBackUpResourceTable() {
526 0 : }
527 :
528 0 : bool QosBackUpResourceTable::WriteToFile() {
529 0 : QosIndexResourceMapSandesh sandesh_data;
530 : return WriteMapToFile<QosIndexResourceMapSandesh, Map>
531 0 : (&sandesh_data, map_);
532 0 : }
533 :
534 0 : void QosBackUpResourceTable::ReadFromFile() {
535 0 : QosIndexResourceMapSandesh sandesh_data;
536 : ReadMapFromFile<QosIndexResourceMapSandesh>
537 0 : (&sandesh_data, backup_dir());
538 0 : map_ = sandesh_data.get_index_map();
539 0 : }
540 :
541 0 : void QosBackUpResourceTable::RestoreResource() {
542 0 : for (MapIter it = map_.begin(); it != map_.end(); it++) {
543 0 : uint32_t index = it->first;
544 0 : QosIndexResource sandesh_key = it->second;
545 :
546 : ResourceManager::KeyPtr key(new QosIndexResourceKey(
547 0 : backup_manager()->resource_manager(),
548 0 : StringToUuid(sandesh_key.get_uuid())));
549 : ResourceManager::DataPtr data(new IndexResourceData
550 0 : (backup_manager()->resource_manager(),
551 0 : index));
552 0 : EnqueueRestore(key, data);
553 0 : }
554 0 : }
555 :
556 : // bgp as service id backup.
557 0 : BgpAsServiceBackUpResourceTable::BgpAsServiceBackUpResourceTable
558 0 : (ResourceBackupManager *manager) :
559 : BackUpResourceTable(manager, "BgpAsServiceBackUpResourceTable",
560 0 : "contrail_bgp_as_service_resource") {
561 0 : }
562 :
563 0 : BgpAsServiceBackUpResourceTable::~BgpAsServiceBackUpResourceTable() {
564 0 : }
565 :
566 0 : bool BgpAsServiceBackUpResourceTable::WriteToFile() {
567 0 : BgpAsServiceIndexResourceMapSandesh sandesh_data;
568 : return WriteMapToFile<BgpAsServiceIndexResourceMapSandesh, Map>
569 0 : (&sandesh_data, map_);
570 0 : }
571 :
572 0 : void BgpAsServiceBackUpResourceTable::ReadFromFile() {
573 0 : BgpAsServiceIndexResourceMapSandesh sandesh_data;
574 : ReadMapFromFile<BgpAsServiceIndexResourceMapSandesh>
575 0 : (&sandesh_data, backup_dir());
576 0 : map_ = sandesh_data.get_index_map();
577 0 : }
578 :
579 0 : void BgpAsServiceBackUpResourceTable::RestoreResource() {
580 0 : for (MapIter it = map_.begin(); it != map_.end(); it++) {
581 0 : uint32_t index = it->first;
582 0 : BgpAsServiceIndexResource sandesh_key = it->second;
583 :
584 : ResourceManager::KeyPtr key(new BgpAsServiceIndexResourceKey(
585 0 : backup_manager()->resource_manager(),
586 0 : StringToUuid(sandesh_key.get_uuid())));
587 : ResourceManager::DataPtr data(new IndexResourceData
588 0 : (backup_manager()->resource_manager(),
589 0 : index));
590 0 : EnqueueRestore(key, data);
591 0 : }
592 0 : }
593 :
594 : // Mirror backup.
595 0 : MirrorBackUpResourceTable::MirrorBackUpResourceTable
596 0 : (ResourceBackupManager *manager) :
597 : BackUpResourceTable(manager, "MirrorBackUpResourceTable",
598 0 : "contrail_mirror_index_resource") {
599 0 : }
600 :
601 0 : MirrorBackUpResourceTable::~MirrorBackUpResourceTable() {
602 0 : }
603 :
604 0 : bool MirrorBackUpResourceTable::WriteToFile() {
605 0 : MirrorIndexResourceMapSandesh sandesh_data;
606 : return WriteMapToFile<MirrorIndexResourceMapSandesh, Map>
607 0 : (&sandesh_data, map_);
608 0 : }
609 :
610 0 : void MirrorBackUpResourceTable::ReadFromFile() {
611 0 : MirrorIndexResourceMapSandesh sandesh_data;
612 : ReadMapFromFile<MirrorIndexResourceMapSandesh>
613 0 : (&sandesh_data, backup_dir());
614 0 : map_ = sandesh_data.get_index_map();
615 0 : }
616 :
617 0 : void MirrorBackUpResourceTable::RestoreResource() {
618 0 : for (MapIter it = map_.begin(); it != map_.end(); it++) {
619 0 : uint32_t index = it->first;
620 0 : MirrorIndexResource sandesh_key = it->second;
621 :
622 : ResourceManager::KeyPtr key(new MirrorIndexResourceKey(
623 0 : backup_manager()->resource_manager(),
624 0 : sandesh_key.get_analyzer_name()));
625 : ResourceManager::DataPtr data(new IndexResourceData
626 0 : (backup_manager()->resource_manager(),
627 0 : index));
628 0 : EnqueueRestore(key, data);
629 0 : }
630 0 : }
631 :
632 0 : ResourceSandeshMaps::ResourceSandeshMaps(ResourceBackupManager *manager) :
633 0 : backup_manager_(manager), agent_(manager->agent()),
634 0 : interface_mpls_index_table_(manager), vrf_mpls_index_table_(manager),
635 0 : vlan_mpls_index_table_(manager), route_mpls_index_table_(manager),
636 0 : vm_interface_index_table_(manager), vrf_index_table_ (manager),
637 0 : qos_index_table_ (manager), bgp_as_service_index_table_(manager),
638 0 : mirror_index_table_(manager) {
639 0 : }
640 :
641 0 : ResourceSandeshMaps::~ResourceSandeshMaps() {
642 0 : }
643 :
644 0 : void ResourceSandeshMaps::ReadFromFile() {
645 0 : interface_mpls_index_table_.ReadFromFile();
646 0 : vrf_mpls_index_table_.ReadFromFile();
647 0 : vlan_mpls_index_table_.ReadFromFile();
648 0 : route_mpls_index_table_.ReadFromFile();
649 0 : vm_interface_index_table_.ReadFromFile();
650 0 : vrf_index_table_.ReadFromFile();
651 0 : qos_index_table_.ReadFromFile();
652 0 : bgp_as_service_index_table_.ReadFromFile();
653 0 : mirror_index_table_.ReadFromFile();
654 0 : }
655 :
656 0 : void ResourceSandeshMaps::EndOfBackup() {
657 : ResourceManager::KeyPtr key
658 0 : (new ResourceBackupEndKey(agent_->resource_manager()));
659 0 : ResourceManager::DataPtr data;
660 0 : agent_->resource_manager()->EnqueueRestore(key, data);
661 0 : }
662 :
663 0 : void ResourceSandeshMaps::RestoreResource() {
664 0 : interface_mpls_index_table_.RestoreResource();
665 0 : vrf_mpls_index_table_.RestoreResource();
666 0 : vlan_mpls_index_table_.RestoreResource();
667 0 : route_mpls_index_table_.RestoreResource();
668 0 : vm_interface_index_table_.RestoreResource();
669 0 : vrf_index_table_.RestoreResource();
670 0 : qos_index_table_.RestoreResource();
671 0 : bgp_as_service_index_table_.RestoreResource();
672 0 : mirror_index_table_.RestoreResource();
673 0 : EndOfBackup();
674 0 : }
675 :
676 0 : void InterfaceIndexResourceMapSandesh::Process(SandeshContext*) {
677 :
678 0 : }
679 :
680 0 : void VrfMplsResourceMapSandesh::Process(SandeshContext*) {
681 :
682 0 : }
683 :
684 0 : void VlanMplsResourceMapSandesh::Process(SandeshContext*) {
685 :
686 0 : }
687 :
688 0 : void RouteMplsResourceMapSandesh::Process(SandeshContext*) {
689 :
690 0 : }
691 :
692 0 : void VmInterfaceIndexResourceMapSandesh::Process(SandeshContext*) {
693 :
694 0 : }
695 :
696 0 : void VrfIndexResourceMapSandesh::Process(SandeshContext*) {
697 :
698 0 : }
699 :
700 0 : void QosIndexResourceMapSandesh::Process(SandeshContext*) {
701 :
702 0 : }
703 0 : void BgpAsServiceIndexResourceMapSandesh::Process(SandeshContext*) {
704 :
705 0 : }
706 :
707 0 : void MirrorIndexResourceMapSandesh::Process(SandeshContext*) {
708 :
709 0 : }
710 :
711 0 : void ResourceSandeshMaps::AddInterfaceMplsResourceEntry(uint32_t index,
712 : InterfaceIndexResource data ) {
713 0 : interface_mpls_index_table_.map().insert(InterfaceMplsResourcePair(index,
714 : data));
715 0 : }
716 0 : void ResourceSandeshMaps::DeleteInterfaceMplsResourceEntry(uint32_t index) {
717 0 : interface_mpls_index_table_.map().erase(index);
718 0 : }
719 :
720 0 : void ResourceSandeshMaps::AddVrfMplsResourceEntry(uint32_t index,
721 : VrfMplsResource data) {
722 0 : vrf_mpls_index_table_.map().insert(VrfMplsResourcePair(index, data));
723 0 : }
724 :
725 0 : void ResourceSandeshMaps::DeleteVrfMplsResourceEntry(uint32_t index) {
726 0 : vrf_mpls_index_table_.map().erase(index);
727 0 : }
728 :
729 0 : void ResourceSandeshMaps::AddVlanMplsResourceEntry(uint32_t index,
730 : VlanMplsResource data) {
731 0 : vlan_mpls_index_table_.map().insert(VlanMplsResourcePair(index, data));
732 0 : }
733 :
734 0 : void ResourceSandeshMaps::DeleteVlanMplsResourceEntry(uint32_t index) {
735 0 : vlan_mpls_index_table_.map().erase(index);
736 0 : }
737 :
738 0 : void ResourceSandeshMaps::AddRouteMplsResourceEntry(uint32_t index,
739 : RouteMplsResource data) {
740 0 : route_mpls_index_table_.map().insert(RouteMplsResourcePair(index, data));
741 0 : }
742 :
743 0 : void ResourceSandeshMaps::DeleteRouteMplsResourceEntry(uint32_t index) {
744 0 : route_mpls_index_table_.map().erase(index);
745 0 : }
746 :
747 0 : void ResourceSandeshMaps::AddVmInterfaceResourceEntry(uint32_t index,
748 : VmInterfaceIndexResource data ) {
749 0 : vm_interface_index_table_.map().insert(VmInterfaceIndexResourcePair
750 : (index, data));
751 0 : }
752 0 : void ResourceSandeshMaps::DeleteVmInterfaceResourceEntry(uint32_t index) {
753 0 : vm_interface_index_table_.map().erase(index);
754 0 : }
755 :
756 0 : void ResourceSandeshMaps::AddVrfResourceEntry(uint32_t index,
757 : VrfIndexResource data ) {
758 0 : vrf_index_table_.map().insert(VrfIndexResourcePair
759 : (index, data));
760 0 : }
761 0 : void ResourceSandeshMaps::DeleteVrfResourceEntry(uint32_t index) {
762 0 : vrf_index_table_.map().erase(index);
763 0 : }
764 :
765 0 : void ResourceSandeshMaps::AddQosResourceEntry(uint32_t index,
766 : QosIndexResource data ) {
767 0 : qos_index_table_.map().insert(QosIndexResourcePair
768 : (index, data));
769 0 : }
770 0 : void ResourceSandeshMaps::DeleteQosResourceEntry(uint32_t index) {
771 0 : qos_index_table_.map().erase(index);
772 0 : }
773 :
774 0 : void ResourceSandeshMaps::AddBgpAsServiceResourceEntry
775 : (uint32_t index, BgpAsServiceIndexResource data ) {
776 0 : bgp_as_service_index_table_.map().insert(BgpAsServiceIndexResourcePair
777 : (index, data));
778 0 : }
779 :
780 0 : void ResourceSandeshMaps::DeleteBgpAsServiceResourceEntry(uint32_t index) {
781 0 : bgp_as_service_index_table_.map().erase(index);
782 0 : }
783 :
784 0 : void ResourceSandeshMaps::AddMirrorResourceEntry(uint32_t index,
785 : MirrorIndexResource data ) {
786 0 : mirror_index_table_.map().insert(MirrorIndexResourcePair
787 : (index, data));
788 0 : }
789 :
790 0 : void ResourceSandeshMaps::DeleteMirrorResourceEntry(uint32_t index) {
791 0 : mirror_index_table_.map().erase(index);
792 0 : }
|