|
1 | 1 | #include "sai_redis.h"
|
2 | 2 | #include "sairedis.h"
|
3 | 3 |
|
| 4 | +#include "meta/sai_serialize.h" |
| 5 | + |
4 | 6 | #include "swss/selectableevent.h"
|
5 | 7 | #include <string.h>
|
6 | 8 |
|
@@ -258,3 +260,153 @@ sai_status_t sai_api_query(
|
258 | 260 | return SAI_STATUS_INVALID_PARAMETER;
|
259 | 261 | }
|
260 | 262 | }
|
| 263 | + |
| 264 | +sai_status_t sai_query_attribute_enum_values_capability( |
| 265 | + _In_ sai_object_id_t switch_id, |
| 266 | + _In_ sai_object_type_t object_type, |
| 267 | + _In_ sai_attr_id_t attr_id, |
| 268 | + _Inout_ sai_s32_list_t *enum_values_capability) |
| 269 | +{ |
| 270 | + MUTEX(); |
| 271 | + |
| 272 | + SWSS_LOG_ENTER(); |
| 273 | + |
| 274 | + const std::string switch_id_str = sai_serialize_object_id(switch_id); |
| 275 | + const std::string object_type_str = sai_serialize_object_type(object_type); |
| 276 | + |
| 277 | + auto meta = sai_metadata_get_attr_metadata(object_type, attr_id); |
| 278 | + if (meta == NULL) |
| 279 | + { |
| 280 | + SWSS_LOG_ERROR("Failed to find attribute metadata: object type %s, attr id %d", object_type_str.c_str(), attr_id); |
| 281 | + return SAI_STATUS_INVALID_PARAMETER; |
| 282 | + } |
| 283 | + |
| 284 | + const std::string attr_id_str = sai_serialize_attr_id(*meta); |
| 285 | + const std::string list_size = std::to_string(enum_values_capability->count); |
| 286 | + |
| 287 | + const std::vector<swss::FieldValueTuple> query_arguments = |
| 288 | + { |
| 289 | + swss::FieldValueTuple("OBJECT_TYPE", object_type_str), |
| 290 | + swss::FieldValueTuple("ATTR_ID", attr_id_str), |
| 291 | + swss::FieldValueTuple("LIST_SIZE", list_size) |
| 292 | + }; |
| 293 | + |
| 294 | + SWSS_LOG_DEBUG( |
| 295 | + "Query arguments: switch %s, object type: %s, attribute: %s, count: %s", |
| 296 | + switch_id_str.c_str(), |
| 297 | + object_type_str.c_str(), |
| 298 | + attr_id_str.c_str(), |
| 299 | + list_size.c_str() |
| 300 | + ); |
| 301 | + |
| 302 | + if (g_record) |
| 303 | + { |
| 304 | + recordLine("q|attribute_enum_values_capability|" + switch_id_str + "|" + joinFieldValues(query_arguments)); |
| 305 | + } |
| 306 | + |
| 307 | + // This query will not put any data into the ASIC view, just into the |
| 308 | + // message queue |
| 309 | + g_asicState->set(switch_id_str, query_arguments, attrEnumValuesCapabilityQuery); |
| 310 | + |
| 311 | + swss::Select callback; |
| 312 | + callback.addSelectable(g_redisGetConsumer.get()); |
| 313 | + |
| 314 | + while (true) |
| 315 | + { |
| 316 | + SWSS_LOG_DEBUG("Waiting for a response"); |
| 317 | + |
| 318 | + swss::Selectable *sel; |
| 319 | + |
| 320 | + auto result = callback.select(&sel, GET_RESPONSE_TIMEOUT); |
| 321 | + |
| 322 | + if (result == swss::Select::OBJECT) |
| 323 | + { |
| 324 | + swss::KeyOpFieldsValuesTuple kco; |
| 325 | + |
| 326 | + g_redisGetConsumer->pop(kco); |
| 327 | + |
| 328 | + const std::string &message_type = kfvOp(kco); |
| 329 | + const std::string &status_str = kfvKey(kco); |
| 330 | + |
| 331 | + SWSS_LOG_DEBUG("Received response: op = %s, key = %s", message_type.c_str(), status_str.c_str()); |
| 332 | + |
| 333 | + // Ignore messages that are not in response to our query |
| 334 | + if (message_type != attrEnumValuesCapabilityResponse) |
| 335 | + { |
| 336 | + continue; |
| 337 | + } |
| 338 | + |
| 339 | + sai_status_t status; |
| 340 | + sai_deserialize_status(status_str, status); |
| 341 | + |
| 342 | + if (status == SAI_STATUS_SUCCESS) |
| 343 | + { |
| 344 | + const std::vector<swss::FieldValueTuple> &values = kfvFieldsValues(kco); |
| 345 | + |
| 346 | + if (values.size() != 2) |
| 347 | + { |
| 348 | + if (g_record) |
| 349 | + { |
| 350 | + recordLine("Q|attribute_enum_values_capability|SAI_STATUS_FAILURE"); |
| 351 | + } |
| 352 | + |
| 353 | + SWSS_LOG_ERROR("Invalid response from syncd: expected 2 values, received %d", values.size()); |
| 354 | + return SAI_STATUS_FAILURE; |
| 355 | + } |
| 356 | + |
| 357 | + const std::string &capability_str = fvValue(values[0]); |
| 358 | + const uint32_t num_capabilities = std::stoi(fvValue(values[1])); |
| 359 | + |
| 360 | + SWSS_LOG_DEBUG("Received payload: capabilites = '%s', count = %d", capability_str.c_str(), num_capabilities); |
| 361 | + |
| 362 | + enum_values_capability->count = num_capabilities; |
| 363 | + |
| 364 | + size_t position = 0; |
| 365 | + for (uint32_t i = 0; i < num_capabilities; i++) |
| 366 | + { |
| 367 | + size_t old_position = position; |
| 368 | + position = capability_str.find(",", old_position); |
| 369 | + std::string capability = capability_str.substr(old_position, position - old_position); |
| 370 | + enum_values_capability->list[i] = std::stoi(capability); |
| 371 | + |
| 372 | + // We have run out of values to add to our list |
| 373 | + if (position == std::string::npos) |
| 374 | + { |
| 375 | + if (num_capabilities != i + 1) |
| 376 | + { |
| 377 | + SWSS_LOG_WARN("Query returned less attributes than expected: expected %d, recieved %d"); |
| 378 | + } |
| 379 | + |
| 380 | + break; |
| 381 | + } |
| 382 | + |
| 383 | + // Skip the commas |
| 384 | + position++; |
| 385 | + } |
| 386 | + |
| 387 | + if (g_record) |
| 388 | + { |
| 389 | + recordLine("Q|attribute_enum_values_capability|" + status_str + "|" + joinFieldValues(values)); |
| 390 | + } |
| 391 | + } |
| 392 | + else |
| 393 | + { |
| 394 | + if (g_record) |
| 395 | + { |
| 396 | + recordLine("Q|attribute_enum_values_capability|" + status_str); |
| 397 | + } |
| 398 | + } |
| 399 | + |
| 400 | + SWSS_LOG_DEBUG("Status: %s", status_str.c_str()); |
| 401 | + return status; |
| 402 | + } |
| 403 | + } |
| 404 | + |
| 405 | + if (g_record) |
| 406 | + { |
| 407 | + recordLine("Q|attribute_enum_values_capability|SAI_STATUS_FAILURE"); |
| 408 | + } |
| 409 | + |
| 410 | + SWSS_LOG_ERROR("Failed to receive a response from syncd"); |
| 411 | + return SAI_STATUS_FAILURE; |
| 412 | +} |
0 commit comments