xref: /openbmc/entity-manager/src/entity_manager/topology.hpp (revision 7962944f072b54e22075e8290e1e6708bdb30c38)
1 #pragma once
2 
3 #include <nlohmann/json.hpp>
4 
5 #include <set>
6 #include <unordered_map>
7 
8 using Association = std::tuple<std::string, std::string, std::string>;
9 
10 using BoardPathsView = decltype(std::views::keys(
11     std::declval<std::map<std::string, std::string>&>()));
12 
13 class Topology
14 {
15   public:
16     explicit Topology() = default;
17 
18     void addBoard(const std::string& path, const std::string& boardType,
19                   const std::string& boardName,
20                   const nlohmann::json& exposesItem);
21     std::unordered_map<std::string, std::set<Association>> getAssocs(
22         BoardPathsView boardPaths);
23     void remove(const std::string& boardName);
24 
25   private:
26     using Path = std::string;
27     using BoardType = std::string;
28     using BoardName = std::string;
29     using PortType = std::string;
30 
31     void addDownstreamPort(const Path& path, const nlohmann::json& exposesItem);
32 
33     // e.g. contained_by, containing, powered_by, ...
34     using AssocName = std::string;
35 
36     // @brief: fill associations map with the associations for a port identifier
37     // such as 'MB Upstream Port'
38     void fillAssocsForPortId(
39         std::unordered_map<std::string, std::set<Association>>& result,
40         BoardPathsView boardPaths, const std::set<Path>& upstreamPaths,
41         const std::set<Path>& downstreamPaths);
42 
43     void fillAssocForPortId(
44         std::unordered_map<std::string, std::set<Association>>& result,
45         BoardPathsView boardPaths, const Path& upstream,
46         const Path& downstream);
47 
48     static std::optional<std::string> getOppositeAssoc(
49         const AssocName& assocName);
50 
51     std::unordered_map<PortType, std::set<Path>> upstreamPorts;
52     std::unordered_map<PortType, std::set<Path>> downstreamPorts;
53     std::set<Path> powerPaths;
54     std::unordered_map<Path, BoardType> boardTypes;
55     std::unordered_map<BoardName, Path> boardNames;
56 };
57