Skip to content

Commit

Permalink
[QoS] Add proto schemas for parsing gNMI QoS config. (#895)
Browse files Browse the repository at this point in the history
Co-authored-by: smolkaj <smolkaj@google.com>
Co-authored-by: kishanps <kishanps@google.com>
  • Loading branch information
3 people authored Dec 27, 2024
1 parent d2b0dca commit e76b22e
Show file tree
Hide file tree
Showing 4 changed files with 1,288 additions and 36 deletions.
3 changes: 3 additions & 0 deletions tests/qos/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ cc_library(
hdrs = ["gnmi_parsers.h"],
deps = [
"//gutil:overload",
"//gutil:proto",
"//lib/gnmi:openconfig_cc_proto",
"//p4_pdpi/netaddr:ipv4_address",
"//p4_pdpi/netaddr:ipv6_address",
Expand All @@ -127,7 +128,9 @@ cc_test(
linkstatic = True,
deps = [
":gnmi_parsers",
"//gutil:proto",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
],
)

Expand Down
40 changes: 10 additions & 30 deletions tests/qos/gnmi_parsers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,14 @@
#include "absl/strings/string_view.h"
#include "google/protobuf/util/json_util.h"
#include "gutil/overload.h"
#include "gutil/proto.h"
#include "gutil/status.h"
#include "lib/gnmi/openconfig.pb.h"
#include "p4_pdpi/netaddr/ipv4_address.h"
#include "p4_pdpi/netaddr/ipv6_address.h"

namespace pins_test {

namespace {

template <class T>
absl::StatusOr<T> ParseProtoFromJsonIgnoringUnknownFields(
absl::string_view json) {
google::protobuf::util::JsonParseOptions options;
options.ignore_unknown_fields = true;
T proto;
// OS protobuf uses its own `Status`-like and `string_view`-like classes, so
// some gymnastics are required here:
// - ToAbslStatus converts any `Status`-like type to an absl::Status.
// - We pass in `{json.data(), json.size()}` instead of `json`, constructing
// a new object of the appropriate `string_view`-like type implicitly.
RETURN_IF_ERROR(
gutil::ToAbslStatus(google::protobuf::util::JsonStringToMessage(
{json.data(), json.size()}, &proto, options)));
return proto;
}

} // namespace

absl::StatusOr<
std::vector<std::variant<netaddr::Ipv4Address, netaddr::Ipv6Address>>>
ParseLoopbackIps(const openconfig::Config& config) {
Expand All @@ -57,25 +37,25 @@ ParseLoopbackIps(const openconfig::Config& config) {
absl::StatusOr<
std::vector<std::variant<netaddr::Ipv4Address, netaddr::Ipv6Address>>>
ParseLoopbackIps(absl::string_view gnmi_config) {
ASSIGN_OR_RETURN(
auto config,
ParseProtoFromJsonIgnoringUnknownFields<openconfig::Config>(gnmi_config));
ASSIGN_OR_RETURN(auto config,
gutil::ParseJsonAsProto<openconfig::Config>(
gnmi_config, /*ignore_unknown_fields=*/true));
return ParseLoopbackIps(config);
}

absl::StatusOr<std::vector<netaddr::Ipv6Address>> ParseLoopbackIpv6s(
absl::string_view gnmi_config) {
ASSIGN_OR_RETURN(
auto config,
ParseProtoFromJsonIgnoringUnknownFields<openconfig::Config>(gnmi_config));
ASSIGN_OR_RETURN(auto config,
gutil::ParseJsonAsProto<openconfig::Config>(
gnmi_config, /*ignore_unknown_fields=*/true));
return ParseLoopbackIpv6s(config);
}

absl::StatusOr<std::vector<netaddr::Ipv4Address>> ParseLoopbackIpv4s(
absl::string_view gnmi_config) {
ASSIGN_OR_RETURN(
auto config,
ParseProtoFromJsonIgnoringUnknownFields<openconfig::Config>(gnmi_config));
ASSIGN_OR_RETURN(auto config,
gutil::ParseJsonAsProto<openconfig::Config>(
gnmi_config, /*ignore_unknown_fields=*/true));
return ParseLoopbackIpv4s(config);
}

Expand Down
Loading

0 comments on commit e76b22e

Please sign in to comment.