| #include "tlbmc/redfish/data/stable_id.h" |
| |
| #include <string> |
| #include <vector> |
| |
| #include "absl/strings/match.h" |
| #include "absl/strings/str_cat.h" |
| #include "absl/strings/str_join.h" |
| #include "absl/strings/str_split.h" |
| #include "topology_config.pb.h" |
| #include "stable_id.pb.h" |
| #include "resource.pb.h" |
| |
| namespace milotic_tlbmc { |
| |
| StableId GetStableId(const TopologyConfigNode& topology_config_node) { |
| StableId stable_id; |
| |
| const LocationContext& location_context = |
| topology_config_node.location_context(); |
| std::string devpath = location_context.devpath(); |
| // Remove /phys from the devpath. |
| if (absl::StartsWith(devpath, "/phys")) { |
| devpath = devpath.substr(5); |
| } |
| |
| // Find and remove the port label from the devpath. |
| std::string embedded_port_label; |
| auto find_device = devpath.find(":device:"); |
| if (find_device != std::string::npos) { |
| embedded_port_label = devpath.substr(find_device + 8); |
| devpath = devpath.substr(0, find_device); |
| } |
| |
| std::vector<std::string> devpath_parts = |
| absl::StrSplit(devpath, '/', absl::SkipEmpty()); |
| if (devpath_parts.empty() && embedded_port_label.empty()) { |
| return stable_id; |
| } |
| |
| if (!embedded_port_label.empty()) { |
| stable_id.set_service_label(embedded_port_label); |
| stable_id.set_part_location_context( |
| absl::StrJoin(devpath_parts.begin(), devpath_parts.end(), "/")); |
| } else { |
| stable_id.set_service_label(devpath_parts.back()); |
| stable_id.set_part_location_context( |
| absl::StrJoin(devpath_parts.begin(), devpath_parts.end() - 1, "/")); |
| } |
| |
| std::string embedded_location_context; |
| for (const auto& logical_identifier : |
| location_context.logical_identifiers()) { |
| absl::StrAppend(&embedded_location_context, |
| embedded_location_context.empty() ? "" : "/", |
| logical_identifier); |
| } |
| if (!embedded_location_context.empty()) { |
| stable_id.set_embedded_location_context(embedded_location_context); |
| } |
| |
| if (location_context.has_location_type()) { |
| stable_id.set_location_type(location_context.location_type()); |
| if (stable_id.location_type() == PART_LOCATION_TYPE_EMBEDDED) { |
| stable_id.set_service_label(topology_config_node.name()); |
| } |
| } |
| |
| return stable_id; |
| } |
| } // namespace milotic_tlbmc |