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.WireFrameMessage.Companion.MAX_PAYLOAD_SIZE
27 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage.Companion.RESERVED_BYTE_COUNT
28 import org.onap.dcae.collectors.veshv.domain.VesEventDomain
29 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.HVMEAS
30 import org.onap.dcae.collectors.veshv.domain.VesEventDomain.OTHER
32 import java.util.UUID.randomUUID
35 val allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT
37 private fun ByteBuf.writeValidWireFrameHeaders() {
38 writeByte(0xFF) // always 0xFF
39 writeByte(0x01) // major version
40 writeByte(0x00) // minor version
41 writeZero(RESERVED_BYTE_COUNT) // reserved
42 writeByte(0x01) // content type = GPB
45 fun vesWireFrameMessage(domain: VesEventDomain = OTHER,
46 id: String = randomUUID().toString()): ByteBuf =
47 allocator.buffer().run {
48 writeValidWireFrameHeaders()
50 val gpb = vesEvent(domain, id).toByteString().asReadOnlyByteBuffer()
51 writeInt(gpb.limit()) // ves event size in bytes
52 writeBytes(gpb) // ves event as GPB bytes
55 fun wireFrameMessageWithInvalidPayload(): ByteBuf = allocator.buffer().run {
56 writeValidWireFrameHeaders()
58 val invalidGpb = "some random data".toByteArray(Charsets.UTF_8)
59 writeInt(invalidGpb.size) // ves event size in bytes
60 writeBytes(invalidGpb)
63 fun garbageFrame(): ByteBuf = allocator.buffer().run {
64 writeBytes("the meaning of life is &@)(*_!".toByteArray())
67 fun invalidWireFrame(): ByteBuf = allocator.buffer().run {
69 writeByte(0x01) // version major
70 writeByte(0x01) // version minor
73 fun vesMessageWithTooBigPayload(domain: VesEventDomain = HVMEAS): ByteBuf =
74 allocator.buffer().run {
75 writeValidWireFrameHeaders()
79 hvRanMeasFields = ByteString.copyFrom(ByteArray(MAX_PAYLOAD_SIZE))
80 ).toByteString().asReadOnlyByteBuffer()
82 writeInt(gpb.limit()) // ves event size in bytes
83 writeBytes(gpb) // ves event as GPB bytes