Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / OF10MatchSerializerTest.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 import io.netty.buffer.UnpooledByteBufAllocator;
13 import junit.framework.Assert;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10Builder;
26
27 /**
28  * @author michal.polkorab
29  *
30  */
31 public class OF10MatchSerializerTest {
32
33     private SerializerRegistry registry;
34     private OFSerializer<MatchV10> matchSerializer;
35
36     /**
37      * Initializes serializer table and stores correct factory in field
38      */
39     @Before
40     public void startUp() {
41         registry = new SerializerRegistryImpl();
42         registry.init();
43         matchSerializer = registry.getSerializer(
44                 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, MatchV10.class));
45     }
46
47     /**
48      * Testing correct serialization of ofp_match
49      */
50     @Test
51     public void test() {
52         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
53         MatchV10Builder builder = new MatchV10Builder();
54         builder.setWildcards(new FlowWildcardsV10(false, false, true, false,
55                 false, true, false, true, true, true));
56         builder.setNwSrcMask((short) 24);
57         builder.setNwDstMask((short) 16);
58         builder.setInPort(6653);
59         builder.setDlSrc(new MacAddress("01:01:01:01:01:01"));
60         builder.setDlDst(new MacAddress("02:02:02:02:02:02"));
61         builder.setDlVlan(128);
62         builder.setDlVlanPcp((short) 2);
63         builder.setDlType(15);
64         builder.setNwTos((short) 14);
65         builder.setNwProto((short) 85);
66         builder.setNwSrc(new Ipv4Address("1.1.1.2"));
67         builder.setNwDst(new Ipv4Address("32.16.8.1"));
68         builder.setTpSrc(2048);
69         builder.setTpDst(4096);
70         MatchV10 match = builder.build();
71         matchSerializer.serialize(match, out);
72         
73         Assert.assertEquals("Wrong wildcards", 2361553, out.readUnsignedInt());
74         Assert.assertEquals("Wrong in-port", 6653, out.readUnsignedShort());
75         byte[] dlSrc = new byte[6];
76         out.readBytes(dlSrc);
77         Assert.assertEquals("Wrong dl-src", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
78         byte[] dlDst = new byte[6];
79         out.readBytes(dlDst);
80         Assert.assertEquals("Wrong dl-dst", "02:02:02:02:02:02", ByteBufUtils.macAddressToString(dlDst));
81         Assert.assertEquals("Wrong dl-vlan", 128, out.readUnsignedShort());
82         Assert.assertEquals("Wrong dl-vlan-pcp", 2, out.readUnsignedByte());
83         out.skipBytes(1);
84         Assert.assertEquals("Wrong dl-type", 15, out.readUnsignedShort());
85         Assert.assertEquals("Wrong nw-tos", 14, out.readUnsignedByte());
86         Assert.assertEquals("Wrong nw-proto", 85, out.readUnsignedByte());
87         out.skipBytes(2);
88         Assert.assertEquals("Wrong nw-src", 16843010, out.readUnsignedInt());
89         Assert.assertEquals("Wrong nw-dst", 537921537, out.readUnsignedInt());
90         Assert.assertEquals("Wrong tp-src", 2048, out.readUnsignedShort());
91         Assert.assertEquals("Wrong tp-dst", 4096, out.readUnsignedShort());
92     }
93     
94     /**
95      * Testing correct serialization of ofp_match
96      */
97     @Test
98     public void test2() {
99         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
100         MatchV10Builder builder = new MatchV10Builder();
101         builder.setWildcards(new FlowWildcardsV10(true, true, true, true,
102                 true, true, true, true, true, true));
103         builder.setNwSrcMask((short) 0);
104         builder.setNwDstMask((short) 0);
105         builder.setInPort(6653);
106         builder.setDlSrc(new MacAddress("01:01:01:01:01:01"));
107         builder.setDlDst(new MacAddress("02:02:02:02:02:02"));
108         builder.setDlVlan(128);
109         builder.setDlVlanPcp((short) 2);
110         builder.setDlType(15);
111         builder.setNwTos((short) 14);
112         builder.setNwProto((short) 85);
113         builder.setNwSrc(new Ipv4Address("1.1.1.2"));
114         builder.setNwDst(new Ipv4Address("32.16.8.1"));
115         builder.setTpSrc(2048);
116         builder.setTpDst(4096);
117         MatchV10 match = builder.build();
118         matchSerializer.serialize(match, out);
119         
120         Assert.assertEquals("Wrong wildcards", 3678463, out.readUnsignedInt());
121         Assert.assertEquals("Wrong in-port", 6653, out.readUnsignedShort());
122         byte[] dlSrc = new byte[6];
123         out.readBytes(dlSrc);
124         Assert.assertEquals("Wrong dl-src", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
125         byte[] dlDst = new byte[6];
126         out.readBytes(dlDst);
127         Assert.assertEquals("Wrong dl-dst", "02:02:02:02:02:02", ByteBufUtils.macAddressToString(dlDst));
128         Assert.assertEquals("Wrong dl-vlan", 128, out.readUnsignedShort());
129         Assert.assertEquals("Wrong dl-vlan-pcp", 2, out.readUnsignedByte());
130         out.skipBytes(1);
131         Assert.assertEquals("Wrong dl-type", 15, out.readUnsignedShort());
132         Assert.assertEquals("Wrong nw-tos", 14, out.readUnsignedByte());
133         Assert.assertEquals("Wrong nw-proto", 85, out.readUnsignedByte());
134         out.skipBytes(2);
135         Assert.assertEquals("Wrong nw-src", 16843010, out.readUnsignedInt());
136         Assert.assertEquals("Wrong nw-dst", 537921537, out.readUnsignedInt());
137         Assert.assertEquals("Wrong tp-src", 2048, out.readUnsignedShort());
138         Assert.assertEquals("Wrong tp-dst", 4096, out.readUnsignedShort());
139     }
140 }