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 arrow.core.Either
23 import arrow.core.Left
24 import arrow.core.Right
25 import io.netty.buffer.ByteBuf
26 import io.netty.buffer.ByteBufAllocator
29 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
32 class WireFrameEncoder(val allocator: ByteBufAllocator) {
34 fun encode(frame: WireFrame): ByteBuf {
35 val bb = allocator.buffer(WireFrame.HEADER_SIZE + frame.payload.size())
37 bb.writeByte(WireFrame.MARKER_BYTE.toInt())
38 bb.writeByte(frame.version.toInt())
39 bb.writeByte(frame.payloadTypeRaw.toInt())
40 bb.writeInt(frame.payloadSize)
41 frame.payload.writeTo(bb)
48 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
51 class WireFrameDecoder {
53 fun decodeFirst(byteBuf: ByteBuf): Either<WireFrameDecodingError, WireFrame> =
55 isEmpty(byteBuf) -> Left(EmptyWireFrame)
56 headerDoesNotFit(byteBuf) -> Left(MissingWireFrameHeaderBytes)
57 else -> parseFrame(byteBuf)
60 private fun headerDoesNotFit(byteBuf: ByteBuf) = byteBuf.readableBytes() < WireFrame.HEADER_SIZE
62 private fun isEmpty(byteBuf: ByteBuf) = byteBuf.readableBytes() < 1
64 private fun parseFrame(byteBuf: ByteBuf): Either<WireFrameDecodingError, WireFrame> {
65 byteBuf.markReaderIndex()
67 val mark = byteBuf.readUnsignedByte()
68 if (mark != WireFrame.MARKER_BYTE) {
69 byteBuf.resetReaderIndex()
70 return Left(InvalidWireFrameMarker(mark))
73 val version = byteBuf.readUnsignedByte()
74 val payloadTypeRaw = byteBuf.readUnsignedByte()
76 val payloadSize = byteBuf.readInt()
78 if (payloadSize > MAX_PAYLOAD_SIZE) {
79 return Left(PayloadSizeExceeded)
82 if (byteBuf.readableBytes() < payloadSize) {
83 byteBuf.resetReaderIndex()
84 return Left(MissingWireFramePayloadBytes)
87 val payload = ByteData.readFrom(byteBuf, payloadSize)
89 return Right(WireFrame(payload, version, payloadTypeRaw, payloadSize))
93 const val MAX_PAYLOAD_SIZE = 1024 * 1024