Remove any inconsistencies with specification
[dcaegen2/collectors/hv-ves.git] / hv-collector-domain / src / main / kotlin / org / onap / dcae / collectors / veshv / domain / wire_frame.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
23 /**
24  * Wire frame structure is presented bellow using ASN.1 notation. Please note that official supported specification
25  * should be available on
26  * [RTD documentation](https://onap.readthedocs.io/en/latest/submodules/dcaegen2.git/docs/sections/apis/ves-hv.html).
27  *
28  * ```
29  * -- Wire Transfer Protocol (binary, defined using ASN.1 notation)
30  * -- Encoding: use "direct encoding" to the number of octets indicated in the comment [n], using network byte order.
31  *
32  * WTP DEFINITIONS ::= BEGIN
33  *
34  * -- Used to sent data from the data provider
35  * WtpData ::= SEQUENCE {
36  * magic           INTEGER (0..255),           -- [1] always 0xAA
37  * versionMajor    INTEGER (0..255),           -- [1] major interface version, forward incompatible
38  *                                             --     with previous  major version, current value: 1
39  * versionMinor    INTEGER (0..255),           -- [1] minor interface version, forward compatible
40  *                                             --     with previous minor version, current value: 0
41  * reserved        OCTET STRING (SIZE (3)),    -- [3] reserved for future use (ignored, but use 0)
42  * payloadId       INTEGER (0..65535),         -- [2] payload type: 0x0000=undefined, 0x0001=ONAP VesEvent (protobuf)
43  * payloadLength   INTEGER (0..4294967295).    -- [4] payload length in octets
44  * payload         OCTET STRING                -- [length as per payloadLength]
45  * }
46  *
47  * END
48  * ```
49  *
50  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
51  * @since May 2018
52  */
53 data class WireFrameMessage(val payload: ByteData,
54                             val versionMajor: Short,
55                             val versionMinor: Short,
56                             val payloadType: Int,
57                             val payloadSize: Int
58 ) {
59     constructor(payload: ByteArray) : this(
60             ByteData(payload),
61             SUPPORTED_VERSION_MAJOR,
62             SUPPORTED_VERSION_MINOR,
63             PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
64             payload.size)
65
66     fun isValid(): Boolean =
67             versionMajor == SUPPORTED_VERSION_MAJOR
68                     && PayloadContentType.isValidHexValue(payloadType)
69                     && payload.size() == payloadSize
70
71     companion object {
72         const val MARKER_BYTE: Short = 0xAA
73         const val RESERVED_BYTE_COUNT: Int = 3
74
75         const val SUPPORTED_VERSION_MAJOR: Short = 1
76         const val SUPPORTED_VERSION_MINOR: Short = 0
77
78         const val HEADER_SIZE =
79                 1 * java.lang.Byte.BYTES +                           // marker
80                         2 * java.lang.Byte.BYTES +                   // single byte fields
81                         1 * java.lang.Short.BYTES +                  // double byte fields
82                         RESERVED_BYTE_COUNT * java.lang.Byte.BYTES + // reserved bytes
83                         1 * java.lang.Integer.BYTES                  // payload length
84
85         const val DEFAULT_MAX_PAYLOAD_SIZE_BYTES = 1024 * 1024
86     }
87 }