Jian Li | 6bd3510 | 2018-06-09 00:18:47 +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.ByteBufferCodec; |
| 19 | import org.onosproject.openstacktelemetry.api.StatsInfo; |
| 20 | import org.onosproject.openstacktelemetry.impl.DefaultStatsInfo; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | |
| 24 | /** |
| 25 | * StatsInfo ByteBuffer Codec. |
| 26 | */ |
| 27 | public class TinaStatsInfoByteBufferCodec extends ByteBufferCodec<StatsInfo> { |
| 28 | |
| 29 | private static final int MESSAGE_SIZE = 48; |
| 30 | |
| 31 | @Override |
| 32 | public ByteBuffer encode(StatsInfo statsInfo) { |
| 33 | |
| 34 | ByteBuffer byteBuffer = ByteBuffer.allocate(MESSAGE_SIZE); |
| 35 | |
| 36 | byteBuffer.putLong(statsInfo.startupTime()) |
| 37 | .putLong(statsInfo.fstPktArrTime()) |
| 38 | .putInt(statsInfo.lstPktOffset()) |
| 39 | .putLong(statsInfo.prevAccBytes()) |
| 40 | .putInt(statsInfo.prevAccPkts()) |
| 41 | .putLong(statsInfo.currAccBytes()) |
| 42 | .putInt(statsInfo.currAccPkts()) |
| 43 | .putShort(statsInfo.errorPkts()) |
| 44 | .putShort(statsInfo.dropPkts()); |
| 45 | |
| 46 | return byteBuffer; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public StatsInfo decode(ByteBuffer byteBuffer) { |
| 51 | |
| 52 | long startupTime = byteBuffer.getLong(); |
| 53 | long fstPktArrTime = byteBuffer.getLong(); |
| 54 | int lstPktOffset = byteBuffer.getInt(); |
| 55 | long prevAccBytes = byteBuffer.getLong(); |
| 56 | int prevAccPkts = byteBuffer.getInt(); |
| 57 | long currAccBytes = byteBuffer.getLong(); |
| 58 | int currAccPkts = byteBuffer.getInt(); |
| 59 | short errorPkts = byteBuffer.getShort(); |
| 60 | short dropPkts = byteBuffer.getShort(); |
| 61 | |
| 62 | return new DefaultStatsInfo.DefaultBuilder() |
| 63 | .withStartupTime(startupTime) |
| 64 | .withFstPktArrTime(fstPktArrTime) |
| 65 | .withLstPktOffset(lstPktOffset) |
| 66 | .withPrevAccBytes(prevAccBytes) |
| 67 | .withPrevAccPkts(prevAccPkts) |
| 68 | .withCurrAccBytes(currAccBytes) |
| 69 | .withCurrAccPkts(currAccPkts) |
| 70 | .withErrorPkts(errorPkts) |
| 71 | .withDropPkts(dropPkts) |
| 72 | .build(); |
| 73 | } |
| 74 | } |