Skip to content

Commit

Permalink
Moved common methods/ds to a different file
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Reddy Karri <vkarri@nvidia.com>
  • Loading branch information
vivekrnv committed May 3, 2022
1 parent 6909333 commit 7f26e83
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 90 deletions.
2 changes: 1 addition & 1 deletion portsyncd/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ else
DBGFLAGS = -g
endif

portsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp portsyncd.cpp linksync.cpp $(top_srcdir)/cfgmgr/shellcmd.h
portsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp portsyncd.cpp linksync.cpp portsyncd_helper.cpp $(top_srcdir)/cfgmgr/shellcmd.h

portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)
portsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)
Expand Down
91 changes: 2 additions & 89 deletions portsyncd/linksync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
#include <netlink/route/link.h>
#include "logger.h"
#include "netmsg.h"
#include "dbconnector.h"
#include "producerstatetable.h"
#include "subscriberstatetable.h"
#include "tokenize.h"
#include "exec.h"

Expand All @@ -32,18 +29,8 @@ const string MGMT_PREFIX = "eth";
const string INTFS_PREFIX = "Ethernet";
const string LAG_PREFIX = "PortChannel";

/*
* This g_portSet contains all the front panel ports that the corresponding
* host interfaces needed to be created. When this LinkSync class is
* initialized, we check the database to see if some of the ports' host
* interfaces are already created and remove them from this set. We will
* remove the rest of the ports in the set when receiving the first netlink
* message indicating that the host interfaces are created. After the set
* is empty, we send out the signal PortInitDone. g_init is used to limit the
* command to be run only once.
*/
set<string> g_portSet;
bool g_init;
extern set<string> g_portSet;
extern bool g_init;

LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) :
m_portTableProducer(appl_db, APP_PORT_TABLE_NAME),
Expand Down Expand Up @@ -273,77 +260,3 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj)
}
}

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, bool warm)
{
SWSS_LOG_ENTER();

SWSS_LOG_NOTICE("Getting port configuration from ConfigDB...");

Table table(&cfgDb, CFG_PORT_TABLE_NAME);
std::vector<FieldValueTuple> ovalues;
std::vector<string> keys;
table.getKeys(keys);

if (keys.empty())
{
SWSS_LOG_NOTICE("ConfigDB does not have port information, "
"however ports can be added later on, continuing...");
}

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);
}
if (!warm)
{
p.set(k, attrs);
}
g_portSet.insert(k);
}
if (!warm)
{
notifyPortConfigDone(p);
}

}

void handlePortConfig(ProducerStateTable &p, map<string, KeyOpFieldsValuesTuple> &port_cfg_map)
{
auto it = port_cfg_map.begin();
while (it != port_cfg_map.end())
{
KeyOpFieldsValuesTuple entry = it->second;
string key = kfvKey(entry);
string op = kfvOp(entry);
auto values = kfvFieldsValues(entry);

/* only push down port config when port is not in hostif create pending state */
if (g_portSet.find(key) == g_portSet.end())
{
/* No support for port delete yet */
if (op == SET_COMMAND)
{
p.set(key, values);
}

it = port_cfg_map.erase(it);
}
else
{
it++;
}
}
}
2 changes: 2 additions & 0 deletions portsyncd/linksync.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "producerstatetable.h"
#include "netmsg.h"
#include "exec.h"
#include "warm_restart.h"
#include "shellcmd.h"

#include <map>
#include <set>
Expand Down
92 changes: 92 additions & 0 deletions portsyncd/portsyncd_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "portsyncd/linksync.h"

using namespace std;
using namespace swss;

/*
* This g_portSet contains all the front panel ports that the corresponding
* host interfaces needed to be created. When this LinkSync class is
* initialized, we check the database to see if some of the ports' host
* interfaces are already created and remove them from this set. We will
* remove the rest of the ports in the set when receiving the first netlink
* message indicating that the host interfaces are created. After the set
* is empty, we send out the signal PortInitDone. g_init is used to limit the
* command to be run only once.
*/
set<string> g_portSet;
bool g_init;

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, bool warm)
{
SWSS_LOG_ENTER();

SWSS_LOG_NOTICE("Getting port configuration from ConfigDB...");

Table table(&cfgDb, CFG_PORT_TABLE_NAME);
std::vector<FieldValueTuple> ovalues;
std::vector<string> keys;
table.getKeys(keys);

if (keys.empty())
{
SWSS_LOG_NOTICE("ConfigDB does not have port information, "
"however ports can be added later on, continuing...");
}

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);
}
if (!warm)
{
p.set(k, attrs);
}
g_portSet.insert(k);
}
if (!warm)
{
notifyPortConfigDone(p);
}

}

void handlePortConfig(ProducerStateTable &p, map<string, KeyOpFieldsValuesTuple> &port_cfg_map)
{
auto it = port_cfg_map.begin();
while (it != port_cfg_map.end())
{
KeyOpFieldsValuesTuple entry = it->second;
string key = kfvKey(entry);
string op = kfvOp(entry);
auto values = kfvFieldsValues(entry);

/* only push down port config when port is not in hostif create pending state */
if (g_portSet.find(key) == g_portSet.end())
{
/* No support for port delete yet */
if (op == SET_COMMAND)
{
p.set(key, values);
}

it = port_cfg_map.erase(it);
}
else
{
it++;
}
}
}
1 change: 1 addition & 0 deletions tests/mock_tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ tests_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis -lpthr

tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \
$(top_srcdir)/portsyncd/linksync.cpp \
$(top_srcdir)/portsyncd/portsyncd_helper.cpp \
mock_dbconnector.cpp \
common/mock_shell_command.cpp \
mock_table.cpp \
Expand Down

0 comments on commit 7f26e83

Please sign in to comment.