Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.openstacktelemetry.codec; |
| 17 | |
| 18 | import org.onosproject.openstacktelemetry.api.FlowInfo; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 19 | import org.onosproject.openstacktelemetry.api.TelemetryCodec; |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | /** |
| 25 | * Tina Message ByteBuffer Codec. |
| 26 | */ |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 27 | public class TinaMessageByteBufferCodec implements TelemetryCodec { |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 28 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 29 | private static final int HEADER_SIZE = 8; |
| 30 | private static final int ENTRY_SIZE = 88; |
| 31 | private static final int MILLISECONDS = 1000; |
| 32 | private static final short KAFKA_MESSAGE_TYPE = 1; |
| 33 | |
| 34 | /** |
| 35 | * Encodes a collection flow infos into byte buffer. |
| 36 | * |
| 37 | * @param flowInfos a collection of flow info |
| 38 | * @return encoded byte buffer |
| 39 | */ |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 40 | @Override |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 41 | public ByteBuffer encode(Set<FlowInfo> flowInfos) { |
| 42 | ByteBuffer byteBuffer = |
| 43 | ByteBuffer.allocate(HEADER_SIZE + flowInfos.size() * ENTRY_SIZE); |
| 44 | |
| 45 | byteBuffer.put(buildMessageHeader(flowInfos)); |
| 46 | byteBuffer.put(buildMessageBody(flowInfos)); |
| 47 | |
| 48 | return byteBuffer; |
| 49 | } |
| 50 | |
| 51 | private byte[] buildMessageHeader(Set<FlowInfo> flowInfos) { |
| 52 | ByteBuffer byteBuffer = ByteBuffer.allocate(HEADER_SIZE); |
| 53 | |
| 54 | byteBuffer.putShort((short) flowInfos.size()); |
| 55 | byteBuffer.putShort(KAFKA_MESSAGE_TYPE); |
| 56 | byteBuffer.putInt((int) (System.currentTimeMillis() / MILLISECONDS)); |
| 57 | |
| 58 | return byteBuffer.array(); |
| 59 | } |
| 60 | |
| 61 | private byte[] buildMessageBody(Set<FlowInfo> flowInfos) { |
| 62 | ByteBuffer byteBuffer = ByteBuffer.allocate(flowInfos.size() * ENTRY_SIZE); |
| 63 | |
| 64 | TinaFlowInfoByteBufferCodec codec = new TinaFlowInfoByteBufferCodec(); |
Jian Li | e7b1bd2 | 2018-06-27 15:15:28 +0900 | [diff] [blame] | 65 | flowInfos.forEach(flowInfo -> byteBuffer.put(codec.encode(flowInfo).array())); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 66 | |
| 67 | return byteBuffer.array(); |
| 68 | } |
| 69 | } |