diff options
5 files changed, 87 insertions, 41 deletions
diff --git a/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/utils.kt b/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/utils.kt index 998f3140..3314c44f 100644 --- a/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/utils.kt +++ b/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/utils.kt @@ -20,22 +20,19 @@ package org.onap.dcae.collectors.veshv.tests.component import com.google.protobuf.ByteString -import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBufAllocator import io.netty.buffer.PooledByteBufAllocator -import io.netty.buffer.Unpooled import org.onap.ves.VesEventV5.VesEvent import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain -import java.nio.charset.Charset -import java.util.* +import java.util.UUID -val alocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT +val allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT -fun vesMessage(domain: Domain = Domain.OTHER, id: String = UUID.randomUUID().toString()) = alocator.buffer().run { +fun vesMessage(domain: Domain = Domain.OTHER, id: String = UUID.randomUUID().toString()) = allocator.buffer().run { writeByte(0xFF) // always 0xFF - writeByte(1) // major version - writeByte(0) // minor version + writeByte(0x01) // version + writeByte(0x01) // content type = GPB val gpb = vesEvent(domain, id).toByteString().asReadOnlyByteBuffer() writeInt(gpb.limit()) // ves event size in bytes @@ -43,10 +40,10 @@ fun vesMessage(domain: Domain = Domain.OTHER, id: String = UUID.randomUUID().toS } -fun invalidVesMessage() = alocator.buffer().run { +fun invalidVesMessage() = allocator.buffer().run { writeByte(0xFF) // always 0xFF - writeByte(1) // major version - writeByte(0) // minor version + writeByte(0x01) // version + writeByte(0x01) // content type = GPB val invalidGpb = "some random data".toByteArray(Charsets.UTF_8) writeInt(invalidGpb.size) // ves event size in bytes @@ -54,14 +51,14 @@ fun invalidVesMessage() = alocator.buffer().run { } -fun garbageFrame() = alocator.buffer().run { +fun garbageFrame() = allocator.buffer().run { writeBytes("the meaning of life is &@)(*_!".toByteArray()) } -fun invalidWireFrame() = alocator.buffer().run { +fun invalidWireFrame() = allocator.buffer().run { writeByte(0xFF) - writeByte(1) - writeByte(0) + writeByte(0x01) // version + writeByte(0x01) // content type = GPB } fun vesEvent(domain: Domain = Domain.OTHER, id: String = UUID.randomUUID().toString()) = diff --git a/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/PayloadContentType.kt b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/PayloadContentType.kt new file mode 100644 index 00000000..0ba4b81d --- /dev/null +++ b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/PayloadContentType.kt @@ -0,0 +1,34 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.domain + +/** + * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> + * @since June 2018 + */ +enum class PayloadContentType(val hexValue: Short) { + GOOGLE_PROTOCOL_BUFFER(0x01); + + companion object { + private val hexValues = PayloadContentType.values().map { it.hexValue } + + fun isValidHexValue(hex: Short) = hexValues.contains(hex) + } +} diff --git a/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrame.kt b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrame.kt index caf13c53..db6e1070 100644 --- a/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrame.kt +++ b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrame.kt @@ -28,7 +28,7 @@ package org.onap.dcae.collectors.veshv.domain * ├─────┼──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┤ * │ bit │ 0│ │ │ │ │ │ │ │ 8│ │ │ │ │ │ │ │16│ │ │ │ │ │ │ │ ... * ├─────┼──┴──┴──┴──┴──┴──┴──┴──┼──┴──┴──┴──┴──┴──┴──┴──┼──┴──┴──┴──┴──┴──┴──┴──┤ - * │field│ 0xFF │ major version │ minor version │ + * │field│ 0xFF │ version │ payload content type │ * └─────┴───────────────────────┴───────────────────────┴───────────────────────┘ * ┌─────┬───────────────────────┬───────────────────────┬───────────────────────┬───────────────────────┐ * │octet│ 3 │ 4 │ 5 │ 6 │ @@ -50,24 +50,27 @@ package org.onap.dcae.collectors.veshv.domain * @since May 2018 */ data class WireFrame(val payload: ByteData, - val majorVersion: Short, - val minorVersion: Short, + val version: Short, + val payloadTypeRaw: Short, val payloadSize: Int) { - constructor(payload: ByteArray) : this(ByteData(payload), 1, 0, payload.size) + constructor(payload: ByteArray) : this( + ByteData(payload), + SUPPORTED_VERSION, + PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue, + payload.size) fun isValid(): Boolean = - majorVersion == SUPPORTED_MAJOR_VERSION + version == SUPPORTED_VERSION + && PayloadContentType.isValidHexValue(payloadTypeRaw) && payload.size() == payloadSize companion object { - const val SUPPORTED_MAJOR_VERSION: Short = 1 + const val SUPPORTED_VERSION: Short = 1 const val HEADER_SIZE = 3 * java.lang.Byte.BYTES + 1 * java.lang.Integer.BYTES const val MARKER_BYTE: Short = 0xFF - } - } 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 d6804c7d..3cd9b19a 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 @@ -35,8 +35,8 @@ class WireFrameEncoder(val allocator: ByteBufAllocator) { val bb = allocator.buffer(WireFrame.HEADER_SIZE + frame.payload.size()) bb.writeByte(WireFrame.MARKER_BYTE.toInt()) - bb.writeByte(frame.majorVersion.toInt()) - bb.writeByte(frame.minorVersion.toInt()) + bb.writeByte(frame.version.toInt()) + bb.writeByte(frame.payloadTypeRaw.toInt()) bb.writeInt(frame.payloadSize) frame.payload.writeTo(bb) @@ -57,12 +57,12 @@ class WireFrameDecoder { verifyMarker(byteBuf) verifyMinimumSize(byteBuf) - val majorVersion = byteBuf.readUnsignedByte() - val minorVersion = byteBuf.readUnsignedByte() + val version = byteBuf.readUnsignedByte() + val payloadTypeRaw = byteBuf.readUnsignedByte() val payloadSize = verifyPayloadSize(byteBuf) val payload = ByteData.readFrom(byteBuf, payloadSize) - return WireFrame(payload, majorVersion, minorVersion, payloadSize) + return WireFrame(payload, version, payloadTypeRaw, payloadSize) } private fun verifyPayloadSize(byteBuf: ByteBuf): Int = 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 ed64f3b3..9694caf7 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 @@ -50,11 +50,23 @@ object WireFrameCodecsTest : Spek({ describe("Wire Frame invariants") { - given("input with unsupported major version") { + given("input with unsupported version") { val input = WireFrame( payload = ByteData.EMPTY, - majorVersion = 100, - minorVersion = 2, + version = 100, + payloadTypeRaw = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue, + payloadSize = 0) + + it("should fail validation") { + assertThat(input.isValid()).isFalse() + } + } + + given("input with unsupported payload type") { + val input = WireFrame( + payload = ByteData.EMPTY, + version = 1, + payloadTypeRaw = 0x69, payloadSize = 0) it("should fail validation") { @@ -65,8 +77,8 @@ object WireFrameCodecsTest : Spek({ given("input with too small payload size") { val input = WireFrame( payload = ByteData(byteArrayOf(1, 2, 3)), - majorVersion = 1, - minorVersion = 0, + version = 1, + payloadTypeRaw = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue, payloadSize = 1) it("should fail validation") { @@ -77,8 +89,8 @@ object WireFrameCodecsTest : Spek({ given("input with too big payload size") { val input = WireFrame( payload = ByteData(byteArrayOf(1, 2, 3)), - majorVersion = 1, - minorVersion = 0, + version = 1, + payloadTypeRaw = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue, payloadSize = 8) it("should fail validation") { @@ -90,8 +102,8 @@ object WireFrameCodecsTest : Spek({ val payload = byteArrayOf(6, 9, 8, 6) val input = WireFrame( payload = ByteData(payload), - majorVersion = 1, - minorVersion = 0, + version = 1, + payloadTypeRaw = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue, payloadSize = payload.size) it("should pass validation") { @@ -109,12 +121,12 @@ object WireFrameCodecsTest : Spek({ val encoded = encodeSampleFrame() val decoded = decoder.decodeFirst(encoded) - it("should decode major version") { - assertThat(decoded.majorVersion).isEqualTo(frame.majorVersion) + it("should decode version") { + assertThat(decoded.version).isEqualTo(frame.version) } - it("should decode minor version") { - assertThat(decoded.minorVersion).isEqualTo(frame.minorVersion) + it("should decode payload type") { + assertThat(decoded.payloadTypeRaw).isEqualTo(frame.payloadTypeRaw) } it("should decode payload size") { |