2 * Copyright (c) 2014 Cisco Systems, Inc. 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.openflowplugin.openflow.md.core.sal.convertor.match;
11 import static org.junit.Assert.assertEquals;
13 import java.math.BigInteger;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpVersion;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
41 * Created by Martin Bobak mbobak@cisco.com on 8/30/14.
43 public class MatchConvertorV10ImplTest {
45 private static final MatchConvertorV10Impl matchConvertorV10 = new MatchConvertorV10Impl();
46 private static final BigInteger dataPathId = BigInteger.TEN;
47 private static final long ETH_TYPE_802_3 = (long) 0x0000;
48 private static final MacAddress ZERO_MAC = MacAddress.getDefaultInstance("00:00:00:00:00:00");
49 private static final MacAddress FF_MAC = MacAddress.getDefaultInstance("ff:ff:ff:ff:ff:ff");
50 private static final String NODE_CONNECTOR_ID = "1234";
51 private static final short ZERO = 0;
52 private static final String DSCP = "0";
53 private static final short IP_PROTOCOL = 6;
54 private static final PortNumber DEFAULT_PORT = new PortNumber(9999);
55 private static final Ipv4Prefix ipv4Prefix = Ipv4Prefix.getDefaultInstance("10.0.0.1/24");
56 private static final VlanId DEFAULT_VLAN_ID = new VlanId(42);
57 private static final Ipv4Address DEFAULT_IPV4_ADDRESS = new Ipv4Address("10.0.0.1");
58 private static final short DEFAULT_MASK = 24;
62 OpenflowPortsUtil.init();
67 * Test method for MatchConvertotV10Impl#convert()
69 public void testConvert() {
70 MatchV10 matchV10 = matchConvertorV10.convert(createL4UdpMatch().build(), dataPathId);
72 assertEquals(ZERO_MAC, matchV10.getDlDst());
73 assertEquals(FF_MAC, matchV10.getDlSrc());
74 assertEquals(0, matchV10.getDlType().intValue());
75 assertEquals(0xffff, matchV10.getDlVlan().intValue());
76 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpSrc().intValue());
77 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpDst().intValue());
78 assertEquals(Integer.parseInt(NODE_CONNECTOR_ID), matchV10.getInPort().intValue());
79 assertEquals(DEFAULT_IPV4_ADDRESS.getValue(), matchV10.getNwDst().getValue());
80 assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
81 assertEquals(0, matchV10.getNwTos().shortValue());
82 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpSrc().intValue());
83 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpDst().intValue());
85 matchV10 = matchConvertorV10.convert(createL4TcpMatch().build(), dataPathId);
86 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpSrc().intValue());
87 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpDst().intValue());
89 matchV10 = matchConvertorV10.convert(createVlanTcpMatch().build(), dataPathId);
90 assertEquals(DEFAULT_VLAN_ID.getValue().intValue(), matchV10.getDlVlan().intValue());
94 private MatchBuilder createL4UdpMatch() {
95 MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
97 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
99 udpMatchBuilder.setUdpDestinationPort(DEFAULT_PORT);
100 udpMatchBuilder.setUdpSourcePort(DEFAULT_PORT);
101 matchBuilder.setLayer4Match(udpMatchBuilder.build());
106 private MatchBuilder createVlanTcpMatch() {
107 MatchBuilder matchBuilder = createL4TcpMatch();
108 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
109 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
110 vlanIdBuilder.setVlanId(DEFAULT_VLAN_ID);
111 vlanIdBuilder.setVlanIdPresent(true);
112 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
113 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
117 private MatchBuilder createL4TcpMatch() {
118 MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
120 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
121 tcpMatchBuilder.setTcpDestinationPort(DEFAULT_PORT);
122 tcpMatchBuilder.setTcpSourcePort(DEFAULT_PORT);
123 matchBuilder.setLayer4Match(tcpMatchBuilder.build());
128 private MatchBuilder createMatchBuilderWithDefaults() {
129 MatchBuilder matchBuilder = new MatchBuilder();
130 EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
131 EthernetTypeBuilder ethernetTypeBuilder = new EthernetTypeBuilder();
134 EtherType etherType = new EtherType(ETH_TYPE_802_3);
135 ethernetTypeBuilder.setType(etherType);
136 ethernetMatchBuilder.setEthernetType(ethernetTypeBuilder.build());
138 EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
139 ethernetDestinationBuilder.setAddress(ZERO_MAC);
140 ethernetDestinationBuilder.setMask(ZERO_MAC);
141 ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
143 EthernetSourceBuilder ethernetSourceBuilder = new EthernetSourceBuilder();
144 ethernetSourceBuilder.setMask(FF_MAC);
145 ethernetSourceBuilder.setAddress(FF_MAC);
146 ethernetMatchBuilder.setEthernetSource(ethernetSourceBuilder.build());
147 matchBuilder.setEthernetMatch(ethernetMatchBuilder.build());
149 Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder();
150 icmpv4MatchBuilder.setIcmpv4Code(ZERO);
151 icmpv4MatchBuilder.setIcmpv4Type(ZERO);
152 matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
154 NodeConnectorId nodeConnectorId = NodeConnectorId.getDefaultInstance(NODE_CONNECTOR_ID);
156 matchBuilder.setInPhyPort(nodeConnectorId);
157 matchBuilder.setInPort(nodeConnectorId);
159 IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
160 ipMatchBuilder.setIpDscp(Dscp.getDefaultInstance(DSCP));
161 ipMatchBuilder.setIpEcn(ZERO);
162 ipMatchBuilder.setIpProto(IpVersion.Ipv4);
163 ipMatchBuilder.setIpProtocol(IP_PROTOCOL);
164 matchBuilder.setIpMatch(ipMatchBuilder.build());
166 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
167 ipv4MatchBuilder.setIpv4Destination(ipv4Prefix);
168 ipv4MatchBuilder.setIpv4Source(ipv4Prefix);
169 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
170 matchBuilder.setInPort(new NodeConnectorId(NODE_CONNECTOR_ID));