Skip to content
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
Revert "Remove CloudEvents Code"
This reverts commit 6c4ac27.
  • Loading branch information
czfdcn committed Jun 19, 2024
commit 6988b8e81ad5b92527923cdd30a1eef58c88029c
1 change: 1 addition & 0 deletions NOTICE.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SPDX-License-Identifier: Apache-2.0

The following are libraries that uProtocol project uses:

* http://cloudevents.io
* http://protobuf.dev

NOTE: Please refer to link:pom.xml[] for more information of library dependencies
3 changes: 3 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ To pull the Library from maven central, setting ${uprotocol.version} to the late
| link:src/main/java/org/eclipse/uprotocol/transport/README.adoc[`*transport*`]
| https://github.com/eclipse-uprotocol/uprotocol-spec/blob/main/up-l1/README.adoc[uP-L1 Transport Layer] interface and data model that wraps communication middlewares like zenoh, mqtt, http, etc... into a thin and simple to use transport interface. This model is used by the communication layer (uP-L2) to send and receive messages and transports are expected to implement the link:src/main/java/org/eclipse/uprotocol/transport/UTransport.java[uTransport] interface.

| link:src/main/java/org/eclipse/uprotocol/cloudevent/README.adoc[`*cloudevent*`]
| A representation of uProtocol messages used in older versions of the specifications and kept for backwards compatibility.

|===

41 changes: 41 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<junit.version>4.12</junit.version>
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
<slf4j.version>2.6.18</slf4j.version>
<cloudevents.version>2.4.2</cloudevents.version>
<protobuf.version>3.21.10</protobuf.version>
<git.tag.name>main</git.tag.name>
</properties>
Expand Down Expand Up @@ -234,6 +235,7 @@
</executions>
<configuration>
<includes>
<include>**/cloudevent/**</include>
<include>**/communication/**</include>
<include>**/transport/**</include>
<include>**/uri/**</include>
Expand Down Expand Up @@ -316,12 +318,21 @@

<dependencyManagement>
<dependencies>

<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-protobuf</artifactId>
<version>2.4.2</version>
</dependency>


<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
<version>2.11.0</version>
</dependency>


<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
Expand All @@ -336,6 +347,15 @@
<type>pom</type>
</dependency>

<!-- CloudEvents Java SDK https://github.com/cloudevents/sdk-java -->
<!-- https://github.com/cloudevents/sdk-java/pull/526 bump the version to fix serialization issue -->
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-bom</artifactId>
<version>${cloudevents.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
Expand Down Expand Up @@ -371,6 +391,27 @@
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/io.cloudevents/cloudevents-protobuf -->
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-protobuf</artifactId>

</dependency>

<!-- CloudEvents support https://cloudevents.github.io/sdk-java/ https://github.com/cloudevents/sdk-java-->
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-core</artifactId>
</dependency>
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-api</artifactId>
</dependency>
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-json-jackson</artifactId>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/eclipse/uprotocol/cloudevent/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
= uProtocol CloudEvents
:toc:
:sectnums:


== Overview

https://github.com/eclipse-uprotocol/up-spec/blob/main/basics/cloudevents.adoc[uProtocol CloudEvents] is a common message envelope that could be used to carry uProtocol messages across the transport. CloudEvents were historically used exclusively in older versions of uProtocol (ex. 1.3.6) and as such are maintained in this project for backwards compatibility support.

This module contains the following classes:

.uProtocol CloudEvent Interfaces
[#ce-interfaces, cols="1,3"]
|===
|Class |Description

| `*UCloudEvent*`
|A class that wraps a CloudEvent and provides utility methods to extract uProtocol specific attributes from the CloudEvent

| `*UCloudEventAttributes*`
|A class that represents the uProtocol specific attributes that are added to the CloudEvent

|`*CloudEventFactory*`
|Factory class that builds the various types of CloudEvents for uProtocol (publish, notification, request, response)

| `*CloudEventSerializer*`
| A class that serializes and deserializes CloudEvents to and from protobuf and/or JSON formats used in some transports like MQTT in the past

| `*CloudEventValidator*`
| Validates that the CloudEvent contains the correct uProtocol information and format for the various messages types (publish, notification, request, response)

|===






Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
/**
* SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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
*/
package org.eclipse.uprotocol.cloudevent.datamodel;

import org.eclipse.uprotocol.v1.UPriority;

import java.util.Objects;
import java.util.Optional;

