Bug 2756 - Match model update
[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
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
22 import org.opendaylight.openflowjava.util.ByteBufUtils;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
28
29 /**
30  * @author michal.polkorab
31  *
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.OF10_VERSION_ID, MatchV10.class));
47     }
48
49     /**
50      * Testing correct serialization of ofp_match
51      */
52     @Test
53     public void test() {
54         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((short) 24);
59         builder.setNwDstMask((short) 16);
60         builder.setInPort(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(128);
64         builder.setDlVlanPcp((short) 2);
65         builder.setDlType(15);
66         builder.setNwTos((short) 14);
67         builder.setNwProto((short) 85);
68         builder.setNwSrc(new Ipv4Address("1.1.1.2"));
69         builder.setNwDst(new Ipv4Address("32.16.8.1"));
70         builder.setTpSrc(2048);
71         builder.setTpDst(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.assertEquals("Wrong dl-src", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
80         byte[] dlDst = new byte[6];
81         out.readBytes(dlDst);
82         Assert.assertEquals("Wrong dl-dst", "02:02:02:02:02:02", ByteBufUtils.macAddressToString(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         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((short) 0);
106         builder.setNwDstMask((short) 0);
107         builder.setInPort(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(128);
111         builder.setDlVlanPcp((short) 2);
112         builder.setDlType(15);
113         builder.setNwTos((short) 14);
114         builder.setNwProto((short) 85);
115         builder.setNwSrc(new Ipv4Address("1.1.1.2"));
116         builder.setNwDst(new Ipv4Address("32.16.8.1"));
117         builder.setTpSrc(2048);
118         builder.setTpDst(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.assertEquals("Wrong dl-src", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
127         byte[] dlDst = new byte[6];
128         out.readBytes(dlDst);
129         Assert.assertEquals("Wrong dl-dst", "02:02:02:02:02:02", ByteBufUtils.macAddressToString(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 }