diff --git a/meta/SaiSerialize.cpp b/meta/SaiSerialize.cpp index 158789153..8757fdd55 100644 --- a/meta/SaiSerialize.cpp +++ b/meta/SaiSerialize.cpp @@ -14,6 +14,14 @@ using json = nlohmann::json; +const std::unordered_map sai_link_event_damping_algorithm_to_name_map = { + {SAI_LINK_EVENT_DAMPING_ALGORITHM_DISABLED, "SAI_LINK_EVENT_DAMPING_ALGORITHM_DISABLED"}, + {SAI_LINK_EVENT_DAMPING_ALGORITHM_AIED, "SAI_LINK_EVENT_DAMPING_ALGORITHM_AIED"}}; + +const std::unordered_map sai_redis_port_attr_to_name_map = { + {SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGORITHM, "SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGORITHM"}, + {SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGO_AIED_CONFIG, "SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGO_AIED_CONFIG"}}; + static int char_to_int( _In_ const char c) { @@ -2617,6 +2625,54 @@ std::string sai_serialize_redis_communication_mode( } } +std::string sai_serialize_redis_port_attr_id( + _In_ const sai_redis_port_attr_t value) +{ + SWSS_LOG_ENTER(); + + auto it = sai_redis_port_attr_to_name_map.find(value); + if (it != sai_redis_port_attr_to_name_map.end()) + { + return it->second; + } + + SWSS_LOG_WARN("enum value %d not found in enum sai_redis_port_attr_t.", value); + + return sai_serialize_number(value, false); +} + +std::string sai_serialize_link_event_damping_algorithm( + _In_ const sai_link_event_damping_algorithm_t value) +{ + SWSS_LOG_ENTER(); + + auto it = sai_link_event_damping_algorithm_to_name_map.find(value); + if (it != sai_link_event_damping_algorithm_to_name_map.end()) + { + return it->second; + } + + SWSS_LOG_WARN("enum value %d not found in enum sai_link_event_damping_algorithm_t.", value); + + return sai_serialize_number(value, false); +} + +std::string sai_serialize_link_event_damping_aied_config( + _In_ const sai_redis_link_event_damping_algo_aied_config_t& value) +{ + SWSS_LOG_ENTER(); + + json j; + + j["max_suppress_time"] = sai_serialize_number(value.max_suppress_time, false); + j["suppress_threshold"] = sai_serialize_number(value.suppress_threshold, false); + j["reuse_threshold"] = sai_serialize_number(value.reuse_threshold, false); + j["decay_half_life"] = sai_serialize_number(value.decay_half_life, false); + j["flap_penalty"] = sai_serialize_number(value.flap_penalty, false); + + return j.dump(); +} + // deserialize void sai_deserialize_bool( @@ -5059,3 +5115,60 @@ void sai_deserialize_redis_communication_mode( SWSS_LOG_THROW("enum '%s' not found in sai_redis_communication_mode_t", s.c_str()); } } + +void sai_deserialize_redis_port_attr_id( + _In_ const std::string& s, + _Out_ sai_redis_port_attr_t& value) +{ + SWSS_LOG_ENTER(); + + for (const auto& entry : sai_redis_port_attr_to_name_map) + { + if (s == entry.second) + { + value = entry.first; + return; + } + } + + SWSS_LOG_WARN("%s is not found in sai_redis_port_attr_to_name_map.", s.c_str()); + + sai_deserialize_number(s, value, false); +} + +// Link event damping. + +void sai_deserialize_link_event_damping_algorithm( + _In_ const std::string& s, + _Out_ sai_link_event_damping_algorithm_t& value) +{ + SWSS_LOG_ENTER(); + + for (const auto& entry : sai_link_event_damping_algorithm_to_name_map) + { + if (s == entry.second) + { + value = entry.first; + return; + } + } + + SWSS_LOG_WARN("%s is not found in sai_link_event_damping_algorithm_to_name_map.", s.c_str()); + + sai_deserialize_number(s, value, false); +} + +void sai_deserialize_link_event_damping_aied_config( + _In_ const std::string& s, + _Out_ sai_redis_link_event_damping_algo_aied_config_t& value) +{ + SWSS_LOG_ENTER(); + + json j = json::parse(s); + + sai_deserialize_number(j["max_suppress_time"], value.max_suppress_time, false); + sai_deserialize_number(j["suppress_threshold"], value.suppress_threshold, false); + sai_deserialize_number(j["reuse_threshold"], value.reuse_threshold, false); + sai_deserialize_number(j["decay_half_life"], value.decay_half_life, false); + sai_deserialize_number(j["flap_penalty"], value.flap_penalty, false); +} diff --git a/meta/sai_serialize.h b/meta/sai_serialize.h index 7d53c11cc..e40766a63 100644 --- a/meta/sai_serialize.h +++ b/meta/sai_serialize.h @@ -12,6 +12,7 @@ extern "C" { #include #include #include +#include #include #include @@ -19,6 +20,10 @@ extern "C" { #include "sairedis.h" +extern const std::unordered_map sai_link_event_damping_algorithm_to_name_map; + +extern const std::unordered_map sai_redis_port_attr_to_name_map; + // util sai_status_t transfer_attributes( @@ -288,6 +293,17 @@ std::string sai_serialize( std::string sai_serialize_redis_communication_mode( _In_ sai_redis_communication_mode_t value); +std::string sai_serialize_redis_port_attr_id( + _In_ const sai_redis_port_attr_t value); + +// Link event damping. + +std::string sai_serialize_link_event_damping_algorithm( + _In_ const sai_link_event_damping_algorithm_t value); + +std::string sai_serialize_link_event_damping_aied_config( + _In_ const sai_redis_link_event_damping_algo_aied_config_t& value); + // deserialize void sai_deserialize_enum( @@ -541,3 +557,17 @@ sai_redis_notify_syncd_t sai_deserialize_redis_notify_syncd( void sai_deserialize_redis_communication_mode( _In_ const std::string& s, _Out_ sai_redis_communication_mode_t& value); + +void sai_deserialize_redis_port_attr_id( + _In_ const std::string& s, + _Out_ sai_redis_port_attr_t& value); + +// Link event damping. + +void sai_deserialize_link_event_damping_algorithm( + _In_ const std::string& s, + _Out_ sai_link_event_damping_algorithm_t& value); + +void sai_deserialize_link_event_damping_aied_config( + _In_ const std::string& s, + _Out_ sai_redis_link_event_damping_algo_aied_config_t& value); diff --git a/unittest/meta/TestSaiSerialize.cpp b/unittest/meta/TestSaiSerialize.cpp index 46a2e0761..067ffc51b 100644 --- a/unittest/meta/TestSaiSerialize.cpp +++ b/unittest/meta/TestSaiSerialize.cpp @@ -5,6 +5,8 @@ #include "sairedis.h" #include "sairediscommon.h" +#include + #include #include @@ -14,6 +16,8 @@ using namespace saimeta; +using json = nlohmann::json; + TEST(SaiSerialize, transfer_attributes) { SWSS_LOG_ENTER(); @@ -539,6 +543,82 @@ TEST(SaiSerialize, sai_serialize_redis_communication_mode) REDIS_COMMUNICATION_MODE_REDIS_SYNC_STRING); } +TEST(SaiSerialize, sai_serialize_redis_port_attr_id) +{ + for (const auto& attr : + {SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGORITHM, SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGO_AIED_CONFIG}) + { + sai_redis_port_attr_t deserialized_attr; + sai_deserialize_redis_port_attr_id( + sai_serialize_redis_port_attr_id(attr), deserialized_attr); + + EXPECT_EQ(deserialized_attr, attr); + } + + // Undefined enum. + int index = 1000; + std::string serialized_attr = sai_serialize_redis_port_attr_id( + static_cast(SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGO_AIED_CONFIG + index)); + + EXPECT_EQ(serialized_attr, + std::to_string(SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGO_AIED_CONFIG + index)); + + sai_redis_port_attr_t deserialized_attr; + sai_deserialize_redis_port_attr_id(serialized_attr, deserialized_attr); + EXPECT_EQ(deserialized_attr, SAI_REDIS_PORT_ATTR_LINK_EVENT_DAMPING_ALGO_AIED_CONFIG + index); +} + +TEST(SaiSerialize, sai_serialize_link_event_damping_algorithm) +{ + for (const auto& algo : {SAI_LINK_EVENT_DAMPING_ALGORITHM_DISABLED, + SAI_LINK_EVENT_DAMPING_ALGORITHM_AIED}) + { + sai_link_event_damping_algorithm_t deserialized_algo; + sai_deserialize_link_event_damping_algorithm( + sai_serialize_link_event_damping_algorithm(algo), deserialized_algo); + + EXPECT_EQ(deserialized_algo, algo); + } + + // Undefined enum. + int index = 1000; + std::string serialized_attr = sai_serialize_link_event_damping_algorithm( + static_cast(SAI_LINK_EVENT_DAMPING_ALGORITHM_AIED + index)); + + EXPECT_EQ(serialized_attr, + std::to_string(SAI_LINK_EVENT_DAMPING_ALGORITHM_AIED + index)); + + sai_link_event_damping_algorithm_t deserialized_attr; + sai_deserialize_link_event_damping_algorithm(serialized_attr, deserialized_attr); + EXPECT_EQ(deserialized_attr, SAI_LINK_EVENT_DAMPING_ALGORITHM_AIED + index); +} + +TEST(SaiSerialize, sai_serialize_link_event_damping_aied_config) +{ + SWSS_LOG_ENTER(); + + sai_redis_link_event_damping_algo_aied_config_t config = { + .max_suppress_time = 500, + .suppress_threshold = 2500, + .reuse_threshold = 1000, + .decay_half_life = 100, + .flap_penalty = 100}; + + std::string expected = "{\"max_suppress_time\":\"500\",\"suppress_threshold\":\"2500\",\"reuse_threshold\":\"1000\",\"decay_half_life\":\"100\",\"flap_penalty\":\"100\"}"; + std::string serialized_config = sai_serialize_link_event_damping_aied_config(config); + + EXPECT_EQ(json::parse(serialized_config), json::parse(expected)); + + sai_redis_link_event_damping_algo_aied_config_t deserialized_config; + sai_deserialize_link_event_damping_aied_config(serialized_config, deserialized_config); + + EXPECT_EQ(deserialized_config.max_suppress_time, config.max_suppress_time); + EXPECT_EQ(deserialized_config.suppress_threshold, config.suppress_threshold); + EXPECT_EQ(deserialized_config.reuse_threshold, config.reuse_threshold); + EXPECT_EQ(deserialized_config.decay_half_life, config.decay_half_life); + EXPECT_EQ(deserialized_config.flap_penalty, config.flap_penalty); +} + TEST(SaiSerialize, sai_deserialize_queue_attr) { sai_queue_attr_t attr = SAI_QUEUE_ATTR_PORT;