blob: 164b2bfc0359a1738a407e6244d14a7cc8e3e8fd [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
Alexander Hansen9b95ae42025-08-27 15:10:34 +020019 AssocName(const std::string& name, const std::string& reverse,
20 const std::set<std::string>& allowedOnBoardTypes,
21 const std::set<std::string>& allowedOnBoardTypesReverse);
Alexander Hansen7b2a77f2025-08-27 12:27:57 +020022 AssocName() = delete;
23
Alexander Hansen9b95ae42025-08-27 15:10:34 +020024 // 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
Alexander Hansen7b2a77f2025-08-27 12:27:57 +020032 AssocName getReverse() const;
33
34 bool operator==(const AssocName& other) const = default;
35 bool operator<(const AssocName& other) const;
36};
37
Alexander Hansen90fabb02025-08-27 14:15:17 +020038extern const std::vector<AssocName> supportedAssocs;
39
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000040class Topology
41{
42 public:
43 explicit Topology() = default;
44
45 void addBoard(const std::string& path, const std::string& boardType,
Matt Spinler6eb60972023-08-14 16:36:20 -050046 const std::string& boardName,
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000047 const nlohmann::json& exposesItem);
Alexander Hansen0519f572025-08-19 18:11:04 +020048 std::unordered_map<std::string, std::set<Association>> getAssocs(
Alexander Hansen32e74182025-08-20 16:16:00 +020049 BoardPathsView boardPaths);
Matt Spinler6eb60972023-08-14 16:36:20 -050050 void remove(const std::string& boardName);
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000051
52 private:
53 using Path = std::string;
54 using BoardType = std::string;
Matt Spinler6eb60972023-08-14 16:36:20 -050055 using BoardName = std::string;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000056 using PortType = std::string;
57
Alexander Hansenec938ad2025-08-19 16:48:44 +020058 void addDownstreamPort(const Path& path, const nlohmann::json& exposesItem);
59
Alexander Hansend7be0ee2025-08-20 14:16:15 +020060 // @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,
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020064 BoardPathsView boardPaths,
65 const std::map<Path, std::set<AssocName>>& pathAssocs);
Alexander Hansend7be0ee2025-08-20 14:16:15 +020066
Alexander Hansen97a1c932025-08-20 16:28:52 +020067 void fillAssocForPortId(
68 std::unordered_map<std::string, std::set<Association>>& result,
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020069 BoardPathsView boardPaths, const Path& upstream, const Path& downstream,
70 const AssocName& assocName);
71
Alexander Hansen90fabb02025-08-27 14:15:17 +020072 void addConfiguredPort(const Path& path, const nlohmann::json& exposesItem);
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020073 void addPort(const PortType& port, const Path& path,
74 const AssocName& assocName);
Alexander Hansen97a1c932025-08-20 16:28:52 +020075
Alexander Hansen90fabb02025-08-27 14:15:17 +020076 static std::optional<AssocName> getAssocByName(const std::string& name);
77
Alexander Hansen68e2b0f2025-08-21 17:44:18 +020078 // 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
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000084 std::unordered_map<Path, BoardType> boardTypes;
Matt Spinler6eb60972023-08-14 16:36:20 -050085 std::unordered_map<BoardName, Path> boardNames;
Benjamin Fairf2f5b7a2022-09-09 19:45:02 +000086};