2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.util;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
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.rev130715.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.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;
30 * @author michal.polkorab
33 public class OF10MatchSerializerTest {
35 private SerializerRegistry registry;
36 private OFSerializer<MatchV10> matchSerializer;
39 * Initializes serializer table and stores correct factory in field
42 public void startUp() {
43 registry = new SerializerRegistryImpl();
45 matchSerializer = registry.getSerializer(
46 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, MatchV10.class));
50 * Testing correct serialization of ofp_match
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);
75 Assert.assertEquals("Wrong wildcards", 2361553, out.readUnsignedInt());
76 Assert.assertEquals("Wrong in-port", 6653, out.readUnsignedShort());
77 byte[] dlSrc = new byte[6];
79 Assert.assertEquals("Wrong dl-src", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
80 byte[] dlDst = new byte[6];
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());
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());
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());
97 * Testing correct serialization of ofp_match
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);
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());
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());
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());