blob: 9fc35362b67b62b6090b93f3825b28fa3159dbb3 [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
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000013class Topology
14{
15 public:
16 explicit Topology() = default;
17
18 void addBoard(const std::string& path, const std::string& boardType,
Matt Spinler6eb60972023-08-14 16:36:20 -050019 const std::string& boardName,
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000020 const nlohmann::json& exposesItem);
Alexander Hansen0519f572025-08-19 18:11:04 +020021 std::unordered_map<std::string, std::set<Association>> getAssocs(
Alexander Hansen32e74182025-08-20 16:16:00 +020022 BoardPathsView boardPaths);
Matt Spinler6eb60972023-08-14 16:36:20 -050023 void remove(const std::string& boardName);
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000024
25 private:
26 using Path = std::string;
27 using BoardType = std::string;
Matt Spinler6eb60972023-08-14 16:36:20 -050028 using BoardName = std::string;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000029 using PortType = std::string;
30
Alexander Hansenec938ad2025-08-19 16:48:44 +020031 void addDownstreamPort(const Path& path, const nlohmann::json& exposesItem);
32
Alexander Hansenca121f52025-08-21 18:25:19 +020033 // e.g. contained_by, containing, powered_by, ...
34 using AssocName = std::string;
35
Alexander Hansend7be0ee2025-08-20 14:16:15 +020036 // @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,
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020040 BoardPathsView boardPaths,
41 const std::map<Path, std::set<AssocName>>& pathAssocs);
Alexander Hansend7be0ee2025-08-20 14:16:15 +020042
Alexander Hansen97a1c932025-08-20 16:28:52 +020043 void fillAssocForPortId(
44 std::unordered_map<std::string, std::set<Association>>& result,
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020045 BoardPathsView boardPaths, const Path& upstream, const Path& downstream,
46 const AssocName& assocName);
47
48 void addPort(const PortType& port, const Path& path,
49 const AssocName& assocName);
Alexander Hansen97a1c932025-08-20 16:28:52 +020050
Alexander Hansenca121f52025-08-21 18:25:19 +020051 static std::optional<std::string> getOppositeAssoc(
52 const AssocName& assocName);
53
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020054 // Maps the port name to the participating paths.
55 // each path also has their role(s) in the association.
56 // For example a PSU path which is part of "MB Upstream Port"
57 // will have "powering" role.
58 std::unordered_map<PortType, std::map<Path, std::set<AssocName>>> ports;
59
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000060 std::unordered_map<Path, BoardType> boardTypes;
Matt Spinler6eb60972023-08-14 16:36:20 -050061 std::unordered_map<BoardName, Path> boardNames;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000062};