Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 1 | #include "topology.hpp" |
| 2 | |
Alexander Hansen | 315929a | 2025-08-19 17:34:10 +0200 | [diff] [blame] | 3 | #include "phosphor-logging/lg2.hpp" |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 4 | |
| 5 | void Topology::addBoard(const std::string& path, const std::string& boardType, |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 6 | const std::string& boardName, |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 7 | const nlohmann::json& exposesItem) |
| 8 | { |
| 9 | auto findType = exposesItem.find("Type"); |
| 10 | if (findType == exposesItem.end()) |
| 11 | { |
| 12 | return; |
| 13 | } |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 14 | |
| 15 | boardNames.try_emplace(boardName, path); |
| 16 | |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 17 | PortType exposesType = findType->get<std::string>(); |
| 18 | |
| 19 | if (exposesType == "DownstreamPort") |
| 20 | { |
Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 21 | addDownstreamPort(path, exposesItem); |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 22 | } |
| 23 | else if (exposesType.ends_with("Port")) |
| 24 | { |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 25 | addPort(exposesType, path, "containing"); |
Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 26 | } |
| 27 | else |
| 28 | { |
| 29 | return; |
| 30 | } |
| 31 | boardTypes[path] = boardType; |
| 32 | } |
| 33 | |
| 34 | void Topology::addDownstreamPort(const Path& path, |
| 35 | const nlohmann::json& exposesItem) |
| 36 | { |
| 37 | auto findConnectsTo = exposesItem.find("ConnectsToType"); |
| 38 | if (findConnectsTo == exposesItem.end()) |
| 39 | { |
Alexander Hansen | 315929a | 2025-08-19 17:34:10 +0200 | [diff] [blame] | 40 | lg2::error("Board at path {PATH} is missing ConnectsToType", "PATH", |
| 41 | path); |
Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 42 | return; |
| 43 | } |
| 44 | PortType connectsTo = findConnectsTo->get<std::string>(); |
| 45 | |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 46 | addPort(connectsTo, path, "contained_by"); |
| 47 | |
Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 48 | auto findPoweredBy = exposesItem.find("PowerPort"); |
| 49 | if (findPoweredBy != exposesItem.end()) |
| 50 | { |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 51 | addPort(connectsTo, path, "powering"); |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 55 | void Topology::addPort(const PortType& port, const Path& path, |
| 56 | const AssocName& assocName) |
| 57 | { |
| 58 | if (!ports.contains(port)) |
| 59 | { |
| 60 | ports.insert({port, {}}); |
| 61 | } |
| 62 | if (!ports[port].contains(path)) |
| 63 | { |
| 64 | ports[port].insert({path, {}}); |
| 65 | } |
| 66 | ports[port][path].insert(assocName); |
| 67 | } |
| 68 | |
Alexander Hansen | 0519f57 | 2025-08-19 18:11:04 +0200 | [diff] [blame] | 69 | std::unordered_map<std::string, std::set<Association>> Topology::getAssocs( |
Alexander Hansen | 32e7418 | 2025-08-20 16:16:00 +0200 | [diff] [blame] | 70 | BoardPathsView boardPaths) |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 71 | { |
Alexander Hansen | 0519f57 | 2025-08-19 18:11:04 +0200 | [diff] [blame] | 72 | std::unordered_map<std::string, std::set<Association>> result; |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 73 | |
| 74 | // look at each upstream port type |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 75 | for (const auto& port : ports) |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 76 | { |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 77 | fillAssocsForPortId(result, boardPaths, port.second); |
Alexander Hansen | d7be0ee | 2025-08-20 14:16:15 +0200 | [diff] [blame] | 78 | } |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | void Topology::fillAssocsForPortId( |
| 83 | std::unordered_map<std::string, std::set<Association>>& result, |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 84 | BoardPathsView boardPaths, |
| 85 | const std::map<Path, std::set<AssocName>>& pathAssocs) |
Alexander Hansen | d7be0ee | 2025-08-20 14:16:15 +0200 | [diff] [blame] | 86 | { |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 87 | for (const auto& member : pathAssocs) |
Alexander Hansen | d7be0ee | 2025-08-20 14:16:15 +0200 | [diff] [blame] | 88 | { |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 89 | for (const auto& other : pathAssocs) |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 90 | { |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 91 | if (other.first == member.first) |
| 92 | { |
| 93 | continue; |
| 94 | } |
| 95 | for (const auto& assocName : member.second) |
| 96 | { |
| 97 | auto optReverse = getOppositeAssoc(assocName); |
| 98 | if (!optReverse.has_value()) |
| 99 | { |
| 100 | continue; |
| 101 | } |
| 102 | // if the other end of the assocation does not declare |
| 103 | // the reverse association, do not associate |
| 104 | const bool otherAgrees = |
| 105 | other.second.contains(optReverse.value()); |
| 106 | |
| 107 | // quirk: since the other side of the association cannot declare |
| 108 | // to be powered_by in the legacy schema, in case of "powering", |
| 109 | // the two associations do not have to agree. |
| 110 | if (!otherAgrees && assocName != "powering") |
| 111 | { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | fillAssocForPortId(result, boardPaths, member.first, |
| 116 | other.first, assocName); |
| 117 | } |
Alexander Hansen | 97a1c93 | 2025-08-20 16:28:52 +0200 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void Topology::fillAssocForPortId( |
| 123 | std::unordered_map<std::string, std::set<Association>>& result, |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 124 | BoardPathsView boardPaths, const Path& upstream, const Path& downstream, |
| 125 | const AssocName& assocName) |
Alexander Hansen | 97a1c93 | 2025-08-20 16:28:52 +0200 | [diff] [blame] | 126 | { |
Alexander Hansen | 5036d94 | 2025-08-20 16:38:59 +0200 | [diff] [blame] | 127 | if (boardTypes[upstream] != "Chassis" && boardTypes[upstream] != "Board") |
Alexander Hansen | 97a1c93 | 2025-08-20 16:28:52 +0200 | [diff] [blame] | 128 | { |
Alexander Hansen | 5036d94 | 2025-08-20 16:38:59 +0200 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | // The downstream path must be one we care about. |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 132 | if (!std::ranges::contains(boardPaths, upstream)) |
Alexander Hansen | 5036d94 | 2025-08-20 16:38:59 +0200 | [diff] [blame] | 133 | { |
| 134 | return; |
| 135 | } |
| 136 | |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 137 | auto optReverse = getOppositeAssoc(assocName); |
Alexander Hansen | ca121f5 | 2025-08-21 18:25:19 +0200 | [diff] [blame] | 138 | |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 139 | if (!optReverse) |
Alexander Hansen | ca121f5 | 2025-08-21 18:25:19 +0200 | [diff] [blame] | 140 | { |
| 141 | return; |
| 142 | } |
| 143 | |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 144 | // quirk: legacy code did not associate from both sides |
| 145 | // TODO(alexander): revisit this |
| 146 | if (assocName == "containing" || assocName == "powered_by") |
Alexander Hansen | 5036d94 | 2025-08-20 16:38:59 +0200 | [diff] [blame] | 147 | { |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 148 | return; |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 149 | } |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 150 | |
| 151 | result[upstream].insert({assocName, optReverse.value(), downstream}); |
Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 152 | } |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 153 | |
Alexander Hansen | ca121f5 | 2025-08-21 18:25:19 +0200 | [diff] [blame] | 154 | const std::set<std::pair<std::string, std::string>> assocs = { |
| 155 | {"powering", "powered_by"}, {"containing", "contained_by"}, |
| 156 | // ... extend as needed |
| 157 | }; |
| 158 | |
| 159 | std::optional<std::string> Topology::getOppositeAssoc( |
| 160 | const AssocName& assocName) |
| 161 | { |
| 162 | for (const auto& entry : assocs) |
| 163 | { |
| 164 | if (entry.first == assocName) |
| 165 | { |
| 166 | return entry.second; |
| 167 | } |
| 168 | if (entry.second == assocName) |
| 169 | { |
| 170 | return entry.first; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return std::nullopt; |
| 175 | } |
| 176 | |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 177 | void Topology::remove(const std::string& boardName) |
| 178 | { |
| 179 | // Remove the board from boardNames, and then using the path |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 180 | // found in boardNames remove it from ports |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 181 | auto boardFind = boardNames.find(boardName); |
| 182 | if (boardFind == boardNames.end()) |
| 183 | { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | std::string boardPath = boardFind->second; |
| 188 | |
| 189 | boardNames.erase(boardFind); |
| 190 | |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 191 | for (auto& port : ports) |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 192 | { |
Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame^] | 193 | port.second.erase(boardPath); |
Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 194 | } |
| 195 | } |