Copyright notice correction
[dcaegen2/collectors/hv-ves.git] / hv-collector-core / src / main / kotlin / org / onap / dcae / collectors / veshv / domain / WireFrame.kt
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.domain
21
22 import io.netty.buffer.ByteBuf
23 import io.netty.buffer.ByteBufAllocator
24
25 /**
26  * Wire frame structure is presented bellow. All fields are in network byte order (big-endian).
27  *
28  * ```
29  *     ┌─────┬───────────────────────┬───────────────────────┬───────────────────────┐
30  *     │octet│           0           │           1           │           2           │
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  *     ┌─────┬───────────────────────
44  *     │octet│           7         ...
45  *     ├─────┼──┬──┬──┬──┬──┬──┬──┬──
46  * ... │ bit │56│  │  │  │  │  │  │...
47  *     ├─────┼──┴──┴──┴──┴──┴──┴──┴──
48  *     │field│   protobuf payload
49  *     └─────┴───────────────────────
50  * ```
51  *
52  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
53  * @since May 2018
54  */
55 data class WireFrame(val payload: ByteBuf,
56                      val mark: Short,
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
64     }
65
66     fun encode(allocator: ByteBufAllocator): ByteBuf {
67         val bb = allocator.buffer(HEADER_SIZE + payload.readableBytes())
68
69         bb.writeByte(mark.toInt())
70         bb.writeByte(majorVersion.toInt())
71         bb.writeByte(minorVersion.toInt())
72         bb.writeInt(payloadSize)
73         bb.writeBytes(payload)
74
75         return bb
76     }
77
78     companion object {
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()
85
86             return WireFrame(payload, mark, majorVersion, minorVersion, payloadSize)
87         }
88
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
92     }
93 }