Skip to content

Commit

Permalink
Merge branch 'main' into branch_thinkit5
Browse files Browse the repository at this point in the history
  • Loading branch information
divyagayathri-hcl authored Jul 24, 2024
2 parents d1a2638 + 8790cfe commit c231fdc
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 32 deletions.
2 changes: 2 additions & 0 deletions lib/gnmi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ cc_library(
"@com_github_nlohmann_json//:nlohmann_json",
"@com_github_p4lang_p4runtime//:p4runtime_cc_grpc",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/numeric:int128",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
"@com_google_protobuf//:protobuf_lite",
"@com_googlesource_code_re2//:re2",
],
Expand Down
62 changes: 49 additions & 13 deletions lib/gnmi/gnmi_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/numeric/int128.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
Expand All @@ -28,7 +30,9 @@
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "glog/logging.h"
#include "google/protobuf/map.h"
#include "grpcpp/impl/codegen/client_context.h"
Expand Down Expand Up @@ -182,13 +186,12 @@ absl::Status PushGnmiConfig(gnmi::gNMI::Stub& stub,
gnmi::Update* replace = req.add_replace();
replace->mutable_path();
replace->mutable_val()->set_json_ietf_val(gnmi_config);
gnmi_ext::MasterArbitration* master_arbitration =
gnmi_ext::MasterArbitration* arbitration =
req.add_extension()->mutable_master_arbitration();
master_arbitration->mutable_role()->set_id("dataplane test");
master_arbitration->mutable_election_id()->set_high(
arbitration->mutable_role()->set_id("dataplane test");
arbitration->mutable_election_id()->set_high(
absl::Uint128High64(election_id));
master_arbitration->mutable_election_id()->set_low(
absl::Uint128Low64(election_id));
arbitration->mutable_election_id()->set_low(absl::Uint128Low64(election_id));

gnmi::SetResponse resp;
grpc::ClientContext context;
Expand Down Expand Up @@ -284,6 +287,40 @@ GetInterfaceToOperStatusMapOverGnmi(gnmi::gNMI::StubInterface& stub,
return interface_to_oper_status_map;
}

absl::Status CheckInterfaceOperStateOverGnmi(
gnmi::gNMI::StubInterface& stub, absl::string_view interface_oper_state,
absl::Span<const std::string> interfaces, absl::Duration timeout) {
ASSIGN_OR_RETURN(const auto interface_to_oper_status_map,
GetInterfaceToOperStatusMapOverGnmi(stub, timeout));

absl::flat_hash_set<std::string> matching_interfaces;
for (const auto& [interface, oper_status] : interface_to_oper_status_map) {
if (oper_status == interface_oper_state) {
matching_interfaces.insert(interface);
}
}

bool all_interfaces_found = true;
for (const std::string& interface : interfaces) {
if (!matching_interfaces.contains(interface)) {
LOG(INFO) << "Interface "
<< interface << " not found in interfaces that are "
<< interface_oper_state;
all_interfaces_found = false;
}
}

if (!all_interfaces_found) {
return absl::UnavailableError(
absl::StrCat("Some interfaces are not in the expected "
"state.\nInterfaces provided: \n",
absl::StrJoin(interfaces, "\n"),
"\nInterfaces in the expected state: \n",
absl::StrJoin(matching_interfaces, "\n")));
}
return absl::OkStatus();
}

absl::Status CheckAllInterfaceOperStateOverGnmi(
gnmi::gNMI::StubInterface& stub, absl::string_view interface_oper_state,
bool skip_non_ethernet_interfaces, absl::Duration timeout) {
Expand Down Expand Up @@ -387,7 +424,7 @@ absl::StatusOr<std::vector<std::string>> GetUpInterfacesOverGnmi(
}

absl::StatusOr<OperStatus> GetInterfaceOperStatusOverGnmi(
gnmi::gNMI::Stub& stub, absl::string_view if_name) {
gnmi::gNMI::StubInterface& stub, absl::string_view if_name) {
std::string if_req = absl::StrCat("interfaces/interface[name=", if_name,
"]/state/oper-status");
ASSIGN_OR_RETURN(auto request,
Expand All @@ -407,13 +444,13 @@ absl::StatusOr<OperStatus> GetInterfaceOperStatusOverGnmi(
std::string oper_status,
ParseGnmiGetResponse(response, "openconfig-interfaces:oper-status"));

if (absl::StrContains((oper_status), "UP")) {
if (absl::StrContains(oper_status, "UP")) {
return OperStatus::kUp;
}
if (absl::StrContains((oper_status), "DOWN")) {
if (absl::StrContains(oper_status, "DOWN")) {
return OperStatus::kDown;
}
if (absl::StrContains((oper_status), "TESTING")) {
if (absl::StrContains(oper_status, "TESTING")) {
return OperStatus::kTesting;
}
return OperStatus::kUnknown;
Expand Down Expand Up @@ -466,11 +503,10 @@ GetAllInterfaceNameToPortId(gnmi::gNMI::StubInterface& stub) {
}

const auto element_id_json =
element_interface_state_json->find("openconfig-pins-interfaces:id");
element_interface_state_json->find("openconfig-p4rt:id");
if (element_id_json == element_interface_state_json->end()) {
return absl::NotFoundError(
absl::StrCat("'openconfig-pins-interfaces:id' not found: ",
element.value().dump()));
return absl::NotFoundError(absl::StrCat(
"'openconfig-p4rt:id' not found: ", element.value().dump()));
}
interface_name_to_port_id[name] = absl::StrCat(element_id_json->get<int>());
}
Expand Down
9 changes: 8 additions & 1 deletion lib/gnmi/gnmi_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "p4_pdpi/p4_runtime_session.h"
#include "absl/types/span.h"
#include "proto/gnmi/gnmi.grpc.pb.h"
#include "proto/gnmi/gnmi.pb.h"
#include "thinkit/switch.h"
Expand Down Expand Up @@ -117,6 +118,12 @@ absl::StatusOr<absl::flat_hash_map<std::string, std::string>>
GetInterfaceToOperStatusMapOverGnmi(gnmi::gNMI::StubInterface& stub,
absl::Duration timeout);

// Checks if given interfaces' oper-status is up/down.
absl::Status CheckInterfaceOperStateOverGnmi(
gnmi::gNMI::StubInterface& stub, absl::string_view interface_oper_state,
absl::Span<const std::string> interfaces,
absl::Duration timeout = absl::Seconds(60));

// Checks if all interfaces oper-status is up/down.
absl::Status CheckAllInterfaceOperStateOverGnmi(
gnmi::gNMI::StubInterface& stub, absl::string_view interface_oper_state,
Expand All @@ -133,7 +140,7 @@ absl::StatusOr<std::vector<std::string>> GetUpInterfacesOverGnmi(

// Gets the operational status of an interface.
absl::StatusOr<OperStatus> GetInterfaceOperStatusOverGnmi(
gnmi::gNMI::Stub& stub, absl::string_view if_name);
gnmi::gNMI::StubInterface& stub, absl::string_view if_name);

// Gets the interface name to port id map.
absl::StatusOr<absl::flat_hash_map<std::string, std::string>>
Expand Down
Loading

0 comments on commit c231fdc

Please sign in to comment.