2 * Copyright © 2016 Red Hat, 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.netvirt.sfc.classifier.utils;
11 import org.opendaylight.netvirt.utils.mdsal.openflow.MatchUtils;
12 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;
13 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;
14 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;
15 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;
16 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;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class AclMatches {
23 private static final Logger LOG = LoggerFactory.getLogger(AclMatches.class);
24 MatchBuilder matchBuilder;
27 public AclMatches(Matches matches) {
28 matchBuilder = new MatchBuilder();
29 this.matches = matches;
33 * Convert the ACL into an OpenFlow {@link MatchBuilder}.
34 * @return {@link MatchBuilder}
36 //TODO: Matches will overwrite previous matches for ethernet and ip since these methods
37 // can be called successively for the same ACL.
38 // This requires fixing the MatchUtils to preserve previously set fields.
39 public MatchBuilder buildMatch() {
40 if (matches.getAceType() instanceof AceEth) {
42 } else if (matches.getAceType() instanceof AceIp) {
46 LOG.info("buildMatch: {}", matchBuilder.build());
50 private void addEthMatch() {
51 AceEth aceEth = (AceEth) matches.getAceType();
52 MatchUtils.createEthSrcDstMatch(matchBuilder, aceEth.getSourceMacAddress(),
53 aceEth.getDestinationMacAddress());
56 private void addIpMatch() {
57 AceIp aceIp = (AceIp)matches.getAceType();
59 if (aceIp.getDscp() != null) {
60 MatchUtils.addDscp(matchBuilder, aceIp.getDscp().getValue());
63 if (aceIp.getProtocol() != null) {
64 addIpProtocolMatch(aceIp);
67 if (aceIp.getAceIpVersion() instanceof AceIpv4) {
71 if (aceIp.getAceIpVersion() instanceof AceIpv6) {
76 private void addIpProtocolMatch(AceIp aceIp) {
80 // TODO Ranges are not supported yet
81 if (aceIp.getSourcePortRange() != null && aceIp.getSourcePortRange().getLowerPort() != null) {
82 srcPort = aceIp.getSourcePortRange().getLowerPort().getValue();
84 if (aceIp.getDestinationPortRange() != null && aceIp.getDestinationPortRange().getLowerPort() != null) {
85 dstPort = aceIp.getDestinationPortRange().getLowerPort().getValue();
87 MatchUtils.createIpProtocolMatch(matchBuilder, aceIp.getProtocol());
88 MatchUtils.addLayer4Match(matchBuilder, aceIp.getProtocol().intValue(), srcPort, dstPort);
91 private void addIpV4Match(AceIp aceIp) {
92 AceIpv4 aceIpv4 = (AceIpv4)aceIp.getAceIpVersion();
94 MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(MatchUtils.ETHERTYPE_IPV4));
95 matchBuilder = MatchUtils.addRemoteIpPrefix(matchBuilder, aceIpv4.getSourceIpv4Network(),
96 aceIpv4.getDestinationIpv4Network());
99 private void addIpV6Match(AceIp aceIp) {
100 AceIpv6 aceIpv6 = (AceIpv6)aceIp.getAceIpVersion();
102 MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(MatchUtils.ETHERTYPE_IPV6));
103 matchBuilder = MatchUtils.addRemoteIpv6Prefix(matchBuilder, aceIpv6.getSourceIpv6Network(),
104 aceIpv6.getDestinationIpv6Network());