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