diff options
Diffstat (limited to 'hv-collector-domain/src')
4 files changed, 12 insertions, 14 deletions
diff --git a/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/codec.kt b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/codec.kt index 4f867f13..f08ec178 100644 --- a/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/codec.kt +++ b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/codec.kt @@ -24,7 +24,6 @@ import arrow.core.Left import arrow.core.Right import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBufAllocator -import org.onap.dcae.collectors.veshv.domain.WireFrameMessage.Companion.MAX_PAYLOAD_SIZE import org.onap.dcae.collectors.veshv.domain.WireFrameMessage.Companion.RESERVED_BYTE_COUNT /** @@ -52,7 +51,7 @@ class WireFrameEncoder(private val allocator: ByteBufAllocator = ByteBufAllocato * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> * @since June 2018 */ -class WireFrameDecoder { +class WireFrameDecoder(private val maxPayloadSizeBytes: Int) { fun decodeFirst(byteBuf: ByteBuf): Either<WireFrameDecodingError, WireFrameMessage> = when { @@ -81,13 +80,13 @@ class WireFrameDecoder { private fun parsePayloadFrame(byteBuf: ByteBuf): Either<WireFrameDecodingError, WireFrameMessage> { val versionMajor = byteBuf.readUnsignedByte() val versionMinor = byteBuf.readUnsignedByte() - byteBuf.skipBytes(RESERVED_BYTE_COUNT) // reserved + byteBuf.skipBytes(RESERVED_BYTE_COUNT) val payloadTypeRaw = byteBuf.readUnsignedByte() val payloadSize = byteBuf.readInt() - if (payloadSize > MAX_PAYLOAD_SIZE) { + if (payloadSize > maxPayloadSizeBytes) { byteBuf.resetReaderIndex() - return Left(PayloadSizeExceeded) + return Left(PayloadSizeExceeded(maxPayloadSizeBytes)) } if (byteBuf.readableBytes() < payloadSize) { diff --git a/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/errors.kt b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/errors.kt index dfadc5b8..0d55cebb 100644 --- a/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/errors.kt +++ b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/errors.kt @@ -19,8 +19,6 @@ */ package org.onap.dcae.collectors.veshv.domain -import org.onap.dcae.collectors.veshv.domain.WireFrameMessage.Companion.MAX_PAYLOAD_SIZE - /** * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> * @since June 2018 @@ -38,7 +36,8 @@ class InvalidWireFrameMarker(actualMarker: Short) : InvalidWireFrame( .format(WireFrameMessage.MARKER_BYTE, actualMarker) ) -object PayloadSizeExceeded : InvalidWireFrame("payload size exceeds the limit ($MAX_PAYLOAD_SIZE bytes)") +class PayloadSizeExceeded(maxPayloadSizeBytes: Int) : + InvalidWireFrame("payload size exceeds the limit ($maxPayloadSizeBytes bytes)") // Missing bytes errors diff --git a/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/wire_frame.kt b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/wire_frame.kt index 036888e1..5fdc5afe 100644 --- a/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/wire_frame.kt +++ b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/wire_frame.kt @@ -70,6 +70,6 @@ data class WireFrameMessage(val payload: ByteData, RESERVED_BYTE_COUNT * java.lang.Byte.BYTES + // reserved bytes 1 * java.lang.Integer.BYTES // payload length - const val MAX_PAYLOAD_SIZE = 1024 * 1024 + const val DEFAULT_MAX_PAYLOAD_SIZE_BYTES = 1024 * 1024 } } diff --git a/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt b/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt index 6756bf82..f17a79ba 100644 --- a/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt +++ b/hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt @@ -28,7 +28,6 @@ import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.describe import org.jetbrains.spek.api.dsl.given import org.jetbrains.spek.api.dsl.it -import org.onap.dcae.collectors.veshv.domain.WireFrameMessage.Companion.MAX_PAYLOAD_SIZE import java.nio.charset.Charset import kotlin.test.assertTrue import kotlin.test.fail @@ -39,8 +38,9 @@ import kotlin.test.fail */ object WireFrameCodecsTest : Spek({ val payloadAsString = "coffeebabe" + val maxPayloadSizeBytes = 1024 val encoder = WireFrameEncoder() - val decoder = WireFrameDecoder() + val decoder = WireFrameDecoder(maxPayloadSizeBytes) fun createSampleFrame() = WireFrameMessage(payloadAsString.toByteArray(Charset.defaultCharset())) @@ -223,7 +223,7 @@ object WireFrameCodecsTest : Spek({ it("should decode successfully when payload size is equal 1 MiB") { - val payload = ByteArray(MAX_PAYLOAD_SIZE) + val payload = ByteArray(maxPayloadSizeBytes) val input = WireFrameMessage( payload = ByteData(payload), versionMajor = 1, @@ -237,7 +237,7 @@ object WireFrameCodecsTest : Spek({ it("should return error when payload exceeds 1 MiB") { - val payload = ByteArray(MAX_PAYLOAD_SIZE + 1) + val payload = ByteArray(maxPayloadSizeBytes + 1) val input = WireFrameMessage( payload = ByteData(payload), versionMajor = 1, @@ -253,7 +253,7 @@ object WireFrameCodecsTest : Spek({ it("should validate only first message") { - val payload = ByteArray(MAX_PAYLOAD_SIZE) + val payload = ByteArray(maxPayloadSizeBytes) val input = WireFrameMessage( payload = ByteData(payload), versionMajor = 1, |