Skip to content

Commit

Permalink
Implement new serialization methods (sonic-net#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik authored Oct 26, 2016
1 parent dc0b0e6 commit 391ed8e
Show file tree
Hide file tree
Showing 18 changed files with 2,763 additions and 1,986 deletions.
3 changes: 3 additions & 0 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ override_dh_installinit:
sed -i 's/ENABLE_SAITHRIFT=0/ENABLE_SAITHRIFT=1/' debian/syncd.init
endif
dh_installinit

override_dh_shlibdeps:
dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info
6 changes: 6 additions & 0 deletions lib/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ libsairedis_la_SOURCES = \

libsairedis_la_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON)
libsairedis_la_LIBADD = -lhiredis -lswsscommon

bin_PROGRAMS = tests

tests_SOURCES = tests.cpp
tests_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON)
tests_LDADD = -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/lib/src/.libs -lsairedis -L$(top_srcdir)/meta/.libs -lsaimetadata
100 changes: 20 additions & 80 deletions lib/src/sai_redis_notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ void handle_switch_state_change(

SWSS_LOG_DEBUG("data: %s", data.c_str());

int index = 0;

sai_switch_oper_status_t switch_oper_status;
sai_deserialize_primitive(data, index, switch_oper_status);

sai_deserialize_switch_oper_status(data, switch_oper_status);

auto on_switch_state_change = redis_switch_notifications.on_switch_state_change;

Expand All @@ -29,47 +28,19 @@ void handle_fdb_event(

SWSS_LOG_DEBUG("data: %s", data.c_str());

int index = 0;

uint32_t count;
sai_deserialize_primitive(data, index, count);

std::vector<sai_fdb_event_notification_data_t> fdbdata;

fdbdata.resize(count);
sai_fdb_event_notification_data_t *fdbevent = NULL;

for (uint32_t i = 0; i < count; i++)
{
sai_fdb_event_notification_data_t *fdb = &fdbdata[i];

sai_status_t status = sai_deserialize_fdb_event_notification_data(data, index, fdb);

if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Unable to deserialize fdb event, status: %u", status);

return;
}
}
sai_deserialize_fdb_event_ntf(data, count, &fdbevent);

auto on_fdb_event = redis_switch_notifications.on_fdb_event;

if (on_fdb_event != NULL)
{
on_fdb_event(count, fdbdata.data());
on_fdb_event(count, fdbevent);
}

for (uint32_t i = 0; i < count; i++)
{
sai_fdb_event_notification_data_t *fdb = &fdbdata[i];

sai_status_t status = sai_deserialize_free_fdb_event_notification_data(fdb);

if (status != SAI_STATUS_SUCCESS)
{
SWSS_LOG_ERROR("Unable to free fdb event, status: %u", status);
}
}
sai_deserialize_free_fdb_event_ntf(count, fdbevent);
}

