forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswssconfig.cpp
217 lines (192 loc) · 6.11 KB
/
swssconfig.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
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
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <vector>
#include "logger.h"
#include "dbconnector.h"
#include "producerstatetable.h"
#include "json.hpp"
using namespace std;
using namespace swss;
using json = nlohmann::json;
const char* const op_name = "OP";
const char* const name_delimiter = ":";
const int el_count = 2;
const string SWSS_CONFIG_DIR = "/etc/swss/config.d/";
void usage()
{
cout << "Usage: swssconfig [FILE...]" << endl;
cout << " (default config folder is /etc/swss/config.d/)" << endl;
}
void dump_db_item(KeyOpFieldsValuesTuple &db_item)
{
SWSS_LOG_DEBUG("db_item: [");
SWSS_LOG_DEBUG("\toperation: %s", kfvOp(db_item).c_str());
SWSS_LOG_DEBUG("\thash: %s", kfvKey(db_item).c_str());
SWSS_LOG_DEBUG("\tfields: [");
for (auto fv: kfvFieldsValues(db_item))
SWSS_LOG_DEBUG("\t\tfield: %s value: %s", fvField(fv).c_str(), fvValue(fv).c_str());
SWSS_LOG_DEBUG("\t]");
SWSS_LOG_DEBUG("]");
}
bool write_db_data(vector<KeyOpFieldsValuesTuple> &db_items)
{
DBConnector db("APPL_DB", 0, true);
for (auto &db_item : db_items)
{
dump_db_item(db_item);
string key = kfvKey(db_item);
size_t pos = key.find(name_delimiter);
if ((string::npos == pos) || ((key.size() - 1) == pos))
{
SWSS_LOG_ERROR("Invalid formatted hash:%s\n", key.c_str());
return false;
}
string table_name = key.substr(0, pos);
string key_name = key.substr(pos + 1);
ProducerStateTable producer(&db, table_name);
if (kfvOp(db_item) == SET_COMMAND)
producer.set(key_name, kfvFieldsValues(db_item), SET_COMMAND);
else if (kfvOp(db_item) == DEL_COMMAND)
producer.del(key_name, DEL_COMMAND);
else
{
SWSS_LOG_ERROR("Invalid operation: %s\n", kfvOp(db_item).c_str());
return false;
}
}
return true;
}
bool load_json_db_data(ifstream &fs, vector<KeyOpFieldsValuesTuple> &db_items)
{
json json_array;
fs >> json_array;
if (!json_array.is_array())
{
SWSS_LOG_ERROR("Root element must be an array.");
return false;
}
for (size_t i = 0; i < json_array.size(); i++)
{
auto &arr_item = json_array[i];
if (arr_item.is_object())
{
if (el_count != arr_item.size())
{
SWSS_LOG_ERROR("Child elements must have both key and op entry. %s",
arr_item.dump().c_str());
return false;
}
db_items.push_back(KeyOpFieldsValuesTuple());
auto &cur_db_item = db_items[db_items.size() - 1];
for (json::iterator child_it = arr_item.begin(); child_it != arr_item.end(); child_it++) {
auto cur_obj_key = child_it.key();
auto &cur_obj = child_it.value();
string field_str;
string value_str;
if (cur_obj.is_object()) {
kfvKey(cur_db_item) = cur_obj_key;
for (json::iterator cur_obj_it = cur_obj.begin(); cur_obj_it != cur_obj.end(); cur_obj_it++)
{
string field_str = cur_obj_it.key();
string value_str;
if ((*cur_obj_it).is_number())
value_str = to_string((*cur_obj_it).get<int>());
else if ((*cur_obj_it).is_string())
value_str = (*cur_obj_it).get<string>();
kfvFieldsValues(cur_db_item).push_back(FieldValueTuple(field_str, value_str));
}
}
else
{
if (op_name != child_it.key())
{
SWSS_LOG_ERROR("Invalid entry. %s", arr_item.dump().c_str());
return false;
}
kfvOp(cur_db_item) = cur_obj.get<string>();
}
}
}
else
{
SWSS_LOG_ERROR("Child elements must be objects. element:%s", arr_item.dump().c_str());
return false;
}
}
return true;
}
vector<string> read_directory(const string &path)
{
vector<string> ret;
DIR *dir;
dirent *entry;
dir = opendir(path.c_str());
if (dir)
{
while ((entry = readdir(dir)))
{
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
{
ret.push_back(path + string(entry->d_name));
}
}
closedir(dir);
}
sort(ret.begin(), ret.end());
return ret;
}
int main(int argc, char **argv)
{
vector<string> files;
if (argc == 1)
{
files = read_directory(SWSS_CONFIG_DIR);
}
if (argc == 2 && !strcmp(argv[1], "-h"))
{
usage();
exit(EXIT_SUCCESS);
}
else
{
for (auto i = 1; i < argc; i++)
{
files.push_back(string(argv[i]));
}
}
for (auto i : files)
{
SWSS_LOG_NOTICE("Loading config from JSON file:%s...", i.c_str());
vector<KeyOpFieldsValuesTuple> db_items;
try
{
ifstream fs(i);
if (!fs)
{
SWSS_LOG_ERROR("Failed to open file %s", i.c_str());
cerr << "Failed to open file " << i.c_str() << endl;
return EXIT_FAILURE;
}
if (!load_json_db_data(fs, db_items))
{
SWSS_LOG_ERROR("Failed loading data from JSON file %s", i.c_str());
return EXIT_FAILURE;
}
if (!write_db_data(db_items))
{
SWSS_LOG_ERROR("Failed applying data from JSON file %s", i.c_str());
return EXIT_FAILURE;
}
}
catch(const exception &e)
{
SWSS_LOG_ERROR("Exception caught: %s", e.what());
cout << "Exception caught: " << e.what() << endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}