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