|
| 1 | +#include "namelabelmapper.h" |
| 2 | +#include <limits> |
| 3 | +#include <sstream> |
| 4 | +#include <string> |
| 5 | +#include <nlohmann/json.hpp> |
| 6 | +#include "logger.h" |
| 7 | +#include "sai_serialize.h" |
| 8 | +extern "C" { |
| 9 | +#include "sai.h" |
| 10 | +} |
| 11 | +using ::nlohmann::json; |
| 12 | +namespace { |
| 13 | +std::string convertToDBField(_In_ const sai_object_type_t object_type, |
| 14 | + _In_ const std::string& key) { |
| 15 | + return sai_serialize_object_type(object_type) + "|" + key; |
| 16 | +} |
| 17 | +} // namespace |
| 18 | +NameLabelMapper::NameLabelMapper() |
| 19 | + : m_db("STATE_DB", 0), m_table(&m_db, "SAI_KEY_LABEL_MAP") {} |
| 20 | +bool NameLabelMapper::setLabel(_In_ sai_object_type_t object_type, |
| 21 | + _In_ const std::string& key, |
| 22 | + _In_ std::string& label) { |
| 23 | + SWSS_LOG_ENTER(); |
| 24 | + if (existsLabel(object_type, key)) { |
| 25 | + SWSS_LOG_ERROR( |
| 26 | + "Key %s with SAI object type %d already exists in label mapper", |
| 27 | + key.c_str(), object_type); |
| 28 | + return false; |
| 29 | + } |
| 30 | + m_labelTables[object_type][key] = label; |
| 31 | + SWSS_LOG_INFO("Created new label %s for Key %s with SAI object type %d", |
| 32 | + label.c_str(), key.c_str(), object_type); |
| 33 | + return true; |
| 34 | +} |
| 35 | +bool NameLabelMapper::getLabel(_In_ sai_object_type_t object_type, |
| 36 | + _In_ const std::string& key, |
| 37 | + _Out_ std::string& label) { |
| 38 | + SWSS_LOG_ENTER(); |
| 39 | + if (!existsLabel(object_type, key)) { |
| 40 | + SWSS_LOG_INFO( |
| 41 | + "Key %s with SAI object type %d does not exist in label mapper", |
| 42 | + key.c_str(), object_type); |
| 43 | + return false; |
| 44 | + } |
| 45 | + label = m_labelTables[object_type][key]; |
| 46 | + return true; |
| 47 | +} |
| 48 | +bool NameLabelMapper::eraseLabel(_In_ sai_object_type_t object_type, |
| 49 | + _In_ const std::string& key) { |
| 50 | + SWSS_LOG_ENTER(); |
| 51 | + if (!existsLabel(object_type, key)) { |
| 52 | + SWSS_LOG_ERROR( |
| 53 | + "Key %s with SAI object type %d does not exist in " |
| 54 | + "label mapper when erasing", |
| 55 | + key.c_str(), object_type); |
| 56 | + return false; |
| 57 | + } |
| 58 | + m_labelTables[object_type].erase(key); |
| 59 | + return true; |
| 60 | +} |
| 61 | +void NameLabelMapper::deleteMapperInDb() { |
| 62 | + SWSS_LOG_ENTER(); |
| 63 | + m_table.del(""); |
| 64 | +} |
| 65 | +size_t NameLabelMapper::getNumEntries( |
| 66 | + _In_ sai_object_type_t object_type) const { |
| 67 | + SWSS_LOG_ENTER(); |
| 68 | + return (m_labelTables[object_type].size()); |
| 69 | +} |
| 70 | +bool NameLabelMapper::existsLabel(_In_ sai_object_type_t object_type, |
| 71 | + _In_ const std::string& key) const { |
| 72 | + SWSS_LOG_ENTER(); |
| 73 | + return m_labelTables[object_type].find(key) != |
| 74 | + m_labelTables[object_type].end(); |
| 75 | +} |
| 76 | +std::string NameLabelMapper::generateKeyFromTableAndObjectName( |
| 77 | + std::string table_name, std::string object_name) { |
| 78 | + return table_name + object_name; |
| 79 | +} |
| 80 | +std::string NameLabelMapper::generateUniqueLabel() { |
| 81 | + char label_buf[UNIQUE_LABEL_SIZE]; |
| 82 | + uint64_t msec = std::chrono::duration_cast<std::chrono::microseconds>( |
| 83 | + std::chrono::system_clock::now().time_since_epoch()) |
| 84 | + .count(); |
| 85 | + snprintf(label_buf, UNIQUE_LABEL_SIZE, "%" PRIu64 "", msec); |
| 86 | + return std::string(reinterpret_cast<char const*>(label_buf)); |
| 87 | +} |
| 88 | +bool NameLabelMapper::allocateLabel(_In_ sai_object_type_t object_type, |
| 89 | + _In_ const std::string& key, |
| 90 | + _Out_ std::string& label) { |
| 91 | + if (!getLabel(object_type, key, label)) { |
| 92 | + label = generateUniqueLabel(); |
| 93 | + return false; |
| 94 | + } |
| 95 | + return true; |
| 96 | +} |
| 97 | +std::string NameLabelMapper::dumpStateCache() { |
| 98 | + json cache = json({}); |
| 99 | + for (int i = 0; i < SAI_OBJECT_TYPE_MAX; i++) { |
| 100 | + if (m_labelTables[i].empty()) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + json label_mapper_j = json({}); |
| 104 | + for (const auto& kv_pair : m_labelTables[i]) { |
| 105 | + label_mapper_j[kv_pair.first] = kv_pair.second; |
| 106 | + } |
| 107 | + std::string sai_object_type = |
| 108 | + sai_serialize_object_type(static_cast<sai_object_type_t>(i)); |
| 109 | + cache[sai_object_type] = label_mapper_j; |
| 110 | + } |
| 111 | + return cache.dump(4); |
| 112 | +} |
| 113 | +void NameLabelMapper::saveMapperToDb() { |
| 114 | + for (int i = 0; i < SAI_OBJECT_TYPE_MAX; i++) { |
| 115 | + if (m_labelTables[i].empty()) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + for (const auto& kv_pair : m_labelTables[i]) { |
| 119 | + auto key = kv_pair.first; |
| 120 | + auto label = kv_pair.second; |
| 121 | + m_table.hset("", convertToDBField(static_cast<sai_object_type_t>(i), key), |
| 122 | + label); |
| 123 | + SWSS_LOG_INFO( |
| 124 | + "label %s for Key %s with SAI object type %d save into state_db", |
| 125 | + label.c_str(), key.c_str(), i); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +void NameLabelMapper::readMapperFromDb() { |
| 130 | + std::vector<swss::FieldValueTuple> tuples; |
| 131 | + m_table.get("", tuples); |
| 132 | + SWSS_LOG_INFO("m_table->get size %zd", tuples.size()); |
| 133 | + for (auto& fv : tuples) { |
| 134 | + std::string combo = fvField(fv); |
| 135 | + std::string label = fvValue(fv); |
| 136 | + SWSS_LOG_INFO("Got field %s label %s from db", combo.c_str(), |
| 137 | + label.c_str()); |
| 138 | + size_t pos = 0; |
| 139 | + std::string obj_type_str, key; |
| 140 | + if ((pos = combo.find("|")) != std::string::npos) { |
| 141 | + obj_type_str = combo.substr(0, pos); |
| 142 | + key = combo.substr(pos + 1, combo.size() - pos - 1); |
| 143 | + sai_object_type_t sai_object_type; |
| 144 | + sai_deserialize_object_type(obj_type_str, sai_object_type); |
| 145 | + setLabel(sai_object_type, key, label); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
0 commit comments