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 10 using BoardPathsView = decltype(std::views::keys( 11 std::declval<std::map<std::string, std::string>&>())); 12 13 class 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 28 class Topology 29 { 30 public: 31 explicit Topology() = default; 32 33 void addBoard(const std::string& path, const std::string& boardType, 34 const std::string& boardName, 35 const nlohmann::json& exposesItem); 36 std::unordered_map<std::string, std::set<Association>> getAssocs( 37 BoardPathsView boardPaths); 38 void remove(const std::string& boardName); 39 40 private: 41 using Path = std::string; 42 using BoardType = std::string; 43 using BoardName = std::string; 44 using PortType = std::string; 45 46 void addDownstreamPort(const Path& path, const nlohmann::json& exposesItem); 47 48 // @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, 52 BoardPathsView boardPaths, 53 const std::map<Path, std::set<AssocName>>& pathAssocs); 54 55 void fillAssocForPortId( 56 std::unordered_map<std::string, std::set<Association>>& result, 57 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); 62 63 // 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 69 std::unordered_map<Path, BoardType> boardTypes; 70 std::unordered_map<BoardName, Path> boardNames; 71 }; 72