xref: /openbmc/entity-manager/src/entity_manager/topology.hpp (revision 9b95ae49198854c3726f3811ac84d9e1c8fa33a7)
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 AssocName
14 {
15   public:
16     std::string name;
17     std::string reverse;
18 
19     AssocName(const std::string& name, const std::string& reverse,
20               const std::set<std::string>& allowedOnBoardTypes,
21               const std::set<std::string>& allowedOnBoardTypesReverse);
22     AssocName() = delete;
23 
24     // The type (e.g. Chassis, Board, Valve, ...) on which the association is
25     // allowed
26     std::set<std::string> allowedOnBoardTypes;
27 
28     // The type (e.g. Chassis, Board, Valve, ...) on which the reverse
29     // association is allowed
30     std::set<std::string> allowedOnBoardTypesReverse;
31 
32     AssocName getReverse() const;
33 
34     bool operator==(const AssocName& other) const = default;
35     bool operator<(const AssocName& other) const;
36 };
37 
38 extern const std::vector<AssocName> supportedAssocs;
39 
40 class Topology
41 {
42   public:
43     explicit Topology() = default;
44 
45     void addBoard(const std::string& path, const std::string& boardType,
46                   const std::string& boardName,
47                   const nlohmann::json& exposesItem);
48     std::unordered_map<std::string, std::set<Association>> getAssocs(
49         BoardPathsView boardPaths);
50     void remove(const std::string& boardName);
51 
52   private:
53     using Path = std::string;
54     using BoardType = std::string;
55     using BoardName = std::string;
56     using PortType = std::string;
57 
58     void addDownstreamPort(const Path& path, const nlohmann::json& exposesItem);
59 
60     // @brief: fill associations map with the associations for a port identifier
61     // such as 'MB Upstream Port'
62     void fillAssocsForPortId(
63         std::unordered_map<std::string, std::set<Association>>& result,
64         BoardPathsView boardPaths,
65         const std::map<Path, std::set<AssocName>>& pathAssocs);
66 
67     void fillAssocForPortId(
68         std::unordered_map<std::string, std::set<Association>>& result,
69         BoardPathsView boardPaths, const Path& upstream, const Path& downstream,
70         const AssocName& assocName);
71 
72     void addConfiguredPort(const Path& path, const nlohmann::json& exposesItem);
73     void addPort(const PortType& port, const Path& path,
74                  const AssocName& assocName);
75 
76     static std::optional<AssocName> getAssocByName(const std::string& name);
77 
78     // Maps the port name to the participating paths.
79     // each path also has their role(s) in the association.
80     // For example a PSU path which is part of "MB Upstream Port"
81     // will have "powering" role.
82     std::unordered_map<PortType, std::map<Path, std::set<AssocName>>> ports;
83 
84     std::unordered_map<Path, BoardType> boardTypes;
85     std::unordered_map<BoardName, Path> boardNames;
86 };
87