Skip to content

Commit

Permalink
Call validation backends setup from the validator.Added port up valid…
Browse files Browse the repository at this point in the history
…ator to pins backend.
  • Loading branch information
kishanps authored and bibhuprasad-hcl committed Jul 18, 2024
1 parent 549c614 commit 00e6fb9
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 1 deletion.
57 changes: 57 additions & 0 deletions lib/gnmi/gnmi_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "absl/time/time.h"
#include "glog/logging.h"
Expand Down Expand Up @@ -174,6 +175,62 @@ absl::Status PushGnmiConfig(gnmi::gNMI::Stub& stub,
return absl::OkStatus();
}

absl::Status CheckAllInterfaceUpOverGnmi(gnmi::gNMI::Stub& stub) {
ASSIGN_OR_RETURN(auto req,
BuildGnmiGetRequest("interfaces", gnmi::GetRequest::STATE));
gnmi::GetResponse resp;
grpc::ClientContext context;
grpc::Status status = stub.Get(&context, req, &resp);
if (!status.ok()) return gutil::GrpcStatusToAbslStatus(status);

if (resp.notification_size() < 1 || resp.notification(0).update_size() < 1) {
return absl::InternalError(
absl::StrCat("Invalid response: ", resp.DebugString()));
}

const auto resp_json = nlohmann::json::parse(
resp.notification(0).update(0).val().json_ietf_val());
const auto oc_intf_json = resp_json.find("openconfig-interfaces:interfaces");
if (oc_intf_json == resp_json.end()) {
return absl::NotFoundError(absl::StrCat(
"'openconfig-interfaces:interfaces' not found: ", resp_json.dump()));
}
const auto oc_intf_list_json = oc_intf_json->find("interface");
if (oc_intf_list_json == oc_intf_json->end()) {
return absl::NotFoundError(
absl::StrCat("'interface' not found: ", oc_intf_json->dump()));
}

std::vector<std::string> unavailable_interfaces;
for (auto const& element : oc_intf_list_json->items()) {
auto const element_name_json = element.value().find("name");
if (element_name_json == element.value().end()) {
return absl::NotFoundError(
absl::StrCat("'name' not found: ", element.value().dump()));
}
auto const element_interface_state_json = element.value().find("state");
if (element_interface_state_json == element.value().end()) {
return absl::NotFoundError(absl::StrCat(
"'state' not found: ", element.value().find("name")->dump()));
}
auto const element_status_json =
element_interface_state_json->find("oper-status");
if (element_status_json == element_interface_state_json->end()) {
return absl::NotFoundError(absl::StrCat(
"'oper-status' not found: ", element.value().find("name")->dump()));
}
if (element_status_json->dump().find("UP") == grpc::string_ref::npos) {
unavailable_interfaces.push_back(element.value().find("name")->dump());
}
}
if (!unavailable_interfaces.empty()) {
return absl::UnavailableError(
absl::StrCat("Interfaces are not ready. ",
absl::StrJoin(unavailable_interfaces, "\n")));
}
return absl::OkStatus();
}

absl::StatusOr<std::string> GetGnmiStatePathInfo(
gnmi::gNMI::Stub* sut_gnmi_stub, absl::string_view state_path,
absl::string_view resp_parse_str) {
Expand Down
2 changes: 2 additions & 0 deletions lib/gnmi/gnmi_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,7 @@ absl::Status PushGnmiConfig(
const std::string& gnmi_config,
absl::uint128 election_id = pdpi::TimeBasedElectionId());

absl::Status CheckAllInterfaceUpOverGnmi(gnmi::gNMI::Stub& stub);

} // namespace pins_test
#endif // GOOGLE_LIB_GNMI_GNMI_HELPER_H_
1 change: 1 addition & 0 deletions lib/validator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ cc_library(
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/functional:bind_front",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
Expand Down
14 changes: 14 additions & 0 deletions lib/validator/pins_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
Expand Down Expand Up @@ -103,4 +104,17 @@ absl::Status PINSBackend::CanGetTimeOverGnoiSystem(absl::string_view chassis,
gnoi_system_stub->Time(&context, request, &response));
}

absl::Status PINSBackend::CheckAllInterfaceUpOverGnmi(absl::string_view chassis,
absl::Duration timeout) {
auto sut = switches_map_.find(chassis);
if (sut == switches_map_.end()) {
return absl::InternalError(
absl::StrCat("ValidatorBackend passed invalid chassis: ", chassis));
}

auto& [sut_name, sut_switch] = *sut;
ASSIGN_OR_RETURN(auto gnmi_stub, sut_switch->CreateGnmiStub());
return pins_test::CheckAllInterfaceUpOverGnmi(*gnmi_stub);
}

} // namespace pins_test
9 changes: 9 additions & 0 deletions lib/validator/pins_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class PINSBackend : public ValidatorBackend {
static constexpr absl::string_view kGnmiUsable = "GnmiUsable";
// Validates if a gNOI system connection can be established and used.
static constexpr absl::string_view kGnoiSystemUsable = "GnoiSystemUsable";
// Validates if all ports are up.
static constexpr absl::string_view kPortsUp = "PortsUp";

PINSBackend(std::vector<std::unique_ptr<thinkit::Switch>> switches);

Expand All @@ -55,6 +57,10 @@ class PINSBackend : public ValidatorBackend {
absl::Status CanGetTimeOverGnoiSystem(absl::string_view chassis,
absl::Duration timeout);

// Checks if "oper-status" of all interfaces are "UP".
absl::Status CheckAllInterfaceUpOverGnmi(absl::string_view chassis,
absl::Duration timeout);

protected:
void SetupValidations() override {
AddCallbacksToValidation(
Expand All @@ -66,6 +72,9 @@ class PINSBackend : public ValidatorBackend {
AddCallbacksToValidation(
kGnoiSystemUsable,
{absl::bind_front(&PINSBackend::CanGetTimeOverGnoiSystem, this)});
AddCallbacksToValidation(
kPortsUp,
{absl::bind_front(&PINSBackend::CheckAllInterfaceUpOverGnmi, this)});
AddCallbacksToValidation(
Validator::kReady,
{absl::bind_front(&PINSBackend::CanEstablishP4RuntimeSession, this),
Expand Down
6 changes: 5 additions & 1 deletion lib/validator/validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
namespace pins_test {

Validator::Validator(std::vector<std::unique_ptr<ValidatorBackend>> backends)
: backends_(std::move(backends)) {}
: backends_(std::move(backends)) {
for (const auto& backend : backends_) {
backend->SetupValidations();
}
}

absl::Status Validator::RunValidations(
absl::Span<const absl::string_view> devices,
Expand Down

0 comments on commit 00e6fb9

Please sign in to comment.