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.domain
22 import io.netty.buffer.ByteBuf
23 import io.netty.buffer.ByteBufAllocator
26 * Wire frame structure is presented bellow. All fields are in network byte order (big-endian).
29 * ┌─────┬───────────────────────┬───────────────────────┬───────────────────────┐
31 * ├─────┼──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┤
32 * │ bit │ 0│ │ │ │ │ │ │ │ 8│ │ │ │ │ │ │ │16│ │ │ │ │ │ │ │ ...
33 * ├─────┼──┴──┴──┴──┴──┴──┴──┴──┼──┴──┴──┴──┴──┴──┴──┴──┼──┴──┴──┴──┴──┴──┴──┴──┤
34 * │field│ 0xFF │ major version │ minor version │
35 * └─────┴───────────────────────┴───────────────────────┴───────────────────────┘
36 * ┌─────┬───────────────────────┬───────────────────────┬───────────────────────┬───────────────────────┐
37 * │octet│ 3 │ 4 │ 5 │ 6 │
38 * ├─────┼──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┼──┬──┬──┬──┬──┬──┬──┬──┤
39 * ... │ bit │24│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ...
40 * ├─────┼──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┤
41 * │field│ payload size │
42 * └─────┴───────────────────────────────────────────────────────────────────────────────────────────────┘
43 * ┌─────┬───────────────────────
45 * ├─────┼──┬──┬──┬──┬──┬──┬──┬──
46 * ... │ bit │56│ │ │ │ │ │ │...
47 * ├─────┼──┴──┴──┴──┴──┴──┴──┴──
48 * │field│ protobuf payload
49 * └─────┴───────────────────────
52 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
55 data class WireFrame(val payload: ByteBuf,
57 val majorVersion: Short,
58 val minorVersion: Short,
59 val payloadSize: Int) {
60 fun isValid(): Boolean {
61 return mark == FF_BYTE
62 && majorVersion == SUPPORTED_MAJOR_VERSION
63 && payload.readableBytes() == payloadSize
66 fun encode(allocator: ByteBufAllocator): ByteBuf {
67 val bb = allocator.buffer(HEADER_SIZE + payload.readableBytes())
69 bb.writeByte(mark.toInt())
70 bb.writeByte(majorVersion.toInt())
71 bb.writeByte(minorVersion.toInt())
72 bb.writeInt(payloadSize)
73 bb.writeBytes(payload)
79 fun decode(byteBuf: ByteBuf): WireFrame {
80 val mark = byteBuf.readUnsignedByte()
81 val majorVersion = byteBuf.readUnsignedByte()
82 val minorVersion = byteBuf.readUnsignedByte()
83 val payloadSize = byteBuf.readInt()
84 val payload = byteBuf.retainedSlice()
86 return WireFrame(payload, mark, majorVersion, minorVersion, payloadSize)
89 private const val HEADER_SIZE = 3 + java.lang.Integer.BYTES
90 private const val FF_BYTE: Short = 0xFF
91 private const val SUPPORTED_MAJOR_VERSION: Short = 1