Skip to content

Commit

Permalink
[Thinkit] Adding lib/p4rt files. (sonic-net#312)
Browse files Browse the repository at this point in the history
* [Thinkit] Adding json_utils files.

* [Thinkit] Adding lib/p4rt files.
  • Loading branch information
divyagayathri-hcl authored Jul 11, 2024
1 parent 5e93bcc commit 0052972
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 32 deletions.
31 changes: 31 additions & 0 deletions lib/p4rt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

cc_library(
name = "p4rt_port",
srcs = ["p4rt_port.cc"],
hdrs = ["p4rt_port.h"],
deps = [
"//p4_pdpi/string_encodings:decimal_string",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
],
)

58 changes: 58 additions & 0 deletions lib/p4rt/p4rt_port.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lib/p4rt/p4rt_port.h"

#include <cstdint>

#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "gutil/status.h"
#include "p4_pdpi/string_encodings/decimal_string.h"

namespace pins_test {

absl::StatusOr<P4rtPortId> P4rtPortId::OfP4rtEncoding(
absl::string_view p4rt_port_id) {
ASSIGN_OR_RETURN(uint32_t p4rt_port_id_int,
pdpi::DecimalStringToUint32(p4rt_port_id));
return P4rtPortId(p4rt_port_id_int);
}

P4rtPortId P4rtPortId::OfOpenConfigEncoding(uint32_t p4rt_port_id) {
return P4rtPortId(p4rt_port_id);
}

uint32_t P4rtPortId::GetOpenConfigEncoding() const { return p4rt_port_id_; }

std::string P4rtPortId::GetP4rtEncoding() const {
return absl::StrCat(p4rt_port_id_);
}

bool P4rtPortId::operator==(const P4rtPortId& other) const {
return p4rt_port_id_ == other.p4rt_port_id_;
}

bool P4rtPortId::operator<(const P4rtPortId& other) const {
return p4rt_port_id_ < other.p4rt_port_id_;
}

std::ostream& operator<<(std::ostream& os, const P4rtPortId& p4rt_port_id) {
return os << absl::StrCat("(p4rt_port_id: ", p4rt_port_id.GetP4rtEncoding(),
")");
}

} // namespace pins_test
43 changes: 11 additions & 32 deletions lib/p4rt/p4rt_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PINS_LIB_P4RT_P4RT_PORT_H_
#define PINS_LIB_P4RT_P4RT_PORT_H_

#ifndef GOOGLE_LIB_P4RT_P4RT_PORT_H_
#define GOOGLE_LIB_P4RT_P4RT_PORT_H_

#include <cstdint>
#include <ostream>
#include <string>
#include <vector>

#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"

namespace pins_test {

// Class representing a P4Runtime port ID.
//
// For historical reasons, there exists three encodings of P4Runtime port IDs:
// For historical reasons, there exists two encodings of P4Runtime port IDs:
// - As an integer (uint32_t) `42`, e.g. in OpenConfig.
// - As a decimal string `"42"`, e.g. in P4Runtime messages such as table
// entries.
// - As a binary string `"\x2A"`, e.g. in P4Runtime messages to BMv2, which
// doesn't currently support decimal strings. This class represents a P4Runtime
// port ID, abstracting away the encoding.
//
// This class represents a P4Runtime port ID, abstracting away the encoding.

// TODO: Agree on a single encoding so this class becomes obsolete.
class P4rtPortId {
public:
// Constructors.
P4rtPortId() = default;

// Expects a decimal string. Else returns InvalidArgumentError.
static absl::StatusOr<P4rtPortId> MakeFromP4rtEncoding(
static absl::StatusOr<P4rtPortId> OfP4rtEncoding(
absl::string_view p4rt_port_id);
static absl::StatusOr<std::vector<P4rtPortId>> MakeVectorFromP4rtEncodings(
absl::Span<const std::string> p4rt_port_id);

// Constructs a P4rtPortId from an OpenConfig encoding, i.e. a uint32. Never
// fails.
static P4rtPortId MakeFromOpenConfigEncoding(uint32_t p4rt_port_id);
static std::vector<P4rtPortId> MakeVectorFromOpenConfigEncodings(
absl::Span<const uint32_t> p4rt_port_id);
static P4rtPortId OfOpenConfigEncoding(uint32_t p4rt_port_id);

// Getters.
// Returns OpenConfig encoding of the port ID, e.g. the uint32 `42`.
Expand All @@ -62,30 +54,17 @@ class P4rtPortId {
// Returns P4Runtime encoding of the port ID, e.g. the string `"42"`.
std::string GetP4rtEncoding() const;

// Returns BMv2 P4Runtime encoding of the port ID, e.g. the byte string
// `"\x2A"`.
absl::StatusOr<std::string> GetBmv2P4rtEncoding() const;

bool operator==(const P4rtPortId& other) const;
bool operator<(const P4rtPortId& other) const;

template <typename H>
friend H AbslHashValue(H h, const P4rtPortId& port_id) {
return H::combine(std::move(h), port_id.p4rt_port_id_);
}

private:
explicit P4rtPortId(uint32_t p4rt_port_id) : p4rt_port_id_(p4rt_port_id) {}
uint32_t p4rt_port_id_ = 0;
};

// TODO: Remove and update the class to use AbslStringify.
std::ostream& operator<<(std::ostream& os, const P4rtPortId& p4rt_port_id);

template <typename Sink>
inline void AbslStringify(Sink& sink, const P4rtPortId& p4rt_port_id) {
absl::Format(&sink, "%s", p4rt_port_id.GetP4rtEncoding());
}

} // namespace pins_test

#endif // PINS_LIB_P4RT_P4RT_PORT_H_
#endif // GOOGLE_LIB_P4RT_P4RT_PORT_H_

0 comments on commit 0052972

Please sign in to comment.