2 * Copyright (c) 2014, 2015 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;
12 import java.math.BigInteger;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpVersion;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
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.common.types.rev130731.FlowWildcardsV10;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
42 * Created by Martin Bobak mbobak@cisco.com on 8/30/14.
44 public class MatchConvertorV10ImplTest {
46 private static final MatchConvertorV10Impl matchConvertorV10 = new MatchConvertorV10Impl();
47 private static final BigInteger dataPathId = BigInteger.TEN;
48 private static final long ETH_TYPE_802_3 = 0x0000;
49 private static final MacAddress ZERO_MAC = MacAddress.getDefaultInstance("00:00:00:00:00:00");
50 private static final MacAddress FF_MAC = MacAddress.getDefaultInstance("ff:ff:ff:ff:ff:ff");
51 private static final String NODE_CONNECTOR_ID = "1234";
52 private static final short ZERO = 0;
53 private static final String DSCP = "0";
54 private static final short IP_PROTOCOL = 6;
55 private static final PortNumber DEFAULT_PORT = new PortNumber(9999);
56 private static final Ipv4Prefix ipv4Prefix = Ipv4Prefix.getDefaultInstance("10.0.0.1/24");
57 private static final VlanId DEFAULT_VLAN_ID = new VlanId(42);
58 private static final Ipv4Address DEFAULT_IPV4_ADDRESS = new Ipv4Address("10.0.0.1");
59 private static final short DEFAULT_MASK = 24;
63 OpenflowPortsUtil.init();
68 * Test method for {@link MatchConvertorV10Impl#convert(Match,BigInteger)}
70 public void testConvert() {
71 MatchV10 matchV10 = matchConvertorV10.convert(createL4UdpMatch().build(), dataPathId);
73 assertEquals(ZERO_MAC, matchV10.getDlDst());
74 assertEquals(FF_MAC, matchV10.getDlSrc());
75 assertEquals(0, matchV10.getDlType().intValue());
76 assertEquals(0xffff, matchV10.getDlVlan().intValue());
77 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpSrc().intValue());
78 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpDst().intValue());
79 assertEquals(Integer.parseInt(NODE_CONNECTOR_ID), matchV10.getInPort().intValue());
80 assertEquals(DEFAULT_IPV4_ADDRESS.getValue(), matchV10.getNwDst().getValue());
81 assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
82 assertEquals(0, matchV10.getNwTos().shortValue());
83 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpSrc().intValue());
84 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpDst().intValue());
86 matchV10 = matchConvertorV10.convert(createL4TcpMatch().build(), dataPathId);
87 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpSrc().intValue());
88 assertEquals(DEFAULT_PORT.getValue().intValue(), matchV10.getTpDst().intValue());
90 matchV10 = matchConvertorV10.convert(createVlanTcpMatch().build(), dataPathId);
91 assertEquals(DEFAULT_VLAN_ID.getValue().intValue(), matchV10.getDlVlan().intValue());
96 * ICMPv4 match test for
97 * {@link MatchConvertorV10Impl#convert(Match,BigInteger)}.
100 public void testConvertIcmpv4() {
101 MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
102 Match match = matchBuilder.build();
103 MatchV10 matchV10 = matchConvertorV10.convert(match, dataPathId);
105 boolean wcTpSrc = true;
106 boolean wcTpDst = true;
107 FlowWildcardsV10 wc = new FlowWildcardsV10(
108 false, false, false, true, true, false, false, false,
110 assertEquals(ZERO_MAC, matchV10.getDlDst());
111 assertEquals(FF_MAC, matchV10.getDlSrc());
112 assertEquals(0, matchV10.getDlType().intValue());
113 assertEquals(0xffff, matchV10.getDlVlan().intValue());
114 assertEquals(Integer.parseInt(NODE_CONNECTOR_ID),
115 matchV10.getInPort().intValue());
116 assertEquals(DEFAULT_IPV4_ADDRESS.getValue(),
117 matchV10.getNwDst().getValue());
118 assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
119 assertEquals(0, matchV10.getNwTos().shortValue());
120 assertEquals(zero, matchV10.getTpSrc());
121 assertEquals(zero, matchV10.getTpDst());
122 assertEquals(wc, matchV10.getWildcards());
124 // Specify ICMP type only.
125 Integer icmpType = 55;
126 Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder().
127 setIcmpv4Type(icmpType.shortValue());
129 wc = new FlowWildcardsV10(
130 false, false, false, true, true, false, false, false,
132 match = matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build()).
134 matchV10 = matchConvertorV10.convert(match, dataPathId);
135 assertEquals(ZERO_MAC, matchV10.getDlDst());
136 assertEquals(FF_MAC, matchV10.getDlSrc());
137 assertEquals(0, matchV10.getDlType().intValue());
138 assertEquals(0xffff, matchV10.getDlVlan().intValue());
139 assertEquals(Integer.parseInt(NODE_CONNECTOR_ID),
140 matchV10.getInPort().intValue());
141 assertEquals(DEFAULT_IPV4_ADDRESS.getValue(),
142 matchV10.getNwDst().getValue());
143 assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
144 assertEquals(0, matchV10.getNwTos().shortValue());
145 assertEquals(icmpType, matchV10.getTpSrc());
146 assertEquals(zero, matchV10.getTpDst());
147 assertEquals(wc, matchV10.getWildcards());
149 // Specify ICMP code only.
150 Integer icmpCode = 31;
151 icmpv4MatchBuilder = new Icmpv4MatchBuilder().
152 setIcmpv4Type(null).setIcmpv4Code(icmpCode.shortValue());
155 wc = new FlowWildcardsV10(
156 false, false, false, true, true, false, false, false,
158 match = matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build()).
160 matchV10 = matchConvertorV10.convert(match, dataPathId);
161 assertEquals(ZERO_MAC, matchV10.getDlDst());
162 assertEquals(FF_MAC, matchV10.getDlSrc());
163 assertEquals(0, matchV10.getDlType().intValue());
164 assertEquals(0xffff, matchV10.getDlVlan().intValue());
165 assertEquals(Integer.parseInt(NODE_CONNECTOR_ID),
166 matchV10.getInPort().intValue());
167 assertEquals(DEFAULT_IPV4_ADDRESS.getValue(),
168 matchV10.getNwDst().getValue());
169 assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
170 assertEquals(0, matchV10.getNwTos().shortValue());
171 assertEquals(zero, matchV10.getTpSrc());
172 assertEquals(icmpCode, matchV10.getTpDst());
173 assertEquals(wc, matchV10.getWildcards());
175 // Specify both ICMP type and code.
178 icmpv4MatchBuilder = new Icmpv4MatchBuilder().
179 setIcmpv4Type(icmpType.shortValue()).
180 setIcmpv4Code(icmpCode.shortValue());
183 wc = new FlowWildcardsV10(
184 false, false, false, true, true, false, false, false,
186 match = matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build()).
188 matchV10 = matchConvertorV10.convert(match, dataPathId);
189 assertEquals(ZERO_MAC, matchV10.getDlDst());
190 assertEquals(FF_MAC, matchV10.getDlSrc());
191 assertEquals(0, matchV10.getDlType().intValue());
192 assertEquals(0xffff, matchV10.getDlVlan().intValue());
193 assertEquals(Integer.parseInt(NODE_CONNECTOR_ID),
194 matchV10.getInPort().intValue());
195 assertEquals(DEFAULT_IPV4_ADDRESS.getValue(),
196 matchV10.getNwDst().getValue());
197 assertEquals(DEFAULT_MASK, matchV10.getNwDstMask().shortValue());
198 assertEquals(0, matchV10.getNwTos().shortValue());
199 assertEquals(icmpType, matchV10.getTpSrc());
200 assertEquals(icmpCode, matchV10.getTpDst());
201 assertEquals(wc, matchV10.getWildcards());
204 private static MatchBuilder createL4UdpMatch() {
205 MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
207 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
209 udpMatchBuilder.setUdpDestinationPort(DEFAULT_PORT);
210 udpMatchBuilder.setUdpSourcePort(DEFAULT_PORT);
211 matchBuilder.setLayer4Match(udpMatchBuilder.build());
216 private static MatchBuilder createVlanTcpMatch() {
217 MatchBuilder matchBuilder = createL4TcpMatch();
218 VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
219 VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
220 vlanIdBuilder.setVlanId(DEFAULT_VLAN_ID);
221 vlanIdBuilder.setVlanIdPresent(true);
222 vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
223 matchBuilder.setVlanMatch(vlanMatchBuilder.build());
227 private static MatchBuilder createL4TcpMatch() {
228 MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
230 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
231 tcpMatchBuilder.setTcpDestinationPort(DEFAULT_PORT);
232 tcpMatchBuilder.setTcpSourcePort(DEFAULT_PORT);
233 matchBuilder.setLayer4Match(tcpMatchBuilder.build());
238 private static MatchBuilder createMatchBuilderWithDefaults() {
239 MatchBuilder matchBuilder = new MatchBuilder();
240 EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
241 EthernetTypeBuilder ethernetTypeBuilder = new EthernetTypeBuilder();
244 EtherType etherType = new EtherType(ETH_TYPE_802_3);
245 ethernetTypeBuilder.setType(etherType);
246 ethernetMatchBuilder.setEthernetType(ethernetTypeBuilder.build());
248 EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
249 ethernetDestinationBuilder.setAddress(ZERO_MAC);
250 ethernetDestinationBuilder.setMask(ZERO_MAC);
251 ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
253 EthernetSourceBuilder ethernetSourceBuilder = new EthernetSourceBuilder();
254 ethernetSourceBuilder.setMask(FF_MAC);
255 ethernetSourceBuilder.setAddress(FF_MAC);
256 ethernetMatchBuilder.setEthernetSource(ethernetSourceBuilder.build());
257 matchBuilder.setEthernetMatch(ethernetMatchBuilder.build());
259 NodeConnectorId nodeConnectorId = NodeConnectorId.getDefaultInstance(NODE_CONNECTOR_ID);
261 matchBuilder.setInPhyPort(nodeConnectorId);
262 matchBuilder.setInPort(nodeConnectorId);
264 IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
265 ipMatchBuilder.setIpDscp(Dscp.getDefaultInstance(DSCP));
266 ipMatchBuilder.setIpEcn(ZERO);
267 ipMatchBuilder.setIpProto(IpVersion.Ipv4);
268 ipMatchBuilder.setIpProtocol(IP_PROTOCOL);
269 matchBuilder.setIpMatch(ipMatchBuilder.build());
271 Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
272 ipv4MatchBuilder.setIpv4Destination(ipv4Prefix);
273 ipv4MatchBuilder.setIpv4Source(ipv4Prefix);
274 matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
275 matchBuilder.setInPort(new NodeConnectorId(NODE_CONNECTOR_ID));