Skip to content

Commit

Permalink
[meta] Use metadata to check port notification port_id type (sonic-ne…
Browse files Browse the repository at this point in the history
…t#1469)

Use metadata for validation instead of hard coded values
  • Loading branch information
kcudnik authored and shiraez committed Dec 12, 2024
1 parent 7425619 commit 8835ba7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
67 changes: 51 additions & 16 deletions meta/Meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <inttypes.h>

#include <boost/algorithm/string/join.hpp>

#include <set>

// TODO add validation for all oids belong to the same switch
Expand Down Expand Up @@ -6694,24 +6696,14 @@ void Meta::meta_sai_on_port_state_change_single(

auto ot = objectTypeQuery(data.port_id);

bool valid = false;
bool valid = isPortObjectIdValid(ot);

switch (ot)
if (!valid)
{
// TODO hardcoded types, must advance SAI repository commit to get metadata for this
case SAI_OBJECT_TYPE_PORT:
case SAI_OBJECT_TYPE_BRIDGE_PORT:
case SAI_OBJECT_TYPE_LAG:

valid = true;
break;

default:

SWSS_LOG_ERROR("data.port_id %s has unexpected type: %s, expected PORT, BRIDGE_PORT or LAG",
sai_serialize_object_id(data.port_id).c_str(),
sai_serialize_object_type(ot).c_str());
break;
SWSS_LOG_ERROR("data.port_id %s has unexpected type: %s, expected: %s",
sai_serialize_object_id(data.port_id).c_str(),
sai_serialize_object_type(ot).c_str(),
boost::algorithm::join(getValidPortObjectTypes(), ",").c_str());
}

if (valid && !m_oids.objectReferenceExists(data.port_id))
Expand Down Expand Up @@ -7131,3 +7123,46 @@ void Meta::populate(
}
}
}

bool Meta::isPortObjectIdValid(
_In_ sai_object_type_t object_type)
{
SWSS_LOG_ENTER();

auto members = sai_metadata_struct_members_sai_port_oper_status_notification_t;

for (size_t i = 0; members[i]; i++)
{
auto* mb = members[i];

if (mb->membername != std::string("port_id"))
continue;

for (size_t idx = 0; idx < mb->allowedobjecttypeslength; idx++)
{
if (mb->allowedobjecttypes[idx] == object_type)
return true;
}

return false;
}

SWSS_LOG_THROW("port_id member not found on sai_port_oper_status_notification");
}

std::vector<std::string> Meta::getValidPortObjectTypes()
{
SWSS_LOG_ENTER();

auto md = sai_metadata_enum_sai_object_type_t;

std::vector<std::string> v;

for (size_t i = 0; i < md.valuescount; i++)
{
if (isPortObjectIdValid((sai_object_type_t)md.values[i]))
v.push_back(md.valuesshortnames[i]);
}

return v;
}
5 changes: 5 additions & 0 deletions meta/Meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ namespace saimeta
static bool is_ipv6_mask_valid(
_In_ const uint8_t* mask);

static bool isPortObjectIdValid(
_In_ sai_object_type_t object_type);

static std::vector<std::string> getValidPortObjectTypes();

private: // unit tests helpers

bool meta_unittests_get_and_erase_set_readonly_flag(
Expand Down
24 changes: 24 additions & 0 deletions unittest/meta/TestMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <gtest/gtest.h>

#include <boost/algorithm/string/join.hpp>

#include <memory>

#define VLAN_ID 2
Expand Down Expand Up @@ -1850,3 +1852,25 @@ TEST(Meta, remove_prefix_compression_entry)

EXPECT_EQ(SAI_STATUS_INVALID_PARAMETER, sai.remove(e));
}

TEST(Meta, isPortObjectIdValid)
{
EXPECT_EQ(Meta::isPortObjectIdValid(SAI_OBJECT_TYPE_PORT), true);
EXPECT_EQ(Meta::isPortObjectIdValid(SAI_OBJECT_TYPE_BRIDGE_PORT), true);
EXPECT_EQ(Meta::isPortObjectIdValid(SAI_OBJECT_TYPE_LAG), true);

EXPECT_EQ(Meta::isPortObjectIdValid(SAI_OBJECT_TYPE_TUNNEL),false);
EXPECT_EQ(Meta::isPortObjectIdValid(SAI_OBJECT_TYPE_NULL), false);
EXPECT_EQ(Meta::isPortObjectIdValid(SAI_OBJECT_TYPE_VLAN), false);
}

TEST(Meta, getValidPortObjectTypes)
{
auto v = Meta::getValidPortObjectTypes();

EXPECT_EQ(v.size(), 3);

auto s = boost::algorithm::join(v, ",");

EXPECT_EQ(s, "PORT,LAG,BRIDGE_PORT");
}

0 comments on commit 8835ba7

Please sign in to comment.