a946ceac74107a57f6bbb7385413f024329c2b32
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
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
21 package org.onap.dcaegen2.services.sdk.services.hvves.client.producer.impl.encoders;
22
23 import io.netty.buffer.ByteBuf;
24 import io.netty.buffer.ByteBufAllocator;
25 import io.vavr.control.Try;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.nio.ByteBuffer;
30
31 /**
32  * @author <a href="mailto:jakub.dudycz@nokia.com">Jakub Dudycz</a>
33  */
34 public class WireFrameEncoder {
35     private static final Logger LOGGER = LoggerFactory.getLogger(WireFrameEncoder.class);
36     private static final short MARKER_BYTE = 0xAA;
37     private static final short SUPPORTED_VERSION_MAJOR = 0x01;
38     private static final short SUPPORTED_VERSION_MINOR = 0x00;
39     private static final int RESERVED_BYTES_COUNT = 3;
40     private static final int HEADER_SIZE = 1 * Byte.BYTES +         // marker
41             2 * Byte.BYTES +                                        // single byte fields (versions)
42             RESERVED_BYTES_COUNT * Byte.BYTES +                     // reserved bytes
43             1 * Short.BYTES +                                       // paylaod type
44             1 * Integer.BYTES;                                      // payload length
45
46     private final ByteBufAllocator allocator;
47
48     public WireFrameEncoder(ByteBufAllocator allocator) {
49         this.allocator = allocator;
50     }
51
52     public Try<ByteBuf> encode(ByteBuffer payload) {
53         return Try.of(() -> encodeMessageAs(payload, PayloadType.PROTOBUF))
54                 .onFailure((ex) -> LOGGER.warn("Failed to encode payload", ex));
55     }
56
57     private ByteBuf encodeMessageAs(ByteBuffer payload, PayloadType payloadType) throws WTPEncodingException {
58         if (payload == null) {
59             throw new WTPEncodingException("Payload is null");
60         }
61
62         final int payloadSize = payload.remaining();
63         if (payloadSize == 0) {
64             throw new WTPEncodingException("Payload is empty");
65         }
66
67         final ByteBuf encodedMessage = allocator.buffer(HEADER_SIZE + payloadSize);
68         writeBasicWTPFrameHeaderBeginning(encodedMessage);
69         writePayloadMessageHeaderEnding(encodedMessage, payloadType, payload, payloadSize);
70         return encodedMessage;
71     }
72
73     private void writeBasicWTPFrameHeaderBeginning(ByteBuf encodedMessage) {
74         encodedMessage.writeByte(MARKER_BYTE);
75         encodedMessage.writeByte(SUPPORTED_VERSION_MAJOR);
76         encodedMessage.writeByte(SUPPORTED_VERSION_MINOR);
77         encodedMessage.writeZero(RESERVED_BYTES_COUNT);
78     }
79
80     private void writePayloadMessageHeaderEnding(ByteBuf encodedMessage,
81                                                  PayloadType payloadType,
82                                                  ByteBuffer payload,
83                                                  int payloadSize) {
84         encodedMessage.writeBytes(payloadType.getPayloadTypeBytes());
85         encodedMessage.writeInt(payloadSize);
86         encodedMessage.writeBytes(payload);
87     }
88 }
89
90
91 class WTPEncodingException extends RuntimeException {
92     WTPEncodingException(String message) {
93         super(message);
94     }
95 }