Migrate users of Optional.get()
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / match / MatchConvertorV10Test.java
1 /*
2  * Copyright (c) 2014, 2015 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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.Optional;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpVersion;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
43 import org.opendaylight.yangtools.yang.common.Uint16;
44 import org.opendaylight.yangtools.yang.common.Uint32;
45 import org.opendaylight.yangtools.yang.common.Uint8;
46
47 /**
48  * Created by Martin Bobak mbobak@cisco.com on 8/30/14.
49  */
50 public class MatchConvertorV10Test {
51
52     private static final Uint32 ETH_TYPE_802_3 = Uint32.ZERO;
53     private static final MacAddress ZERO_MAC = MacAddress.getDefaultInstance("00:00:00:00:00:00");
54     private static final MacAddress FF_MAC = MacAddress.getDefaultInstance("ff:ff:ff:ff:ff:ff");
55     private static final String NODE_CONNECTOR_ID = "1234";
56     private static final String DSCP = "0";
57     private static final Uint8 IP_PROTOCOL = Uint8.valueOf(6);
58     private static final PortNumber DEFAULT_PORT = new PortNumber(Uint16.valueOf(9999));
59     private static final Ipv4Prefix IPV4_PREFIX = Ipv4Prefix.getDefaultInstance("10.0.0.1/24");
60     private static final VlanId DEFAULT_VLAN_ID = new VlanId(Uint16.valueOf(42));
61     private static final Ipv4Address DEFAULT_IPV4_ADDRESS = new Ipv4Address("10.0.0.1");
62     private static final Uint8 DEFAULT_MASK = Uint8.valueOf(24);
63     private ConvertorManager converterManager;
64
65     @Before
66     public void setup() {
67         converterManager = ConvertorManagerFactory.createDefaultManager();
68     }
69
70     @Test
71     public void testConvert() {
72
73         Optional<MatchV10> matchV10Optional = converterManager.convert(createL4UdpMatch().build(),
74                 new VersionConvertorData(OFConstants.OFP_VERSION_1_0));
75         MatchV10 matchV10 = matchV10Optional.orElseThrow();
76
77         assertEquals(ZERO_MAC, matchV10.getDlDst());
78         assertEquals(FF_MAC, matchV10.getDlSrc());
79         assertEquals(Uint16.ZERO, matchV10.getDlType());
80         assertEquals(Uint16.MAX_VALUE, matchV10.getDlVlan());
81         assertEquals(DEFAULT_PORT.getValue(), matchV10.getTpSrc());
82         assertEquals(DEFAULT_PORT.getValue(), matchV10.getTpDst());
83         assertEquals(Uint16.valueOf(NODE_CONNECTOR_ID), matchV10.getInPort());
84         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(), matchV10.getNwDst().getValue());
85         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask());
86         assertEquals(Uint8.ZERO, matchV10.getNwTos());
87         assertEquals(DEFAULT_PORT.getValue(), matchV10.getTpSrc());
88         assertEquals(DEFAULT_PORT.getValue(), matchV10.getTpDst());
89
90         matchV10Optional = converterManager.convert(createL4TcpMatch().build(),
91                 new VersionConvertorData(OFConstants.OFP_VERSION_1_0));
92         matchV10 = matchV10Optional.orElseThrow();
93         assertEquals(DEFAULT_PORT.getValue(), matchV10.getTpSrc());
94         assertEquals(DEFAULT_PORT.getValue(), matchV10.getTpDst());
95
96         matchV10Optional = converterManager.convert(createVlanTcpMatch().build(),
97                 new VersionConvertorData(OFConstants.OFP_VERSION_1_0));
98         matchV10 = matchV10Optional.orElseThrow();
99         assertEquals(DEFAULT_VLAN_ID.getValue(), matchV10.getDlVlan());
100     }
101
102     @Test
103     public void testConvertIcmpv4() {
104         MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
105         Match match = matchBuilder.build();
106         Optional<MatchV10> matchV10Optional = converterManager.convert(match,
107                 new VersionConvertorData(OFConstants.OFP_VERSION_1_0));
108         MatchV10 matchV10 = matchV10Optional.orElseThrow();
109
110         assertEquals(ZERO_MAC, matchV10.getDlDst());
111         assertEquals(FF_MAC, matchV10.getDlSrc());
112         assertEquals(Uint16.ZERO, matchV10.getDlType());
113         assertEquals(Uint16.MAX_VALUE, matchV10.getDlVlan());
114         assertEquals(Uint16.valueOf(NODE_CONNECTOR_ID), matchV10.getInPort());
115         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(), matchV10.getNwDst().getValue());
116         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask());
117         assertEquals(Uint8.ZERO, matchV10.getNwTos());
118         assertEquals(Uint16.ZERO, matchV10.getTpSrc());
119         assertEquals(Uint16.ZERO, matchV10.getTpDst());
120
121         boolean wcTpSrc = true;
122         boolean wcTpDst = true;
123         FlowWildcardsV10 wc = new FlowWildcardsV10(
124                 false, false, false, true, true, false, false, false,
125                 wcTpDst, wcTpSrc);
126         assertEquals(wc, matchV10.getWildcards());
127
128         // Specify ICMP type only.
129         Uint8 icmpType = Uint8.valueOf(55);
130         Icmpv4MatchBuilder icmpv4MatchBuilder = new Icmpv4MatchBuilder().setIcmpv4Type(icmpType);
131         wcTpSrc = false;
132         wc = new FlowWildcardsV10(
133             false, false, false, true, true, false, false, false,
134             wcTpDst, wcTpSrc);
135         match = matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build()).build();
136         matchV10Optional = converterManager.convert(match,
137                 new VersionConvertorData(OFConstants.OFP_VERSION_1_0));
138         matchV10 = matchV10Optional.orElseThrow();
139         assertEquals(ZERO_MAC, matchV10.getDlDst());
140         assertEquals(FF_MAC, matchV10.getDlSrc());
141         assertEquals(0, matchV10.getDlType().intValue());
142         assertEquals(0xffff, matchV10.getDlVlan().intValue());
143         assertEquals(Integer.parseInt(NODE_CONNECTOR_ID),
144                      matchV10.getInPort().intValue());
145         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(),
146                      matchV10.getNwDst().getValue());
147         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask());
148         assertEquals(Uint8.ZERO, matchV10.getNwTos());
149         assertEquals(55, matchV10.getTpSrc().toJava());
150         assertEquals(Uint16.ZERO, matchV10.getTpDst());
151         assertEquals(wc, matchV10.getWildcards());
152
153         // Specify ICMP code only.
154         Uint8 icmpCode = Uint8.valueOf(31);
155         icmpv4MatchBuilder = new Icmpv4MatchBuilder().setIcmpv4Type((Uint8) null).setIcmpv4Code(icmpCode);
156         wcTpSrc = true;
157         wcTpDst = false;
158         wc = new FlowWildcardsV10(
159             false, false, false, true, true, false, false, false,
160             wcTpDst, wcTpSrc);
161         match = matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build()).build();
162         matchV10Optional = converterManager.convert(match,
163                 new VersionConvertorData(OFConstants.OFP_VERSION_1_0));
164         matchV10 = matchV10Optional.orElseThrow();
165         assertEquals(ZERO_MAC, matchV10.getDlDst());
166         assertEquals(FF_MAC, matchV10.getDlSrc());
167         assertEquals(Uint16.ZERO, matchV10.getDlType());
168         assertEquals(Uint16.MAX_VALUE, matchV10.getDlVlan());
169         assertEquals(Uint16.valueOf(NODE_CONNECTOR_ID), matchV10.getInPort());
170         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(), matchV10.getNwDst().getValue());
171         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask());
172         assertEquals(Uint8.ZERO, matchV10.getNwTos());
173         assertEquals(Uint16.ZERO, matchV10.getTpSrc());
174         assertEquals(icmpCode.toUint16(), matchV10.getTpDst());
175         assertEquals(wc, matchV10.getWildcards());
176
177         // Specify both ICMP type and code.
178         icmpType = Uint8.valueOf(11);
179         icmpCode = Uint8.valueOf(22);
180         icmpv4MatchBuilder = new Icmpv4MatchBuilder().setIcmpv4Type(icmpType).setIcmpv4Code(icmpCode);
181         wcTpSrc = false;
182         wcTpDst = false;
183         wc = new FlowWildcardsV10(false, false, false, true, true, false, false, false, wcTpDst, wcTpSrc);
184         match = matchBuilder.setIcmpv4Match(icmpv4MatchBuilder.build()).build();
185         matchV10Optional = converterManager.convert(match, new VersionConvertorData(OFConstants.OFP_VERSION_1_0));
186         matchV10 = matchV10Optional.orElseThrow();
187         assertEquals(ZERO_MAC, matchV10.getDlDst());
188         assertEquals(FF_MAC, matchV10.getDlSrc());
189         assertEquals(Uint16.ZERO, matchV10.getDlType());
190         assertEquals(Uint16.MAX_VALUE, matchV10.getDlVlan());
191         assertEquals(Uint16.valueOf(NODE_CONNECTOR_ID), matchV10.getInPort());
192         assertEquals(DEFAULT_IPV4_ADDRESS.getValue(), matchV10.getNwDst().getValue());
193         assertEquals(DEFAULT_MASK, matchV10.getNwDstMask());
194         assertEquals(Uint8.ZERO, matchV10.getNwTos());
195         assertEquals(icmpType.toUint16(), matchV10.getTpSrc());
196         assertEquals(icmpCode.toUint16(), matchV10.getTpDst());
197         assertEquals(wc, matchV10.getWildcards());
198     }
199
200     private static MatchBuilder createL4UdpMatch() {
201         MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
202
203         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
204
205         udpMatchBuilder.setUdpDestinationPort(DEFAULT_PORT);
206         udpMatchBuilder.setUdpSourcePort(DEFAULT_PORT);
207         matchBuilder.setLayer4Match(udpMatchBuilder.build());
208
209         return matchBuilder;
210     }
211
212     private static MatchBuilder createVlanTcpMatch() {
213         final MatchBuilder matchBuilder = createL4TcpMatch();
214         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
215         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
216         vlanIdBuilder.setVlanId(DEFAULT_VLAN_ID);
217         vlanIdBuilder.setVlanIdPresent(true);
218         vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
219         matchBuilder.setVlanMatch(vlanMatchBuilder.build());
220         return matchBuilder;
221     }
222
223     private static MatchBuilder createL4TcpMatch() {
224         MatchBuilder matchBuilder = createMatchBuilderWithDefaults();
225
226         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
227         tcpMatchBuilder.setTcpDestinationPort(DEFAULT_PORT);
228         tcpMatchBuilder.setTcpSourcePort(DEFAULT_PORT);
229         matchBuilder.setLayer4Match(tcpMatchBuilder.build());
230
231         return matchBuilder;
232     }
233
234     private static MatchBuilder createMatchBuilderWithDefaults() {
235         EthernetMatchBuilder ethernetMatchBuilder = new EthernetMatchBuilder();
236         EthernetTypeBuilder ethernetTypeBuilder = new EthernetTypeBuilder();
237
238         //IEEE802.3
239         EtherType etherType = new EtherType(ETH_TYPE_802_3);
240         ethernetTypeBuilder.setType(etherType);
241         ethernetMatchBuilder.setEthernetType(ethernetTypeBuilder.build());
242
243         EthernetDestinationBuilder ethernetDestinationBuilder = new EthernetDestinationBuilder();
244         ethernetDestinationBuilder.setAddress(ZERO_MAC);
245         ethernetDestinationBuilder.setMask(ZERO_MAC);
246         ethernetMatchBuilder.setEthernetDestination(ethernetDestinationBuilder.build());
247
248         EthernetSourceBuilder ethernetSourceBuilder = new EthernetSourceBuilder();
249         ethernetSourceBuilder.setMask(FF_MAC);
250         ethernetSourceBuilder.setAddress(FF_MAC);
251         ethernetMatchBuilder.setEthernetSource(ethernetSourceBuilder.build());
252
253         MatchBuilder matchBuilder = new MatchBuilder();
254         matchBuilder.setEthernetMatch(ethernetMatchBuilder.build());
255
256         NodeConnectorId nodeConnectorId = NodeConnectorId.getDefaultInstance(NODE_CONNECTOR_ID);
257
258         matchBuilder.setInPhyPort(nodeConnectorId);
259         matchBuilder.setInPort(nodeConnectorId);
260
261         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
262         ipMatchBuilder.setIpDscp(Dscp.getDefaultInstance(DSCP));
263         ipMatchBuilder.setIpEcn(Uint8.ZERO);
264         ipMatchBuilder.setIpProto(IpVersion.Ipv4);
265         ipMatchBuilder.setIpProtocol(IP_PROTOCOL);
266         matchBuilder.setIpMatch(ipMatchBuilder.build());
267
268         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
269         ipv4MatchBuilder.setIpv4Destination(IPV4_PREFIX);
270         ipv4MatchBuilder.setIpv4Source(IPV4_PREFIX);
271         matchBuilder.setLayer3Match(ipv4MatchBuilder.build());
272         matchBuilder.setInPort(new NodeConnectorId(NODE_CONNECTOR_ID));
273         return matchBuilder;
274     }
275 }