Skip to content

Commit

Permalink
[portsyncd] support read port configuration from configDB (sonic-net#409
Browse files Browse the repository at this point in the history
)

read port configuration from configDB when '-p' is not specified.

Signed-off-by: Wataru Ishida <ishida.wataru@lab.ntt.co.jp>
  • Loading branch information
Wataru Ishida authored and lguohan committed Dec 7, 2017
1 parent 00ea0ab commit d923f71
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions portsyncd/portsyncd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include "subscriberstatetable.h"
#include "exec.h"

#define DEFAULT_PORT_CONFIG_FILE "port_config.ini"

using namespace std;
using namespace swss;

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

void handlePortConfigFile(ProducerStateTable &p, string file);
void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb);
void handleVlanIntfFile(string file);
void handlePortConfig(ProducerStateTable &p, map<string, KeyOpFieldsValuesTuple> &port_cfg_map);

int main(int argc, char **argv)
{
Logger::linkToDbNative("portsyncd");
int opt;
string port_config_file = DEFAULT_PORT_CONFIG_FILE;
string port_config_file;
map<string, KeyOpFieldsValuesTuple> port_cfg_map;

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

handlePortConfigFile(p, port_config_file);
if (!port_config_file.empty())
{
handlePortConfigFile(p, port_config_file);
} else {
handlePortConfigFromConfigDB(p, cfgDb);
}

s.addSelectable(&netlink);
s.addSelectable(&portCfg);
Expand Down Expand Up @@ -153,6 +157,37 @@ int main(int argc, char **argv)
return 1;
}

static void notifyPortConfigDone(ProducerStateTable &p)
{
/* Notify that all ports added */
FieldValueTuple finish_notice("count", to_string(g_portSet.size()));
vector<FieldValueTuple> attrs = { finish_notice };
p.set("PortConfigDone", attrs);
}

void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb)
{
cout << "Get port configuration from ConfigDB..." << endl;

Table table(&cfgDb, CFG_PORT_TABLE_NAME, CONFIGDB_TABLE_NAME_SEPARATOR);
std::vector<FieldValueTuple> ovalues;
std::vector<string> keys;
table.getKeys(keys);
for ( auto &k : keys )
{
table.get(k, ovalues);
vector<FieldValueTuple> attrs;
for ( auto &v : ovalues )
{
FieldValueTuple attr(v.first, v.second);
attrs.push_back(attr);
}
p.set(k, attrs);
g_portSet.insert(k);
}
notifyPortConfigDone(p);
}

void handlePortConfigFile(ProducerStateTable &p, string file)
{
cout << "Read port configuration file..." << endl;
Expand Down Expand Up @@ -225,11 +260,7 @@ void handlePortConfigFile(ProducerStateTable &p, string file)
}

infile.close();

/* Notify that all ports added */
FieldValueTuple finish_notice("count", to_string(g_portSet.size()));
vector<FieldValueTuple> attrs = { finish_notice };
p.set("PortConfigDone", attrs);
notifyPortConfigDone(p);
}

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

0 comments on commit d923f71

Please sign in to comment.