blob: 8b28797078cb0b38435aa21c0cdb9994c719c973 [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;
Jian Li0bbbb1c2018-06-22 22:01:17 +090038 private static final String OF_PREFIX = "of:";
Jian Li6bd35102018-06-09 00:18:47 +090039
40 @Override
41 public ByteBuffer encode(FlowInfo flowInfo) {
42
43 ByteBuffer byteBuffer = ByteBuffer.allocate(MESSAGE_SIZE);
44
Jian Li0bbbb1c2018-06-22 22:01:17 +090045 String deviceId = flowInfo.deviceId().toString();
46 short switchId = (short) Integer.parseInt(deviceId.substring(3,
47 deviceId.length()), 16);
48
Jian Li6bd35102018-06-09 00:18:47 +090049 byteBuffer.put(flowInfo.flowType())
Jian Li0bbbb1c2018-06-22 22:01:17 +090050 .putShort(switchId)
Jian Li6bd35102018-06-09 00:18:47 +090051 .putInt(flowInfo.inputInterfaceId())
52 .putInt(flowInfo.outputInterfaceId())
53 .putShort(flowInfo.vlanId().toShort())
54 .put(flowInfo.srcIp().address().toOctets())
55 .put((byte) flowInfo.srcIp().prefixLength())
56 .putShort((short) flowInfo.srcPort().toInt())
57 .put(flowInfo.dstIp().address().toOctets())
58 .put((byte) flowInfo.dstIp().prefixLength())
59 .putShort((short) flowInfo.dstPort().toInt())
60 .put(flowInfo.protocol())
61 .put(flowInfo.srcMac().toBytes())
62 .put(flowInfo.dstMac().toBytes());
63
64 TinaStatsInfoByteBufferCodec statsInfoByteBufferCodec =
65 new TinaStatsInfoByteBufferCodec();
66 byteBuffer.put(statsInfoByteBufferCodec.encode(flowInfo.statsInfo()).array());
67
68 return byteBuffer;
69 }
70
71 @Override
72 public FlowInfo decode(ByteBuffer byteBuffer) {
73
74 byte flowType = byteBuffer.get();
Jian Li0bbbb1c2018-06-22 22:01:17 +090075 String deviceIdStr = String.format("%016x", byteBuffer.getShort());
76 DeviceId deviceId = DeviceId.deviceId(OF_PREFIX + deviceIdStr);
Jian Li6bd35102018-06-09 00:18:47 +090077 int inputInterfaceId = byteBuffer.getInt();
78 int outputInterfaceId = byteBuffer.getInt();
79 VlanId vlanId = VlanId.vlanId(byteBuffer.getShort());
80 IpAddress srcIp = IpAddress.valueOf(Version.INET, getIpv4Octets(byteBuffer));
81 int srcPrefixLen = byteBuffer.get();
82 TpPort srcPort = TpPort.tpPort((int) byteBuffer.getShort());
83 IpAddress dstIp = IpAddress.valueOf(Version.INET, getIpv4Octets(byteBuffer));
84 int dstPrefixLen = byteBuffer.get();
85 TpPort dstPort = TpPort.tpPort((int) byteBuffer.getShort());
86
87 byte protocol = byteBuffer.get();
88 MacAddress srcMac = MacAddress.valueOf(getMacByteArray(byteBuffer));
89 MacAddress dstMac = MacAddress.valueOf(getMacByteArray(byteBuffer));
90
91 TinaStatsInfoByteBufferCodec statsInfoByteBufferCodec =
92 new TinaStatsInfoByteBufferCodec();
93 StatsInfo statsInfo = statsInfoByteBufferCodec.decode(byteBuffer);
94
95 return new DefaultFlowInfo.DefaultBuilder()
96 .withFlowType(flowType)
97 .withDeviceId(deviceId)
98 .withInputInterfaceId(inputInterfaceId)
99 .withOutputInterfaceId(outputInterfaceId)
100 .withVlanId(vlanId)
101 .withSrcIp(IpPrefix.valueOf(srcIp, srcPrefixLen))
102 .withSrcPort(srcPort)
103 .withDstIp(IpPrefix.valueOf(dstIp, dstPrefixLen))
104 .withDstPort(dstPort)
105 .withProtocol(protocol)
106 .withSrcMac(srcMac)
107 .withDstMac(dstMac)
108 .withStatsInfo(statsInfo)
109 .build();
110 }
111
112 /**
113 * Obtains IPv4 Octets from ByteBuffer.
114 *
115 * @param buffer byte buffer
116 * @return Ipv4 Octets
117 */
118 private byte[] getIpv4Octets(ByteBuffer buffer) {
119 byte[] octets = new byte[4];
120 for (int i = 0; i < octets.length; i++) {
121 octets[i] = buffer.get();
122 }
123 return octets;
124 }
125
126 /**
127 * Obtains MAC address byte array from ByteBuffer.
128 *
129 * @param buffer byte buffer
130 * @return MAC address byte array
131 */
132 private byte[] getMacByteArray(ByteBuffer buffer) {
133 byte[] array = new byte[6];
134 for (int i = 0; i < array.length; i++) {
135 array[i] = buffer.get();
136 }
137 return array;
138 }
139}