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