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