9091dbcc1552c6313746c1a240377600a5fded93
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / multipart / MultipartReplyFlowTest.java
1 /*
2  * Copyright (c) 2014 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.multipart;
9
10 import io.netty.buffer.ByteBuf;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
19 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.MultipartReplyMessageFactory;
20 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
27 import org.opendaylight.yangtools.yang.common.Uint64;
28
29 /**
30  * Unit tests for MultipartReplyFlow.
31  *
32  * @author michal.polkorab
33  */
34 public class MultipartReplyFlowTest {
35
36     private OFDeserializer<MultipartReplyMessage> factory;
37
38     /**
39      * Initializes deserializer registry and lookups correct deserializer.
40      */
41     @Before
42     public void startUp() {
43         DeserializerRegistry registry = new DeserializerRegistryImpl();
44         registry.init();
45         factory = registry.getDeserializer(
46                 new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, 19, MultipartReplyMessage.class));
47     }
48
49     /**
50      * Testing {@link MultipartReplyMessageFactory} for correct translation into POJO.
51      */
52     @Test
53     public void testEmptyMultipartReplyFlowBody() {
54         ByteBuf bb = BufferHelper.buildBuffer("00 01 00 01 00 00 00 00");
55         MultipartReplyMessage builtByFactory = BufferHelper.deserialize(factory, bb);
56
57         BufferHelper.checkHeaderV13(builtByFactory);
58         Assert.assertEquals("Wrong type", 0x01, builtByFactory.getType().getIntValue());
59         Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE());
60         MultipartReplyFlowCase messageCase = (MultipartReplyFlowCase) builtByFactory.getMultipartReplyBody();
61         MultipartReplyFlow message = messageCase.getMultipartReplyFlow();
62         Assert.assertEquals("Wrong flow stats size", 0, message.getFlowStats().size());
63     }
64
65     /**
66      * Testing {@link MultipartReplyMessageFactory} for correct translation into POJO.
67      */
68     @Test
69     public void testMultipartReplyFlowBody() {
70         ByteBuf bb = BufferHelper.buildBuffer("00 01 00 01 00 00 00 00 " + //
71                                               // first flow stat
72                                               "00 48 08 00 " + // length, tableId, padding
73                                               "00 00 00 09 " + //durationSec
74                                               "00 00 00 07 " + //durationNsec
75                                               "00 0C 00 0E 00 0F 00 1F " + //priority, idleTimeout, hardTimeout, flags
76                                               "00 00 00 00 " + //pad_02
77                                               "FF 01 01 01 01 01 01 01 " + //cookie
78                                               "EF 01 01 01 01 01 01 01 " + //packetCount
79                                               "7F 01 01 01 01 01 01 01 " + //byteCount
80                                               "00 01 00 04 00 00 00 00 " + //empty match
81                                               "00 01 00 08 06 00 00 00 " + //
82                                               "00 01 00 08 06 00 00 00 " + //
83                                               // second flow stat
84                                               "00 48 08 00 " + // length, tableId, padding
85                                               "00 00 00 09 " + //durationSec
86                                               "00 00 00 07 " + //durationNsec
87                                               "00 0C 00 0E 00 0F 00 00 " + // priority, idleTimeout, hardTimeout, flags
88                                               "00 00 00 00 " + //pad_02
89                                               "FF 01 01 01 01 01 01 01 " + //cookie
90                                               "EF 01 01 01 01 01 01 01 " + //packetCount
91                                               "7F 01 01 01 01 01 01 01 " + //byteCount
92                                               "00 01 00 04 00 00 00 00 " + //empty match
93                                               "00 01 00 08 06 00 00 00 " + //
94                                               "00 01 00 08 06 00 00 00");
95         MultipartReplyMessage builtByFactory = BufferHelper.deserialize(factory, bb);
96
97         BufferHelper.checkHeaderV13(builtByFactory);
98         Assert.assertEquals("Wrong type", 0x01, builtByFactory.getType().getIntValue());
99         Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE());
100         MultipartReplyFlowCase messageCase = (MultipartReplyFlowCase) builtByFactory.getMultipartReplyBody();
101         MultipartReplyFlow message = messageCase.getMultipartReplyFlow();
102         Assert.assertEquals("Wrong flow stats size", 2, message.getFlowStats().size());
103         FlowStats flowStats1 = message.getFlowStats().get(0);
104         Assert.assertEquals("Wrong tableId", 8, flowStats1.getTableId().intValue());
105         Assert.assertEquals("Wrong durationSec", 9, flowStats1.getDurationSec().intValue());
106         Assert.assertEquals("Wrong durationNsec", 7, flowStats1.getDurationNsec().intValue());
107         Assert.assertEquals("Wrong priority", 12, flowStats1.getPriority().intValue());
108         Assert.assertEquals("Wrong idleTimeOut", 14, flowStats1.getIdleTimeout().intValue());
109         Assert.assertEquals("Wrong hardTimeOut", 15, flowStats1.getHardTimeout().intValue());
110         Assert.assertEquals("Wrong flags", new FlowModFlags(true, true, true, true, true), flowStats1.getFlags());
111         Assert.assertEquals("Wrong cookie", Uint64.valueOf("FF01010101010101", 16), flowStats1.getCookie());
112         Assert.assertEquals("Wrong packetCount",  Uint64.valueOf("EF01010101010101", 16), flowStats1.getPacketCount());
113         Assert.assertEquals("Wrong byteCount",  Uint64.valueOf("7F01010101010101", 16), flowStats1.getByteCount());
114         Assert.assertEquals("Wrong match type", OxmMatchType.class, flowStats1.getMatch().getType());
115         flowStats1 = message.getFlowStats().get(1);
116         Assert.assertEquals("Wrong tableId", 8, flowStats1.getTableId().intValue());
117         Assert.assertEquals("Wrong durationSec", 9, flowStats1.getDurationSec().intValue());
118         Assert.assertEquals("Wrong durationNsec", 7, flowStats1.getDurationNsec().intValue());
119         Assert.assertEquals("Wrong priority", 12, flowStats1.getPriority().intValue());
120         Assert.assertEquals("Wrong idleTimeOut", 14, flowStats1.getIdleTimeout().intValue());
121         Assert.assertEquals("Wrong hardTimeOut", 15, flowStats1.getHardTimeout().intValue());
122         Assert.assertEquals("Wrong flags", new FlowModFlags(false, false, false, false, false), flowStats1.getFlags());
123         Assert.assertEquals("Wrong cookie", Uint64.valueOf("FF01010101010101", 16), flowStats1.getCookie());
124         Assert.assertEquals("Wrong packetCount", Uint64.valueOf("EF01010101010101", 16), flowStats1.getPacketCount());
125         Assert.assertEquals("Wrong byteCount", Uint64.valueOf("7F01010101010101", 16), flowStats1.getByteCount());
126         Assert.assertEquals("Wrong match type", OxmMatchType.class, flowStats1.getMatch().getType());
127     }
128 }