| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 1 | #pragma once | 
 | 2 |  | 
 | 3 | #include <nlohmann/json.hpp> | 
 | 4 |  | 
 | 5 | #include <set> | 
 | 6 | #include <unordered_map> | 
 | 7 |  | 
 | 8 | using Association = std::tuple<std::string, std::string, std::string>; | 
 | 9 |  | 
| Alexander Hansen | 32e7418 | 2025-08-20 16:16:00 +0200 | [diff] [blame] | 10 | using BoardPathsView = decltype(std::views::keys( | 
 | 11 |     std::declval<std::map<std::string, std::string>&>())); | 
 | 12 |  | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 13 | class AssocName | 
 | 14 | { | 
 | 15 |   public: | 
 | 16 |     std::string name; | 
 | 17 |     std::string reverse; | 
 | 18 |  | 
| Alexander Hansen | 9b95ae4 | 2025-08-27 15:10:34 +0200 | [diff] [blame] | 19 |     AssocName(const std::string& name, const std::string& reverse, | 
 | 20 |               const std::set<std::string>& allowedOnBoardTypes, | 
 | 21 |               const std::set<std::string>& allowedOnBoardTypesReverse); | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 22 |     AssocName() = delete; | 
 | 23 |  | 
| Alexander Hansen | 9b95ae4 | 2025-08-27 15:10:34 +0200 | [diff] [blame] | 24 |     // 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 Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 32 |     AssocName getReverse() const; | 
 | 33 |  | 
 | 34 |     bool operator==(const AssocName& other) const = default; | 
 | 35 |     bool operator<(const AssocName& other) const; | 
 | 36 | }; | 
 | 37 |  | 
| Alexander Hansen | 90fabb0 | 2025-08-27 14:15:17 +0200 | [diff] [blame] | 38 | extern const std::vector<AssocName> supportedAssocs; | 
 | 39 |  | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 40 | class Topology | 
 | 41 | { | 
 | 42 |   public: | 
 | 43 |     explicit Topology() = default; | 
 | 44 |  | 
 | 45 |     void addBoard(const std::string& path, const std::string& boardType, | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 46 |                   const std::string& boardName, | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 47 |                   const nlohmann::json& exposesItem); | 
| Alexander Hansen | 0519f57 | 2025-08-19 18:11:04 +0200 | [diff] [blame] | 48 |     std::unordered_map<std::string, std::set<Association>> getAssocs( | 
| Alexander Hansen | 32e7418 | 2025-08-20 16:16:00 +0200 | [diff] [blame] | 49 |         BoardPathsView boardPaths); | 
| Christopher Meis | 9a5eec9 | 2025-08-07 08:43:03 +0200 | [diff] [blame] | 50 |  | 
 | 51 |     // Adds an entry to probePaths for object mapper board path | 
 | 52 |     // and inventory board path. | 
 | 53 |     void addProbePath(const std::string& boardPath, | 
 | 54 |                       const std::string& probePath); | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 55 |     void remove(const std::string& boardName); | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 56 |  | 
 | 57 |   private: | 
 | 58 |     using Path = std::string; | 
 | 59 |     using BoardType = std::string; | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 60 |     using BoardName = std::string; | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 61 |     using PortType = std::string; | 
 | 62 |  | 
| Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 63 |     void addDownstreamPort(const Path& path, const nlohmann::json& exposesItem); | 
 | 64 |  | 
| Alexander Hansen | d7be0ee | 2025-08-20 14:16:15 +0200 | [diff] [blame] | 65 |     // @brief: fill associations map with the associations for a port identifier | 
 | 66 |     // such as 'MB Upstream Port' | 
 | 67 |     void fillAssocsForPortId( | 
 | 68 |         std::unordered_map<std::string, std::set<Association>>& result, | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 69 |         BoardPathsView boardPaths, | 
 | 70 |         const std::map<Path, std::set<AssocName>>& pathAssocs); | 
| Alexander Hansen | d7be0ee | 2025-08-20 14:16:15 +0200 | [diff] [blame] | 71 |  | 
| Alexander Hansen | 97a1c93 | 2025-08-20 16:28:52 +0200 | [diff] [blame] | 72 |     void fillAssocForPortId( | 
 | 73 |         std::unordered_map<std::string, std::set<Association>>& result, | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 74 |         BoardPathsView boardPaths, const Path& upstream, const Path& downstream, | 
 | 75 |         const AssocName& assocName); | 
 | 76 |  | 
| Alexander Hansen | 90fabb0 | 2025-08-27 14:15:17 +0200 | [diff] [blame] | 77 |     void addConfiguredPort(const Path& path, const nlohmann::json& exposesItem); | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 78 |     void addPort(const PortType& port, const Path& path, | 
 | 79 |                  const AssocName& assocName); | 
| Alexander Hansen | 97a1c93 | 2025-08-20 16:28:52 +0200 | [diff] [blame] | 80 |  | 
| Alexander Hansen | 90fabb0 | 2025-08-27 14:15:17 +0200 | [diff] [blame] | 81 |     static std::optional<AssocName> getAssocByName(const std::string& name); | 
 | 82 |  | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 83 |     // Maps the port name to the participating paths. | 
 | 84 |     // each path also has their role(s) in the association. | 
 | 85 |     // For example a PSU path which is part of "MB Upstream Port" | 
 | 86 |     // will have "powering" role. | 
 | 87 |     std::unordered_map<PortType, std::map<Path, std::set<AssocName>>> ports; | 
 | 88 |  | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 89 |     std::unordered_map<Path, BoardType> boardTypes; | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 90 |     std::unordered_map<BoardName, Path> boardNames; | 
| Christopher Meis | 9a5eec9 | 2025-08-07 08:43:03 +0200 | [diff] [blame] | 91 |  | 
 | 92 |     // Represents the mapping between inventory object pathes of a | 
 | 93 |     // probed configuration and the object paths of DBus interfaces | 
 | 94 |     // it was probed on. | 
 | 95 |     std::unordered_map<Path, std::set<Path>> probePaths; | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 96 | }; |