blob: cab4533329f9131e4cb4e4f5edf2f199fdacf9d3 [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
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000028class Topology
29{
30 public:
31 explicit Topology() = default;
32
33 void addBoard(const std::string& path, const std::string& boardType,
Matt Spinler6eb60972023-08-14 16:36:20 -050034 const std::string& boardName,
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000035 const nlohmann::json& exposesItem);
Alexander Hansen0519f572025-08-19 18:11:04 +020036 std::unordered_map<std::string, std::set<Association>> getAssocs(
Alexander Hansen32e74182025-08-20 16:16:00 +020037 BoardPathsView boardPaths);
Matt Spinler6eb60972023-08-14 16:36:20 -050038 void remove(const std::string& boardName);
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000039
40 private:
41 using Path = std::string;
42 using BoardType = std::string;
Matt Spinler6eb60972023-08-14 16:36:20 -050043 using BoardName = std::string;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000044 using PortType = std::string;
45
Alexander Hansenec938ad2025-08-19 16:48:44 +020046 void addDownstreamPort(const Path& path, const nlohmann::json& exposesItem);
47
Alexander Hansend7be0ee2025-08-20 14:16:15 +020048 // @brief: fill associations map with the associations for a port identifier
49 // such as 'MB Upstream Port'
50 void fillAssocsForPortId(
51 std::unordered_map<std::string, std::set<Association>>& result,
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020052 BoardPathsView boardPaths,
53 const std::map<Path, std::set<AssocName>>& pathAssocs);
Alexander Hansend7be0ee2025-08-20 14:16:15 +020054
Alexander Hansen97a1c932025-08-20 16:28:52 +020055 void fillAssocForPortId(
56 std::unordered_map<std::string, std::set<Association>>& result,
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020057 BoardPathsView boardPaths, const Path& upstream, const Path& downstream,
58 const AssocName& assocName);
59
60 void addPort(const PortType& port, const Path& path,
61 const AssocName& assocName);
Alexander Hansen97a1c932025-08-20 16:28:52 +020062
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020063 // Maps the port name to the participating paths.
64 // each path also has their role(s) in the association.
65 // For example a PSU path which is part of "MB Upstream Port"
66 // will have "powering" role.
67 std::unordered_map<PortType, std::map<Path, std::set<AssocName>>> ports;
68
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000069 std::unordered_map<Path, BoardType> boardTypes;
Matt Spinler6eb60972023-08-14 16:36:20 -050070 std::unordered_map<BoardName, Path> boardNames;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000071};