forked from sonic-net/sonic-pins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Comb] Add parsers for @refers_to and @referenced_by Return empty lis…
…t instead of KNotFound when annotation does not exist.[pdpi] Refactor code base to use annotation library for refers_to annotation. (sonic-net#277) * cleanup some header files.Internal Code Change.Don't craft packets with UDP headers if UDP port is already reserved. * [SFE-P4][Garbage Collection] Support multicast entity as input to garbage collection.Improve error messages of InstallPdTableEntr{y, ies}.[pdpi] Convert referring optionals to exacts in test program.[pdpi] Add IrTableEntryReference to P4info.[pdpi] Add conversions from/to string for IrBuiltIns * [pdpi] Add parsers for @refers_to and @referenced_by Return empty list instead of KNotFound when annotation does not exist.[pdpi] Refactor code base to use annotation library for refers_to annotation. --------- Co-authored-by: rhalstea <[email protected]> Co-authored-by: Srikishen Pondicherry Shanmugam <[email protected]>
- Loading branch information
1 parent
5b99679
commit c478b38
Showing
10 changed files
with
243 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "p4_pdpi/reference_annotations.h" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "absl/status/statusor.h" | ||
#include "google/protobuf/repeated_ptr_field.h" | ||
#include "gutil/status.h" | ||
#include "p4_pdpi/utils/annotation_parser.h" | ||
|
||
namespace pdpi { | ||
|
||
using google::protobuf::RepeatedPtrField; | ||
|
||
namespace { | ||
|
||
absl::StatusOr<ParsedRefersToAnnotation> ParseAsRefersToAnnotation( | ||
std::string body) { | ||
ASSIGN_OR_RETURN(auto arg_list, annotation::ParseAsArgList(body)); | ||
if (arg_list.size() != 2) { | ||
return gutil::InvalidArgumentErrorBuilder() | ||
<< "Invalid argument. Expected 2 args, but got " << arg_list.size(); | ||
} | ||
return ParsedRefersToAnnotation{.table = arg_list[0], .field = arg_list[1]}; | ||
} | ||
|
||
absl::StatusOr<ParsedReferencedByAnnotation> ParseAsReferencedByAnnotation( | ||
std::string body) { | ||
ASSIGN_OR_RETURN(auto arg_list, annotation::ParseAsArgList(body)); | ||
if (arg_list.size() != 2) { | ||
return gutil::InvalidArgumentErrorBuilder() | ||
<< "Invalid argument. Expected 2 args, but got " << arg_list.size(); | ||
} | ||
return ParsedReferencedByAnnotation{.table = arg_list[0], | ||
.field = arg_list[1]}; | ||
} | ||
|
||
} // namespace | ||
|
||
absl::StatusOr<std::vector<ParsedRefersToAnnotation>> | ||
ParseAllRefersToAnnotations(const RepeatedPtrField<std::string>& annotations) { | ||
return GetAllParsedAnnotations<ParsedRefersToAnnotation>( | ||
"refers_to", annotations, ParseAsRefersToAnnotation); | ||
} | ||
|
||
absl::StatusOr<std::vector<ParsedReferencedByAnnotation>> | ||
ParseAllReferencedByAnnotations( | ||
const RepeatedPtrField<std::string>& annotations) { | ||
return GetAllParsedAnnotations<ParsedReferencedByAnnotation>( | ||
"referenced_by", annotations, ParseAsReferencedByAnnotation); | ||
} | ||
} // namespace pdpi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef PINS_P4_PDPI_REFERENCE_ANNOTATIONS_H_ | ||
#define PINS_P4_PDPI_REFERENCE_ANNOTATIONS_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "absl/status/statusor.h" | ||
#include "google/protobuf/repeated_ptr_field.h" | ||
|
||
namespace pdpi { | ||
|
||
// Parsed contents of an `@refers_to(table, field)` annotation. | ||
struct ParsedRefersToAnnotation { | ||
std::string table; | ||
std::string field; | ||
}; | ||
|
||
// Contents of an `@reference_by(table, field)` annotation. | ||
struct ParsedReferencedByAnnotation { | ||
std::string table; | ||
std::string field; | ||
}; | ||
|
||
// Returns a list of `RefersToAnnotation` parsed from the `annotations`. | ||
// Returns empty list if no annotation contained the label `@refers_to`. | ||
// Return InvalidArgument if any annotation with the label `@refers_to` did not | ||
// contain exactly 2 arguments. | ||
absl::StatusOr<std::vector<ParsedRefersToAnnotation>> | ||
ParseAllRefersToAnnotations( | ||
const google::protobuf::RepeatedPtrField<std::string>& annotations); | ||
|
||
// Returns a list of `ReferencedByAnnotation`s parsed from the `annotations`. | ||
// Returns empty list if no annotation contained the label `@referenced_by`. | ||
// Return InvalidArgument if any annotation with the label `@referenced_by` did | ||
// not contain exactly 2 arguments. | ||
absl::StatusOr<std::vector<ParsedReferencedByAnnotation>> | ||
ParseAllReferencedByAnnotations( | ||
const google::protobuf::RepeatedPtrField<std::string>& annotations); | ||
|
||
} // namespace pdpi | ||
|
||
#endif // PINS_P4_PDPI_REFERENCE_ANNOTATIONS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "p4_pdpi/reference_annotations.h" | ||
|
||
#include <string> | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/strings/substitute.h" | ||
#include "gmock/gmock.h" | ||
#include "google/protobuf/repeated_ptr_field.h" | ||
#include "gtest/gtest.h" | ||
#include "gutil/status_matchers.h" | ||
|
||
namespace pdpi { | ||
namespace { | ||
|
||
using ::gutil::IsOkAndHolds; | ||
using ::gutil::StatusIs; | ||
using ::testing::ElementsAre; | ||
|
||
// -- Matchers ----------------------------------------------------------------- | ||
|
||
MATCHER_P2(HasTableAndField, table, field, | ||
absl::Substitute("Matches: [table: $0, field: $1]", table, field)) { | ||
return arg.table == table && arg.field == field; | ||
} | ||
|
||
// -- Tests -------------------------------------------------------------------- | ||
|
||
TEST(ParseAllRefersToAnnotationArgs, ReturnsAllRefersToAnnotations) { | ||
google::protobuf::RepeatedPtrField<std::string> annotations; | ||
annotations.Add("@refers_to(a,b)"); | ||
annotations.Add("@referenced_by(c,d)"); | ||
annotations.Add("@refers_to(\n e,f \t)"); | ||
EXPECT_THAT(ParseAllRefersToAnnotations(annotations), | ||
IsOkAndHolds(ElementsAre(HasTableAndField("a", "b"), | ||
HasTableAndField("e", "f")))); | ||
} | ||
|
||
TEST(ParseAllRefersToAnnotationArgs, | ||
FailsWithRefersToWithMoreThanTwoArguments) { | ||
google::protobuf::RepeatedPtrField<std::string> annotations; | ||
annotations.Add("@refers_to(a,b,c)"); | ||
EXPECT_THAT(ParseAllRefersToAnnotations(annotations), | ||
StatusIs(absl::StatusCode::kInvalidArgument)); | ||
} | ||
|
||
TEST(ParseAllReferencedByAnnotationArgs, ReturnsAllRefersToAnnotations) { | ||
google::protobuf::RepeatedPtrField<std::string> annotations; | ||
annotations.Add("@referenced_by(a,b)"); | ||
annotations.Add("@refers_to(c,d)"); | ||
annotations.Add("@referenced_by(\n e,f \t)"); | ||
EXPECT_THAT(ParseAllReferencedByAnnotations(annotations), | ||
IsOkAndHolds(ElementsAre(HasTableAndField("a", "b"), | ||
HasTableAndField("e", "f")))); | ||
} | ||
|
||
TEST(ParseAllReferencedByAnnotationArgs, | ||
FailsWithReferencedByWithMoreThanTwoArguments) { | ||
google::protobuf::RepeatedPtrField<std::string> annotations; | ||
annotations.Add("@referenced_by(a,b,c)"); | ||
EXPECT_THAT(ParseAllReferencedByAnnotations(annotations), | ||
StatusIs(absl::StatusCode::kInvalidArgument)); | ||
} | ||
|
||
} // namespace | ||
} // namespace pdpi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.