blob: a55b99508273240b3af87b3e9f2d5f04749059e5 [file] [log] [blame]
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +00001#pragma once
2
3#include <nlohmann/json.hpp>
4
5#include <set>
6#include <unordered_map>
7
8using Association = std::tuple<std::string, std::string, std::string>;
9
Alexander Hansen32e74182025-08-20 16:16:00 +020010using BoardPathsView = decltype(std::views::keys(
11 std::declval<std::map<std::string, std::string>&>()));
12
Alexander Hansen7b2a77f2025-08-27 12:27:57 +020013class AssocName
14{
15 public:
16 std::string name;
17 std::string reverse;
18
19 AssocName(const std::string& name, const std::string& reverse);
20 AssocName() = delete;
21
22 AssocName getReverse() const;
23
24 bool operator==(const AssocName& other) const = default;
25 bool operator<(const AssocName& other) const;
26};
27
Alexander Hansen90fabb02025-08-27 14:15:17 +020028extern const std::vector<AssocName> supportedAssocs;
29
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000030class Topology
31{
32 public:
33 explicit Topology() = default;
34
35 void addBoard(const std::string& path, const std::string& boardType,
Matt Spinler6eb60972023-08-14 16:36:20 -050036 const std::string& boardName,
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000037 const nlohmann::json& exposesItem);
Alexander Hansen0519f572025-08-19 18:11:04 +020038 std::unordered_map<std::string, std::set<Association>> getAssocs(
Alexander Hansen32e74182025-08-20 16:16:00 +020039 BoardPathsView boardPaths);
Matt Spinler6eb60972023-08-14 16:36:20 -050040 void remove(const std::string& boardName);
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000041
42 private:
43 using Path = std::string;
44 using BoardType = std::string;
Matt Spinler6eb60972023-08-14 16:36:20 -050045 using BoardName = std::string;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000046 using PortType = std::string;
47
Alexander Hansenec938ad2025-08-19 16:48:44 +020048 void addDownstreamPort(const Path& path, const nlohmann::json& exposesItem);
49
Alexander Hansend7be0ee2025-08-20 14:16:15 +020050 // @brief: fill associations map with the associations for a port identifier
51 // such as 'MB Upstream Port'
52 void fillAssocsForPortId(
53 std::unordered_map<std::string, std::set<Association>>& result,
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020054 BoardPathsView boardPaths,
55 const std::map<Path, std::set<AssocName>>& pathAssocs);
Alexander Hansend7be0ee2025-08-20 14:16:15 +020056
Alexander Hansen97a1c932025-08-20 16:28:52 +020057 void fillAssocForPortId(
58 std::unordered_map<std::string, std::set<Association>>& result,
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020059 BoardPathsView boardPaths, const Path& upstream, const Path& downstream,
60 const AssocName& assocName);
61
Alexander Hansen90fabb02025-08-27 14:15:17 +020062 void addConfiguredPort(const Path& path, const nlohmann::json& exposesItem);
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020063 void addPort(const PortType& port, const Path& path,
64 const AssocName& assocName);
Alexander Hansen97a1c932025-08-20 16:28:52 +020065
Alexander Hansen90fabb02025-08-27 14:15:17 +020066 static std::optional<AssocName> getAssocByName(const std::string& name);
67
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020068 // Maps the port name to the participating paths.
69 // each path also has their role(s) in the association.
70 // For example a PSU path which is part of "MB Upstream Port"
71 // will have "powering" role.
72 std::unordered_map<PortType, std::map<Path, std::set<AssocName>>> ports;
73
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000074 std::unordered_map<Path, BoardType> boardTypes;
Matt Spinler6eb60972023-08-14 16:36:20 -050075 std::unordered_map<BoardName, Path> boardNames;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000076};