BUG-2794: incorporate openflowjava api changes to openflowplugin
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorV10ImplTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
10
11 import static org.junit.Assert.assertEquals;
12
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;
39
40 /**
41  * Created by Martin Bobak mbobak@cisco.com on 8/30/14.
42  */
43 public class MatchConvertorV10ImplTest {
44
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;
59
60     @Before
61     public void setup() {
62         OpenflowPortsUtil.init();
63     }
64
65     @Test
66     /**
67      * Test method for MatchConvertotV10Impl#convert()
68      */
69     public void testConvert() {
70         MatchV10 matchV10 = matchConvertorV10.convert(createL4UdpMatch().build(), dataPathId);
71
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());
84
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());
88
89         matchV10 = matchConvertorV10.convert(createVlanTcpMatch().build(), dataPathId);
90         assertEquals(DEFAULT_VLAN_ID.getValue().intValue(), matchV10.getDlVlan().intValue());
91
92     }
93
94     private MatchBuilder createL4UdpMatch() {
95         MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
96
97         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
98
99         udpMatchBuilder.setUdpDestinationPort(DEFAULT_PORT);
100         udpMatchBuilder.setUdpSourcePort(DEFAULT_PORT);
101         matchBuilder.setLayer4Match(udpMatchBuilder.build());
102
103         return matchBuilder;
104     }
105
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());
114         return matchBuilder;
115     }
116
117     private MatchBuilder createL4TcpMatch() {
118         MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
119
120         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
121         tcpMatchBuilder.setTcpDestinationPort(DEFAULT_PORT);
122         tcpMatchBuilder.setTcpSourcePort(DEFAULT_PORT);
123         matchBuilder.setLayer4Match(tcpMatchBuilder.build());
124
125         return matchBuilder;
126     }
127
128     private MatchBuilder createMatchBuilderWithDefaults() {
129         MatchBuilder matchBuilder = new MatchBuilder();
130         EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
131         EthernetTypeBuilder ethernetTypeBuilder = new EthernetTypeBuilder();
132
133         //IEEE802.3
134         EtherType etherType = new EtherType(ETH_TYPE_802_3);
135         ethernetTypeBuilder.setType(etherType);
136         ethernetMatchBuilder.setEthernetType(ethernetTypeBuilder.build());
137
138         EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
139         ethernetDestinationBuilder.setAddress(ZERO_MAC);
140         ethernetDestinationBuilder.setMask(ZERO_MAC);
141         ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
142
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());
148
149         Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder();
150         icmpv4MatchBuilder.setIcmpv4Code(ZERO);
151         icmpv4MatchBuilder.setIcmpv4Type(ZERO);
152         matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build());
153
154         NodeConnectorId nodeConnectorId = NodeConnectorId.getDefaultInstance(NODE_CONNECTOR_ID);
155
156         matchBuilder.setInPhyPort(nodeConnectorId);
157         matchBuilder.setInPort(nodeConnectorId);
158
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());
165
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));
171         return matchBuilder;
172     }
173 }