blob: 39dcae7806ee55bfad69ac904a2176cd2f8b847f [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,
40 BoardPathsView boardPaths, const std::set<Path>& upstreamPaths,
41 const std::set<Path>& downstreamPaths);
42
Alexander Hansen97a1c932025-08-20 16:28:52 +020043 void fillAssocForPortId(
44 std::unordered_map<std::string, std::set<Association>>& result,
45 BoardPathsView boardPaths, const Path& upstream,
46 const Path& downstream);
47
Alexander Hansenca121f52025-08-21 18:25:19 +020048 static std::optional<std::string> getOppositeAssoc(
49 const AssocName& assocName);
50
Alexander Hansen7cfc0432025-08-20 14:43:04 +020051 std::unordered_map<PortType, std::set<Path>> upstreamPorts;
52 std::unordered_map<PortType, std::set<Path>> downstreamPorts;
Jeff Linb02752f2023-12-01 11:23:54 +080053 std::set<Path> powerPaths;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000054 std::unordered_map<Path, BoardType> boardTypes;
Matt Spinler6eb60972023-08-14 16:36:20 -050055 std::unordered_map<BoardName, Path> boardNames;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000056};