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;
13 import junit.framework.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
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;
29 * @author michal.polkorab
32 public class OF10MatchSerializerTest {
34 private SerializerRegistry registry;
35 private OFSerializer<MatchV10> matchSerializer;
38 * Initializes serializer table and stores correct factory in field
41 public void startUp() {
42 registry = new SerializerRegistryImpl();
44 matchSerializer = registry.getSerializer(
45 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, MatchV10.class));
49 * Testing correct serialization of ofp_match
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);
74 Assert.assertEquals("Wrong wildcards", 2361553, out.readUnsignedInt());
75 Assert.assertEquals("Wrong in-port", 6653, out.readUnsignedShort());
76 byte[] dlSrc = new byte[6];
78 Assert.assertEquals("Wrong dl-src", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
79 byte[] dlDst = new byte[6];
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());
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());
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());
96 * Testing correct serialization of ofp_match
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);
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());
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());
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());