/**
* Specifies the properties that can configure the UCloudEvent.
*/
public class UCloudEventAttributes {

private static final UCloudEventAttributes EMPTY = new UCloudEventAttributes(null, null, null, null);

private final String hash;
private final UPriority priority;
private final Integer ttl;
private final String token;
private final String traceparent;

/**
* Construct the properties object.
*
* @param hash An HMAC generated on the data portion of the CloudEvent
* message using the device key.
* @param priority uProtocol Prioritization classifications.
* @param ttl How long this event should live for after it was generated
* (in milliseconds).
* Events without this attribute (or value is 0) MUST NOT
* timeout.
* @param token Oauth2 access token to perform the access request defined in
* the request message.
*/
private UCloudEventAttributes(String hash, UPriority priority, Integer ttl, String token) {
this.hash = hash;
this.priority = priority;
this.ttl = ttl;
this.token = token;
this.traceparent = null;
}

private UCloudEventAttributes(UCloudEventAttributesBuilder builder) {
this.hash = builder.hash;
this.priority = builder.priority;
this.ttl = builder.ttl;
this.token = builder.token;
this.traceparent = builder.traceparent;
}

/**
* Static factory method for creating an empty cloud event attributes object, to
* avoid working with null<br>
*
* @return Returns an empty cloud event attributes that indicates
* that there are no added additional attributes to configure.
*/
public static UCloudEventAttributes empty() {
return EMPTY;
}

/**
* Indicates that there are no added additional attributes to configure when
* building a CloudEvent.
*
* @return Returns true if this attributes container is an empty container and
* has no valuable information in building a CloudEvent.
*/
public boolean isEmpty() {
return hash().isEmpty() &&
priority().isEmpty() &&
ttl().isEmpty() &&
token().isEmpty() &&
traceparent().isEmpty();
}

/**
* An HMAC generated on the data portion of the CloudEvent message using the
* device key.
*
* @return Returns an Optional hash attribute.
*/
public Optional<String> hash() {
return hash == null || hash.isBlank() ? Optional.empty() : Optional.of(hash);
}

/**
* uProtocol Prioritization classifications.
*
* @return Returns an Optional priority attribute.
*/
public Optional<UPriority> priority() {
return priority == null ? Optional.empty() : Optional.of(priority);
}

/**
* How long this event should live for after it was generated (in milliseconds).
*
* @return Returns an Optional time to live attribute.
*/
public Optional<Integer> ttl() {
return ttl == null ? Optional.empty() : Optional.of(ttl);
}

/**
* Oauth2 access token to perform the access request defined in the request
* message.
*
* @return Returns an Optional OAuth token attribute.
*/
public Optional<String> token() {
return token == null || token.isBlank() ? Optional.empty() : Optional.of(token);
}

/**
* An identifier used to correlate observability across related events.
*
* @return Returns an Optional traceparent attribute.
*/
public Optional<String> traceparent() {
return traceparent == null || traceparent.isBlank() ? Optional.empty() : Optional.of(traceparent);
}

/**
* Builder for constructing the UCloudEventAttributes.
*/
public static class UCloudEventAttributesBuilder {
private String hash;
private UPriority priority;
private Integer ttl;
private String token;
private String traceparent;

public UCloudEventAttributesBuilder() {
}

/**
* add an HMAC generated on the data portion of the CloudEvent message using the
* device key.
*
* @param hash an HMAC generated on the data portion of the CloudEvent message
* using the device key.
* @return Returns the UCloudEventAttributesBuilder with the configured hash.
*/
public UCloudEventAttributesBuilder withHash(String hash) {
this.hash = hash;
return this;
}

/**
* add a uProtocol Prioritization classifications.
*
* @param priority uProtocol Prioritization classifications.
* @return Returns the UCloudEventAttributesBuilder with the configured
* priority.
*/
public UCloudEventAttributesBuilder withPriority(UPriority priority) {
this.priority = priority;
return this;
}

/**
* add a time to live which is how long this event should live for after it was
* generated (in milliseconds).
* Events without this attribute (or value is 0) MUST NOT timeout.
*
* @param ttl How long this event should live for after it was generated (in
* milliseconds).
* Events without this attribute (or value is 0) MUST NOT timeout.
* @return Returns the UCloudEventAttributesBuilder with the configured time to
* live.
*/
public UCloudEventAttributesBuilder withTtl(Integer ttl) {
this.ttl = ttl;
return this;
}

/**
* Add an Oauth2 access token to perform the access request defined in the
* request message.
*
* @param token An Oauth2 access token to perform the access request defined in
* the request message.
* @return Returns the UCloudEventAttributesBuilder with the configured OAuth
* token.
*/
public UCloudEventAttributesBuilder withToken(String token) {
this.token = token;
return this;
}

/**
* Add an identifier used to correlate observability across related events.
*
* @param traceparent An identifier used to correlate observability across
* related events.
* @return Returns the UCloudEventAttributesBuilder with the configured
* traceparent.
*/
public UCloudEventAttributesBuilder withTraceparent(String traceparent) {
this.traceparent = traceparent;
return this;
}

/**
* Construct the UCloudEventAttributes from the builder.
*
* @return Returns a constructed UProperty.
*/
public UCloudEventAttributes build() {
// validation if needed
return new UCloudEventAttributes(this);
}
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
UCloudEventAttributes that = (UCloudEventAttributes) o;
return Objects.equals(hash, that.hash) && priority == that.priority
&& Objects.equals(ttl, that.ttl) && Objects.equals(token, that.token)
&& Objects.equals(traceparent, that.traceparent);
}

@Override
public int hashCode() {
return Objects.hash(hash, priority, ttl, token, traceparent);
}

@Override
public String toString() {
String traceParentString = "";
if (traceparent != null) {
traceParentString = ", traceparent='" + traceparent + '\'';
}
return "UCloudEventAttributes{" +
"hash='" + hash + '\'' +
", priority=" + priority +
", ttl=" + ttl +
", token='" + token + '\'' +
traceParentString +
'}';
}
}
Loading