Extensibility support (deserialization part)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF10MatchDeserializerTest.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
9 package org.opendaylight.openflowjava.protocol.impl.util;
10
11 import io.netty.buffer.ByteBuf;
12
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
19 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
24
25 /**
26  * @author michal.polkorab
27  *
28  */
29 public class OF10MatchDeserializerTest {
30
31     private OFDeserializer<MatchV10> matchDeserializer;
32
33     /**
34      * Initializes deserializer registry and lookups correct deserializer
35      */
36     @Before
37     public void startUp() {
38         DeserializerRegistry registry = new DeserializerRegistryImpl();
39         registry.init();
40         matchDeserializer = registry.getDeserializer(
41                 new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, EncodeConstants.EMPTY_VALUE, MatchV10.class));
42     }
43
44     /**
45      * Testing correct deserialization of ofp_match
46      */
47     @Test
48     public void test() {
49         ByteBuf message = BufferHelper.buildBuffer("00 24 08 91 00 20 AA BB CC DD EE FF "
50                 + "AA BB CC DD EE FF 00 05 10 00 00 08 07 06 00 00 10 11 12 13 01 02 03 04 "
51                 + "50 50 20 20");
52         message.skipBytes(4); // skip XID
53         MatchV10 match = matchDeserializer.deserialize(message);
54         Assert.assertEquals("Wrong wildcards", new FlowWildcardsV10(false, false, true, false,
55                 false, true, false, true, true, false), match.getWildcards());
56         Assert.assertEquals("Wrong srcMask", 24, match.getNwSrcMask().shortValue());
57         Assert.assertEquals("Wrong dstMask", 16, match.getNwDstMask().shortValue());
58         Assert.assertEquals("Wrong in-port", 32, match.getInPort().intValue());
59         Assert.assertEquals("Wrong dl-src", new MacAddress("AA:BB:CC:DD:EE:FF"), match.getDlSrc());
60         Assert.assertEquals("Wrong dl-dst", new MacAddress("AA:BB:CC:DD:EE:FF"), match.getDlDst());
61         Assert.assertEquals("Wrong dl-vlan", 5, match.getDlVlan().intValue());
62         Assert.assertEquals("Wrong dl-vlan-pcp", 16, match.getDlVlanPcp().shortValue());
63         Assert.assertEquals("Wrong dl-type", 8, match.getDlType().intValue());
64         Assert.assertEquals("Wrong nw-tos", 7, match.getNwTos().shortValue());
65         Assert.assertEquals("Wrong nw-proto", 6, match.getNwProto().shortValue());
66         Assert.assertEquals("Wrong nw-src", new Ipv4Address("16.17.18.19"), match.getNwSrc());
67         Assert.assertEquals("Wrong nw-dst", new Ipv4Address("1.2.3.4"), match.getNwDst());
68         Assert.assertEquals("Wrong tp-src", 20560, match.getTpSrc().shortValue());
69         Assert.assertEquals("Wrong tp-dst", 8224, match.getTpDst().shortValue());
70     }
71     
72     /**
73      * Testing correct deserialization of ofp_match
74      */
75     @Test
76     public void test2() {
77         ByteBuf message = BufferHelper.buildBuffer("00 3F FF FF 00 20 AA BB CC DD EE FF "
78                 + "AA BB CC DD EE FF 00 05 10 00 00 08 07 06 00 00 10 11 12 13 01 02 03 04 "
79                 + "50 50 20 20");
80         message.skipBytes(4); // skip XID
81         MatchV10 match = matchDeserializer.deserialize(message);
82         Assert.assertEquals("Wrong wildcards", new FlowWildcardsV10(true, true, true, true,
83                 true, true, true, true, true, true), match.getWildcards());
84         Assert.assertEquals("Wrong srcMask", 0, match.getNwSrcMask().shortValue());
85         Assert.assertEquals("Wrong dstMask", 0, match.getNwDstMask().shortValue());
86         Assert.assertEquals("Wrong in-port", 32, match.getInPort().intValue());
87         Assert.assertEquals("Wrong dl-src", new MacAddress("AA:BB:CC:DD:EE:FF"), match.getDlSrc());
88         Assert.assertEquals("Wrong dl-dst", new MacAddress("AA:BB:CC:DD:EE:FF"), match.getDlDst());
89         Assert.assertEquals("Wrong dl-vlan", 5, match.getDlVlan().intValue());
90         Assert.assertEquals("Wrong dl-vlan-pcp", 16, match.getDlVlanPcp().shortValue());
91         Assert.assertEquals("Wrong dl-type", 8, match.getDlType().intValue());
92         Assert.assertEquals("Wrong nw-tos", 7, match.getNwTos().shortValue());
93         Assert.assertEquals("Wrong nw-proto", 6, match.getNwProto().shortValue());
94         Assert.assertEquals("Wrong nw-src", new Ipv4Address("16.17.18.19"), match.getNwSrc());
95         Assert.assertEquals("Wrong nw-dst", new Ipv4Address("1.2.3.4"), match.getNwDst());
96         Assert.assertEquals("Wrong tp-src", 20560, match.getTpSrc().shortValue());
97         Assert.assertEquals("Wrong tp-dst", 8224, match.getTpDst().shortValue());
98     }
99
100 }