Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow json output format to be modified #4407

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
allow json output format to be modified
Expose JsonPrintOptions to allow JSON output format to be modified.
  • Loading branch information
grg committed Feb 16, 2024
commit 0f50dfcff1c9c241a91b880c14a3bd5ab1da4dab
5 changes: 5 additions & 0 deletions control-plane/p4RuntimeArchHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ limitations under the License.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wpedantic"
#include <google/protobuf/util/json_util.h>

#include "p4/config/v1/p4info.pb.h"
#include "p4/v1/p4runtime.pb.h"
#pragma GCC diagnostic pop
Expand Down Expand Up @@ -184,6 +186,9 @@ class P4RuntimeArchHandlerIface {
const IR::ExternBlock *externBlock) = 0;
/// called when processing annotations via setPreamble
virtual bool filterAnnotations(cstring anno) = 0;

/// Control how JSON is output
virtual google::protobuf::util::JsonPrintOptions getJsonPrintOptions() = 0;
};

/// A functor interface that needs to be implemented for each
Expand Down
11 changes: 10 additions & 1 deletion control-plane/p4RuntimeArchStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,9 @@ class P4RuntimeArchHandlerCommon : public P4RuntimeArchHandlerIface {

P4RuntimeArchHandlerCommon(ReferenceMap *refMap, TypeMap *typeMap,
const IR::ToplevelBlock *evaluatedProgram)
: refMap(refMap), typeMap(typeMap), evaluatedProgram(evaluatedProgram) {}
: refMap(refMap), typeMap(typeMap), evaluatedProgram(evaluatedProgram) {
jsonPrintOptions.add_whitespace = true;
}

void collectTableProperties(P4RuntimeSymbolTableIface *symbols,
const IR::TableBlock *tableBlock) override {
Expand Down Expand Up @@ -708,6 +710,10 @@ class P4RuntimeArchHandlerCommon : public P4RuntimeArchHandlerIface {
const IR::ExternBlock *) override {}
bool filterAnnotations(cstring) override { return false; }

google::protobuf::util::JsonPrintOptions getJsonPrintOptions() override {
return jsonPrintOptions;
}

static std::optional<ActionProfile> getActionProfile(cstring name, const IR::Type_Extern *type,
int64_t size,
const IR::IAnnotated *annotations) {
Expand Down Expand Up @@ -924,6 +930,9 @@ class P4RuntimeArchHandlerCommon : public P4RuntimeArchHandlerIface {

/// The extern instances we've serialized so far. Used for deduplication.
std::set<p4rt_id_t> serializedInstances;

// JSON printing options for serialization
google::protobuf::util::JsonPrintOptions jsonPrintOptions;
};

/// Implements a common @ref P4RuntimeArchHandlerIface for the PSA and PNA architecture. The
Expand Down
14 changes: 6 additions & 8 deletions control-plane/p4RuntimeSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static std::optional<cstring> explicitNameAnnotation(const IR::IAnnotated *item)
namespace writers {

using google::protobuf::Message;
using google::protobuf::util::JsonPrintOptions;

/// Serialize the protobuf @message to @destination in the binary protocol
/// buffers format.
Expand All @@ -113,14 +114,11 @@ static bool writeTo(const Message &message, std::ostream *destination) {

/// Serialize the protobuf @message to @destination in the JSON protocol buffers
/// format. This is intended for debugging and testing.
static bool writeJsonTo(const Message &message, std::ostream *destination) {
static bool writeJsonTo(const Message &message, std::ostream *destination,
const JsonPrintOptions &options) {
using namespace google::protobuf::util;
CHECK_NULL(destination);

// Serialize the JSON in a human-readable format.
JsonPrintOptions options;
options.add_whitespace = true;

std::string output;
if (!MessageToJsonString(message, &output, options).ok()) {
::error(ErrorType::ERR_IO, "Failed to serialize protobuf message to JSON");
Expand Down Expand Up @@ -1413,7 +1411,7 @@ class P4RuntimeEntriesConverter {

auto *p4Info = analyzer.getP4Info();
auto *p4Entries = entriesConverter.getEntries();
return P4RuntimeAPI{p4Info, p4Entries};
return P4RuntimeAPI{p4Info, p4Entries, archHandler->getJsonPrintOptions()};
}

} // namespace ControlPlaneAPI
Expand Down Expand Up @@ -1464,7 +1462,7 @@ void P4RuntimeAPI::serializeP4InfoTo(std::ostream *destination, P4RuntimeFormat
success = writers::writeTo(*p4Info, destination);
break;
case P4RuntimeFormat::JSON:
success = writers::writeJsonTo(*p4Info, destination);
success = writers::writeJsonTo(*p4Info, destination, jsonPrintOptions);
break;
case P4RuntimeFormat::TEXT_PROTOBUF:
case P4RuntimeFormat::TEXT:
Expand All @@ -1484,7 +1482,7 @@ void P4RuntimeAPI::serializeEntriesTo(std::ostream *destination, P4RuntimeFormat
success = writers::writeTo(*entries, destination);
break;
case P4RuntimeFormat::JSON:
success = writers::writeJsonTo(*entries, destination);
success = writers::writeJsonTo(*entries, destination, jsonPrintOptions);
break;
case P4RuntimeFormat::TEXT_PROTOBUF:
case P4RuntimeFormat::TEXT:
Expand Down
19 changes: 19 additions & 0 deletions control-plane/p4RuntimeSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ limitations under the License.
#include <iosfwd>
#include <unordered_map>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wpedantic"
#include <google/protobuf/text_format.h>
#include <google/protobuf/util/json_util.h>
#pragma GCC diagnostic pop

#include "lib/cstring.h"

namespace p4 {
Expand Down Expand Up @@ -60,6 +67,18 @@ struct P4RuntimeAPI {
/// All static table entries as one P4Runtime WriteRequest object. Never
/// null.
const ::p4::v1::WriteRequest *entries;

// Print options to use while outputting JSON.
google::protobuf::util::JsonPrintOptions jsonPrintOptions;

P4RuntimeAPI(const ::p4::config::v1::P4Info *p4Info, const ::p4::v1::WriteRequest *entries)
: p4Info(p4Info), entries(entries) {
jsonPrintOptions.add_whitespace = true;
}

P4RuntimeAPI(const ::p4::config::v1::P4Info *p4Info, const ::p4::v1::WriteRequest *entries,
google::protobuf::util::JsonPrintOptions jsonPrintOptions)
: p4Info(p4Info), entries(entries), jsonPrintOptions(jsonPrintOptions) {}
};

namespace ControlPlaneAPI {
Expand Down
53 changes: 53 additions & 0 deletions test/gtest/p4runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,59 @@ TEST_F(P4Runtime, Documentation) {
}
}

TEST_F(P4Runtime, JsonSerializationPrintOptions) {
auto test = createP4RuntimeTestCase(P4_SOURCE(P4Headers::V1MODEL, R"(
struct Headers { }
struct Metadata { }
parser parse(packet_in p, out Headers h, inout Metadata m,
inout standard_metadata_t sm) {
state start { transition accept; } }
control verifyChecksum(inout Headers h, inout Metadata m) { apply { } }
control egress(inout Headers h, inout Metadata m,
inout standard_metadata_t sm) { apply { } }
control computeChecksum(inout Headers h, inout Metadata m) { apply { } }
control deparse(packet_out p, in Headers h) { apply { } }

control ingress(inout Headers h, inout Metadata m,
inout standard_metadata_t sm) {
action noop() { }

action drop() { mark_to_drop(sm); }

table t {
key = { sm.ingress_port : exact; }
actions = { noop; drop; }
default_action = noop;
}

apply {
t.apply();
}
}

V1Switch(parse(), verifyChecksum(), ingress(), egress(),
computeChecksum(), deparse()) main;
)"));

ASSERT_TRUE(test);
EXPECT_EQ(0U, ::diagnosticCount());

{
// Default options: expect whitespace
std::ostringstream json_output;
test->serializeP4InfoTo(&json_output, P4::P4RuntimeFormat::JSON);
EXPECT_NE(json_output.str().find(' '), std::string::npos);
}

{
// Disable adding whitespace: json should not contain whitespace
std::ostringstream json_output;
test->jsonPrintOptions.add_whitespace = false;
test->serializeP4InfoTo(&json_output, P4::P4RuntimeFormat::JSON);
EXPECT_EQ(json_output.str().find(' '), std::string::npos);
}
}

class P4RuntimePkgInfo : public P4CTest {
protected:
static std::optional<P4::P4RuntimeAPI> createTestCase(const char *annotations);
Expand Down