Include payload content type in wire frame 09/58609/1
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>
Tue, 19 Jun 2018 08:27:53 +0000 (10:27 +0200)
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>
Thu, 2 Aug 2018 07:57:34 +0000 (09:57 +0200)
Closes ONAP-404
Change-Id: I6adfb542ffdafad678e7bc6e062d3d59c250b39e
Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
Issue-ID: DCAEGEN2-601

hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/utils.kt
hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/PayloadContentType.kt [new file with mode: 0644]
hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrame.kt
hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/codec.kt
hv-collector-domain/src/test/kotlin/org/onap/dcae/collectors/veshv/domain/WireFrameCodecsTest.kt

index 998f314..3314c44 100644 (file)
 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 (file)
index 0000000..0ba4b81
--- /dev/null
@@ -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)
+    }
+}
index caf13c5..db6e107 100644 (file)
@@ -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
-
     }
-
 }
index d6804c7..3cd9b19 100644 (file)
@@ -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 =
index ed64f3b..9694caf 100644 (file)
@@ -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") {