forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswssplayer.cpp
79 lines (64 loc) · 1.41 KB
/
swssplayer.cpp
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
#include <fstream>
#include <iostream>
#include <dbconnector.h>
#include <producerstatetable.h>
#include <schema.h>
#include <tokenize.h>
using namespace std;
using namespace swss;
static int line_index = 0;
static DBConnector db("APPL_DB", 0, true);
void usage()
{
cout << "Usage: swssplayer <file>" << endl;
/* TODO: Add sample input file */
}
vector<FieldValueTuple> processFieldsValuesTuple(string s)
{
vector<FieldValueTuple> result;
auto tuples = tokenize(s, '|');
for (auto tuple : tuples)
{
auto v_tuple = tokenize(tuple, ':', 1);
auto field = v_tuple[0];
auto value = v_tuple.size() == 1 ? "" : v_tuple[1];
result.push_back(FieldValueTuple(field, value));
}
return result;
}
void processTokens(vector<string> tokens)
{
auto key = tokens[1];
/* Process the key */
auto v_key = tokenize(key, ':', 1);
auto table_name = v_key[0];
auto key_name = v_key[1];
ProducerStateTable producer(&db, table_name);
/* Process the operation */
auto op = tokens[2];
if (op == SET_COMMAND)
{
auto tuples = processFieldsValuesTuple(tokens[3]);
producer.set(key_name, tuples, SET_COMMAND);
}
else if (op == DEL_COMMAND)
{
producer.del(key_name, DEL_COMMAND);
}
}
int main(int argc, char **argv)
{
if (argc != 2)
{
usage();
exit(EXIT_FAILURE);
}
ifstream file(argv[1]);
string line;
while (getline(file, line))
{
auto tokens = tokenize(line, '|', 3);
processTokens(tokens);
line_index++;
}
}