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