2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
\r
10 import io.netty.buffer.ByteBuf;
\r
12 import java.math.BigInteger;
\r
14 import org.junit.Assert;
\r
15 import org.junit.Before;
\r
16 import org.junit.Test;
\r
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
\r
18 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;
\r
19 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
\r
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
\r
21 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
\r
22 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidAction;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetVlanVid;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase;
\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortStatsCase;
\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCase;
\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableCase;
\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.aggregate._case.MultipartReplyAggregate;
\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.MultipartReplyPortStats;
\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueue;
\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.MultipartReplyTable;
\r
45 * @author michal.polkorab
\r
48 public class OF10StatsReplyMessageFactoryTest {
\r
50 private OFDeserializer<MultipartReplyMessage> statsFactory;
\r
53 * Initializes deserializer registry and lookups correct deserializer
\r
56 public void startUp() {
\r
57 DeserializerRegistry registry = new DeserializerRegistryImpl();
\r
59 statsFactory = registry.getDeserializer(
\r
60 new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, 17, MultipartReplyMessage.class));
\r
64 * Testing OF10StatsReplyMessageFactory (Desc) for correct deserialization
\r
67 public void testDesc() {
\r
68 final int DESC_STR_LEN = 256;
\r
69 final int SERIAL_NUM_LEN = 32;
\r
70 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 00");
\r
72 String mfrDesc = "Manufacturer description";
\r
73 byte[] mfrDescBytes = new byte[256];
\r
74 mfrDescBytes = mfrDesc.getBytes();
\r
75 bb.writeBytes(mfrDescBytes);
\r
76 bb.writeZero(DESC_STR_LEN - mfrDescBytes.length);
\r
78 String hwDesc = "Hardware description";
\r
79 byte[] hwDescBytes = new byte[256];
\r
80 hwDescBytes = hwDesc.getBytes();
\r
81 bb.writeBytes(hwDescBytes);
\r
82 bb.writeZero(DESC_STR_LEN - hwDescBytes.length);
\r
84 String swDesc = "Software description";
\r
85 byte[] swDescBytes = new byte[256];
\r
86 swDescBytes = swDesc.getBytes();
\r
87 bb.writeBytes(swDescBytes);
\r
88 bb.writeZero(DESC_STR_LEN - swDescBytes.length);
\r
90 String serialNum = "SN0123456789";
\r
91 byte[] serialNumBytes = new byte[32];
\r
92 serialNumBytes = serialNum.getBytes();
\r
93 bb.writeBytes(serialNumBytes);
\r
94 bb.writeZero(SERIAL_NUM_LEN - serialNumBytes.length);
\r
96 String dpDesc = "switch3 in room 3120";
\r
97 byte[] dpDescBytes = new byte[256];
\r
98 dpDescBytes = dpDesc.getBytes();
\r
99 bb.writeBytes(dpDescBytes);
\r
100 bb.writeZero(DESC_STR_LEN - dpDescBytes.length);
\r
102 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
104 BufferHelper.checkHeaderV10(builtByFactory);
\r
105 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().getIntValue());
\r
106 Assert.assertEquals("Wrong flag", false, builtByFactory.getFlags().isOFPMPFREQMORE().booleanValue());
\r
107 MultipartReplyDescCase messageCase = (MultipartReplyDescCase) builtByFactory.getMultipartReplyBody();
\r
108 MultipartReplyDesc message = messageCase.getMultipartReplyDesc();
\r
109 Assert.assertEquals("Wrong mfrDesc", "Manufacturer description", message.getMfrDesc());
\r
110 Assert.assertEquals("Wrong hwDesc", "Hardware description", message.getHwDesc());
\r
111 Assert.assertEquals("Wrong swDesc", "Software description", message.getSwDesc());
\r
112 Assert.assertEquals("Wrong serialNum", "SN0123456789", message.getSerialNum());
\r
113 Assert.assertEquals("Wrong dpDesc", "switch3 in room 3120", message.getDpDesc());
\r
114 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
118 * Testing OF10StatsReplyMessageFactory (Flow) for correct deserialization
\r
121 public void testFlow() {
\r
122 ByteBuf bb = BufferHelper.buildBuffer("00 01 00 01 00 68 01 00 "
\r
123 + "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
\r
124 + "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
\r
125 + "00 00 00 02 00 00 00 03 00 04 00 05 00 06 00 00 00 00 00 00 "
\r
126 + "FF 01 02 03 04 05 06 07 FF 01 02 03 04 05 06 07 FF 00 00 00 00 00 00 20 "
\r
127 + "00 00 00 08 00 01 00 02 00 01 00 08 00 03 00 00");
\r
129 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
131 BufferHelper.checkHeaderV10(builtByFactory);
\r
132 Assert.assertEquals("Wrong type", 0x01, builtByFactory.getType().getIntValue());
\r
133 Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE().booleanValue());
\r
134 MultipartReplyFlowCase messageCase = (MultipartReplyFlowCase) builtByFactory.getMultipartReplyBody();
\r
135 MultipartReplyFlow message = messageCase.getMultipartReplyFlow();
\r
136 Assert.assertEquals("Wrong tableId", 1, message.getFlowStats().get(0).getTableId().intValue());
\r
137 Assert.assertEquals("Wrong durationSec", 2, message.getFlowStats().get(0).getDurationSec().intValue());
\r
138 Assert.assertEquals("Wrong durationNsec", 3, message.getFlowStats().get(0).getDurationNsec().intValue());
\r
139 Assert.assertEquals("Wrong priority", 4, message.getFlowStats().get(0).getPriority().intValue());
\r
140 Assert.assertEquals("Wrong idleTimeOut", 5, message.getFlowStats().get(0).getIdleTimeout().intValue());
\r
141 Assert.assertEquals("Wrong hardTimeOut", 6, message.getFlowStats().get(0).getHardTimeout().intValue());
\r
142 Assert.assertEquals("Wrong cookie",
\r
143 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}),
\r
144 message.getFlowStats().get(0).getCookie());
\r
145 Assert.assertEquals("Wrong packetCount",
\r
146 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}),
\r
147 message.getFlowStats().get(0).getPacketCount());
\r
148 Assert.assertEquals("Wrong byteCount",
\r
149 new BigInteger(1, new byte[]{(byte) 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20}),
\r
150 message.getFlowStats().get(0).getByteCount());
\r
151 Action action1 = message.getFlowStats().get(0).getAction().get(0);
\r
152 Assert.assertEquals("Wrong action type", Output.class, action1.getType());
\r
153 Assert.assertEquals("Wrong action port", 1, action1.getAugmentation(PortAction.class)
\r
154 .getPort().getValue().intValue());
\r
155 Assert.assertEquals("Wrong action port", 2, action1.getAugmentation(MaxLengthAction.class)
\r
156 .getMaxLength().intValue());
\r
157 Action action2 = message.getFlowStats().get(0).getAction().get(1);
\r
158 Assert.assertEquals("Wrong action type", SetVlanVid.class, action2.getType());
\r
159 Assert.assertEquals("Wrong action port", 3, action2.getAugmentation(VlanVidAction.class)
\r
160 .getVlanVid().intValue());
\r
161 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
165 * Testing OF10StatsReplyMessageFactory (Aggregate) for correct deserialization
\r
168 public void testAggregate() {
\r
169 ByteBuf bb = BufferHelper.buildBuffer("00 02 00 01 "
\r
170 + "FF 01 02 03 04 05 06 07 FF 00 00 00 00 00 00 20 00 00 00 30 00 00 00 00");
\r
172 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
174 BufferHelper.checkHeaderV10(builtByFactory);
\r
175 Assert.assertEquals("Wrong type", 0x02, builtByFactory.getType().getIntValue());
\r
176 Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE().booleanValue());
\r
177 MultipartReplyAggregateCase messageCase = (MultipartReplyAggregateCase) builtByFactory.getMultipartReplyBody();
\r
178 MultipartReplyAggregate message = messageCase.getMultipartReplyAggregate();
\r
179 Assert.assertEquals("Wrong packet-count",
\r
180 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}),
\r
181 message.getPacketCount());
\r
182 Assert.assertEquals("Wrong byte-count",
\r
183 new BigInteger(1, new byte[]{(byte) 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20}),
\r
184 message.getByteCount());
\r
185 Assert.assertEquals("Wrong flow-count", 48, message.getFlowCount().intValue());
\r
186 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
190 * Testing OF10StatsReplyMessageFactory (Table) for correct deserialization
\r
193 public void testTable() {
\r
194 ByteBuf bb = BufferHelper.buildBuffer("00 03 00 01 "
\r
195 + "08 00 00 00 4A 41 4D 45 53 20 42 4F 4E 44 00 00 00 00 00 00 00 00 00 "
\r
196 + "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
\r
197 + "00 00 00 30 00 00 00 10 FF 01 01 01 01 01 01 01 FF 01 01 01 01 01 01 00");
\r
199 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
201 BufferHelper.checkHeaderV10(builtByFactory);
\r
202 Assert.assertEquals("Wrong type", 0x03, builtByFactory.getType().getIntValue());
\r
203 Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE());
\r
205 MultipartReplyTableCase messageCase = (MultipartReplyTableCase) builtByFactory.getMultipartReplyBody();
\r
206 MultipartReplyTable message = messageCase.getMultipartReplyTable();
\r
207 Assert.assertEquals("Wrong tableId", 8, message.getTableStats().get(0).getTableId().intValue());
\r
208 Assert.assertEquals("Wrong name", "JAMES BOND", message.getTableStats().get(0).getName());
\r
209 Assert.assertEquals("Wrong wildcards", new FlowWildcardsV10(false, false, false, false, false, false, false,
\r
210 false, false, false), message.getTableStats().get(0).getWildcards());
\r
211 Assert.assertEquals("Wrong src-mask", 32, message.getTableStats().get(0).getNwSrcMask().intValue());
\r
212 Assert.assertEquals("Wrong dst-mask", 32, message.getTableStats().get(0).getNwDstMask().intValue());
\r
213 Assert.assertEquals("Wrong max-entries", 48, message.getTableStats().get(0).getMaxEntries().longValue());
\r
214 Assert.assertEquals("Wrong activeCount", 16, message.getTableStats().get(0).getActiveCount().longValue());
\r
215 Assert.assertEquals("Wrong lookupCount",
\r
216 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}),
\r
217 message.getTableStats().get(0).getLookupCount());
\r
218 Assert.assertEquals("Wrong matchedCount",
\r
219 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00}),
\r
220 message.getTableStats().get(0).getMatchedCount());
\r
221 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
225 * Testing OF10StatsReplyMessageFactory (Port) for correct deserialization
\r
228 public void testPort() {
\r
229 ByteBuf bb = BufferHelper.buildBuffer("00 04 00 01 "
\r
230 + "00 FF 00 00 00 00 00 00 "
\r
231 + "FF 01 01 01 01 01 01 01 FF 02 02 02 02 02 02 02 "
\r
232 + "FF 02 03 02 03 02 03 02 FF 02 03 02 03 02 03 02 "
\r
233 + "FF 02 03 02 03 02 03 02 FF 02 03 02 03 02 03 02 "
\r
234 + "FF 02 03 02 03 02 03 02 FF 02 03 02 03 02 03 02 "
\r
235 + "FF 02 03 02 03 02 03 02 FF 02 03 02 03 02 03 02 FF 02 03 02 03 02 03 02 "
\r
236 + "FF 02 03 02 03 02 03 02");
\r
238 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
240 BufferHelper.checkHeaderV10(builtByFactory);
\r
241 Assert.assertEquals("Wrong type", 0x04, builtByFactory.getType().getIntValue());
\r
242 Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE());
\r
243 MultipartReplyPortStatsCase messageCase = (MultipartReplyPortStatsCase) builtByFactory.getMultipartReplyBody();
\r
244 MultipartReplyPortStats message = messageCase.getMultipartReplyPortStats();
\r
245 Assert.assertEquals("Wrong portNo", 255, message.getPortStats().get(0).getPortNo().intValue());
\r
246 Assert.assertEquals("Wrong rxPackets",
\r
247 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}),
\r
248 message.getPortStats().get(0).getRxPackets());
\r
249 Assert.assertEquals("Wrong txPackets",
\r
250 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02}),
\r
251 message.getPortStats().get(0).getTxPackets());
\r
252 Assert.assertEquals("Wrong rxBytes",
\r
253 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
254 message.getPortStats().get(0).getRxBytes());
\r
255 Assert.assertEquals("Wrong txBytes",
\r
256 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
257 message.getPortStats().get(0).getTxBytes());
\r
258 Assert.assertEquals("Wrong rxDropped",
\r
259 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
260 message.getPortStats().get(0).getRxDropped());
\r
261 Assert.assertEquals("Wrong txDropped",
\r
262 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
263 message.getPortStats().get(0).getTxDropped());
\r
264 Assert.assertEquals("Wrong rxErrors",
\r
265 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
266 message.getPortStats().get(0).getRxErrors());
\r
267 Assert.assertEquals("Wrong txErrors",
\r
268 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
269 message.getPortStats().get(0).getTxErrors());
\r
270 Assert.assertEquals("Wrong rxFrameErr",
\r
271 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
272 message.getPortStats().get(0).getRxFrameErr());
\r
273 Assert.assertEquals("Wrong rxOverErr",
\r
274 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
275 message.getPortStats().get(0).getRxOverErr());
\r
276 Assert.assertEquals("Wrong rxCrcErr",
\r
277 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
278 message.getPortStats().get(0).getRxCrcErr());
\r
279 Assert.assertEquals("Wrong collisions",
\r
280 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
281 message.getPortStats().get(0).getCollisions());
\r
282 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
286 * Testing OF10StatsReplyMessageFactory (Queue) for correct deserialization
\r
289 public void testQueue() {
\r
290 ByteBuf bb = BufferHelper.buildBuffer("00 05 00 00 "
\r
291 + "00 FF 00 00 00 00 00 10 "
\r
292 + "FF 02 03 02 03 02 03 02 "
\r
293 + "FF 02 02 02 02 02 02 02 "
\r
294 + "FF 02 03 02 03 02 03 02");
\r
296 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
298 BufferHelper.checkHeaderV10(builtByFactory);
\r
299 Assert.assertEquals("Wrong type", 0x05, builtByFactory.getType().getIntValue());
\r
300 Assert.assertEquals("Wrong flag", false, builtByFactory.getFlags().isOFPMPFREQMORE());
\r
301 MultipartReplyQueueCase messageCase = (MultipartReplyQueueCase) builtByFactory.getMultipartReplyBody();
\r
302 MultipartReplyQueue message = messageCase.getMultipartReplyQueue();
\r
303 Assert.assertEquals("Wrong portNo", 255, message.getQueueStats().get(0).getPortNo().intValue());
\r
304 Assert.assertEquals("Wrong queueId", 16, message.getQueueStats().get(0).getQueueId().intValue());
\r
305 Assert.assertEquals("Wrong txBytes",
\r
306 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
307 message.getQueueStats().get(0).getTxBytes());
\r
308 Assert.assertEquals("Wrong txPackets",
\r
309 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02}),
\r
310 message.getQueueStats().get(0).getTxPackets());
\r
311 Assert.assertEquals("Wrong txErrors",
\r
312 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
313 message.getQueueStats().get(0).getTxErrors());
\r
314 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r