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