void handle_port_state_change(
Expand All @@ -79,29 +50,19 @@ void handle_port_state_change(

SWSS_LOG_DEBUG("data: %s", data.c_str());

int index = 0;

uint32_t count;
sai_port_oper_status_notification_t *portoperstatus = NULL;

sai_deserialize_primitive(data, index, count);

std::vector<sai_port_oper_status_notification_t> portdata;

portdata.resize(count);

for (uint32_t i = 0; i < count; i++)
{
sai_port_oper_status_notification_t *oper_stat = &portdata[i];

sai_deserialize_primitive(data, index, *oper_stat);
}
sai_deserialize_port_oper_status_ntf(data, count, &portoperstatus);

auto on_port_state_change = redis_switch_notifications.on_port_state_change;

if (on_port_state_change != NULL)
{
on_port_state_change(count, portdata.data());
on_port_state_change(count, portoperstatus);
}

sai_deserialize_free_port_oper_status_ntf(count, portoperstatus);
}

void handle_port_event(
Expand All @@ -111,37 +72,27 @@ void handle_port_event(

SWSS_LOG_DEBUG("data: %s", data.c_str());

int index = 0;

uint32_t count;
sai_port_event_notification_t *portevent = NULL;

sai_deserialize_primitive(data, index, count);

std::vector<sai_port_event_notification_t> portdata;

portdata.resize(count);

for (uint32_t i = 0; i < count; i++)
{
sai_port_event_notification_t *oper_stat = &portdata[i];

sai_deserialize_primitive(data, index, *oper_stat);
}
sai_deserialize_port_event_ntf(data, count, &portevent);

auto on_port_event = redis_switch_notifications.on_port_event;

if (on_port_event != NULL)
{
on_port_event(count, portdata.data());
on_port_event(count, portevent);
}

sai_deserialize_free_port_event_ntf(count, portevent);
}

void handle_switch_shutdown_request(
_In_ const std::string &data)
{
SWSS_LOG_ENTER();

SWSS_LOG_DEBUG("data: %s", data.c_str());
SWSS_LOG_NOTICE("switch shutdown request");

auto on_switch_shutdown_request = redis_switch_notifications.on_switch_shutdown_request;

Expand All @@ -159,26 +110,15 @@ void handle_packet_event(

SWSS_LOG_DEBUG("data: %s, values: %lu", data.c_str(), values.size());

sai_size_t buffer_size;

int index = 0;

sai_deserialize_primitive(data, index, buffer_size);

std::vector<unsigned char> buffer;

buffer.resize(buffer_size);

sai_deserialize_buffer(data, index, buffer_size, buffer.data());

SaiAttributeList list(SAI_OBJECT_TYPE_PACKET, values, false);
SWSS_LOG_ERROR("not implemented");

/*
auto on_packet_event = redis_switch_notifications.on_packet_event;
if (on_packet_event != NULL)
{
on_packet_event(buffer.data(), buffer_size, list.get_attr_count(), list.get_attr_list());
}
}*/
}

void handle_notification(
Expand Down
56 changes: 56 additions & 0 deletions lib/src/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <map>

#include "swss/logger.h"

extern "C" {
#include <sai.h>
}

const char* profile_get_value(
_In_ sai_switch_profile_id_t profile_id,
_In_ const char* variable)
{
SWSS_LOG_ENTER();

return NULL;
}

int profile_get_next_value(
_In_ sai_switch_profile_id_t profile_id,
_Out_ const char** variable,
_Out_ const char** value)
{
SWSS_LOG_ENTER();

if (value == NULL)
{
SWSS_LOG_INFO("resetting profile map iterator");

return 0;
}

if (variable == NULL)
{
SWSS_LOG_WARN("variable is null");
return -1;
}

SWSS_LOG_INFO("iterator reached end");
return -1;
}

service_method_table_t test_services = {
profile_get_value,
profile_get_next_value
};

int main()
{
swss::Logger::getInstance().setMinPrio(swss::Logger::SWSS_DEBUG);

SWSS_LOG_ENTER();

sai_api_initialize(0, (service_method_table_t*)&test_services);

return 0;
}
6 changes: 3 additions & 3 deletions meta/sai_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ const sai_attr_metadata_t* get_attribute_metadata(
{
SWSS_LOG_ENTER();

SWSS_LOG_DEBUG("objecttype: %s, attrid: %d", get_object_type_name(objecttype), attrid);

const auto &it = AttributesMetadata.find(objecttype);

if (it == AttributesMetadata.end())
Expand All @@ -270,11 +268,13 @@ const sai_attr_metadata_t* get_attribute_metadata(

if (ita == attrset.end())
{
SWSS_LOG_ERROR("attribute %d not found in metadata", attrid);
SWSS_LOG_ERROR("attribute %d not found in metadata for object type %s", attrid, get_object_type_name(objecttype));

return NULL;
}

SWSS_LOG_DEBUG("objecttype: %s, attrid: %s", get_object_type_name(objecttype), ita->second->attridname);

return ita->second;
}

Expand Down
3 changes: 0 additions & 3 deletions meta/sai_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,6 @@ extern const sai_attribute_t* get_object_previous_attr(
extern sai_status_t meta_init_db();
extern void meta_init();

extern const sai_attr_metadata_t* sai_metadata_get_attr_metadata(
_In_ sai_object_type_t objecttype,
_In_ sai_attr_id_t attrid);

// GENERIC FUNCTION POINTERS

Expand Down
2 changes: 1 addition & 1 deletion meta/sai_meta_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ const sai_attr_metadata_t sai_port_attr_metadata[] = {
.objecttype = SAI_OBJECT_TYPE_PORT,
.attrid = SAI_PORT_ATTR_NUMBER_OF_PRIORITY_GROUPS,
.attridname = "SAI_PORT_ATTR_NUMBER_OF_PRIORITY_GROUPS",
.serializationtype = SAI_SERIALIZATION_TYPE_BOOL,
.serializationtype = SAI_SERIALIZATION_TYPE_UINT32,
.flags = SAI_ATTR_FLAGS_READ_ONLY,
.allowedobjecttypes = { },
.allownullobjectid = false,
Expand Down
23 changes: 0 additions & 23 deletions meta/sai_meta_sanity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,29 +950,6 @@ std::string get_attr_info(const sai_attr_metadata_t& md)
std::string(get_serialization_type_name(md.serializationtype));
}

const sai_attr_metadata_t* sai_metadata_get_attr_metadata(
_In_ sai_object_type_t objecttype,
_In_ sai_attr_id_t attrid)
{
SWSS_LOG_ENTER();

if ((objecttype > SAI_OBJECT_TYPE_NULL) &&
(objecttype < SAI_OBJECT_TYPE_MAX))
{
auto &attrset = AttributesMetadata[objecttype];

auto it = attrset.find(attrid);

if (it != attrset.end())
{
return it->second;
}
}

SWSS_LOG_ERROR("unable to find metadata for object type %d attr id %d", objecttype, attrid);
return NULL;
}

void metadata_sanity_check(const sai_attr_metadata_t& md)
{
SWSS_LOG_ENTER();
Expand Down
5 changes: 3 additions & 2 deletions meta/saiattributelist.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "saiattributelist.h"
#include "sai_meta.h"
#include "saiserialize.h"

SaiAttributeList::SaiAttributeList(
_In_ const sai_object_type_t object_type,
Expand All @@ -23,7 +24,7 @@ SaiAttributeList::SaiAttributeList(

sai_deserialize_attr_id(str_attr_id, attr.id);

auto meta = sai_metadata_get_attr_metadata(object_type, attr.id);
auto meta = get_attribute_metadata(object_type, attr.id);

if (meta == NULL)
{
Expand Down Expand Up @@ -64,7 +65,7 @@ std::vector<swss::FieldValueTuple> SaiAttributeList::serialize_attr_list(
{
const sai_attribute_t *attr = &attr_list[index];

auto meta = sai_metadata_get_attr_metadata(object_type, attr->id);
auto meta = get_attribute_metadata(object_type, attr->id);

if (meta == NULL)
{
Expand Down
Loading

0 comments on commit 391ed8e

Please sign in to comment.