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

Move to core-api UPayload data model #41

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 13 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@
<dependency>
<groupId>org.eclipse.uprotocol</groupId>
<artifactId>uprotocol-core-api</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>
</dependency>

<!-- JSON library for JUnit test cases -->
Expand Down Expand Up @@ -378,7 +378,18 @@
</plugins>
</build>


</profile>
</profiles>
<repositories>
<repository>
<id>oss.sonatype.org-snapshot</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
2 changes: 1 addition & 1 deletion src/main/java/org/eclipse/uprotocol/rpc/RpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import java.util.concurrent.CompletableFuture;

import org.eclipse.uprotocol.transport.datamodel.UPayload;
import org.eclipse.uprotocol.v1.UPayload;
import org.eclipse.uprotocol.v1.UAttributes;
import org.eclipse.uprotocol.v1.UUri;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/eclipse/uprotocol/rpc/RpcMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

import org.eclipse.uprotocol.transport.datamodel.UPayload;
import org.eclipse.uprotocol.v1.UPayload;

/**
* RPC Wrapper is an interface that provides static methods to be able to wrap an RPC request with
Expand All @@ -60,7 +60,7 @@ static <T extends Message> CompletableFuture<T> mapResponse(CompletableFuture<UP
}
Any any;
try {
any = Any.parseFrom(payload.data());
any = Any.parseFrom(payload.getValue());

// Expected type
if (any.is(expectedClazz)) {
Expand Down Expand Up @@ -93,7 +93,7 @@ static <T extends Message> CompletableFuture<RpcResult<T>> mapResponseToResult(C
}
Any any;
try {
any = Any.parseFrom(payload.data());
any = Any.parseFrom(payload.getValue());

// Expected type
if (any.is(expectedClazz)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
package org.eclipse.uprotocol.transport;

import org.eclipse.uprotocol.transport.datamodel.UListener;
import org.eclipse.uprotocol.transport.datamodel.UPayload;
import org.eclipse.uprotocol.transport.datamodel.UStatus;
import org.eclipse.uprotocol.v1.UAttributes;
import org.eclipse.uprotocol.v1.UEntity;
import org.eclipse.uprotocol.v1.UUri;
import org.eclipse.uprotocol.v1.UPayload;

/**
* UTransport is the uP-L1 interface that provides a common API for uE developers to send and receive messages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.eclipse.uprotocol.v1.UAttributes;
import org.eclipse.uprotocol.v1.UUri;
import org.eclipse.uprotocol.v1.UPayload;

/**
* For any implementation that defines some kind of callback or function that will be called to handle incoming messages.
Expand Down
105 changes: 0 additions & 105 deletions src/main/java/org/eclipse/uprotocol/transport/datamodel/UPayload.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.eclipse.uprotocol.cloudevent.datamodel.UCloudEventType;
import org.eclipse.uprotocol.uri.serializer.LongUriSerializer;
import org.eclipse.uprotocol.uuid.factory.UuidFactory;
import org.eclipse.uprotocol.uuid.factory.UuidUtils;
import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer;
import org.eclipse.uprotocol.v1.*;
import org.junit.jupiter.api.Assertions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.eclipse.uprotocol.cloudevent.factory.UCloudEvent;
import org.eclipse.uprotocol.uri.serializer.LongUriSerializer;
import org.eclipse.uprotocol.uuid.factory.UuidFactory;
import org.eclipse.uprotocol.uuid.factory.UuidUtils;
import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer;
import org.eclipse.uprotocol.v1.*;
import org.eclipse.uprotocol.validation.ValidationResult;
Expand All @@ -44,8 +43,6 @@

import java.net.URI;
import java.time.Instant;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

class CloudEventValidatorTest {
Expand Down
53 changes: 38 additions & 15 deletions src/test/java/org/eclipse/uprotocol/rpc/RpcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@
package org.eclipse.uprotocol.rpc;

import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.Int32Value;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.rpc.Code;
import com.google.rpc.Status;
import org.eclipse.uprotocol.transport.builder.UAttributesBuilder;
import org.eclipse.uprotocol.transport.datamodel.UPayload;
import org.eclipse.uprotocol.uri.serializer.LongUriSerializer;
import org.eclipse.uprotocol.v1.*;
import org.eclipse.uprotocol.v1.UUri;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have the version in the package name. makes upgrading even messier

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to ensure that if/when we go to a non-backwards compatible version of uProtocol (ex. v1) that we have differentiate the data model between them like we do for uSubscription, uDiscovery, etc...

import org.eclipse.uprotocol.v1.UPayload;
import org.eclipse.uprotocol.v1.UPayloadFormat;
import org.eclipse.uprotocol.v1.UAttributes;
import org.eclipse.uprotocol.v1.UEntity;
import org.eclipse.uprotocol.v1.UPriority;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -46,7 +52,10 @@ class RpcTest {
RpcClient ReturnsNumber3 = new RpcClient() {
@Override
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) {
UPayload data = new UPayload(Any.pack(Int32Value.of(3)).toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF);
UPayload data = UPayload.newBuilder()
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF)
.setValue(Any.pack(Int32Value.of(3)).toByteString())
.build();
return CompletableFuture.completedFuture(data);
}
};
Expand All @@ -64,8 +73,10 @@ public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UA
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) {
Status status = Status.newBuilder().setCode(Code.INVALID_ARGUMENT_VALUE).setMessage("boom").build();
Any any = Any.pack(status);
UPayload data = new UPayload(any.toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF);

UPayload data = UPayload.newBuilder()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally I prefer working with the constructors and not verbose builders if they are not really needed.
I do understand why you wanted to define the payload as a protobuf object
But then again - at the end of the day, uTransport gets a byte[], so are we not creating a protobuf object whose job is transport and then just translating it into the real transport object which is the byte[]
what value is achieved by the payload being a protobuf object?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UPayload is to wrap the data + metadata and keep it separate from the message attributes (priority, addressing, timeout, etc...). Yes you are right for the data but the metadata will most likely be mapped somehow into the local transport (HTTP header, zenoh header, etc...).
the benefit of the UPayload being in proto is the consistently for all data types (and the UPayloadFormat as well).

.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF)
.setValue(any.toByteString())
.build();
return CompletableFuture.completedFuture(data);
}
};
Expand All @@ -75,16 +86,21 @@ public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UA
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) {
Status status = Status.newBuilder().setCode(Code.OK_VALUE).setMessage("all good").build();
Any any = Any.pack(status);
UPayload data = new UPayload(any.toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF);

UPayload data = UPayload.newBuilder()
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF)
.setValue(any.toByteString())
.build();
return CompletableFuture.completedFuture(data);
}
};

RpcClient ThatBarfsCrapyPayload = new RpcClient() {
@Override
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) {
UPayload response = new UPayload(new byte[]{0}, UPayloadFormat.UPAYLOAD_FORMAT_RAW);
UPayload response = UPayload.newBuilder()
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_RAW)
.setValue(ByteString.copyFrom(new byte[]{0}))
.build();
return CompletableFuture.completedFuture(response);
}
};
Expand All @@ -102,7 +118,11 @@ public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UA
@Override
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) {
Any any = Any.pack(Int32Value.of(42));
return CompletableFuture.completedFuture(new UPayload(any.toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF));
UPayload data = UPayload.newBuilder()
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF)
.setValue(any.toByteString())
.build();
return CompletableFuture.completedFuture(data);
}
};

Expand All @@ -121,7 +141,10 @@ private static io.cloudevents.v1.proto.CloudEvent buildCloudEvent() {

private static UPayload buildUPayload() {
Any any = Any.pack(buildCloudEvent());
return new UPayload(any.toByteArray(), UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF);
return UPayload.newBuilder()
.setFormat(UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF)
.setValue(any.toByteString())
.build();
}

private static UUri buildTopic() {
Expand All @@ -142,7 +165,7 @@ private static CompletableFuture<io.cloudevents.v1.proto.CloudEvent> rpcResponse
(payload, exception) -> {
Any any;
try {
any = Any.parseFrom(payload.data());
any = Any.parseFrom(payload.getValue());
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e.getMessage(), e);
}
Expand Down Expand Up @@ -379,7 +402,7 @@ void test_success_invoke_method_happy_flow() {
assertFalse(true);

try {
any = Any.parseFrom(payload.data());
any = Any.parseFrom(payload.getValue());
// happy flow, no exception
assertNull(exception);

Expand Down Expand Up @@ -409,7 +432,7 @@ void test_fail_invoke_method_when_invoke_method_returns_a_status() {
final CompletableFuture<io.cloudevents.v1.proto.CloudEvent> stubReturnValue = rpcResponse.handle(
(payload, exception) -> {
try {
Any any = Any.parseFrom(payload.data());
Any any = Any.parseFrom(payload.getValue());
// happy flow, no exception
assertNull(exception);

Expand Down Expand Up @@ -472,7 +495,7 @@ void test_fail_invoke_method_when_invoke_method_returns_a_bad_proto() {
final CompletableFuture<io.cloudevents.v1.proto.CloudEvent> stubReturnValue = rpcResponse.handle(
(payload, exception) -> {
try {
Any any = Any.parseFrom(payload.data());
Any any = Any.parseFrom(payload.getValue());
// happy flow, no exception
assertNull(exception);

Expand Down Expand Up @@ -609,7 +632,7 @@ void what_the_stub_looks_like() throws InterruptedException {
RpcClient client = new RpcClient() {
@Override
public CompletableFuture<UPayload> invokeMethod(UUri topic, UPayload payload, UAttributes attributes) {
return CompletableFuture.completedFuture(UPayload.empty());
return CompletableFuture.completedFuture(UPayload.getDefaultInstance());
}
};

Expand Down
Loading