| 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 |  | 
| Alexander Hansen | 9b95ae4 | 2025-08-27 15:10:34 +0200 | [diff] [blame] | 5 | const AssocName assocContaining = | 
|  | 6 | AssocName("containing", "contained_by", {"Chassis"}, | 
|  | 7 | {"Board", "Chassis", "PowerSupply"}); | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 8 | const AssocName assocContainedBy = assocContaining.getReverse(); | 
| Alexander Hansen | 9b95ae4 | 2025-08-27 15:10:34 +0200 | [diff] [blame] | 9 |  | 
|  | 10 | // Topology tests say that a chassis can be powering another chassis. | 
|  | 11 | // In case there is any confusion as to why 'Chassis' can have 'powering' | 
|  | 12 | // association. | 
|  | 13 | const AssocName assocPowering = | 
|  | 14 | AssocName("powering", "powered_by", {"Chassis", "PowerSupply"}, | 
|  | 15 | {"Board", "Chassis", "PowerSupply"}); | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 16 | const AssocName assocPoweredBy = assocPowering.getReverse(); | 
|  | 17 |  | 
| Christopher Meis | 9a5eec9 | 2025-08-07 08:43:03 +0200 | [diff] [blame] | 18 | const AssocName assocProbing = AssocName("probing", "probed_by", {}, {}); | 
|  | 19 | const AssocName assocProbedBy = assocProbing.getReverse(); | 
|  | 20 |  | 
| Alexander Hansen | 90fabb0 | 2025-08-27 14:15:17 +0200 | [diff] [blame] | 21 | const std::vector<AssocName> supportedAssocs = { | 
|  | 22 | assocContaining, | 
|  | 23 | assocContainedBy, | 
|  | 24 | assocPowering, | 
|  | 25 | assocPoweredBy, | 
|  | 26 | }; | 
|  | 27 |  | 
| Alexander Hansen | 9b95ae4 | 2025-08-27 15:10:34 +0200 | [diff] [blame] | 28 | AssocName::AssocName(const std::string& name, const std::string& reverse, | 
|  | 29 | const std::set<std::string>& allowedOnBoardTypes, | 
|  | 30 | const std::set<std::string>& allowedOnBoardTypesReverse) : | 
|  | 31 | name(name), reverse(reverse), allowedOnBoardTypes(allowedOnBoardTypes), | 
|  | 32 | allowedOnBoardTypesReverse(allowedOnBoardTypesReverse) | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 33 | {} | 
|  | 34 |  | 
|  | 35 | AssocName AssocName::getReverse() const | 
|  | 36 | { | 
| Alexander Hansen | 9b95ae4 | 2025-08-27 15:10:34 +0200 | [diff] [blame] | 37 | return {reverse, name, allowedOnBoardTypesReverse, allowedOnBoardTypes}; | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
|  | 40 | bool AssocName::operator<(const AssocName& other) const | 
|  | 41 | { | 
|  | 42 | return name < other.name; | 
|  | 43 | } | 
|  | 44 |  | 
| Alexander Hansen | 90fabb0 | 2025-08-27 14:15:17 +0200 | [diff] [blame] | 45 | std::optional<AssocName> Topology::getAssocByName(const std::string& name) | 
|  | 46 | { | 
|  | 47 | for (const auto& assoc : supportedAssocs) | 
|  | 48 | { | 
|  | 49 | if (assoc.name == name) | 
|  | 50 | { | 
|  | 51 | return assoc; | 
|  | 52 | } | 
|  | 53 | } | 
|  | 54 | return std::nullopt; | 
|  | 55 | } | 
|  | 56 |  | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 57 | void Topology::addBoard(const std::string& path, const std::string& boardType, | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 58 | const std::string& boardName, | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 59 | const nlohmann::json& exposesItem) | 
|  | 60 | { | 
|  | 61 | auto findType = exposesItem.find("Type"); | 
|  | 62 | if (findType == exposesItem.end()) | 
|  | 63 | { | 
|  | 64 | return; | 
|  | 65 | } | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 66 |  | 
|  | 67 | boardNames.try_emplace(boardName, path); | 
|  | 68 |  | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 69 | PortType exposesType = findType->get<std::string>(); | 
|  | 70 |  | 
|  | 71 | if (exposesType == "DownstreamPort") | 
|  | 72 | { | 
| Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 73 | addDownstreamPort(path, exposesItem); | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 74 | } | 
| Alexander Hansen | 90fabb0 | 2025-08-27 14:15:17 +0200 | [diff] [blame] | 75 | else if (exposesType == "Port") | 
|  | 76 | { | 
|  | 77 | addConfiguredPort(path, exposesItem); | 
|  | 78 | } | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 79 | else if (exposesType.ends_with("Port")) | 
|  | 80 | { | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 81 | addPort(exposesType, path, assocContaining); | 
| Alexander Hansen | daa4ac0 | 2025-08-27 15:42:08 +0200 | [diff] [blame] | 82 |  | 
|  | 83 | // this represents the legacy quirk of upstream ports having no choice | 
|  | 84 | // in the | 
|  | 85 | // powered_by association | 
|  | 86 | addPort(exposesType, path, assocPoweredBy); | 
| Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 87 | } | 
|  | 88 | else | 
|  | 89 | { | 
|  | 90 | return; | 
|  | 91 | } | 
|  | 92 | boardTypes[path] = boardType; | 
|  | 93 | } | 
|  | 94 |  | 
| Alexander Hansen | 90fabb0 | 2025-08-27 14:15:17 +0200 | [diff] [blame] | 95 | void Topology::addConfiguredPort(const Path& path, | 
|  | 96 | const nlohmann::json& exposesItem) | 
|  | 97 | { | 
|  | 98 | const auto findConnectsToName = exposesItem.find("Name"); | 
|  | 99 | if (findConnectsToName == exposesItem.end()) | 
|  | 100 | { | 
|  | 101 | lg2::error("Board at path {PATH} is missing 'Name'", "PATH", path); | 
|  | 102 | return; | 
|  | 103 | } | 
|  | 104 | const std::string connectsToName = findConnectsToName->get<std::string>(); | 
|  | 105 |  | 
|  | 106 | const auto findPortType = exposesItem.find("PortType"); | 
|  | 107 | if (findPortType == exposesItem.end()) | 
|  | 108 | { | 
|  | 109 | lg2::error("Board at path {PATH} is missing PortType", "PATH", path); | 
|  | 110 | return; | 
|  | 111 | } | 
|  | 112 | const std::string portType = findPortType->get<std::string>(); | 
|  | 113 |  | 
|  | 114 | const auto assoc = getAssocByName(portType); | 
|  | 115 | if (!assoc.has_value()) | 
|  | 116 | { | 
|  | 117 | lg2::error("Could not find configured association name {ASSOC}", | 
|  | 118 | "ASSOC", portType); | 
|  | 119 | return; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | addPort(connectsToName, path, assoc.value()); | 
|  | 123 | } | 
|  | 124 |  | 
| Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 125 | void Topology::addDownstreamPort(const Path& path, | 
|  | 126 | const nlohmann::json& exposesItem) | 
|  | 127 | { | 
|  | 128 | auto findConnectsTo = exposesItem.find("ConnectsToType"); | 
|  | 129 | if (findConnectsTo == exposesItem.end()) | 
|  | 130 | { | 
| Alexander Hansen | 315929a | 2025-08-19 17:34:10 +0200 | [diff] [blame] | 131 | lg2::error("Board at path {PATH} is missing ConnectsToType", "PATH", | 
|  | 132 | path); | 
| Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 133 | return; | 
|  | 134 | } | 
|  | 135 | PortType connectsTo = findConnectsTo->get<std::string>(); | 
|  | 136 |  | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 137 | addPort(connectsTo, path, assocContainedBy); | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 138 |  | 
| Alexander Hansen | ec938ad | 2025-08-19 16:48:44 +0200 | [diff] [blame] | 139 | auto findPoweredBy = exposesItem.find("PowerPort"); | 
|  | 140 | if (findPoweredBy != exposesItem.end()) | 
|  | 141 | { | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 142 | addPort(connectsTo, path, assocPowering); | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 143 | } | 
|  | 144 | } | 
|  | 145 |  | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 146 | void Topology::addPort(const PortType& port, const Path& path, | 
|  | 147 | const AssocName& assocName) | 
|  | 148 | { | 
|  | 149 | if (!ports.contains(port)) | 
|  | 150 | { | 
|  | 151 | ports.insert({port, {}}); | 
|  | 152 | } | 
|  | 153 | if (!ports[port].contains(path)) | 
|  | 154 | { | 
|  | 155 | ports[port].insert({path, {}}); | 
|  | 156 | } | 
|  | 157 | ports[port][path].insert(assocName); | 
|  | 158 | } | 
|  | 159 |  | 
| Alexander Hansen | 0519f57 | 2025-08-19 18:11:04 +0200 | [diff] [blame] | 160 | std::unordered_map<std::string, std::set<Association>> Topology::getAssocs( | 
| Alexander Hansen | 32e7418 | 2025-08-20 16:16:00 +0200 | [diff] [blame] | 161 | BoardPathsView boardPaths) | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 162 | { | 
| Alexander Hansen | 0519f57 | 2025-08-19 18:11:04 +0200 | [diff] [blame] | 163 | std::unordered_map<std::string, std::set<Association>> result; | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 164 |  | 
|  | 165 | // look at each upstream port type | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 166 | for (const auto& port : ports) | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 167 | { | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 168 | fillAssocsForPortId(result, boardPaths, port.second); | 
| Alexander Hansen | d7be0ee | 2025-08-20 14:16:15 +0200 | [diff] [blame] | 169 | } | 
| Christopher Meis | 9a5eec9 | 2025-08-07 08:43:03 +0200 | [diff] [blame] | 170 |  | 
|  | 171 | for (const auto& [boardPath, probePaths] : probePaths) | 
|  | 172 | { | 
|  | 173 | if (std::ranges::contains(boardPaths, boardPath)) | 
|  | 174 | { | 
|  | 175 | for (const auto& path : probePaths) | 
|  | 176 | { | 
|  | 177 | result[boardPath].insert( | 
|  | 178 | {assocProbing.name, assocProbedBy.name, path}); | 
|  | 179 | } | 
|  | 180 | } | 
|  | 181 | } | 
| Alexander Hansen | d7be0ee | 2025-08-20 14:16:15 +0200 | [diff] [blame] | 182 | return result; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | void Topology::fillAssocsForPortId( | 
|  | 186 | std::unordered_map<std::string, std::set<Association>>& result, | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 187 | BoardPathsView boardPaths, | 
|  | 188 | const std::map<Path, std::set<AssocName>>& pathAssocs) | 
| Alexander Hansen | d7be0ee | 2025-08-20 14:16:15 +0200 | [diff] [blame] | 189 | { | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 190 | for (const auto& member : pathAssocs) | 
| Alexander Hansen | d7be0ee | 2025-08-20 14:16:15 +0200 | [diff] [blame] | 191 | { | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 192 | for (const auto& other : pathAssocs) | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 193 | { | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 194 | if (other.first == member.first) | 
|  | 195 | { | 
|  | 196 | continue; | 
|  | 197 | } | 
|  | 198 | for (const auto& assocName : member.second) | 
|  | 199 | { | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 200 | // if the other end of the assocation does not declare | 
|  | 201 | // the reverse association, do not associate | 
|  | 202 | const bool otherAgrees = | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 203 | other.second.contains(assocName.getReverse()); | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 204 |  | 
| Alexander Hansen | daa4ac0 | 2025-08-27 15:42:08 +0200 | [diff] [blame] | 205 | if (!otherAgrees) | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 206 | { | 
|  | 207 | continue; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | fillAssocForPortId(result, boardPaths, member.first, | 
|  | 211 | other.first, assocName); | 
|  | 212 | } | 
| Alexander Hansen | 97a1c93 | 2025-08-20 16:28:52 +0200 | [diff] [blame] | 213 | } | 
|  | 214 | } | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | void Topology::fillAssocForPortId( | 
|  | 218 | std::unordered_map<std::string, std::set<Association>>& result, | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 219 | BoardPathsView boardPaths, const Path& upstream, const Path& downstream, | 
|  | 220 | const AssocName& assocName) | 
| Alexander Hansen | 97a1c93 | 2025-08-20 16:28:52 +0200 | [diff] [blame] | 221 | { | 
| Alexander Hansen | 9b95ae4 | 2025-08-27 15:10:34 +0200 | [diff] [blame] | 222 | if (!assocName.allowedOnBoardTypes.contains(boardTypes[upstream])) | 
| Alexander Hansen | 97a1c93 | 2025-08-20 16:28:52 +0200 | [diff] [blame] | 223 | { | 
| Alexander Hansen | 9b95ae4 | 2025-08-27 15:10:34 +0200 | [diff] [blame] | 224 | lg2::error( | 
|  | 225 | "Cannot create Association Definition {ASSOC} for {PATH} with board type {TYPE}", | 
|  | 226 | "ASSOC", assocName.name, "PATH", upstream, "TYPE", | 
|  | 227 | boardTypes[upstream]); | 
| Alexander Hansen | 5036d94 | 2025-08-20 16:38:59 +0200 | [diff] [blame] | 228 | return; | 
|  | 229 | } | 
|  | 230 | // The downstream path must be one we care about. | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 231 | if (!std::ranges::contains(boardPaths, upstream)) | 
| Alexander Hansen | 5036d94 | 2025-08-20 16:38:59 +0200 | [diff] [blame] | 232 | { | 
|  | 233 | return; | 
|  | 234 | } | 
|  | 235 |  | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 236 | // quirk: legacy code did not associate from both sides | 
|  | 237 | // TODO(alexander): revisit this | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 238 | if (assocName == assocContaining || assocName == assocPoweredBy) | 
| Alexander Hansen | 5036d94 | 2025-08-20 16:38:59 +0200 | [diff] [blame] | 239 | { | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 240 | return; | 
| Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 241 | } | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 242 |  | 
| Alexander Hansen | 7b2a77f | 2025-08-27 12:27:57 +0200 | [diff] [blame] | 243 | result[upstream].insert({assocName.name, assocName.reverse, downstream}); | 
| Alexander Hansen | ca121f5 | 2025-08-21 18:25:19 +0200 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 246 | void Topology::remove(const std::string& boardName) | 
|  | 247 | { | 
|  | 248 | // Remove the board from boardNames, and then using the path | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 249 | // found in boardNames remove it from ports | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 250 | auto boardFind = boardNames.find(boardName); | 
|  | 251 | if (boardFind == boardNames.end()) | 
|  | 252 | { | 
|  | 253 | return; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | std::string boardPath = boardFind->second; | 
|  | 257 |  | 
|  | 258 | boardNames.erase(boardFind); | 
|  | 259 |  | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 260 | for (auto& port : ports) | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 261 | { | 
| Alexander Hansen | 68e2b0f | 2025-08-21 17:44:18 +0200 | [diff] [blame] | 262 | port.second.erase(boardPath); | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 263 | } | 
| Christopher Meis | 9a5eec9 | 2025-08-07 08:43:03 +0200 | [diff] [blame] | 264 |  | 
|  | 265 | probePaths.erase(boardName); | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | void Topology::addProbePath(const std::string& boardPath, | 
|  | 269 | const std::string& probePath) | 
|  | 270 | { | 
|  | 271 | lg2::info("Probe path added: {PROBE} probed_by {BOARD} boards config", | 
|  | 272 | "PROBE", probePath, "BOARD", boardPath); | 
|  | 273 | probePaths[boardPath].emplace(probePath); | 
| Matt Spinler | 6eb6097 | 2023-08-14 16:36:20 -0500 | [diff] [blame] | 274 | } |