summaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-domain/src/main/kotlin/org
diff options
context:
space:
mode:
Diffstat (limited to 'sources/hv-collector-domain/src/main/kotlin/org')
-rw-r--r--sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/PayloadContentType.kt2
-rw-r--r--sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/errors.kt16
-rw-r--r--sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/wire_frame.kt16
3 files changed, 29 insertions, 5 deletions
diff --git a/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/PayloadContentType.kt b/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/PayloadContentType.kt
index 7cbf3530..e4a1dd85 100644
--- a/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/PayloadContentType.kt
+++ b/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/PayloadContentType.kt
@@ -27,7 +27,7 @@ enum class PayloadContentType(val hexValue: Int) {
GOOGLE_PROTOCOL_BUFFER(0x0001);
companion object {
- private val hexValues = PayloadContentType.values().map { it.hexValue }
+ val hexValues = PayloadContentType.values().map { it.hexValue }
fun isValidHexValue(hex: Int) = hexValues.contains(hex)
}
diff --git a/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/errors.kt b/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/errors.kt
index 0d55cebb..4d60d62c 100644
--- a/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/errors.kt
+++ b/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/errors.kt
@@ -46,3 +46,19 @@ sealed class MissingWireFrameBytes(msg: String) : WireFrameDecodingError(msg)
object MissingWireFrameHeaderBytes : MissingWireFrameBytes("readable bytes < header size")
object MissingWireFramePayloadBytes : MissingWireFrameBytes("readable bytes < payload size")
object EmptyWireFrame : MissingWireFrameBytes("empty wire frame")
+
+// WireFrameMessage validation exceptions
+
+sealed class WireFrameMessageValidationError(val message: String)
+
+class InvalidMajorVersion(actualVersion: Short) : WireFrameMessageValidationError(
+ "Invalid major version in wire frame header. " +
+ "Expected ${WireFrameMessage.SUPPORTED_VERSION_MAJOR} but was $actualVersion")
+
+class UnsupportedPayloadContentType(actualType: Int) : WireFrameMessageValidationError(
+ "Invalid content type in wire frame header. " +
+ "Expected one of ${PayloadContentType.hexValues}, but was $actualType")
+
+class NotMatchingPayloadSize(definedInHeader: Int, actual: Int) : WireFrameMessageValidationError(
+ "Payload size does not match one defined in wire frame header.\n" +
+ "Defined in header: $definedInHeader, but was: $actual")
diff --git a/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/wire_frame.kt b/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/wire_frame.kt
index de37b140..1257c6bb 100644
--- a/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/wire_frame.kt
+++ b/sources/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/wire_frame.kt
@@ -19,6 +19,11 @@
*/
package org.onap.dcae.collectors.veshv.domain
+import arrow.core.Either
+import arrow.core.Either.Companion.left
+import arrow.core.Either.Companion.right
+
+
/**
* Wire frame structure is presented bellow using ASN.1 notation. Please note that official supported specification
* should be available on
@@ -62,10 +67,13 @@ data class WireFrameMessage(val payload: ByteData,
PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
payload.size)
- fun isValid(): Boolean =
- versionMajor == SUPPORTED_VERSION_MAJOR
- && PayloadContentType.isValidHexValue(payloadType)
- && payload.size() == payloadSize
+ fun validate(): Either<WireFrameMessageValidationError, WireFrameMessage> =
+ when {
+ versionMajor != SUPPORTED_VERSION_MAJOR -> left(InvalidMajorVersion(versionMajor))
+ !PayloadContentType.isValidHexValue(payloadType) -> left(UnsupportedPayloadContentType(payloadType))
+ payload.size() != payloadSize -> left(NotMatchingPayloadSize(payload.size(), payloadSize))
+ else -> right(this)
+ }
companion object {
const val MARKER_BYTE: Short = 0xAA