summaryrefslogtreecommitdiff
path: root/redfish-core/src/registries.cpp
blob: 2a426bfa9b3cd4b29bc9b661c91c86e9d0532692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#include "registries.hpp"

// We need the registries_selector pulled into some cpp part so that the
// registration hooks run.
// NOLINTNEXTLINE(misc-include-cleaner)
#include "registries_selector.hpp"
#include "str_utility.hpp"

#include <algorithm>
#include <cstring>
#include <functional>
#include <map>
#include <optional>
#include <ranges>
#include <span>
#include <string>
#include <string_view>
#include <vector>

namespace redfish::registries
{

auto allRegistries() -> std::map<std::string, RegistryEntry>&
{
    static std::map<std::string, RegistryEntry> registries;
    return registries;
}

auto getRegistryFromPrefix(const std::string& registryName)
    -> std::optional<RegistryEntryRef>
{
    auto& registries = allRegistries();
    if (auto it = registries.find(registryName); it != registries.end())
    {
        return std::ref(it->second);
    }

    return std::nullopt;
}

auto getRegistryMessagesFromPrefix(const std::string& registryName)
    -> MessageEntries
{
    auto registry = getRegistryFromPrefix(registryName);
    if (!registry)
    {
        return {};
    }

    return registry->get().entries;
}

const Message* getMessageFromRegistry(const std::string& messageKey,
                                      std::span<const MessageEntry> registry)
{
    std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if(
        registry, [&messageKey](const MessageEntry& messageEntry) {
            return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
        });
    if (messageIt != registry.end())
    {
        return &messageIt->second;
    }

    return nullptr;
}

const Message* getMessage(std::string_view messageID)
{
    // Redfish MessageIds are in the form
    // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
    // the right Message
    std::vector<std::string> fields;
    fields.reserve(4);
    bmcweb::split(fields, messageID, '.');
    if (fields.size() != 4)
    {
        return nullptr;
    }

    const std::string& registryName = fields[0];
    const std::string& messageKey = fields[3];

    // Find the right registry and check it for the MessageKey
    return getMessageFromRegistry(messageKey,
                                  getRegistryMessagesFromPrefix(registryName));
}

} // namespace redfish::registries