/
ifmap_server_show.cc
1950 lines (1666 loc) · 73.8 KB
/
ifmap_server_show.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <sandesh/sandesh_types.h>
#include <sandesh/sandesh.h>
#include <sandesh/request_pipeline.h>
#include <boost/bind.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/regex.hpp>
#include "base/logging.h"
#include "db/db.h"
#include "db/db_graph.h"
#include "db/db_graph_vertex.h"
#include "db/db_table_partition.h"
#include "base/bitset.h"
#include "ifmap/ifmap_client.h"
#include "ifmap/ifmap_exporter.h"
#include "ifmap/ifmap_link.h"
#include "ifmap/ifmap_link_table.h"
#include "ifmap/ifmap_log.h"
#include "ifmap/ifmap_node.h"
#include "ifmap/ifmap_object.h"
#include "ifmap/ifmap_origin.h"
#include "ifmap/ifmap_sandesh_context.h"
#include "ifmap/ifmap_server.h"
#include "ifmap/ifmap_server_show_types.h"
#include "ifmap/ifmap_server_show_internal_types.h"
#include "ifmap/ifmap_log_types.h"
#include "ifmap/ifmap_table.h"
#include "ifmap/ifmap_update.h"
#include "ifmap/ifmap_uuid_mapper.h"
#include <pugixml/pugixml.hpp>
using boost::regex;
using boost::regex_search;
using namespace boost::assign;
using namespace std;
using namespace pugi;
class IFMapNodeCopier {
public:
IFMapNodeCopier(IFMapNodeShowInfo *dest, DBEntryBase *src,
IFMapServer *server) {
// Get the name of the node
IFMapNode *src_node = static_cast<IFMapNode *>(src);
dest->node_name = src_node->ToString();
IFMapNodeState *state = server->exporter()->NodeStateLookup(src_node);
if (state) {
// Get the interests and advertised from state
dest->interests = state->interest().ToNumberedString();
dest->advertised = state->advertised().ToNumberedString();
} else {
dest->dbentryflags.append("No state, ");
}
if (src_node->IsDeleted()) {
dest->dbentryflags.append("Deleted, ");
}
if (src_node->is_onlist()) {
dest->dbentryflags.append("OnList, ");
}
if (src_node->IsOnRemoveQ()) {
dest->dbentryflags.append("OnRemoveQ");
}
dest->obj_info.reserve(src_node->list_.size());
for (IFMapNode::ObjectList::const_iterator iter =
src_node->list_.begin(); iter != src_node->list_.end(); ++iter) {
const IFMapObject *src_obj = iter.operator->();
IFMapObjectShowInfo dest_obj;
dest_obj.sequence_number = src_obj->sequence_number();
dest_obj.origin = src_obj->origin().ToString();
dest_obj.data = GetIFMapObjectData(src_obj);
dest->obj_info.push_back(dest_obj);
}
if (src_node->IsVertexValid()) {
DBGraph *graph = server->graph();
int neighbor_count = 0;
for (DBGraphVertex::adjacency_iterator iter =
src_node->begin(graph); iter != src_node->end(graph); ++iter) {
++neighbor_count;
}
dest->neighbors.reserve(neighbor_count);
for (DBGraphVertex::adjacency_iterator iter =
src_node->begin(graph); iter != src_node->end(graph); ++iter) {
IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
dest->neighbors.push_back(adj->ToString());
}
}
dest->last_modified = src_node->last_change_at_str();
}
string GetIFMapObjectData(const IFMapObject *src_obj) {
pugi::xml_document xdoc;
pugi::xml_node xnode = xdoc.append_child("iq");
src_obj->EncodeUpdate(&xnode);
ostringstream oss;
xnode.print(oss);
string objdata = oss.str();
return objdata;
}
};
const string kShowIterSeparator = "||";
// almost everything in this class is static since we dont really want to
// intantiate this class
class ShowIFMapTable {
public:
static const uint32_t kMaxElementsPerRound = 50;
struct ShowData : public RequestPipeline::InstData {
vector<IFMapNodeShowInfo> send_buffer;
string next_table_name;
string last_node_name;
};
static RequestPipeline::InstData *AllocBuffer(int stage) {
return static_cast<RequestPipeline::InstData *>(new ShowData);
}
static bool BufferStageCommon(const IFMapTableShowReq *request,
RequestPipeline::InstData *data,
const string &next_table_name,
const string &last_node_name);
static bool BufferStage(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage,
int instNum, RequestPipeline::InstData *data);
static bool BufferStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage,
int instNum, RequestPipeline::InstData *data);
static void SendStageCommon(const IFMapTableShowReq *request,
const RequestPipeline::PipeSpec ps,
IFMapTableShowResp *response);
static bool SendStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data);
static bool SendStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage,
int instNum, RequestPipeline::InstData *data);
static bool TableToBuffer(const IFMapTableShowReq *request,
IFMapTable *table, IFMapServer *server,
const string &last_node_name,
ShowData *show_data);
static bool BufferAllTables(const IFMapTableShowReq *req,
RequestPipeline::InstData *data,
const string &next_table_name,
const string &last_node_name);
static bool BufferOneTable(const IFMapTableShowReq *request,
RequestPipeline::InstData *data,
const string &last_node_name);
static bool ConvertReqIterateToReq(
const IFMapTableShowReqIterate *req_iterate,
IFMapTableShowReq *req, string *next_table_name,
string *last_node_name);
};
// Return true if the buffer is full.
bool ShowIFMapTable::TableToBuffer(const IFMapTableShowReq *request,
IFMapTable *table, IFMapServer *server, const string &last_node_name,
ShowData *show_data) {
DBEntryBase *src = NULL;
if (last_node_name.length()) {
// If the last_node_name is set, it was the last node printed in the
// previous round. Search for the node 'after' last_node_name and start
// this round with it. If there is no next node, we are done with this
// table.
IFMapNode *last_node = table->FindNextNode(last_node_name);
if (last_node) {
src = last_node;
} else {
return false;
}
}
bool buffer_full = false;
regex search_expr(request->get_search_string());
DBTablePartBase *partition = table->GetTablePartition(0);
if (!src) {
src = partition->GetFirst();
}
for (; src != NULL; src = partition->GetNext(src)) {
IFMapNode *src_node = static_cast<IFMapNode *>(src);
if (!regex_search(src_node->ToString(), search_expr)) {
continue;
}
IFMapNodeShowInfo dest;
IFMapNodeCopier copyNode(&dest, src, server);
show_data->send_buffer.push_back(dest);
// If we have picked up enough nodes for this round...
if (show_data->send_buffer.size() == kMaxElementsPerRound) {
// Save the values needed for the next round. When we come
// back, we will use the 'names' to lookup the elements since
// the 'names' are the keys in the respective tables.
show_data->next_table_name = table->name();
show_data->last_node_name = src_node->name();
buffer_full = true;
break;
}
}
return buffer_full;
}
bool ShowIFMapTable::BufferOneTable(const IFMapTableShowReq *request,
RequestPipeline::InstData *data, const string &last_node_name) {
IFMapSandeshContext *sctx =
static_cast<IFMapSandeshContext *>(request->module_context("IFMap"));
IFMapTable *table = IFMapTable::FindTable(sctx->ifmap_server()->database(),
request->get_table_name());
if (table) {
ShowData *show_data = static_cast<ShowData *>(data);
show_data->send_buffer.reserve(kMaxElementsPerRound);
TableToBuffer(request, table, sctx->ifmap_server(), last_node_name,
show_data);
} else {
IFMAP_WARN(IFMapTblNotFound, "Cant show/find table ",
request->get_table_name());
}
return true;
}
bool ShowIFMapTable::BufferAllTables(const IFMapTableShowReq *request,
RequestPipeline::InstData *data, const string &next_table_name,
const string &last_node_name) {
IFMapSandeshContext *sctx =
static_cast<IFMapSandeshContext *>(request->module_context("IFMap"));
string last_name = last_node_name;
DB *db = sctx->ifmap_server()->database();
DB::iterator iter;
if (next_table_name.empty()) {
iter = db->lower_bound("__ifmap__.");
} else {
iter = db->FindTableIter(next_table_name);
}
ShowData *show_data = static_cast<ShowData *>(data);
show_data->send_buffer.reserve(kMaxElementsPerRound);
for (; iter != db->end(); ++iter) {
if (iter->first.find("__ifmap__.") != 0) {
break;
}
IFMapTable *table = static_cast<IFMapTable *>(iter->second);
bool buffer_full = TableToBuffer(request, table, sctx->ifmap_server(),
last_name, show_data);
if (buffer_full) {
break;
}
// last_node_name is only relevant for the first iteration.
last_name.clear();
}
return true;
}
// Format of node_info string:
// table_name||search_string||next_table_name||last_node_name
// table_name/search_string: original input. Could be empty.
// next_table_name: next table to lookup in
// last_node_name: name of last node that was printed in the previous round
bool ShowIFMapTable::ConvertReqIterateToReq(
const IFMapTableShowReqIterate *req_iterate,
IFMapTableShowReq *req, string *next_table_name,
string *last_node_name) {
// First, set the context from the original request since we might return
// due to parsing errors.
req->set_context(req_iterate->context());
string node_info = req_iterate->get_node_info();
size_t sep_size = kShowIterSeparator.size();
// table_name
size_t pos1 = node_info.find(kShowIterSeparator);
if (pos1 == string::npos) {
return false;
}
string table_name = node_info.substr(0, pos1);
// search_string
size_t pos2 = node_info.find(kShowIterSeparator, (pos1 + sep_size));
if (pos2 == string::npos) {
return false;
}
string search_string = node_info.substr((pos1 + sep_size),
pos2 - (pos1 + sep_size));
// next_table_name
size_t pos3 = node_info.find(kShowIterSeparator, (pos2 + sep_size));
if (pos3 == string::npos) {
return false;
}
*next_table_name = node_info.substr((pos2 + sep_size),
pos3 - (pos2 + sep_size));
// last_node_name
*last_node_name = node_info.substr(pos3 + sep_size);
// Fill up the fields of IFMapTableShowReq appropriately.
req->set_table_name(table_name);
req->set_search_string(search_string);
return true;
}
bool ShowIFMapTable::BufferStageCommon(const IFMapTableShowReq *request,
RequestPipeline::InstData *data, const string &next_table_name,
const string &last_node_name) {
// If table name has not been passed, print all tables
if (request->get_table_name().length()) {
return BufferOneTable(request, data, last_node_name);
} else {
return BufferAllTables(request, data, next_table_name, last_node_name);
}
}
bool ShowIFMapTable::BufferStage(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data) {
const IFMapTableShowReq *request =
static_cast<const IFMapTableShowReq *>(ps.snhRequest_.get());
string next_table_name;
string last_node_name;
return BufferStageCommon(request, data, next_table_name, last_node_name);
}
bool ShowIFMapTable::BufferStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data) {
const IFMapTableShowReqIterate *request_iterate =
static_cast<const IFMapTableShowReqIterate *>(ps.snhRequest_.get());
string next_table_name;
string last_node_name;
IFMapTableShowReq *request = new IFMapTableShowReq;
bool success = ConvertReqIterateToReq(request_iterate, request,
&next_table_name, &last_node_name);
if (success) {
BufferStageCommon(request, data, next_table_name, last_node_name);
}
request->Release();
return true;
}
void ShowIFMapTable::SendStageCommon(const IFMapTableShowReq *request,
const RequestPipeline::PipeSpec ps,
IFMapTableShowResp *response) {
const RequestPipeline::StageData *prev_stage_data = ps.GetStageData(0);
const ShowIFMapTable::ShowData &show_data =
static_cast<const ShowIFMapTable::ShowData &> (prev_stage_data->at(0));
vector<IFMapNodeShowInfo> dest_buffer;
dest_buffer = show_data.send_buffer;
// If we have filled the buffer, set next_batch with all the values we will
// need in the next round.
string next_batch;
if (dest_buffer.size() == kMaxElementsPerRound) {
next_batch = request->get_table_name() + kShowIterSeparator +
request->get_search_string() + kShowIterSeparator +
show_data.next_table_name + kShowIterSeparator +
show_data.last_node_name;
}
response->set_ifmap_db(dest_buffer);
response->set_next_batch(next_batch);
}
bool ShowIFMapTable::SendStage(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data) {
const IFMapTableShowReq *request =
static_cast<const IFMapTableShowReq *>(ps.snhRequest_.get());
IFMapTableShowResp *response = new IFMapTableShowResp;
SendStageCommon(request, ps, response);
response->set_context(request->context());
response->set_more(false);
response->Response();
return true;
}
bool ShowIFMapTable::SendStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data) {
const IFMapTableShowReqIterate *request_iterate =
static_cast<const IFMapTableShowReqIterate *>(ps.snhRequest_.get());
string next_table_name;
string last_node_name;
IFMapTableShowResp *response = new IFMapTableShowResp;
IFMapTableShowReq *request = new IFMapTableShowReq;
bool success = ConvertReqIterateToReq(request_iterate, request,
&next_table_name, &last_node_name);
if (success) {
SendStageCommon(request, ps, response);
}
response->set_context(request->context());
response->set_more(false);
response->Response();
request->Release();
return true;
}
void IFMapTableShowReq::HandleRequest() const {
RequestPipeline::StageSpec s0, s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();
// 2 stages - first: gather/read, second: send
s0.taskId_ = scheduler->GetTaskId("db::IFMapTable");
s0.allocFn_ = ShowIFMapTable::AllocBuffer;
s0.cbFn_ = ShowIFMapTable::BufferStage;
s0.instances_.push_back(0);
// control-node ifmap show command task
s1.taskId_ = scheduler->GetTaskId("cn_ifmap::ShowCommand");
s1.cbFn_ = ShowIFMapTable::SendStage;
s1.instances_.push_back(0);
RequestPipeline::PipeSpec ps(this);
ps.stages_= list_of(s0)(s1);
RequestPipeline rp(ps);
}
void IFMapTableShowReqIterate::HandleRequest() const {
RequestPipeline::StageSpec s0, s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();
// 2 stages - first: gather/read, second: send
s0.taskId_ = scheduler->GetTaskId("db::IFMapTable");
s0.allocFn_ = ShowIFMapTable::AllocBuffer;
s0.cbFn_ = ShowIFMapTable::BufferStageIterate;
s0.instances_.push_back(0);
// control-node ifmap show command task
s1.taskId_ = scheduler->GetTaskId("cn_ifmap::ShowCommand");
s1.cbFn_ = ShowIFMapTable::SendStageIterate;
s1.instances_.push_back(0);
RequestPipeline::PipeSpec ps(this);
ps.stages_= list_of(s0)(s1);
RequestPipeline rp(ps);
}
// Code to display link-table entries
class ShowIFMapLinkTable {
public:
static const uint32_t kMaxElementsPerRound = 50;
struct ShowData : public RequestPipeline::InstData {
vector<IFMapLinkShowInfo> send_buffer;
uint32_t table_size;
string last_link_name;
};
static RequestPipeline::InstData *AllocBuffer(int stage) {
return static_cast<RequestPipeline::InstData *>(new ShowData);
}
static bool IncludeLink(DBEntryBase *src, const string &search_string,
const regex &search_expr, const string &metadata,
const regex &metadata_expr);
static void CopyNode(IFMapLinkShowInfo *dest, DBEntryBase *src,
IFMapServer *server);
static bool BufferStageCommon(const IFMapLinkTableShowReq *request,
RequestPipeline::InstData *data,
const string &last_link_name);
static bool BufferStage(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage,
int instNum, RequestPipeline::InstData *data);
static bool BufferStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data);
static void SendStageCommon(const IFMapLinkTableShowReq *request,
const RequestPipeline::PipeSpec ps,
IFMapLinkTableShowResp *response);
static bool SendStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data);
static bool SendStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage,
int instNum, RequestPipeline::InstData *data);
static bool ConvertReqIterateToReq(
const IFMapLinkTableShowReqIterate *req_iterate,
IFMapLinkTableShowReq *req, string *last_link_name);
};
bool ShowIFMapLinkTable::IncludeLink(DBEntryBase *src,
const string &search_string, const regex &search_expr,
const string &metadata, const regex &metadata_expr) {
IFMapLink *link = static_cast<IFMapLink *>(src);
IFMapNode *left = link->left();
IFMapNode *right = link->right();
// If we do not find the search string in the names of either of the
// two ends, do not include the link.
if (!search_string.empty() &&
(!left || !regex_search(left->ToString(), search_expr)) &&
(!right || !regex_search(right->ToString(), search_expr))) {
return false;
}
// If the metadata does not match, do not include the link.
if (!metadata.empty() && !regex_search(link->metadata(), metadata_expr)) {
return false;
}
return true;
}
void ShowIFMapLinkTable::CopyNode(IFMapLinkShowInfo *dest, DBEntryBase *src,
IFMapServer *server) {
IFMapLink *src_link = static_cast<IFMapLink *>(src);
dest->metadata = src_link->metadata();
if (src_link->left()) {
dest->left = src_link->left()->ToString();
}
if (src_link->right()) {
dest->right = src_link->right()->ToString();
}
// Get the interests and advertised from state
IFMapLinkState *state = server->exporter()->LinkStateLookup(src_link);
if (state) {
dest->interests = state->interest().ToNumberedString();
dest->advertised = state->advertised().ToNumberedString();
} else {
dest->dbentryflags.append("No state, ");
}
if (src_link->IsDeleted()) {
dest->dbentryflags.append("Deleted, ");
}
if (src_link->is_onlist()) {
dest->dbentryflags.append("OnList");
}
if (src_link->IsOnRemoveQ()) {
dest->dbentryflags.append("OnRemoveQ");
}
dest->last_modified = src_link->last_change_at_str();
for (std::vector<IFMapLink::LinkOriginInfo>::const_iterator iter =
src_link->origin_info_.begin(); iter != src_link->origin_info_.end();
++iter) {
const IFMapLink::LinkOriginInfo *origin_info = iter.operator->();
IFMapLinkOriginShowInfo dest_origin;
dest_origin.sequence_number = origin_info->sequence_number;
dest_origin.origin = origin_info->origin.ToString();
dest->origins.push_back(dest_origin);
}
}
// Format of link_info string:
// search_string||metadata||last_link_name
// search_string: original input. Could be empty.
// metadata: original input. Could be empty.
// last_link_name: name of last link that was printed in the previous round
bool ShowIFMapLinkTable::ConvertReqIterateToReq(
const IFMapLinkTableShowReqIterate *req_iterate,
IFMapLinkTableShowReq *req, string *last_link_name) {
// First, set the context from the original request since we might return
// due to parsing errors.
req->set_context(req_iterate->context());
string link_info = req_iterate->get_link_info();
size_t sep_size = kShowIterSeparator.size();
// search_string
size_t pos1 = link_info.find(kShowIterSeparator);
if (pos1 == string::npos) {
return false;
}
string search_string = link_info.substr(0, pos1);
// metadata
size_t pos2 = link_info.find(kShowIterSeparator, (pos1 + sep_size));
if (pos2 == string::npos) {
return false;
}
string metadata = link_info.substr((pos1 + sep_size),
pos2 - (pos1 + sep_size));
// last_link_name
*last_link_name = link_info.substr(pos2 + sep_size);
// Fill up the fields of IFMapLinkTableShowReq appropriately.
req->set_search_string(search_string);
req->set_metadata(metadata);
return true;
}
bool ShowIFMapLinkTable::BufferStageCommon(const IFMapLinkTableShowReq *request,
RequestPipeline::InstData *data,
const string &last_link_name) {
bool buffer_full = false;
IFMapSandeshContext *sctx =
static_cast<IFMapSandeshContext *>(request->module_context("IFMap"));
IFMapLinkTable *table = static_cast<IFMapLinkTable *>(
sctx->ifmap_server()->database()->FindTable("__ifmap_metadata__.0"));
if (table) {
regex search_expr(request->get_search_string());
regex metadata_expr(request->get_metadata());
ShowData *show_data = static_cast<ShowData *>(data);
show_data->send_buffer.reserve(kMaxElementsPerRound);
show_data->table_size = table->Size();
DBEntryBase *src = NULL;
DBTablePartBase *partition = table->GetTablePartition(0);
if (last_link_name.length()) {
src = table->FindNextLink(last_link_name);
} else {
src = partition->GetFirst();
}
for (; src != NULL; src = partition->GetNext(src)) {
if (IncludeLink(src, request->get_search_string(), search_expr,
request->get_metadata(), metadata_expr)) {
IFMapLinkShowInfo dest;
CopyNode(&dest, src, sctx->ifmap_server());
show_data->send_buffer.push_back(dest);
// If we have picked up enough links for this round...
if (show_data->send_buffer.size() == kMaxElementsPerRound) {
IFMapLink *src_link = static_cast<IFMapLink *>(src);
show_data->last_link_name = src_link->link_name();
buffer_full = true;
break;
}
}
}
} else {
IFMAP_WARN(IFMapTblNotFound, "Cant show/find ", "link table");
}
return buffer_full;
}
bool ShowIFMapLinkTable::BufferStage(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data) {
const IFMapLinkTableShowReq *request =
static_cast<const IFMapLinkTableShowReq *>(ps.snhRequest_.get());
string last_link_name;
BufferStageCommon(request, data, last_link_name);
return true;
}
bool ShowIFMapLinkTable::BufferStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data) {
const IFMapLinkTableShowReqIterate *request_iterate =
static_cast<const IFMapLinkTableShowReqIterate *>(ps.snhRequest_.get());
IFMapLinkTableShowReq *request = new IFMapLinkTableShowReq;
string last_link_name;
bool success = ConvertReqIterateToReq(request_iterate, request,
&last_link_name);
if (success) {
BufferStageCommon(request, data, last_link_name);
}
request->Release();
return true;
}
void ShowIFMapLinkTable::SendStageCommon(const IFMapLinkTableShowReq *request,
const RequestPipeline::PipeSpec ps,
IFMapLinkTableShowResp *response) {
const RequestPipeline::StageData *prev_stage_data = ps.GetStageData(0);
const ShowIFMapLinkTable::ShowData &show_data =
static_cast<const ShowIFMapLinkTable::ShowData &>
(prev_stage_data->at(0));
vector<IFMapLinkShowInfo> dest_buffer;
dest_buffer = show_data.send_buffer;
// If we have filled the buffer, set next_batch with all the values we will
// need in the next round.
string next_batch;
if (dest_buffer.size() == kMaxElementsPerRound) {
next_batch = request->get_search_string() + kShowIterSeparator +
request->get_metadata() + kShowIterSeparator +
show_data.last_link_name;
}
response->set_table_size(show_data.table_size);
response->set_ifmap_db(dest_buffer);
response->set_next_batch(next_batch);
}
bool ShowIFMapLinkTable::SendStage(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data) {
const IFMapLinkTableShowReq *request =
static_cast<const IFMapLinkTableShowReq *>(ps.snhRequest_.get());
IFMapLinkTableShowResp *response = new IFMapLinkTableShowResp;
SendStageCommon(request, ps, response);
response->set_context(request->context());
response->set_more(false);
response->Response();
return true;
}
bool ShowIFMapLinkTable::SendStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data) {
const IFMapLinkTableShowReqIterate *request_iterate =
static_cast<const IFMapLinkTableShowReqIterate *>(ps.snhRequest_.get());
IFMapLinkTableShowResp *response = new IFMapLinkTableShowResp;
IFMapLinkTableShowReq *request = new IFMapLinkTableShowReq;
string last_link_name;
bool success = ConvertReqIterateToReq(request_iterate, request,
&last_link_name);
if (success) {
SendStageCommon(request, ps, response);
}
response->set_context(request->context());
response->set_more(false);
response->Response();
request->Release();
return true;
}
void IFMapLinkTableShowReq::HandleRequest() const {
RequestPipeline::StageSpec s0, s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();
// 2 stages - first: gather/read, second: send
s0.taskId_ = scheduler->GetTaskId("db::IFMapTable");
s0.allocFn_ = ShowIFMapLinkTable::AllocBuffer;
s0.cbFn_ = ShowIFMapLinkTable::BufferStage;
s0.instances_.push_back(0);
// control-node ifmap show command task
s1.taskId_ = scheduler->GetTaskId("cn_ifmap::ShowCommand");
s1.cbFn_ = ShowIFMapLinkTable::SendStage;
s1.instances_.push_back(0);
RequestPipeline::PipeSpec ps(this);
ps.stages_= list_of(s0)(s1);
RequestPipeline rp(ps);
}
void IFMapLinkTableShowReqIterate::HandleRequest() const {
RequestPipeline::StageSpec s0, s1;
TaskScheduler *scheduler = TaskScheduler::GetInstance();
// 2 stages - first: gather/read, second: send
s0.taskId_ = scheduler->GetTaskId("db::IFMapTable");
s0.allocFn_ = ShowIFMapLinkTable::AllocBuffer;
s0.cbFn_ = ShowIFMapLinkTable::BufferStageIterate;
s0.instances_.push_back(0);
// control-node ifmap show command task
s1.taskId_ = scheduler->GetTaskId("cn_ifmap::ShowCommand");
s1.cbFn_ = ShowIFMapLinkTable::SendStageIterate;
s1.instances_.push_back(0);
RequestPipeline::PipeSpec ps(this);
ps.stages_= list_of(s0)(s1);
RequestPipeline rp(ps);
}
static bool IFMapNodeShowReqHandleRequest(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data) {
const IFMapNodeShowReq *request =
static_cast<const IFMapNodeShowReq *>(ps.snhRequest_.get());
IFMapSandeshContext *sctx =
static_cast<IFMapSandeshContext *>(request->module_context("IFMap"));
IFMapNodeShowResp *response = new IFMapNodeShowResp();
string fq_node_name = request->get_fq_node_name();
// EG: "virtual-network:my:virtual:network" i.e. type:name
size_t type_length = fq_node_name.find(":");
if (type_length != string::npos) {
string node_type = fq_node_name.substr(0, type_length);
// +1 to go to the next character after ':'
string node_name = fq_node_name.substr(type_length + 1);
DB *db = sctx->ifmap_server()->database();
IFMapTable *table = IFMapTable::FindTable(db, node_type);
if (table) {
IFMapNode *src = table->FindNode(node_name);
if (src) {
IFMapNodeShowInfo dest;
IFMapNodeCopier copyNode(&dest, src, sctx->ifmap_server());
response->set_node_info(dest);
} else {
IFMAP_WARN(IFMapIdentifierNotFound, "Cant find identifier",
node_name);
}
} else {
IFMAP_WARN(IFMapTblNotFound, "Cant show/find table with node-type",
node_type);
}
}
response->set_context(request->context());
response->set_more(false);
response->Response();
// Return 'true' so that we are not called again
return true;
}
void IFMapNodeShowReq::HandleRequest() const {
RequestPipeline::StageSpec s0;
TaskScheduler *scheduler = TaskScheduler::GetInstance();
s0.taskId_ = scheduler->GetTaskId("db::IFMapTable");
s0.cbFn_ = IFMapNodeShowReqHandleRequest;
s0.instances_.push_back(0);
RequestPipeline::PipeSpec ps(this);
ps.stages_= list_of(s0);
RequestPipeline rp(ps);
}
class ShowIFMapPerClientNodes {
public:
static const uint32_t kMaxElementsPerRound = 50;
struct ShowData : public RequestPipeline::InstData {
vector<IFMapPerClientNodesShowInfo> send_buffer;
string next_table_name;
string last_node_name;
};
static RequestPipeline::InstData *AllocBuffer(int stage) {
return static_cast<RequestPipeline::InstData *>(new ShowData);
}
static bool ConvertReqIterateToReq(
const IFMapPerClientNodesShowReqIterate *req_iterate,
IFMapPerClientNodesShowReq *req, string *next_table_name,
string *last_node_name);
static bool CopyNode(IFMapPerClientNodesShowInfo *dest, IFMapNode *src,
IFMapServer *server, int client_index);
static bool TableToBuffer(const IFMapPerClientNodesShowReq *request,
IFMapTable *table, IFMapServer *server,
const string &last_node_name, int client_index,
ShowData *show_data);
static bool BufferStageCommon(const IFMapPerClientNodesShowReq *request,
RequestPipeline::InstData *data,
const string &next_table_name,
const string &last_node_name);
static bool BufferStage(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage,
int instNum, RequestPipeline::InstData *data);
static bool BufferStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data);
static void SendStageCommon(const IFMapPerClientNodesShowReq *request,
const RequestPipeline::PipeSpec ps,
IFMapPerClientNodesShowResp *response);
static bool SendStage(const Sandesh *sr, const RequestPipeline::PipeSpec ps,
int stage, int instNum,
RequestPipeline::InstData *data);
static bool SendStageIterate(const Sandesh *sr,
const RequestPipeline::PipeSpec ps, int stage,
int instNum, RequestPipeline::InstData *data);
};
// Format of node_info string:
// client_index_or_name||search_string||next_table_name||last_node_name
// client_index_or_name: original input
// search_string: original input; can be empty
// next_table_name: next table to lookup in
// last_node_name: name of last node that was printed in the previous round
bool ShowIFMapPerClientNodes::ConvertReqIterateToReq(
const IFMapPerClientNodesShowReqIterate *req_iterate,
IFMapPerClientNodesShowReq *req, string *next_table_name,
string *last_node_name) {
// First, set the context from the original request since we might return
// due to parsing errors.
req->set_context(req_iterate->context());
string node_info = req_iterate->get_node_info();
size_t sep_size = kShowIterSeparator.size();
// client_index_or_name
size_t pos1 = node_info.find(kShowIterSeparator);
if (pos1 == string::npos) {
return false;
}
string client_index_or_name = node_info.substr(0, pos1);
// search_string
size_t pos2 = node_info.find(kShowIterSeparator, (pos1 + sep_size));
if (pos2 == string::npos) {
return false;
}
string search_string = node_info.substr((pos1 + sep_size),
pos2 - (pos1 + sep_size));
// next_table_name
size_t pos3 = node_info.find(kShowIterSeparator, (pos2 + sep_size));
if (pos3 == string::npos) {
return false;
}
*next_table_name = node_info.substr((pos2 + sep_size),
pos3 - (pos2 + sep_size));
// last_node_name
*last_node_name = node_info.substr(pos3 + sep_size);
// Fill up the fields of IFMapTableShowReq appropriately.
req->set_client_index_or_name(client_index_or_name);
req->set_search_string(search_string);
return true;
}
bool ShowIFMapPerClientNodes::CopyNode(IFMapPerClientNodesShowInfo *dest,
IFMapNode *src, IFMapServer *server,
int client_index) {
IFMapNodeState *state = server->exporter()->NodeStateLookup(src);
if (state && state->interest().test(client_index)) {
dest->node_name = src->ToString();
if (state->advertised().test(client_index)) {
dest->sent = "Yes";
} else {
dest->sent = "No";
}
if (server->exporter()->ClientHasConfigTracker(
IFMapExporter::INTEREST, client_index)) {
if (server->exporter()->ClientConfigTrackerHasState(
IFMapExporter::INTEREST, client_index, state)) {
dest->interest_tracked = "Yes";
} else {
dest->interest_tracked = "No";
}
} else {
dest->interest_tracked = "No tracker";
}
if (server->exporter()->ClientHasConfigTracker(
IFMapExporter::ADVERTISED, client_index)) {
if (server->exporter()->ClientConfigTrackerHasState(
IFMapExporter::ADVERTISED, client_index, state)) {
dest->advertised_tracked = "Yes";
} else {
dest->advertised_tracked = "No";
}
} else {
dest->advertised_tracked = "No tracker";
}
return true;
} else {
return false;
}
}
bool ShowIFMapPerClientNodes::TableToBuffer(
const IFMapPerClientNodesShowReq *request, IFMapTable *table,
IFMapServer *ifmap_server, const string &last_node_name,
int client_index, ShowData *show_data) {
DBEntryBase *src = NULL;
if (last_node_name.length()) {
// If the last_node_name is set, it was the last node printed in the