2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dcae.collectors.veshv.tests.utils
22 import com.google.protobuf.ByteString
23 import io.netty.buffer.ByteBuf
24 import io.netty.buffer.ByteBufAllocator
25 import io.netty.buffer.PooledByteBufAllocator
26 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
27 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.OTHER
28 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.PERF3GPP
29 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage.Companion.RESERVED_BYTE_COUNT
30 import org.onap.ves.VesEventOuterClass.VesEvent
31 import java.util.UUID.randomUUID
34 val allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT
36 private fun validWireFrame() = allocator.buffer().run {
37 writeByte(0xAA) // always 0xAA
38 writeByte(0x01) // major version
39 writeByte(0x00) // minor version
40 writeZero(RESERVED_BYTE_COUNT) // reserved
41 writeShort(0x0001) // content type = GPB
44 private fun invalidWireFrame() = allocator.buffer().run {
45 writeByte(0xAA) // always 0xAA
46 writeByte(0x00) // invalid major version
47 writeByte(0x00) // minor version
48 writeZero(RESERVED_BYTE_COUNT) // reserved
49 writeShort(0x0001) // content type = GPB
52 fun garbageFrame(): ByteBuf = allocator.buffer().run {
53 writeBytes("the meaning of life is &@)(*_!".toByteArray())
56 fun vesWireFrameMessage(domain: VesEventDomain = OTHER,
57 id: String = randomUUID().toString(),
58 eventFields: ByteString = ByteString.EMPTY,
59 vesEventListenerVersion: String = "7.0.2"): ByteBuf =
60 vesWireFrameMessage(vesEvent(domain, id, eventFields, vesEventListenerVersion))
62 fun vesWireFrameMessage(vesEvent: VesEvent): ByteBuf =
63 validWireFrame().run {
64 val gpb = vesEvent.toByteString().asReadOnlyByteBuffer()
65 writeInt(gpb.limit()) // ves event size in bytes
66 writeBytes(gpb) // ves event as GPB bytes
69 fun messageWithInvalidWireFrameHeader(vesEvent: VesEvent = vesEvent()): ByteBuf =
70 invalidWireFrame().run {
71 val gpb = vesEvent.toByteString().asReadOnlyByteBuffer()
72 writeInt(gpb.limit()) // ves event size in bytes
73 writeBytes(gpb) // ves event as GPB bytes
76 fun wireFrameMessageWithInvalidPayload(): ByteBuf =
77 validWireFrame().run {
78 val invalidGpb = "some random data".toByteArray(Charsets.UTF_8)
79 writeInt(invalidGpb.size) // ves event size in bytes
80 writeBytes(invalidGpb)
83 fun messageWithPayloadOfSize(payloadSizeBytes: Int, domain: VesEventDomain = PERF3GPP): ByteBuf =
86 eventFields = ByteString.copyFrom(ByteArray(payloadSizeBytes))
89 fun messageWithInvalidListenerVersion() = vesWireFrameMessage(vesEventListenerVersion = "invalid")