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.impl.deserialization.DeserializerRegistryImpl;
\r
21 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
\r
22 import org.opendaylight.openflowjava.util.ByteBufUtils;
\r
23 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidAction;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetVlanVid;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCase;
\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortStatsCase;
\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCase;
\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableCase;
\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.aggregate._case.MultipartReplyAggregate;
\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.MultipartReplyPortStats;
\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueue;
\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.MultipartReplyTable;
\r
46 * @author michal.polkorab
\r
49 public class OF10StatsReplyMessageFactoryTest {
\r
51 private OFDeserializer<MultipartReplyMessage> statsFactory;
\r
54 * Initializes deserializer registry and lookups correct deserializer
\r
57 public void startUp() {
\r
58 DeserializerRegistry registry = new DeserializerRegistryImpl();
\r
60 statsFactory = registry.getDeserializer(
\r
61 new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, 17, MultipartReplyMessage.class));
\r
65 * Testing OF10StatsReplyMessageFactory (Desc) for correct deserialization
\r
68 public void testDesc() {
\r
69 final int DESC_STR_LEN = 256;
\r
70 final int SERIAL_NUM_LEN = 32;
\r
71 ByteBuf bb = BufferHelper.buildBuffer("00 00 00 00");
\r
73 String mfrDesc = "Manufacturer description";
\r
74 byte[] mfrDescBytes = new byte[256];
\r
75 mfrDescBytes = mfrDesc.getBytes();
\r
76 bb.writeBytes(mfrDescBytes);
\r
77 ByteBufUtils.padBuffer((DESC_STR_LEN - mfrDescBytes.length), bb);
\r
79 String hwDesc = "Hardware description";
\r
80 byte[] hwDescBytes = new byte[256];
\r
81 hwDescBytes = hwDesc.getBytes();
\r
82 bb.writeBytes(hwDescBytes);
\r
83 ByteBufUtils.padBuffer((DESC_STR_LEN - hwDescBytes.length), bb);
\r
85 String swDesc = "Software description";
\r
86 byte[] swDescBytes = new byte[256];
\r
87 swDescBytes = swDesc.getBytes();
\r
88 bb.writeBytes(swDescBytes);
\r
89 ByteBufUtils.padBuffer((DESC_STR_LEN - swDescBytes.length), bb);
\r
91 String serialNum = "SN0123456789";
\r
92 byte[] serialNumBytes = new byte[32];
\r
93 serialNumBytes = serialNum.getBytes();
\r
94 bb.writeBytes(serialNumBytes);
\r
95 ByteBufUtils.padBuffer((SERIAL_NUM_LEN - serialNumBytes.length), bb);
\r
97 String dpDesc = "switch3 in room 3120";
\r
98 byte[] dpDescBytes = new byte[256];
\r
99 dpDescBytes = dpDesc.getBytes();
\r
100 bb.writeBytes(dpDescBytes);
\r
101 ByteBufUtils.padBuffer((DESC_STR_LEN - dpDescBytes.length), bb);
\r
103 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
105 BufferHelper.checkHeaderV10(builtByFactory);
\r
106 Assert.assertEquals("Wrong type", 0, builtByFactory.getType().getIntValue());
\r
107 Assert.assertEquals("Wrong flag", false, builtByFactory.getFlags().isOFPMPFREQMORE().booleanValue());
\r
108 MultipartReplyDescCase messageCase = (MultipartReplyDescCase) builtByFactory.getMultipartReplyBody();
\r
109 MultipartReplyDesc message = messageCase.getMultipartReplyDesc();
\r
110 Assert.assertEquals("Wrong mfrDesc", "Manufacturer description", message.getMfrDesc());
\r
111 Assert.assertEquals("Wrong hwDesc", "Hardware description", message.getHwDesc());
\r
112 Assert.assertEquals("Wrong swDesc", "Software description", message.getSwDesc());
\r
113 Assert.assertEquals("Wrong serialNum", "SN0123456789", message.getSerialNum());
\r
114 Assert.assertEquals("Wrong dpDesc", "switch3 in room 3120", message.getDpDesc());
\r
115 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
119 * Testing OF10StatsReplyMessageFactory (Flow) for correct deserialization
\r
122 public void testFlow() {
\r
123 ByteBuf bb = BufferHelper.buildBuffer("00 01 00 01 00 68 01 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 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
\r
126 + "00 00 00 02 00 00 00 03 00 04 00 05 00 06 00 00 00 00 00 00 "
\r
127 + "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
128 + "00 00 00 08 00 01 00 02 00 01 00 08 00 03 00 00");
\r
130 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
132 BufferHelper.checkHeaderV10(builtByFactory);
\r
133 Assert.assertEquals("Wrong type", 0x01, builtByFactory.getType().getIntValue());
\r
134 Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE().booleanValue());
\r
135 MultipartReplyFlowCase messageCase = (MultipartReplyFlowCase) builtByFactory.getMultipartReplyBody();
\r
136 MultipartReplyFlow message = messageCase.getMultipartReplyFlow();
\r
137 Assert.assertEquals("Wrong tableId", 1, message.getFlowStats().get(0).getTableId().intValue());
\r
138 Assert.assertEquals("Wrong durationSec", 2, message.getFlowStats().get(0).getDurationSec().intValue());
\r
139 Assert.assertEquals("Wrong durationNsec", 3, message.getFlowStats().get(0).getDurationNsec().intValue());
\r
140 Assert.assertEquals("Wrong priority", 4, message.getFlowStats().get(0).getPriority().intValue());
\r
141 Assert.assertEquals("Wrong idleTimeOut", 5, message.getFlowStats().get(0).getIdleTimeout().intValue());
\r
142 Assert.assertEquals("Wrong hardTimeOut", 6, message.getFlowStats().get(0).getHardTimeout().intValue());
\r
143 Assert.assertEquals("Wrong cookie",
\r
144 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}),
\r
145 message.getFlowStats().get(0).getCookie());
\r
146 Assert.assertEquals("Wrong packetCount",
\r
147 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}),
\r
148 message.getFlowStats().get(0).getPacketCount());
\r
149 Assert.assertEquals("Wrong byteCount",
\r
150 new BigInteger(1, new byte[]{(byte) 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20}),
\r
151 message.getFlowStats().get(0).getByteCount());
\r
152 Action action1 = message.getFlowStats().get(0).getAction().get(0);
\r
153 Assert.assertEquals("Wrong action type", Output.class, action1.getType());
\r
154 Assert.assertEquals("Wrong action port", 1, action1.getAugmentation(PortAction.class)
\r
155 .getPort().getValue().intValue());
\r
156 Assert.assertEquals("Wrong action port", 2, action1.getAugmentation(MaxLengthAction.class)
\r
157 .getMaxLength().intValue());
\r
158 Action action2 = message.getFlowStats().get(0).getAction().get(1);
\r
159 Assert.assertEquals("Wrong action type", SetVlanVid.class, action2.getType());
\r
160 Assert.assertEquals("Wrong action port", 3, action2.getAugmentation(VlanVidAction.class)
\r
161 .getVlanVid().intValue());
\r
162 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
166 * Testing OF10StatsReplyMessageFactory (Aggregate) for correct deserialization
\r
169 public void testAggregate() {
\r
170 ByteBuf bb = BufferHelper.buildBuffer("00 02 00 01 "
\r
171 + "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
173 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
175 BufferHelper.checkHeaderV10(builtByFactory);
\r
176 Assert.assertEquals("Wrong type", 0x02, builtByFactory.getType().getIntValue());
\r
177 Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE().booleanValue());
\r
178 MultipartReplyAggregateCase messageCase = (MultipartReplyAggregateCase) builtByFactory.getMultipartReplyBody();
\r
179 MultipartReplyAggregate message = messageCase.getMultipartReplyAggregate();
\r
180 Assert.assertEquals("Wrong packet-count",
\r
181 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}),
\r
182 message.getPacketCount());
\r
183 Assert.assertEquals("Wrong byte-count",
\r
184 new BigInteger(1, new byte[]{(byte) 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20}),
\r
185 message.getByteCount());
\r
186 Assert.assertEquals("Wrong flow-count", 48, message.getFlowCount().intValue());
\r
187 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
191 * Testing OF10StatsReplyMessageFactory (Table) for correct deserialization
\r
194 public void testTable() {
\r
195 ByteBuf bb = BufferHelper.buildBuffer("00 03 00 01 "
\r
196 + "08 00 00 00 4A 41 4D 45 53 20 42 4F 4E 44 00 00 00 00 00 00 00 00 00 "
\r
197 + "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
\r
198 + "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
200 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
202 BufferHelper.checkHeaderV10(builtByFactory);
\r
203 Assert.assertEquals("Wrong type", 0x03, builtByFactory.getType().getIntValue());
\r
204 Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE());
\r
206 MultipartReplyTableCase messageCase = (MultipartReplyTableCase) builtByFactory.getMultipartReplyBody();
\r
207 MultipartReplyTable message = messageCase.getMultipartReplyTable();
\r
208 Assert.assertEquals("Wrong tableId", 8, message.getTableStats().get(0).getTableId().intValue());
\r
209 Assert.assertEquals("Wrong name", "JAMES BOND", message.getTableStats().get(0).getName());
\r
210 Assert.assertEquals("Wrong wildcards", new FlowWildcardsV10(false, false, false, false, false, false, false,
\r
211 false, false, false), message.getTableStats().get(0).getWildcards());
\r
212 Assert.assertEquals("Wrong src-mask", 32, message.getTableStats().get(0).getNwSrcMask().intValue());
\r
213 Assert.assertEquals("Wrong dst-mask", 32, message.getTableStats().get(0).getNwDstMask().intValue());
\r
214 Assert.assertEquals("Wrong max-entries", 48, message.getTableStats().get(0).getMaxEntries().longValue());
\r
215 Assert.assertEquals("Wrong activeCount", 16, message.getTableStats().get(0).getActiveCount().longValue());
\r
216 Assert.assertEquals("Wrong lookupCount",
\r
217 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}),
\r
218 message.getTableStats().get(0).getLookupCount());
\r
219 Assert.assertEquals("Wrong matchedCount",
\r
220 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00}),
\r
221 message.getTableStats().get(0).getMatchedCount());
\r
222 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
226 * Testing OF10StatsReplyMessageFactory (Port) for correct deserialization
\r
229 public void testPort() {
\r
230 ByteBuf bb = BufferHelper.buildBuffer("00 04 00 01 "
\r
231 + "00 FF 00 00 00 00 00 00 "
\r
232 + "FF 01 01 01 01 01 01 01 FF 02 02 02 02 02 02 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 "
\r
236 + "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
237 + "FF 02 03 02 03 02 03 02");
\r
239 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
241 BufferHelper.checkHeaderV10(builtByFactory);
\r
242 Assert.assertEquals("Wrong type", 0x04, builtByFactory.getType().getIntValue());
\r
243 Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE());
\r
244 MultipartReplyPortStatsCase messageCase = (MultipartReplyPortStatsCase) builtByFactory.getMultipartReplyBody();
\r
245 MultipartReplyPortStats message = messageCase.getMultipartReplyPortStats();
\r
246 Assert.assertEquals("Wrong portNo", 255, message.getPortStats().get(0).getPortNo().intValue());
\r
247 Assert.assertEquals("Wrong rxPackets",
\r
248 new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}),
\r
249 message.getPortStats().get(0).getRxPackets());
\r
250 Assert.assertEquals("Wrong txPackets",
\r
251 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02}),
\r
252 message.getPortStats().get(0).getTxPackets());
\r
253 Assert.assertEquals("Wrong rxBytes",
\r
254 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
255 message.getPortStats().get(0).getRxBytes());
\r
256 Assert.assertEquals("Wrong txBytes",
\r
257 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
258 message.getPortStats().get(0).getTxBytes());
\r
259 Assert.assertEquals("Wrong rxDropped",
\r
260 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
261 message.getPortStats().get(0).getRxDropped());
\r
262 Assert.assertEquals("Wrong txDropped",
\r
263 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
264 message.getPortStats().get(0).getTxDropped());
\r
265 Assert.assertEquals("Wrong rxErrors",
\r
266 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
267 message.getPortStats().get(0).getRxErrors());
\r
268 Assert.assertEquals("Wrong txErrors",
\r
269 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
270 message.getPortStats().get(0).getTxErrors());
\r
271 Assert.assertEquals("Wrong rxFrameErr",
\r
272 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
273 message.getPortStats().get(0).getRxFrameErr());
\r
274 Assert.assertEquals("Wrong rxOverErr",
\r
275 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
276 message.getPortStats().get(0).getRxOverErr());
\r
277 Assert.assertEquals("Wrong rxCrcErr",
\r
278 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
279 message.getPortStats().get(0).getRxCrcErr());
\r
280 Assert.assertEquals("Wrong collisions",
\r
281 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
282 message.getPortStats().get(0).getCollisions());
\r
283 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r
287 * Testing OF10StatsReplyMessageFactory (Queue) for correct deserialization
\r
290 public void testQueue() {
\r
291 ByteBuf bb = BufferHelper.buildBuffer("00 05 00 00 "
\r
292 + "00 FF 00 00 00 00 00 10 "
\r
293 + "FF 02 03 02 03 02 03 02 "
\r
294 + "FF 02 02 02 02 02 02 02 "
\r
295 + "FF 02 03 02 03 02 03 02");
\r
297 MultipartReplyMessage builtByFactory = BufferHelper.deserialize(statsFactory, bb);
\r
299 BufferHelper.checkHeaderV10(builtByFactory);
\r
300 Assert.assertEquals("Wrong type", 0x05, builtByFactory.getType().getIntValue());
\r
301 Assert.assertEquals("Wrong flag", false, builtByFactory.getFlags().isOFPMPFREQMORE());
\r
302 MultipartReplyQueueCase messageCase = (MultipartReplyQueueCase) builtByFactory.getMultipartReplyBody();
\r
303 MultipartReplyQueue message = messageCase.getMultipartReplyQueue();
\r
304 Assert.assertEquals("Wrong portNo", 255, message.getQueueStats().get(0).getPortNo().intValue());
\r
305 Assert.assertEquals("Wrong queueId", 16, message.getQueueStats().get(0).getQueueId().intValue());
\r
306 Assert.assertEquals("Wrong txBytes",
\r
307 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
308 message.getQueueStats().get(0).getTxBytes());
\r
309 Assert.assertEquals("Wrong txPackets",
\r
310 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02}),
\r
311 message.getQueueStats().get(0).getTxPackets());
\r
312 Assert.assertEquals("Wrong txErrors",
\r
313 new BigInteger(1, new byte[]{(byte) 0xFF, 0x02, 0x03, 0x02, 0x03, 0x02, 0x03, 0x02}),
\r
314 message.getQueueStats().get(0).getTxErrors());
\r
315 Assert.assertTrue("Unread data", bb.readableBytes() == 0);
\r