Skip to content

Commit d923f71

Browse files
Wataru Ishidalguohan
Wataru Ishida
authored andcommitted
[portsyncd] support read port configuration from configDB (sonic-net#409)
read port configuration from configDB when '-p' is not specified. Signed-off-by: Wataru Ishida <[email protected]>
1 parent 00ea0ab commit d923f71

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

portsyncd/portsyncd.cpp

+42-11
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include "subscriberstatetable.h"
1616
#include "exec.h"
1717

18-
#define DEFAULT_PORT_CONFIG_FILE "port_config.ini"
19-
2018
using namespace std;
2119
using namespace swss;
2220

@@ -36,19 +34,20 @@ bool g_init = false;
3634
void usage()
3735
{
3836
cout << "Usage: portsyncd [-p port_config.ini]" << endl;
39-
cout << " -p port_config.ini: MANDATORY import port lane mapping" << endl;
40-
cout << " default: port_config.ini" << endl;
37+
cout << " -p port_config.ini: import port lane mapping" << endl;
38+
cout << " use configDB data if not specified" << endl;
4139
}
4240

4341
void handlePortConfigFile(ProducerStateTable &p, string file);
42+
void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb);
4443
void handleVlanIntfFile(string file);
4544
void handlePortConfig(ProducerStateTable &p, map<string, KeyOpFieldsValuesTuple> &port_cfg_map);
4645

4746
int main(int argc, char **argv)
4847
{
4948
Logger::linkToDbNative("portsyncd");
5049
int opt;
51-
string port_config_file = DEFAULT_PORT_CONFIG_FILE;
50+
string port_config_file;
5251
map<string, KeyOpFieldsValuesTuple> port_cfg_map;
5352

5453
while ((opt = getopt(argc, argv, "p:v:h")) != -1 )
@@ -85,7 +84,12 @@ int main(int argc, char **argv)
8584
netlink.registerGroup(RTNLGRP_LINK);
8685
cout << "Listen to link messages..." << endl;
8786

88-
handlePortConfigFile(p, port_config_file);
87+
if (!port_config_file.empty())
88+
{
89+
handlePortConfigFile(p, port_config_file);
90+
} else {
91+
handlePortConfigFromConfigDB(p, cfgDb);
92+
}
8993

9094
s.addSelectable(&netlink);
9195
s.addSelectable(&portCfg);
@@ -153,6 +157,37 @@ int main(int argc, char **argv)
153157
return 1;
154158
}
155159

160+
static void notifyPortConfigDone(ProducerStateTable &p)
161+
{
162+
/* Notify that all ports added */
163+
FieldValueTuple finish_notice("count", to_string(g_portSet.size()));
164+
vector<FieldValueTuple> attrs = { finish_notice };
165+
p.set("PortConfigDone", attrs);
166+
}
167+
168+
void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb)
169+
{
170+
cout << "Get port configuration from ConfigDB..." << endl;
171+
172+
Table table(&cfgDb, CFG_PORT_TABLE_NAME, CONFIGDB_TABLE_NAME_SEPARATOR);
173+
std::vector<FieldValueTuple> ovalues;
174+
std::vector<string> keys;
175+
table.getKeys(keys);
176+
for ( auto &k : keys )
177+
{
178+
table.get(k, ovalues);
179+
vector<FieldValueTuple> attrs;
180+
for ( auto &v : ovalues )
181+
{
182+
FieldValueTuple attr(v.first, v.second);
183+
attrs.push_back(attr);
184+
}
185+
p.set(k, attrs);
186+
g_portSet.insert(k);
187+
}
188+
notifyPortConfigDone(p);
189+
}
190+
156191
void handlePortConfigFile(ProducerStateTable &p, string file)
157192
{
158193
cout << "Read port configuration file..." << endl;
@@ -225,11 +260,7 @@ void handlePortConfigFile(ProducerStateTable &p, string file)
225260
}
226261

227262
infile.close();
228-
229-
/* Notify that all ports added */
230-
FieldValueTuple finish_notice("count", to_string(g_portSet.size()));
231-
vector<FieldValueTuple> attrs = { finish_notice };
232-
p.set("PortConfigDone", attrs);
263+
notifyPortConfigDone(p);
233264
}
234265

235266
void handlePortConfig(ProducerStateTable &p, map<string, KeyOpFieldsValuesTuple> &port_cfg_map)

0 commit comments

Comments
 (0)