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