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