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

Sync with up-java for 1.5.6 #23

Merged
merged 9 commits into from
Mar 14, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ object CloudEventFactory {
*/
.withData(protoPayloadBytes)
attributes.ttl?.let { ttl -> cloudEventBuilder.withExtension("ttl", ttl) }
attributes.priority?.let { priority -> cloudEventBuilder.withExtension("priority", priority.toString()) }
attributes.priority?.let { priority ->
cloudEventBuilder.withExtension(
"priority",
UCloudEvent.getCeName(priority.valueDescriptor)
)
}
attributes.hash?.let { hash -> cloudEventBuilder.withExtension("hash", hash) }
attributes.token?.let { token -> cloudEventBuilder.withExtension("token", token) }
attributes.traceparent?.let { traceparent: String ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ import org.eclipse.uprotocol.uuid.factory.getTime
import org.eclipse.uprotocol.uuid.factory.isUuid
import org.eclipse.uprotocol.uuid.serializer.LongUuidSerializer
import org.eclipse.uprotocol.v1.*
import org.eclipse.uprotocol.v1.UUID
import java.net.URI
import java.time.OffsetDateTime
import java.time.temporal.ChronoUnit


/**
* Class to extract information from a CloudEvent.
*/
Expand Down Expand Up @@ -136,7 +136,7 @@ object UCloudEvent {
*/
fun getCommunicationStatus(cloudEvent: CloudEvent): Int {
return try {
extractIntegerValueFromExtension("commstatus", cloudEvent)?:UCode.OK_VALUE
extractIntegerValueFromExtension("commstatus", cloudEvent) ?: UCode.OK_VALUE
} catch (e: Exception) {
UCode.OK_VALUE
}
Expand Down Expand Up @@ -304,27 +304,47 @@ object UCloudEvent {
* @param type The UMessageType
* @return returns the string representation of the UMessageType
*/
fun getEventType(type: UMessageType?): String {
return when (type) {
UMessageType.UMESSAGE_TYPE_PUBLISH -> "pub.v1"
UMessageType.UMESSAGE_TYPE_REQUEST -> "req.v1"
UMessageType.UMESSAGE_TYPE_RESPONSE -> "res.v1"
else -> ""
}
fun getEventType(type: UMessageType): String {
return getCeName(type.valueDescriptor)
}

/**
* Get the string representation of the UPriority
* @param priority
* @return returns the string representation of the UPriority
*/
fun getCePriority(priority: UPriority): String {
return getCeName(priority.valueDescriptor)
}

/**
* Get the UPriority from the string name
* @param priority
* @return returns the UPriority
*/
fun getUPriority(priority: String, default : UPriority = UPriority.UNRECOGNIZED): UPriority {
return UPriority.getDescriptor().values
.filter { value ->
value.options.hasExtension(UprotocolOptions.ceName) && value.options
.getExtension(UprotocolOptions.ceName) == priority
}.map { value ->
UPriority.forNumber(value.number)
}.firstOrNull() ?: default
}

/**
* Get the UMessageType from the string representation
* @param type The string representation of the UMessageType
* @return returns the UMessageType
*/
fun getMessageType(type: String?): UMessageType {
return when (type) {
"pub.v1" -> UMessageType.UMESSAGE_TYPE_PUBLISH
"req.v1" -> UMessageType.UMESSAGE_TYPE_REQUEST
"res.v1" -> UMessageType.UMESSAGE_TYPE_RESPONSE
else -> UMessageType.UMESSAGE_TYPE_UNSPECIFIED
}
fun getMessageType(type: String): UMessageType {
return UMessageType.getDescriptor().values
.filter { value ->
value.options.hasExtension(UprotocolOptions.ceName) && value.options
.getExtension(UprotocolOptions.ceName) == type
}.map { value ->
UMessageType.forNumber(value.number)
}.firstOrNull() ?: UMessageType.UNRECOGNIZED
}

/**
Expand All @@ -346,11 +366,8 @@ object UCloudEvent {
if (hasCommunicationStatusProblem(event)) {
commstatus = getCommunicationStatus(event)
}


getPriority(event)?.let {
val adjustedPriority = if (it.startsWith("UPRIORITY_")) it else "UPRIORITY_$it"
priority = UPriority.valueOf(adjustedPriority)
getPriority(event)?.let { p ->
priority = getUPriority(p, UPriority.UPRIORITY_UNSPECIFIED)
}

getSink(event)?.let { sink = LongUriSerializer.INSTANCE.deserialize(it) }
Expand Down Expand Up @@ -403,7 +420,7 @@ object UCloudEvent {
}

if (attributes.priorityValue > 0) {
builder.withExtension("priority", attributes.priority.name)
builder.withExtension("priority", getCePriority(attributes.priority))
}

if (attributes.hasSink()) {
Expand Down Expand Up @@ -463,5 +480,15 @@ object UCloudEvent {
}
return format.valueDescriptor.options.getExtension(UprotocolOptions.mimeType)
}

/**
* Retrieves the string representation of the data content type based on the provided Enum value descriptor. <BR></BR>
*
* @param descriptor The EnumDescriptor enumeration representing the payload format.
* @return The corresponding string name for the value.
*/
fun getCeName(descriptor: EnumValueDescriptor): String {
return descriptor.options.getExtension(UprotocolOptions.ceName)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ import org.eclipse.uprotocol.uuid.factory.getTime
import org.eclipse.uprotocol.uuid.factory.isUuid
import org.eclipse.uprotocol.v1.*
import org.eclipse.uprotocol.validation.ValidationResult
import java.util.stream.Collectors
import java.util.stream.Stream


/**
* [UAttributes] is the class that defines the Payload. It is the place for configuring time to live, priority,
Expand All @@ -48,16 +47,19 @@ abstract class UAttributesValidator {
/**
* Take a [UAttributes] object and run validations.
*
* @param attributes The UAttriubes to validate.
* @param attributes The UAttributes to validate.
* @return Returns a [ValidationResult] that is success or failed with a message containing all validation
* errors for
* invalid configurations.
*/
fun validate(attributes: UAttributes): ValidationResult {
val errorMessage = Stream.of(validateType(attributes),
validateTtl(attributes), validateSink(attributes),
validateCommStatus(attributes), validatePermissionLevel(attributes), validateReqId(attributes))
.filter(ValidationResult::isFailure).map { obj: ValidationResult -> obj.getMessage() }.collect(Collectors.joining(","))
val errorMessage = listOf(
validateType(attributes),
validateTtl(attributes), validateSink(attributes), validatePriority(attributes),
validateCommStatus(attributes), validatePermissionLevel(attributes), validateReqId(attributes)
).filter {
it.isFailure()
}.joinToString(",") { obj -> obj.getMessage() }
return if (errorMessage.isBlank()) ValidationResult.success() else ValidationResult.failure(errorMessage)
}

Expand All @@ -75,7 +77,7 @@ abstract class UAttributesValidator {
val maybeTime = uAttributes.id.getTime()

// if the message does not have a ttl or the original time is not present or the ttl is less than 0
if (!uAttributes.hasTtl() || maybeTime==null || ttl <= 0) {
if (!uAttributes.hasTtl() || maybeTime == null || ttl <= 0) {
return false
}
// the original time plus the ttl is less than the current time, the message has expired
Expand Down Expand Up @@ -139,7 +141,7 @@ abstract class UAttributesValidator {
run {
if (attributes.hasCommstatus()) {
val enumValue = UCode.forNumber(attributes.commstatus)
return if (enumValue!=null) {
return if (enumValue != null) {
ValidationResult.success()
} else {
ValidationResult.failure("Invalid Communication Status Code")
Expand All @@ -164,6 +166,21 @@ abstract class UAttributesValidator {
}
}

/**
* Validate the priority value to ensure it is one of the known CS values
*
* @param attributes Attributes object containing the Priority to validate.
* @return Returns a [ValidationResult] that is success or failed with a failure message.
*/
open fun validatePriority(attributes: UAttributes): ValidationResult {
return if (attributes.priority.number >= UPriority.UPRIORITY_CS0_VALUE) {
ValidationResult.success()
} else ValidationResult.failure(
"Invalid UPriority [${attributes.priority.name}]"
)
}


/**
* Validate the [UMessageType] attribute, it is required.
*
Expand All @@ -176,13 +193,13 @@ abstract class UAttributesValidator {
* Validators Factory. Example:
* UAttributesValidator validateForPublishMessageType = UAttributesValidator.Validators.PUBLISH.validator()
*/
enum class Validators(private val uattributesValidator: UAttributesValidator) {
enum class Validators(private val uAttributesValidator: UAttributesValidator) {
PUBLISH(Publish()),
REQUEST(Request()),
RESPONSE(Response());

fun validator(): UAttributesValidator {
return uattributesValidator
return uAttributesValidator
}
}

Expand All @@ -197,7 +214,9 @@ abstract class UAttributesValidator {
* @return Returns a [ValidationResult] that is success or failed with a failure message.
*/
override fun validateType(attributes: UAttributes): ValidationResult {
return if (UMessageType.UMESSAGE_TYPE_PUBLISH == attributes.type) ValidationResult.success() else ValidationResult.failure(String.format("Wrong Attribute Type [%s]", attributes.type))
return if (UMessageType.UMESSAGE_TYPE_PUBLISH == attributes.type) ValidationResult.success() else ValidationResult.failure(
String.format("Wrong Attribute Type [%s]", attributes.type)
)
dddj698 marked this conversation as resolved.
Show resolved Hide resolved
}

override fun toString(): String {
Expand All @@ -216,7 +235,9 @@ abstract class UAttributesValidator {
* @return Returns a [ValidationResult] that is success or failed with a failure message.
*/
override fun validateType(attributes: UAttributes): ValidationResult {
return if (UMessageType.UMESSAGE_TYPE_REQUEST == attributes.type) ValidationResult.success() else ValidationResult.failure(String.format("Wrong Attribute Type [%s]", attributes.type))
return if (UMessageType.UMESSAGE_TYPE_REQUEST == attributes.type) ValidationResult.success() else ValidationResult.failure(
String.format("Wrong Attribute Type [%s]", attributes.type)
)
dddj698 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -251,6 +272,20 @@ abstract class UAttributesValidator {
}
}

/**
* Validate the priority value to ensure it is one of the known CS values
*
* @param attributes Attributes object containing the Priority to validate.
* @return Returns a [ValidationResult] that is success or failed with a failure message.
*/
override fun validatePriority(attributes: UAttributes): ValidationResult {
return if (attributes.priority.number >= UPriority.UPRIORITY_CS4_VALUE) {
ValidationResult.success()
} else ValidationResult.failure(
"Invalid UPriority [${attributes.priority.name}]"
)
}

override fun toString(): String {
return "UAttributesValidator.Request"
}
Expand All @@ -267,7 +302,9 @@ abstract class UAttributesValidator {
* @return Returns a [ValidationResult] that is success or failed with a failure message.
*/
override fun validateType(attributes: UAttributes): ValidationResult {
return if (UMessageType.UMESSAGE_TYPE_RESPONSE == attributes.type) ValidationResult.success() else ValidationResult.failure(String.format("Wrong Attribute Type [%s]", attributes.type))
return if (UMessageType.UMESSAGE_TYPE_RESPONSE == attributes.type) ValidationResult.success() else ValidationResult.failure(
String.format("Wrong Attribute Type [%s]", attributes.type)
)
}

/**
Expand Down Expand Up @@ -301,6 +338,20 @@ abstract class UAttributesValidator {
}
}

/**
* Validate the priority value to ensure it is one of the known CS values
*
* @param attributes Attributes object containing the Priority to validate.
* @return Returns a [ValidationResult] that is success or failed with a failure message.
*/
override fun validatePriority(attributes: UAttributes): ValidationResult {
return if (attributes.priority.number >= UPriority.UPRIORITY_CS4_VALUE) {
ValidationResult.success()
} else ValidationResult.failure(
"Invalid UPriority [${attributes.priority.name}]"
)
}

override fun toString(): String {
return "UAttributesValidator.Response"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.eclipse.uprotocol.uri.validator

import org.eclipse.uprotocol.v1.UAuthority
import org.eclipse.uprotocol.v1.UResource
import org.eclipse.uprotocol.v1.UUri
import org.eclipse.uprotocol.validation.ValidationResult
import kotlin.contracts.ExperimentalContracts
Expand Down Expand Up @@ -83,7 +82,8 @@ fun UUri.validateRpcResponse(): ValidationResult {
* @return Returns true if URI is of type RPC.
*/
fun UUri.isRpcMethod(): Boolean {
return resource.name.contains("rpc") && (resource

return resource.name == "rpc" && (resource
.hasInstance() && resource.instance.trim().isNotEmpty() || resource
.hasId() && resource.id != 0)
}
Expand All @@ -105,9 +105,7 @@ fun UUri.isResolved(): Boolean {
* @return Returns true if URI is of type RPC response.
*/
fun UUri.isRpcResponse(): Boolean {
val resource: UResource = resource
return resource.getName().contains("rpc") &&
resource.hasInstance() && resource.getInstance().contains("response") &&
return resource.name == "rpc" && resource.hasInstance() && resource.instance == "response" &&
resource.hasId() && resource.id == 0
}

Expand Down
Loading
Loading