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