Skip to content

Commit

Permalink
Update UUri conversion, UUri conversion test, PubSub test drafting
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Jun 21, 2024
1 parent ec9d5f1 commit a1c58c0
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 55 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ find_package(up-core-api REQUIRED)
find_package(up-cpp REQUIRED)
find_package(zenohc REQUIRED)
find_package(zenohcxx REQUIRED zenohc::lib)
find_package(GTest REQUIRED)
include(GoogleTest)

# TODO NEEDED?
#add_definitions(-DSPDLOG_FMT_EXTERNAL)
Expand Down Expand Up @@ -58,6 +60,7 @@ target_link_libraries(${PROJECT_NAME}
up-cpp::up-cpp
up-core-api::up-core-api
protobuf::libprotobuf
GTest::gtest_main
spdlog::spdlog)

enable_testing()
Expand Down
19 changes: 19 additions & 0 deletions include/up-transport-zenoh-cpp/ZenohUTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef UP_TRANSPORT_ZENOH_CPP_ZENOHUTRANSPORT_H
#define UP_TRANSPORT_ZENOH_CPP_ZENOHUTRANSPORT_H

#include <gtest/gtest.h>
#include <up-cpp/transport/UTransport.h>

#include <filesystem>
Expand Down Expand Up @@ -145,6 +146,24 @@ struct ZenohUTransport : public UTransport {
virtual void cleanupListener(CallableConn listener) override;

private:
FRIEND_TEST(TestZenohUTransport, toZenohKeyString);

static v1::UStatus uError(v1::UCode code, std::string_view message);

static std::string toZenohKeyString(
const std::string& default_authority_name, const v1::UUri& source,
const std::optional<v1::UUri>& sink);

static std::vector<std::pair<std::string, std::string>>
uattributesToAttachment(const v1::UAttributes& attributes);

static v1::UAttributes attachmentToUAttributes(
const zenoh::AttachmentView& attachment);

static zenoh::Priority mapZenohPriority(v1::UPriority upriority);

static v1::UMessage sampleToUMessage(const zenoh::Sample& sample);

v1::UStatus registerRequestListener_(const std::string& zenoh_key,
CallableConn listener);

Expand Down
95 changes: 55 additions & 40 deletions src/ZenohUTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,76 @@ namespace uprotocol::transport {

const char UATTRIBUTE_VERSION = 1;

const uint32_t WILDCARD_ENTITY_ID = 0x0000FFFF;
const uint32_t WILDCARD_ENTITY_VERSION = 0x000000FF;
const uint32_t WILDCARD_RESOURCE_ID = 0x0000FFFF;

using namespace zenoh;
using namespace uprotocol::v1;
using namespace uprotocol::datamodel;

namespace {

UStatus uError(UCode code, std::string_view message) {
UStatus ZenohUTransport::uError(UCode code, std::string_view message) {
UStatus status;
status.set_code(code);
status.set_message(std::string(message));
return status;
}

std::string transformAuthority(const std::string& authority) {
std::string result;
for (char c : authority) {
if (std::isalnum(c)) {
result += std::tolower(c);
} else if (c == '.' || c == '-' || c == '_' || c == '*') {
result += c;
} else if (c == '$') {
result += "{dollar}";
std::string ZenohUTransport::toZenohKeyString(
const std::string& default_authority_name, const UUri& source,
const std::optional<v1::UUri>& sink) {
std::ostringstream zenoh_key;

auto writeUUri = [&](const v1::UUri& uuri) {
zenoh_key << "/";

// authority_name
if (uuri.authority_name().empty()) {
zenoh_key << default_authority_name;
} else {
result += "{}";
zenoh_key << uuri.authority_name();
}
}
return result;
}
zenoh_key << "/";

// TODO: FFFF -> * not clear for spec
std::string toZenohKeyString(const UUri& source,
const std::optional<v1::UUri>& sink) {
std::ostringstream zenoh_key;
zenoh_key << std::uppercase << std::hex << std::setw(8)
<< std::setfill('0');
// ue_id
if (uuri.ue_id() == WILDCARD_ENTITY_ID) {
zenoh_key << "*";
} else {
zenoh_key << uuri.ue_id();
}
zenoh_key << "/";

zenoh_key << "up/";
// ue_version_major
if (uuri.ue_version_major() == WILDCARD_ENTITY_VERSION) {
zenoh_key << "*";
} else {
zenoh_key << uuri.ue_version_major();
}
zenoh_key << "/";

zenoh_key << transformAuthority(source.authority_name()) << "/";
zenoh_key << source.ue_id() << "/";
zenoh_key << source.ue_version_major() << "/";
zenoh_key << source.resource_id() << "/";
// resource_id
if (uuri.resource_id() == WILDCARD_RESOURCE_ID) {
zenoh_key << "*";
} else {
zenoh_key << uuri.resource_id();
}
};

zenoh_key << "up";
zenoh_key << std::uppercase << std::hex;

writeUUri(source);

if (sink.has_value()) {
zenoh_key << transformAuthority(sink->authority_name()) << "/";
zenoh_key << sink->ue_id() << "/";
zenoh_key << sink->ue_version_major() << "/";
zenoh_key << sink->resource_id();
writeUUri(*sink);
} else {
zenoh_key << "/{}/{}/{}/{}";
}
return zenoh_key.str();
}

std::vector<std::pair<std::string, std::string>> uattributesToAttachment(
const UAttributes& attributes) {
std::vector<std::pair<std::string, std::string>>
ZenohUTransport::uattributesToAttachment(const UAttributes& attributes) {
std::vector<std::pair<std::string, std::string>> res;

std::string version(&UATTRIBUTE_VERSION, 1);
Expand All @@ -90,7 +104,8 @@ std::vector<std::pair<std::string, std::string>> uattributesToAttachment(
return res;
}

UAttributes attachmentToUAttributes(const AttachmentView& attachment) {
UAttributes ZenohUTransport::attachmentToUAttributes(
const AttachmentView& attachment) {
std::vector<BytesView> attachment_vec;
attachment.iterate(
[&](const BytesView& key, const BytesView& value) -> bool {
Expand All @@ -113,7 +128,7 @@ UAttributes attachmentToUAttributes(const AttachmentView& attachment) {
return res;
}

Priority mapZenohPriority(UPriority upriority) {
Priority ZenohUTransport::mapZenohPriority(UPriority upriority) {
switch (upriority) {
case UPriority::UPRIORITY_CS0:
return Z_PRIORITY_BACKGROUND;
Expand All @@ -135,7 +150,7 @@ Priority mapZenohPriority(UPriority upriority) {
}
}

UMessage sampleToUMessage(const Sample& sample) {
UMessage ZenohUTransport::sampleToUMessage(const Sample& sample) {
UAttributes attributes;
if (sample.get_attachment().check()) {
attributes = attachmentToUAttributes(sample.get_attachment());
Expand All @@ -148,8 +163,6 @@ UMessage sampleToUMessage(const Sample& sample) {
return message;
}

} // namespace

ZenohUTransport::ZenohUTransport(const UUri& defaultUri,
const std::filesystem::path& configFile)
: UTransport(defaultUri),
Expand Down Expand Up @@ -294,7 +307,8 @@ v1::UStatus ZenohUTransport::sendImpl(const UMessage& message) {
}

std::string zenoh_key =
toZenohKeyString(attributes.sink(), attributes.source());
toZenohKeyString(getDefaultSource().authority_name(), attributes.sink(),
attributes.source());
switch (attributes.type()) {
case UMessageType::UMESSAGE_TYPE_PUBLISH: {
auto [valid, maybe_reason] =
Expand Down Expand Up @@ -343,7 +357,8 @@ v1::UStatus ZenohUTransport::sendImpl(const UMessage& message) {
v1::UStatus ZenohUTransport::registerListenerImpl(
const v1::UUri& sink_filter, CallableConn&& listener,
std::optional<v1::UUri>&& source_filter) {
std::string zenoh_key = toZenohKeyString(sink_filter, source_filter);
std::string zenoh_key = toZenohKeyString(
getDefaultSource().authority_name(), sink_filter, source_filter);
// TODO: Is 0 == none?
if (!sink_filter.authority_name().empty() && sink_filter.ue_id() == 0 &&
sink_filter.resource_id() == 0) {
Expand Down
85 changes: 77 additions & 8 deletions test/coverage/ZenohUTransportTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <up-transport-zenoh-cpp/ZenohUTransport.h>
#include <up-cpp/datamodel/builder/Uuid.h>
#include <up-cpp/datamodel/validator/UUri.h>

namespace {
#include <zenoh.hxx>

class TestFixture : public testing::Test {
#include "up-transport-zenoh-cpp/ZenohUTransport.h"

namespace uprotocol::transport {

using namespace uprotocol::v1;
using namespace uprotocol::transport;

constexpr const char* AUTHORITY_NAME = "test";

class TestZenohUTransport : public testing::Test {
protected:
// Run once per TEST_F.
// Used to set up clean environments per test.
Expand All @@ -23,16 +33,75 @@ class TestFixture : public testing::Test {

// Run once per execution of the test application.
// Used for setup of all tests. Has access to this instance.
TestFixture() = default;
~TestFixture() = default;
TestZenohUTransport() = default;
~TestZenohUTransport() = default;

// Run once per execution of the test application.
// Used only for global setup outside of tests.
static void SetUpTestSuite() {}
static void TearDownTestSuite() {}
};

// TODO replace
TEST_F(TestFixture, SomeTestName) {}
uprotocol::v1::UUri create_uuri(const std::string& authority, uint32_t ue_id,
uint32_t ue_version_major,
uint32_t resource_id) {
uprotocol::v1::UUri uuri;
uuri.set_authority_name(authority);
uuri.set_ue_id(ue_id);
uuri.set_ue_version_major(ue_version_major);
uuri.set_resource_id(resource_id);

return uuri;
}

// TODO(sashacmc): config generation
TEST_F(TestZenohUTransport, ConstructDestroy) {
uprotocol::v1::UUri def_src_uuri;
def_src_uuri.set_authority_name(AUTHORITY_NAME);
def_src_uuri.set_ue_id(0x18000);
def_src_uuri.set_ue_version_major(1);
def_src_uuri.set_resource_id(0);

zenoh::init_logger();
try {
auto ut = ZenohUTransport(def_src_uuri,
"/home/sashacmc/src/up-client-zenoh-cpp/test/"
"extra/DEFAULT_CONFIG.json5");
} catch (zenoh::ErrorMessage& e) {
throw std::runtime_error(std::string(e.as_string_view()));
}
}

TEST_F(TestZenohUTransport, toZenohKeyString) {
EXPECT_EQ(
ZenohUTransport::toZenohKeyString(
"", create_uuri("192.168.1.100", 0x10AB, 3, 0x80CD), std::nullopt),
"up/192.168.1.100/10AB/3/80CD/{}/{}/{}/{}");

EXPECT_EQ(ZenohUTransport::toZenohKeyString(
"", create_uuri("192.168.1.100", 0x10AB, 3, 0x80CD),
create_uuri("192.168.1.101", 0x20EF, 4, 0)),
"up/192.168.1.100/10AB/3/80CD/192.168.1.101/20EF/4/0");

EXPECT_EQ(ZenohUTransport::toZenohKeyString(
"", create_uuri("*", 0xFFFF, 0xFF, 0xFFFF),
create_uuri("192.168.1.101", 0x20EF, 4, 0)),
"up/*/*/*/*/192.168.1.101/20EF/4/0");

EXPECT_EQ(ZenohUTransport::toZenohKeyString(
"", create_uuri("my-host1", 0x10AB, 3, 0),
create_uuri("my-host2", 0x20EF, 4, 0xB)),
"up/my-host1/10AB/3/0/my-host2/20EF/4/B");

EXPECT_EQ(ZenohUTransport::toZenohKeyString(
"", create_uuri("*", 0xFFFF, 0xFF, 0xFFFF),
create_uuri("my-host2", 0x20EF, 4, 0xB)),
"up/*/*/*/*/my-host2/20EF/4/B");

EXPECT_EQ(ZenohUTransport::toZenohKeyString(
"", create_uuri("*", 0xFFFF, 0xFF, 0xFFFF),
create_uuri("[::1]", 0xFFFF, 0xFF, 0xFFFF)),
"up/*/*/*/*/[::1]/*/*/*");
}

} // namespace
} // namespace uprotocol::transport
Loading

0 comments on commit a1c58c0

Please sign in to comment.