Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
ununhexium committed Jun 17, 2024
1 parent 459e9fb commit 73f1455
Show file tree
Hide file tree
Showing 39 changed files with 478 additions and 409 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,17 @@ dependencies {
compileOnly(libs.lombok)

implementation(project(":extensions:sovity-messenger"))

implementation(libs.edc.controlPlaneCore)
implementation(libs.edc.dspApiConfiguration)
implementation(libs.edc.dspHttpSpi)
implementation(libs.edc.httpSpi)
implementation(libs.edc.managementApiConfiguration)

// implementation("com.squareup.okhttp3:okhttp:${okHttpVersion}")
// implementation("org.json:json:${jsonVersion}")
// implementation("org.glassfish.jersey.media:jersey-media-multipart:3.1.3")
//
// implementation("de.fraunhofer.iais.eis.ids.infomodel:infomodel-java:1.0.2-basecamp")
// implementation("de.fraunhofer.iais.eis.ids.infomodel:infomodel-util:1.0.2-basecamp")
//
implementation(libs.edc.coreSpi)

testAnnotationProcessor(libs.lombok)

testCompileOnly(libs.lombok)

testImplementation(project(":utils:test-connector-remote"))

testImplementation(libs.edc.junit)
testImplementation(libs.edc.dataPlaneSelectorCore)
testImplementation(libs.edc.dspApiConfiguration)
testImplementation(libs.edc.dspHttpCore)
testImplementation(libs.edc.iamMock)
testImplementation(libs.edc.jsonLd)

testImplementation(libs.edc.http) {
exclude(group = "org.eclipse.jetty", module = "jetty-client")
Expand All @@ -49,9 +33,6 @@ dependencies {
// Updated jetty versions for e.g. CVE-2023-26048
testImplementation(libs.bundles.jetty.cve2023)

testImplementation(libs.assertj.core)
testImplementation(libs.junit.api)
testImplementation(libs.jsonAssert)
testImplementation(libs.mockito.core)
testImplementation(libs.mockito.inline)
testImplementation(libs.restAssured.restAssured)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@

package de.sovity.edc.extension.sovitymessenger.demo;

import de.sovity.edc.extension.messenger.api.MessageHandlerRegistry;
import de.sovity.edc.extension.messenger.api.SovityMessenger;
import de.sovity.edc.extension.messenger.SovityMessengerRegistry;
import de.sovity.edc.extension.messenger.SovityMessenger;
import de.sovity.edc.extension.sovitymessenger.demo.message.Addition;
import de.sovity.edc.extension.sovitymessenger.demo.message.Answer;
import de.sovity.edc.extension.sovitymessenger.demo.message.Signal;
import de.sovity.edc.extension.sovitymessenger.demo.message.Sqrt;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;

import java.util.function.Function;

import static java.lang.Math.sqrt;


Expand All @@ -47,13 +46,17 @@ public String name() {
private SovityMessenger sovityMessenger;

@Inject
private MessageHandlerRegistry registry;
private SovityMessengerRegistry registry;

@Override
public void initialize(ServiceExtensionContext context) {
// Register the various messages that you would like to process.
registry.register(Sqrt.class, (Function<Sqrt, Answer>) single -> new Answer(sqrt(single.getValue())));
registry.register(Addition.class, (Function<Addition, Answer>) add -> new Answer(add.op1 + add.op2));
// By class, safer.
registry.register(Sqrt.class, single -> new Answer(sqrt(single.getValue())));
// By String, could be unsafe during refactorings.
registry.register(Addition.class, Addition.TYPE, add -> new Answer(add.op1 + add.op2));

registry.registerSignal(Signal.class, signal -> System.out.println("Received signal."));

/*
* In the counterpart connector, messages can be sent with the code below.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
package de.sovity.edc.extension.sovitymessenger.demo.message;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.sovity.edc.extension.messenger.api.SovityMessage;
import de.sovity.edc.extension.messenger.SovityMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static de.sovity.edc.extension.sovitymessenger.demo.message.Common.ROOT;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Addition implements SovityMessage {

public static final String TYPE = "add";

@Override
public String getType() {
return ROOT + "add";
return TYPE;
}

@JsonProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@
package de.sovity.edc.extension.sovitymessenger.demo.message;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.sovity.edc.extension.messenger.api.SovityMessage;
import de.sovity.edc.extension.messenger.SovityMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static de.sovity.edc.extension.sovitymessenger.demo.message.Common.ROOT;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class Answer implements SovityMessage {

@Override
public String getType() {
return ROOT + "answer";
return "answer";
}

@JsonProperty
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 sovity GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* sovity GmbH - initial API and implementation
*/

package de.sovity.edc.extension.sovitymessenger.demo.message;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.sovity.edc.extension.messenger.SovityMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Multiplication implements SovityMessage {

@Override
public String getType() {
return "multiply";
}

@JsonProperty
public int op1;

@JsonProperty
public int op2;

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

package de.sovity.edc.extension.sovitymessenger.demo.message;

public interface Common {
String ROOT = "de.sovity.demo.calculator.";
import de.sovity.edc.extension.messenger.SovityMessage;

public class Signal implements SovityMessage {
@Override
public String getType() {
return "signal";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@


import com.fasterxml.jackson.annotation.JsonProperty;
import de.sovity.edc.extension.messenger.api.SovityMessage;
import de.sovity.edc.extension.messenger.SovityMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static de.sovity.edc.extension.sovitymessenger.demo.message.Common.ROOT;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class Sqrt implements SovityMessage {

private static final String TYPE = ROOT + "sqrt";
private static final String TYPE = "sqrt";

@Override
public String getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

package de.sovity.edc.extension.sovitymessenger.demo.message;

import de.sovity.edc.extension.messenger.api.SovityMessage;
import de.sovity.edc.extension.messenger.SovityMessage;

public class UnregisteredMessage implements SovityMessage {

@Override
public String getType() {
return "UNREGISTERED";
return "unregistered";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import de.sovity.edc.extension.e2e.connector.config.ConnectorConfig;
import de.sovity.edc.extension.e2e.db.TestDatabase;
import de.sovity.edc.extension.e2e.db.TestDatabaseViaTestcontainers;
import de.sovity.edc.extension.messenger.api.SovityMessenger;
import de.sovity.edc.extension.messenger.SovityMessenger;
import de.sovity.edc.extension.sovitymessenger.demo.message.Addition;
import de.sovity.edc.extension.sovitymessenger.demo.message.Answer;
import de.sovity.edc.extension.sovitymessenger.demo.message.Signal;
import de.sovity.edc.extension.sovitymessenger.demo.message.Sqrt;
import de.sovity.edc.extension.sovitymessenger.demo.message.UnregisteredMessage;
import lombok.val;
Expand Down Expand Up @@ -87,12 +88,13 @@ void demo() throws ExecutionException, InterruptedException, TimeoutException {
*/
val messenger = emitterEdcContext.getContext().getService(SovityMessenger.class);

// Select the target EDC
System.out.println("START MARKER");

// Send messages
val added = messenger.send(Answer.class, receiverAddress, new Addition(20, 30));
val rooted = messenger.send(Answer.class, receiverAddress, new Sqrt(9.0));
val unregistered = messenger.send(Answer.class, receiverAddress, new UnregisteredMessage());
messenger.send(receiverAddress, new Signal());

// Wait for the answers
added.get(2, TimeUnit.SECONDS).onSuccess(it -> System.out.println(it.getAnswer()));
Expand All @@ -102,10 +104,12 @@ void demo() throws ExecutionException, InterruptedException, TimeoutException {
unregistered.get();
} catch (ExecutionException e) {
/*
* When a problem happens, a SovityMessenger exception is thrown and encapsulated in an ExecutionException.
* When a problem happens, a SovityMessengerException is thrown and encapsulated in an ExecutionException.
*/
System.out.println(e.getCause().getMessage());
}

System.out.println("END MARKER");
}

}
44 changes: 37 additions & 7 deletions extensions/sovity-messenger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,54 @@
</p>
</div>


## About this Extension

This extension provides a convenient way to exchange messages between connectors.
To provide a simpler way to exchange messages between EDCs while re-using the Dataspace's Connector-to-Connector authentication mechanisms, we created our own extension with a much simpler API surface omitting JSON-LD.

## Why does this extension exist?

To provide a simple way to exchange messages between EDCs without complying with the JsonLd conventions and with minimal setup.
Adding custom DSP messages to a vanilla EDC is verbose and requires the handling of JSON-LD and implementing your own Transformers. Since we do not care about JSON-LD we wanted a simpler API surface.

## Architecture

The sovity Messenger is implemented on top of the DSP messaging protocol and re-uses its exchange and authentication.

It is abstracted from the internals of the DSP protocol such that changing the underlying implementation remains an option.


```mermaid
---
title: Registering a handler
---
sequenceDiagram
Caller ->> SovityMessengerRegistry: register(inputClass, intputType, handler)
```

```mermaid
---
title: Sending a message
---
sequenceDiagram
Caller ->>+SovityMessenger: send(resultClass, counterPartyAddress, payload)
SovityMessenger -->> RemoteMessageDispatcherRegistry: dispatch(genericMessage)
SovityMessenger -->> -Caller: Future<resultClass>
RemoteMessageDispatcherRegistry ->> +CustomMessageReceiverController: <<sending via DSP>>
CustomMessageReceiverController ->> CustomMessageReceiverController: processMessage(handler, payload)
CustomMessageReceiverController -->> -RemoteMessageDispatcherRegistry: <<result via DSP>>
RemoteMessageDispatcherRegistry -->> SovityMessenger: <<result via DSP>>
SovityMessenger ->> +Caller: Future<resultClass>
Caller ->> -Caller: future.get()
```

## Demo

You can find a demo project in [sovity-messenger-demo](../sovity-messenger-demo).
You can find a demo project in [sovity-messenger-demo](../../demo/sovity-messenger-demo).

The 2 key entry points are:

- The configuration of the backend, to receive messages, in [SovityMessengerDemo.java](../sovity-messenger-demo/src/main/java/de/sovity/edc/extension/sovitymessenger/demo/SovityMessengerDemo.java)
- The usage of the custom messages, implemented as a client e2e test [SovityMessengerDemoTest.java](../sovity-messenger-demo/src/test/java/de/sovity/edc/extension/sovitymessenger/demo/SovityMessengerDemoTest.java)

For more information, check the documentation in that project and in the the Sovity Messenger's [api](src%2Fmain%2Fjava%2Fde%2Fsovity%2Fedc%2Fextension%2Fmessenger%2Fapi).
- The backend's configuration, to receive messages, in [SovityMessengerDemo.java](../sovity-messenger-demo/src/main/java/de/sovity/edc/extension/sovitymessenger/demo/SovityMessengerDemo.java)
- To send messages, simply call the SovityMessenger as done [here](../sovity-messenger-demo/src/test/java/de/sovity/edc/extension/sovitymessenger/demo/SovityMessengerDemoTest.java)

## License

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* sovity GmbH - initial API and implementation
*/

package de.sovity.edc.extension.messenger.api;
package de.sovity.edc.extension.messenger;

import com.fasterxml.jackson.annotation.JsonIgnore;

Expand All @@ -22,6 +22,8 @@
* {@link com.fasterxml.jackson.annotation.JsonProperty}.
* {@code public} fields are serialized automatically.
* <br>
* It is recommended to have a no-args constructor.
* <br>
* See <a href="https://www.baeldung.com/jackson-field-serializable-deserializable-or-not">this doc</a>
* for more detailed info about Jackson's serialization.
*/
Expand Down
Loading

0 comments on commit 73f1455

Please sign in to comment.