blob: 35547b299c772ce5b9537aa29d952117d769e85b [file] [log] [blame]
Jian Li6bd35102018-06-09 00:18:47 +09001/*
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 */
16package org.onosproject.openstacktelemetry.codec;
17
18import org.onlab.packet.IpAddress;
19import org.onlab.packet.IpAddress.Version;
20import org.onlab.packet.IpPrefix;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.TpPort;
23import org.onlab.packet.VlanId;
24import org.onosproject.net.DeviceId;
25import org.onosproject.openstacktelemetry.api.ByteBufferCodec;
26import org.onosproject.openstacktelemetry.api.FlowInfo;
27import org.onosproject.openstacktelemetry.api.StatsInfo;
28import org.onosproject.openstacktelemetry.impl.DefaultFlowInfo;
29
30import java.nio.ByteBuffer;
31
32/**
33 * FlowInfo ByteBuffer Codec.
34 */
35public class TinaFlowInfoByteBufferCodec extends ByteBufferCodec<FlowInfo> {
36
37 private static final int MESSAGE_SIZE = 88;
38
39 @Override
40 public ByteBuffer encode(FlowInfo flowInfo) {
41
42 ByteBuffer byteBuffer = ByteBuffer.allocate(MESSAGE_SIZE);
43
44 byteBuffer.put(flowInfo.flowType())
45 .putShort(Short.valueOf(flowInfo.deviceId().toString()))
46 .putInt(flowInfo.inputInterfaceId())
47 .putInt(flowInfo.outputInterfaceId())
48 .putShort(flowInfo.vlanId().toShort())
49 .put(flowInfo.srcIp().address().toOctets())
50 .put((byte) flowInfo.srcIp().prefixLength())
51 .putShort((short) flowInfo.srcPort().toInt())
52 .put(flowInfo.dstIp().address().toOctets())
53 .put((byte) flowInfo.dstIp().prefixLength())
54 .putShort((short) flowInfo.dstPort().toInt())
55 .put(flowInfo.protocol())
56 .put(flowInfo.srcMac().toBytes())
57 .put(flowInfo.dstMac().toBytes());
58
59 TinaStatsInfoByteBufferCodec statsInfoByteBufferCodec =
60 new TinaStatsInfoByteBufferCodec();
61 byteBuffer.put(statsInfoByteBufferCodec.encode(flowInfo.statsInfo()).array());
62
63 return byteBuffer;
64 }
65
66 @Override
67 public FlowInfo decode(ByteBuffer byteBuffer) {
68
69 byte flowType = byteBuffer.get();
70 DeviceId deviceId = DeviceId.deviceId(String.valueOf(byteBuffer.getShort()));
71 int inputInterfaceId = byteBuffer.getInt();
72 int outputInterfaceId = byteBuffer.getInt();
73 VlanId vlanId = VlanId.vlanId(byteBuffer.getShort());
74 IpAddress srcIp = IpAddress.valueOf(Version.INET, getIpv4Octets(byteBuffer));
75 int srcPrefixLen = byteBuffer.get();
76 TpPort srcPort = TpPort.tpPort((int) byteBuffer.getShort());
77 IpAddress dstIp = IpAddress.valueOf(Version.INET, getIpv4Octets(byteBuffer));
78 int dstPrefixLen = byteBuffer.get();
79 TpPort dstPort = TpPort.tpPort((int) byteBuffer.getShort());
80
81 byte protocol = byteBuffer.get();
82 MacAddress srcMac = MacAddress.valueOf(getMacByteArray(byteBuffer));
83 MacAddress dstMac = MacAddress.valueOf(getMacByteArray(byteBuffer));
84
85 TinaStatsInfoByteBufferCodec statsInfoByteBufferCodec =
86 new TinaStatsInfoByteBufferCodec();
87 StatsInfo statsInfo = statsInfoByteBufferCodec.decode(byteBuffer);
88
89 return new DefaultFlowInfo.DefaultBuilder()
90 .withFlowType(flowType)
91 .withDeviceId(deviceId)
92 .withInputInterfaceId(inputInterfaceId)
93 .withOutputInterfaceId(outputInterfaceId)
94 .withVlanId(vlanId)
95 .withSrcIp(IpPrefix.valueOf(srcIp, srcPrefixLen))
96 .withSrcPort(srcPort)
97 .withDstIp(IpPrefix.valueOf(dstIp, dstPrefixLen))
98 .withDstPort(dstPort)
99 .withProtocol(protocol)
100 .withSrcMac(srcMac)
101 .withDstMac(dstMac)
102 .withStatsInfo(statsInfo)
103 .build();
104 }
105
106 /**
107 * Obtains IPv4 Octets from ByteBuffer.
108 *
109 * @param buffer byte buffer
110 * @return Ipv4 Octets
111 */
112 private byte[] getIpv4Octets(ByteBuffer buffer) {
113 byte[] octets = new byte[4];
114 for (int i = 0; i < octets.length; i++) {
115 octets[i] = buffer.get();
116 }
117 return octets;
118 }
119
120 /**
121 * Obtains MAC address byte array from ByteBuffer.
122 *
123 * @param buffer byte buffer
124 * @return MAC address byte array
125 */
126 private byte[] getMacByteArray(ByteBuffer buffer) {
127 byte[] array = new byte[6];
128 for (int i = 0; i < array.length; i++) {
129 array[i] = buffer.get();
130 }
131 return array;
132 }
133}