diff options
author | Filip Krzywka <filip.krzywka@nokia.com> | 2018-11-29 11:58:40 +0100 |
---|---|---|
committer | Filip Krzywka <filip.krzywka@nokia.com> | 2018-12-04 13:31:17 +0100 |
commit | d632aef8303701a1802f817c3d6fdcd4064c32b2 (patch) | |
tree | 70614ef073f437810beea848c9f9a81189b794d8 /sources/hv-collector-domain | |
parent | dde383a2aa75f94c26d7949665b79cc95486a223 (diff) |
Harmonize logging and add new logs
- corrected docker-compose consul url
Change-Id: I78df868e0dd51008ef39d01553e6a0a3b8273a54
Issue-ID: DCAEGEN2-1003
Signed-off-by: Filip Krzywka <filip.krzywka@nokia.com>
Diffstat (limited to 'sources/hv-collector-domain')
4 files changed, 36 insertions, 12 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 diff --git a/sources/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt b/sources/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt index f17a79ba..f8fbc0a3 100644 --- a/sources/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt +++ b/sources/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt @@ -60,7 +60,7 @@ object WireFrameCodecsTest : Spek({ payloadSize = 0) it("should fail validation") { - assertThat(input.isValid()).isFalse() + input.validate().assertFailedWithError { it.isInstanceOf(InvalidMajorVersion::class.java) } } } @@ -73,7 +73,7 @@ object WireFrameCodecsTest : Spek({ payloadSize = 0) it("should pass validation") { - assertThat(input.isValid()).isTrue() + assertTrue(input.validate().isRight()) } } @@ -86,7 +86,7 @@ object WireFrameCodecsTest : Spek({ payloadSize = 0) it("should fail validation") { - assertThat(input.isValid()).isFalse() + input.validate().assertFailedWithError { it.isInstanceOf(UnsupportedPayloadContentType::class.java) } } } @@ -99,7 +99,7 @@ object WireFrameCodecsTest : Spek({ payloadSize = 1) it("should fail validation") { - assertThat(input.isValid()).isFalse() + input.validate().assertFailedWithError { it.isInstanceOf(NotMatchingPayloadSize::class.java) } } } @@ -112,7 +112,7 @@ object WireFrameCodecsTest : Spek({ payloadSize = 8) it("should fail validation") { - assertThat(input.isValid()).isFalse() + input.validate().assertFailedWithError { it.isInstanceOf(NotMatchingPayloadSize::class.java) } } } @@ -126,7 +126,7 @@ object WireFrameCodecsTest : Spek({ payloadSize = payload.size) it("should pass validation") { - assertThat(input.isValid()).isTrue() + assertTrue(input.validate().isRight()) } } @@ -214,7 +214,7 @@ object WireFrameCodecsTest : Spek({ .writeByte(0xAB) val decoded = decoder.decodeFirst(buff).getMessageOrFail() - assertThat(decoded.isValid()).describedAs("should be valid").isTrue() + assertTrue(decoded.validate().isRight(), "should be valid") assertThat(buff.readableBytes()).isEqualTo(1) } } |