NETVIRT-1637 CSIT failure
[netvirt.git] / sfc / classifier / impl / src / main / java / org / opendaylight / netvirt / sfc / classifier / utils / AclMatches.java
1 /*
2  * Copyright © 2016 Red Hat, 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.netvirt.sfc.classifier.utils;
10
11 import com.google.common.collect.Sets;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import org.opendaylight.genius.mdsalutil.NwConstants;
21 import org.opendaylight.genius.mdsalutil.packet.IPProtocols;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.Matches;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.MatchesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceEth;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceEthBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4Builder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv6;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv6Builder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.DestinationPortRange;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.DestinationPortRangeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.SourcePortRange;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.SourcePortRangeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlowBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.ExtensionBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionListBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpDstKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfTcpSrcKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpDstKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmOfUdpSrcKey;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.dst.grouping.NxmOfTcpDstBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.tcp.src.grouping.NxmOfTcpSrcBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.udp.dst.grouping.NxmOfUdpDstBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.of.udp.src.grouping.NxmOfUdpSrcBuilder;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72 public class AclMatches {
73     private static final Logger LOG = LoggerFactory.getLogger(AclMatches.class);
74     private MatchBuilder matchBuilder;
75     private List<GeneralAugMatchNodesNodeTableFlow> portMatches;
76     private final Matches matches;
77     private boolean ipv4EtherTypeSet;
78     private boolean ipv6EtherTypeSet;
79
80     public AclMatches(Matches matches) {
81         this.matches = matches;
82         this.ipv4EtherTypeSet = false;
83         this.ipv6EtherTypeSet = false;
84     }
85
86     /**
87      * Convert the ACL into an OpenFlow {@link MatchBuilder}.
88      * @return {@link MatchBuilder}
89      */
90     public List<MatchBuilder> buildMatch() {
91         matchBuilder = new MatchBuilder();
92         portMatches = new ArrayList<>();
93         List<MatchBuilder> newMatches = new ArrayList<>();
94         if (matches.getAceType() instanceof AceEth) {
95             addEthMatch();
96         } else if (matches.getAceType() instanceof AceIp) {
97             addIpMatch();
98         }
99         if (portMatches.isEmpty()) {
100             newMatches.add(this.matchBuilder);
101         } else {
102             for (GeneralAugMatchNodesNodeTableFlow portMatch : portMatches) {
103                 newMatches.add(new MatchBuilder(matchBuilder.build())
104                     .addAugmentation(GeneralAugMatchNodesNodeTableFlow.class, portMatch));
105             }
106         }
107         LOG.debug("returned matches: {}", newMatches);
108         return newMatches;
109     }
110
111     private void addEthMatch() {
112         AceEth aceEth = (AceEth) matches.getAceType();
113
114         if (aceEth.getSourceMacAddress() != null) {
115             EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
116             EthernetSourceBuilder ethSourceBuilder = new EthernetSourceBuilder();
117             ethSourceBuilder.setAddress(new MacAddress(aceEth.getSourceMacAddress()));
118             ethernetMatch.setEthernetSource(ethSourceBuilder.build());
119
120             matchBuilder.setEthernetMatch(mergeEthernetMatch(matchBuilder, ethernetMatch));
121         }
122
123         if (aceEth.getDestinationMacAddress() != null) {
124             EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
125             EthernetDestinationBuilder ethDestBuilder = new EthernetDestinationBuilder();
126             ethDestBuilder.setAddress(new MacAddress(aceEth.getDestinationMacAddress()));
127             ethernetMatch.setEthernetDestination(ethDestBuilder.build());
128
129             matchBuilder.setEthernetMatch(mergeEthernetMatch(matchBuilder, ethernetMatch));
130         }
131     }
132
133     private void addIpMatch() {
134         AceIp aceIp = (AceIp)matches.getAceType();
135
136         if (aceIp.getDscp() != null) {
137             addDscpMatch(aceIp);
138         }
139
140         if (aceIp.getProtocol() != null) {
141             addIpProtocolMatch(aceIp);
142         }
143
144         if (aceIp.getAceIpVersion() instanceof AceIpv4) {
145             addIpV4Match(aceIp);
146         }
147
148         if (aceIp.getAceIpVersion() instanceof AceIpv6) {
149             addIpV6Match(aceIp);
150         }
151     }
152
153     private void addDscpMatch(AceIp aceIp) {
154         setIpv4EtherType();
155
156         IpMatchBuilder ipMatch = new IpMatchBuilder();
157         Dscp dscp = new Dscp(aceIp.getDscp());
158         ipMatch.setIpDscp(dscp);
159
160         matchBuilder.setIpMatch(mergeIpMatch(matchBuilder, ipMatch));
161     }
162
163     private void addIpProtocolMatch(AceIp aceIp) {
164         // Match on IP
165         setIpv4EtherType();
166         IpMatchBuilder ipMatch = new IpMatchBuilder();
167         ipMatch.setIpProtocol(aceIp.getProtocol());
168         matchBuilder.setIpMatch(mergeIpMatch(matchBuilder, ipMatch));
169
170         Integer srcPort = null;
171         if (aceIp.getSourcePortRange() != null && aceIp.getSourcePortRange().getLowerPort() != null) {
172             srcPort = aceIp.getSourcePortRange().getLowerPort().getValue().toJava();
173         }
174
175         Integer srcPortMax = srcPort;
176         if (aceIp.getSourcePortRange() != null && aceIp.getSourcePortRange().getUpperPort() != null) {
177             srcPortMax = aceIp.getSourcePortRange().getUpperPort().getValue().toJava();
178         }
179
180         Integer dstPort = null;
181         if (aceIp.getDestinationPortRange() != null && aceIp.getDestinationPortRange().getLowerPort() != null) {
182             dstPort = aceIp.getDestinationPortRange().getLowerPort().getValue().toJava();
183         }
184
185         Integer dstPortMax = dstPort;
186         if (aceIp.getDestinationPortRange() != null && aceIp.getDestinationPortRange().getUpperPort() != null) {
187             dstPortMax = aceIp.getDestinationPortRange().getUpperPort().getValue().toJava();
188         }
189
190         // Match on a TCP/UDP src/dst port
191         if (srcPort != null || dstPort != null) {
192             Map<Integer,Integer> srcPortMaskMap = srcPort == null ? Collections.singletonMap(0, 0) :
193                 getLayer4MaskForRange(srcPort, srcPortMax);
194             Map<Integer,Integer> dstPortMaskMap = dstPort == null ? Collections.singletonMap(0, 0) :
195                 getLayer4MaskForRange(dstPort, dstPortMax);
196             Set<List<Map.Entry<Integer,Integer>>> srcDstMatches = Sets
197                 .cartesianProduct(srcPortMaskMap.entrySet(), dstPortMaskMap.entrySet());
198             if (aceIp.getProtocol().shortValue() == IPProtocols.TCP.shortValue()) {
199                 portMatches = srcDstMatches.stream().map(srcDstPairList -> buildTcpMatch(srcDstPairList
200                     .get(0), srcDstPairList.get(1))).collect(Collectors.toList());
201             } else if (aceIp.getProtocol().shortValue() == IPProtocols.UDP.shortValue()) {
202                 portMatches = srcDstMatches.stream().map(srcDstPairList -> buildUdpMatch(srcDstPairList
203                     .get(0), srcDstPairList.get(1))).collect(Collectors.toList());
204             }
205         }
206     }
207
208     private static GeneralAugMatchNodesNodeTableFlow buildTcpMatch(Map.Entry<Integer,Integer> srcEntry,
209         Map.Entry<Integer,Integer> dstEntry) {
210         List<ExtensionList> srcDstExtList = new ArrayList<>();
211
212         if (srcEntry.getValue() != 0) {
213             NxmOfTcpSrcBuilder tcpSrc = new NxmOfTcpSrcBuilder();
214             tcpSrc.setMask(srcEntry.getValue());
215             tcpSrc.setPort(new PortNumber(srcEntry.getKey()));
216             NxAugMatchNodesNodeTableFlow nxAugMatchTcpSrc =
217                 new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfTcpSrc(tcpSrc.build()).build();
218             srcDstExtList.add(new ExtensionListBuilder().setExtensionKey(NxmOfTcpSrcKey.class)
219                 .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class,
220                     nxAugMatchTcpSrc).build()).build());
221         }
222
223         if (dstEntry.getValue() != 0) {
224             NxmOfTcpDstBuilder tcpDst = new NxmOfTcpDstBuilder();
225             tcpDst.setMask(dstEntry.getValue());
226             tcpDst.setPort(new PortNumber(dstEntry.getKey()));
227             NxAugMatchNodesNodeTableFlow nxAugMatchTcpDst =
228                 new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfTcpDst(tcpDst.build()).build();
229             srcDstExtList.add(new ExtensionListBuilder().setExtensionKey(NxmOfTcpDstKey.class)
230                 .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class,
231                     nxAugMatchTcpDst).build()).build());
232         }
233
234         GeneralAugMatchNodesNodeTableFlow genAugMatch =
235             new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(srcDstExtList).build();
236
237         return genAugMatch;
238     }
239
240     private static GeneralAugMatchNodesNodeTableFlow buildUdpMatch(Map.Entry<Integer,Integer> srcEntry,
241         Map.Entry<Integer,Integer> dstEntry) {
242         List<ExtensionList> srcDstExtList = new ArrayList<>();
243
244         if (srcEntry.getValue() != 0) {
245             NxmOfUdpSrcBuilder udpSrc = new NxmOfUdpSrcBuilder();
246             udpSrc.setMask(srcEntry.getValue());
247             udpSrc.setPort(new PortNumber(srcEntry.getKey()));
248             NxAugMatchNodesNodeTableFlow nxAugMatchUdpSrc =
249                 new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfUdpSrc(udpSrc.build()).build();
250             srcDstExtList.add(new ExtensionListBuilder().setExtensionKey(NxmOfUdpSrcKey.class)
251                 .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class,
252                     nxAugMatchUdpSrc).build()).build());
253         }
254
255         if (dstEntry.getValue() != 0) {
256             NxmOfUdpDstBuilder udpDst = new NxmOfUdpDstBuilder();
257             udpDst.setMask(dstEntry.getValue());
258             udpDst.setPort(new PortNumber(dstEntry.getKey()));
259             NxAugMatchNodesNodeTableFlow nxAugMatchUdpDst =
260                 new NxAugMatchNodesNodeTableFlowBuilder().setNxmOfUdpDst(udpDst.build()).build();
261             srcDstExtList.add(new ExtensionListBuilder().setExtensionKey(NxmOfUdpDstKey.class)
262                 .setExtension(new ExtensionBuilder().addAugmentation(NxAugMatchNodesNodeTableFlow.class,
263                     nxAugMatchUdpDst).build()).build());
264         }
265
266         GeneralAugMatchNodesNodeTableFlow genAugMatch =
267             new GeneralAugMatchNodesNodeTableFlowBuilder().setExtensionList(srcDstExtList).build();
268
269         return genAugMatch;
270     }
271
272     public static Map<Integer,Integer>  getLayer4MaskForRange(int portMin, int portMax) {
273         final int[] offset = { 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 };
274         final int[] mask = { 0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0,
275             0xFFF0, 0xFFF8, 0xFFFC, 0xFFFE, 0xFFFF };
276         int noOfPorts = portMax - portMin + 1;
277         Map<Integer,Integer> portMap = new HashMap<>();
278         if (noOfPorts == 1) {
279             portMap.put(portMin, mask[15]);
280             return portMap;
281         } else if (noOfPorts == 65535) {
282             portMap.put(portMin, 0x0000);
283             return portMap;
284         }
285         if (noOfPorts < 0) { // TODO: replace with infrautils.counter in case of high repetitive usage
286             LOG.warn("Cannot convert port range into a set of masked port ranges - Illegal port range {}-{}", portMin,
287                     portMax);
288             return portMap;
289         }
290         String binaryNoOfPorts = Integer.toBinaryString(noOfPorts);
291         if (binaryNoOfPorts.length() > 16) { // TODO: replace with infrautils.counter in case of high repetitive usage
292             LOG.warn("Cannot convert port range into a set of masked port ranges - Illegal port range {}-{}", portMin,
293                     portMax);
294             return portMap;
295         }
296         int medianOffset = 16 - binaryNoOfPorts.length();
297         int medianLength = offset[medianOffset];
298         int median = 0;
299         for (int tempMedian = 0;tempMedian < portMax;) {
300             tempMedian = medianLength + tempMedian;
301             if (portMin < tempMedian) {
302                 median = tempMedian;
303                 break;
304             }
305         }
306         int tempMedian = 0;
307         int currentMedain = median;
308         for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
309             tempMedian = currentMedain - offset[tempMedianOffset];
310             for (;portMin <= tempMedian;) {
311                 portMap.put(tempMedian, mask[tempMedianOffset]);
312                 currentMedain = tempMedian;
313                 tempMedian = tempMedian - offset[tempMedianOffset];
314             }
315         }
316         currentMedain = median;
317         for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
318             tempMedian = currentMedain + offset[tempMedianOffset];
319             for (;portMax >= tempMedian - 1;) {
320                 portMap.put(currentMedain, mask[tempMedianOffset]);
321                 currentMedain = tempMedian;
322                 tempMedian = tempMedian  + offset[tempMedianOffset];
323             }
324         }
325         return portMap;
326     }
327
328     private void addIpV4Match(AceIp aceIp) {
329         setIpv4EtherType();
330
331         AceIpv4 aceIpv4 = (AceIpv4)aceIp.getAceIpVersion();
332         if (aceIpv4.getDestinationIpv4Network() != null) {
333             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
334             ipv4match.setIpv4Destination(aceIpv4.getDestinationIpv4Network());
335             matchBuilder.setLayer3Match(mergeIpv4Match(matchBuilder, ipv4match));
336         }
337
338         if (aceIpv4.getSourceIpv4Network() != null) {
339             Ipv4MatchBuilder ipv4match = new Ipv4MatchBuilder();
340             ipv4match.setIpv4Source(aceIpv4.getSourceIpv4Network());
341             matchBuilder.setLayer3Match(mergeIpv4Match(matchBuilder, ipv4match));
342         }
343     }
344
345     private void addIpV6Match(AceIp aceIp) {
346         setIpv6EtherType();
347
348         AceIpv6 aceIpv6 = (AceIpv6)aceIp.getAceIpVersion();
349         if (aceIpv6.getSourceIpv6Network() != null) {
350             Ipv6MatchBuilder ipv6match = new Ipv6MatchBuilder();
351             ipv6match.setIpv6Source(aceIpv6.getSourceIpv6Network());
352             matchBuilder.setLayer3Match(mergeIpv6Match(matchBuilder, ipv6match));
353         }
354
355         if (aceIpv6.getDestinationIpv6Network() != null) {
356             Ipv6MatchBuilder ipv6match = new Ipv6MatchBuilder();
357             ipv6match.setIpv6Destination(aceIpv6.getDestinationIpv6Network());
358             matchBuilder.setLayer3Match(mergeIpv6Match(matchBuilder, ipv6match));
359         }
360     }
361
362     // If we call multiple Layer3 match methods, the MatchBuilder
363     // Ipv4Match object gets overwritten each time, when we actually
364     // want to set additional fields on the existing Ipv4Match object
365     private static Ipv4Match mergeIpv4Match(MatchBuilder match, Ipv4MatchBuilder ipMatchBuilder) {
366         Ipv4Match ipv4Match = (Ipv4Match) match.getLayer3Match();
367         if (ipv4Match == null) {
368             return ipMatchBuilder.build();
369         }
370
371         if (ipv4Match.getIpv4Destination() != null) {
372             ipMatchBuilder.setIpv4Destination(ipv4Match.getIpv4Destination());
373         }
374
375         if (ipv4Match.getIpv4Source() != null) {
376             ipMatchBuilder.setIpv4Source(ipv4Match.getIpv4Source());
377         }
378
379         return ipMatchBuilder.build();
380     }
381
382     private void setIpv6EtherType() {
383         if (this.ipv6EtherTypeSet) {
384             // No need to set it twice
385             return;
386         }
387
388         setEtherType(NwConstants.ETHTYPE_IPV6);
389         this.ipv6EtherTypeSet = true;
390     }
391
392     private void setIpv4EtherType() {
393         if (this.ipv4EtherTypeSet) {
394             // No need to set it twice
395             return;
396         }
397
398         setEtherType(NwConstants.ETHTYPE_IPV4);
399         this.ipv4EtherTypeSet = true;
400     }
401
402     private void setEtherType(long etherType) {
403         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
404         ethTypeBuilder.setType(new EtherType(etherType));
405         EthernetMatchBuilder ethernetMatch = new EthernetMatchBuilder();
406         ethernetMatch.setEthernetType(ethTypeBuilder.build());
407         matchBuilder.setEthernetMatch(mergeEthernetMatch(matchBuilder, ethernetMatch));
408     }
409
410     // If we call multiple Layer3 match methods, the MatchBuilder
411     // Ipv6Match object gets overwritten each time, when we actually
412     // want to set additional fields on the existing Ipv6Match object
413     private static Ipv6Match mergeIpv6Match(MatchBuilder match, Ipv6MatchBuilder ipMatchBuilder) {
414         Ipv6Match ipv6Match = (Ipv6Match) match.getLayer3Match();
415         if (ipv6Match == null) {
416             return ipMatchBuilder.build();
417         }
418
419         if (ipv6Match.getIpv6Destination() != null) {
420             ipMatchBuilder.setIpv6Destination(ipv6Match.getIpv6Destination());
421         }
422
423         if (ipv6Match.getIpv6Source() != null) {
424             ipMatchBuilder.setIpv6Source(ipv6Match.getIpv6Source());
425         }
426
427         return ipMatchBuilder.build();
428     }
429
430     // If we call multiple IpMatch match methods, the MatchBuilder
431     // IpMatch object gets overwritten each time, when we actually
432     // want to set additional fields on the existing IpMatch object
433     private static IpMatch mergeIpMatch(MatchBuilder match, IpMatchBuilder ipMatchBuilder) {
434         IpMatch ipMatch = match.getIpMatch();
435         if (ipMatch == null) {
436             return ipMatchBuilder.build();
437         }
438
439         if (ipMatch.getIpDscp() != null) {
440             ipMatchBuilder.setIpDscp(ipMatch.getIpDscp());
441         }
442
443         if (ipMatch.getIpEcn() != null) {
444             ipMatchBuilder.setIpEcn(ipMatch.getIpEcn());
445         }
446
447         if (ipMatch.getIpProto() != null) {
448             ipMatchBuilder.setIpProto(ipMatch.getIpProto());
449         }
450
451         if (ipMatch.getIpProtocol() != null) {
452             ipMatchBuilder.setIpProtocol(ipMatch.getIpProtocol());
453         }
454
455         return ipMatchBuilder.build();
456     }
457
458     // If we call multiple ethernet match methods, the MatchBuilder
459     // EthernetMatch object gets overwritten each time, when we actually
460     // want to set additional fields on the existing EthernetMatch object
461     private static EthernetMatch mergeEthernetMatch(MatchBuilder match, EthernetMatchBuilder ethMatchBuilder) {
462         EthernetMatch ethMatch = match.getEthernetMatch();
463         if (ethMatch == null) {
464             return ethMatchBuilder.build();
465         }
466
467         if (ethMatch.getEthernetDestination() != null) {
468             ethMatchBuilder.setEthernetDestination(ethMatch.getEthernetDestination());
469         }
470
471         if (ethMatch.getEthernetSource() != null) {
472             ethMatchBuilder.setEthernetSource(ethMatch.getEthernetSource());
473         }
474
475         if (ethMatch.getEthernetType() != null) {
476             ethMatchBuilder.setEthernetType(ethMatch.getEthernetType());
477         }
478
479         return ethMatchBuilder.build();
480     }
481
482     public static Matches invertMatches(Matches matches) {
483         LOG.trace("Invert matches: {}", matches);
484         MatchesBuilder matchesBuilder = new MatchesBuilder(matches);
485
486         if (matches.getAceType() instanceof AceIp) {
487             AceIp aceIp = (AceIp) matches.getAceType();
488             AceIpBuilder aceIpBuilder = new AceIpBuilder(aceIp);
489             aceIpBuilder.setDestinationPortRange(null);
490             aceIpBuilder.setSourcePortRange(null);
491             SourcePortRange sourcePortRange = aceIp.getSourcePortRange();
492             DestinationPortRange destinationPortRange = aceIp.getDestinationPortRange();
493
494             if (sourcePortRange != null) {
495                 DestinationPortRangeBuilder destinationPortRangeBuilder = new DestinationPortRangeBuilder();
496                 destinationPortRangeBuilder.setLowerPort(sourcePortRange.getLowerPort());
497                 destinationPortRangeBuilder.setUpperPort(sourcePortRange.getUpperPort());
498                 aceIpBuilder.setDestinationPortRange(destinationPortRangeBuilder.build());
499             }
500
501             if (destinationPortRange != null) {
502                 SourcePortRangeBuilder sourcePortRangeBuilder = new SourcePortRangeBuilder();
503                 sourcePortRangeBuilder.setLowerPort(destinationPortRange.getLowerPort());
504                 sourcePortRangeBuilder.setUpperPort(destinationPortRange.getUpperPort());
505                 aceIpBuilder.setSourcePortRange(sourcePortRangeBuilder.build());
506             }
507
508             if (aceIp.getAceIpVersion() instanceof AceIpv4) {
509                 AceIpv4 aceIpv4 = (AceIpv4) aceIp.getAceIpVersion();
510                 Ipv4Prefix destinationIpv4Network = aceIpv4.getDestinationIpv4Network();
511                 Ipv4Prefix sourceIpv4Network = aceIpv4.getSourceIpv4Network();
512
513                 AceIpv4Builder aceIpv4Builder = new AceIpv4Builder(aceIpv4);
514                 aceIpv4Builder.setDestinationIpv4Network(sourceIpv4Network);
515                 aceIpv4Builder.setSourceIpv4Network(destinationIpv4Network);
516                 aceIpBuilder.setAceIpVersion(aceIpv4Builder.build());
517
518             } else if (aceIp.getAceIpVersion() instanceof AceIpv6) {
519                 AceIpv6 aceIpv6 = (AceIpv6) aceIp.getAceIpVersion();
520                 Ipv6Prefix destinationIpv6Network = aceIpv6.getDestinationIpv6Network();
521                 Ipv6Prefix sourceIpv6Network = aceIpv6.getSourceIpv6Network();
522
523                 AceIpv6Builder aceIpv6Builder = new AceIpv6Builder(aceIpv6);
524                 aceIpv6Builder.setDestinationIpv6Network(sourceIpv6Network);
525                 aceIpv6Builder.setSourceIpv6Network(destinationIpv6Network);
526                 aceIpBuilder.setAceIpVersion(aceIpv6Builder.build());
527             }
528
529             matchesBuilder.setAceType(aceIpBuilder.build());
530
531         } else if (matches.getAceType() instanceof AceEth) {
532             AceEth aceEth = (AceEth) matches.getAceType();
533             MacAddress destinationMacAddress = aceEth.getDestinationMacAddress();
534             MacAddress destinationMacAddressMask = aceEth.getDestinationMacAddressMask();
535             MacAddress sourceMacAddress = aceEth.getSourceMacAddress();
536             MacAddress sourceMacAddressMask = aceEth.getSourceMacAddressMask();
537
538             AceEthBuilder aceEthBuilder = new AceEthBuilder(aceEth);
539             aceEthBuilder.setDestinationMacAddress(sourceMacAddress);
540             aceEthBuilder.setDestinationMacAddressMask(sourceMacAddressMask);
541             aceEthBuilder.setSourceMacAddress(destinationMacAddress);
542             aceEthBuilder.setSourceMacAddressMask(destinationMacAddressMask);
543             matchesBuilder.setAceType(aceEthBuilder.build());
544         }
545
546         Matches invertedMatches = matchesBuilder.build();
547         LOG.trace("Inverted matches: {}", invertedMatches);
548         return invertedMatches;
549     }
550
551 }