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