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; |
| 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.util.Set; |
| 22 | |
| 23 | /** |
| 24 | * Tina Message ByteBuffer Codec. |
| 25 | */ |
| 26 | public class TinaMessageByteBufferCodec { |
| 27 | |
Jian Li | a494768 | 2018-07-07 14:53:32 +0900 | [diff] [blame] | 28 | public static final String KAFKA_TOPIC = "sona.flow"; |
| 29 | public static final String KAFKA_KEY = "flowdata"; |
| 30 | |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 31 | private static final int HEADER_SIZE = 8; |
| 32 | private static final int ENTRY_SIZE = 88; |
| 33 | private static final int MILLISECONDS = 1000; |
| 34 | private static final short KAFKA_MESSAGE_TYPE = 1; |
| 35 | |
| 36 | /** |
| 37 | * Encodes a collection flow infos into byte buffer. |
| 38 | * |
| 39 | * @param flowInfos a collection of flow info |
| 40 | * @return encoded byte buffer |
| 41 | */ |
| 42 | public ByteBuffer encode(Set<FlowInfo> flowInfos) { |
| 43 | ByteBuffer byteBuffer = |
| 44 | ByteBuffer.allocate(HEADER_SIZE + flowInfos.size() * ENTRY_SIZE); |
| 45 | |
| 46 | byteBuffer.put(buildMessageHeader(flowInfos)); |
| 47 | byteBuffer.put(buildMessageBody(flowInfos)); |
| 48 | |
| 49 | return byteBuffer; |
| 50 | } |
| 51 | |
| 52 | private byte[] buildMessageHeader(Set<FlowInfo> flowInfos) { |
| 53 | ByteBuffer byteBuffer = ByteBuffer.allocate(HEADER_SIZE); |
| 54 | |
| 55 | byteBuffer.putShort((short) flowInfos.size()); |
| 56 | byteBuffer.putShort(KAFKA_MESSAGE_TYPE); |
| 57 | byteBuffer.putInt((int) (System.currentTimeMillis() / MILLISECONDS)); |
| 58 | |
| 59 | return byteBuffer.array(); |
| 60 | } |
| 61 | |
| 62 | private byte[] buildMessageBody(Set<FlowInfo> flowInfos) { |
| 63 | ByteBuffer byteBuffer = ByteBuffer.allocate(flowInfos.size() * ENTRY_SIZE); |
| 64 | |
| 65 | TinaFlowInfoByteBufferCodec codec = new TinaFlowInfoByteBufferCodec(); |
Jian Li | e7b1bd2 | 2018-06-27 15:15:28 +0900 | [diff] [blame] | 66 | flowInfos.forEach(flowInfo -> byteBuffer.put(codec.encode(flowInfo).array())); |
Jian Li | 0bbbb1c | 2018-06-22 22:01:17 +0900 | [diff] [blame] | 67 | |
| 68 | return byteBuffer.array(); |
| 69 | } |
| 70 | } |