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 extern const std::vector<AssocName> supportedAssocs; 29 30 class Topology 31 { 32 public: 33 explicit Topology() = default; 34 35 void addBoard(const std::string& path, const std::string& boardType, 36 const std::string& boardName, 37 const nlohmann::json& exposesItem); 38 std::unordered_map<std::string, std::set<Association>> getAssocs( 39 BoardPathsView boardPaths); 40 void remove(const std::string& boardName); 41 42 private: 43 using Path = std::string; 44 using BoardType = std::string; 45 using BoardName = std::string; 46 using PortType = std::string; 47 48 void addDownstreamPort(const Path& path, const nlohmann::json& exposesItem); 49 50 // @brief: fill associations map with the associations for a port identifier 51 // such as 'MB Upstream Port' 52 void fillAssocsForPortId( 53 std::unordered_map<std::string, std::set<Association>>& result, 54 BoardPathsView boardPaths, 55 const std::map<Path, std::set<AssocName>>& pathAssocs); 56 57 void fillAssocForPortId( 58 std::unordered_map<std::string, std::set<Association>>& result, 59 BoardPathsView boardPaths, const Path& upstream, const Path& downstream, 60 const AssocName& assocName); 61 62 void addConfiguredPort(const Path& path, const nlohmann::json& exposesItem); 63 void addPort(const PortType& port, const Path& path, 64 const AssocName& assocName); 65 66 static std::optional<AssocName> getAssocByName(const std::string& name); 67 68 // Maps the port name to the participating paths. 69 // each path also has their role(s) in the association. 70 // For example a PSU path which is part of "MB Upstream Port" 71 // will have "powering" role. 72 std::unordered_map<PortType, std::map<Path, std::set<AssocName>>> ports; 73 74 std::unordered_map<Path, BoardType> boardTypes; 75 std::unordered_map<BoardName, Path> boardNames; 76 }; 77