Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame] | 1 | #include "topology.hpp" |
| 2 | |
| 3 | #include <iostream> |
| 4 | |
| 5 | void Topology::addBoard(const std::string& path, const std::string& boardType, |
| 6 | const nlohmann::json& exposesItem) |
| 7 | { |
| 8 | auto findType = exposesItem.find("Type"); |
| 9 | if (findType == exposesItem.end()) |
| 10 | { |
| 11 | return; |
| 12 | } |
| 13 | PortType exposesType = findType->get<std::string>(); |
| 14 | |
| 15 | if (exposesType == "DownstreamPort") |
| 16 | { |
| 17 | auto findConnectsTo = exposesItem.find("ConnectsToType"); |
| 18 | if (findConnectsTo == exposesItem.end()) |
| 19 | { |
| 20 | std::cerr << "Board at path " << path |
| 21 | << " is missing ConnectsToType" << std::endl; |
| 22 | return; |
| 23 | } |
| 24 | PortType connectsTo = findConnectsTo->get<std::string>(); |
| 25 | |
| 26 | downstreamPorts[connectsTo].emplace_back(path); |
| 27 | boardTypes[path] = boardType; |
| 28 | } |
| 29 | else if (exposesType.ends_with("Port")) |
| 30 | { |
| 31 | upstreamPorts[exposesType].emplace_back(path); |
| 32 | boardTypes[path] = boardType; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | std::unordered_map<std::string, std::vector<Association>> Topology::getAssocs() |
| 37 | { |
| 38 | std::unordered_map<std::string, std::vector<Association>> result; |
| 39 | |
| 40 | // look at each upstream port type |
| 41 | for (const auto& upstreamPortPair : upstreamPorts) |
| 42 | { |
| 43 | auto downstreamMatch = downstreamPorts.find(upstreamPortPair.first); |
| 44 | |
| 45 | if (downstreamMatch == downstreamPorts.end()) |
| 46 | { |
| 47 | // no match |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | for (const Path& upstream : upstreamPortPair.second) |
| 52 | { |
| 53 | if (boardTypes[upstream] == "Chassis" || |
| 54 | boardTypes[upstream] == "Board") |
| 55 | { |
| 56 | for (const Path& downstream : downstreamMatch->second) |
| 57 | { |
| 58 | result[downstream].emplace_back("contained_by", |
| 59 | "containing", upstream); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return result; |
| 66 | } |