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