HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / AceIpAndEthWriter.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.translate.v3po.interfaces.acl.common;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
22 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
23 import java.util.ArrayList;
24 import java.util.List;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.PacketHandling;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpAndEth;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.and.eth.AceIpVersion;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.and.eth.ace.ip.version.AceIpv4;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.and.eth.ace.ip.version.AceIpv6;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public final class AceIpAndEthWriter
37     implements AceWriter<AceIpAndEth>, AclTranslator, L2AclTranslator, Ip4AclTranslator, Ip6AclTranslator {
38
39     private static final Logger LOG = LoggerFactory.getLogger(AceIpAndEthWriter.class);
40
41     private static int maskLength(@Nonnull final AceIpAndEth ace, final int vlanTags) {
42         if (ace.getAceIpVersion() != null) {
43             if (ace.getAceIpVersion() instanceof AceIpv4) {
44                 return 48;
45             } else {
46                 return vlanTags == 2
47                     ? 80
48                     : 64;
49             }
50         }
51         return 16;
52     }
53
54     @Override
55     public ClassifyAddDelTable createTable(@Nonnull final AceIpAndEth ace, @Nullable final InterfaceMode mode,
56                                            final int nextTableIndex, final int vlanTags) {
57         final int numberOfSessions = PortPair.fromRange(ace.getSourcePortRange(), ace.getDestinationPortRange()).size();
58         final ClassifyAddDelTable request = createTable(nextTableIndex, numberOfSessions);
59         final int maskLength = maskLength(ace, vlanTags);
60         request.mask = new byte[maskLength];
61         request.skipNVectors = 0;
62         request.matchNVectors = maskLength / 16;
63
64         boolean aceIsEmpty =
65             destinationMacAddressMask(ace.getDestinationMacAddressMask(), ace.getDestinationMacAddress(), request);
66         aceIsEmpty &= sourceMacAddressMask(ace.getSourceMacAddressMask(), ace.getSourceMacAddress(), request);
67
68         // if we use classifier API, we need to know ip version (fields common for ip4 and ip6 have different offsets):
69         final AceIpVersion aceIpVersion = ace.getAceIpVersion();
70         checkArgument(aceIpVersion != null, "AceIpAndEth have to define IpVersion");
71
72         final int baseOffset = getVlanTagsLen(vlanTags);
73         if (aceIpVersion instanceof AceIpv4) {
74             final AceIpv4 ipVersion = (AceIpv4) aceIpVersion;
75             aceIsEmpty &= ip4Mask(baseOffset, mode, ace, ipVersion, request);
76         } else if (aceIpVersion instanceof AceIpv6) {
77             final AceIpv6 ipVersion = (AceIpv6) aceIpVersion;
78             aceIsEmpty &= ip6Mask(baseOffset, mode, ace, ipVersion, request);
79         } else {
80             throw new IllegalArgumentException(String.format("Unsupported IP version %s", aceIpVersion));
81         }
82
83         if (aceIsEmpty) {
84             throw new IllegalArgumentException(
85                 String.format("Ace %s does not define packet field match values", ace.toString()));
86         }
87
88         LOG.debug("ACE rule={} translated to table={}.", ace, request);
89         return request;
90     }
91
92     @Override
93     public List<ClassifyAddDelSession> createSession(@Nonnull final PacketHandling action,
94                                                      @Nonnull final AceIpAndEth ace,
95                                                      @Nullable final InterfaceMode mode, final int tableIndex,
96                                                      final int vlanTags) {
97         final List<PortPair> portPairs = PortPair.fromRange(ace.getSourcePortRange(), ace.getDestinationPortRange());
98         final List<ClassifyAddDelSession> requests = new ArrayList<>(portPairs.size());
99         for (final PortPair pair : portPairs) {
100             final ClassifyAddDelSession request = createSession(action, tableIndex);
101             request.match = new byte[maskLength(ace, vlanTags)];
102
103             boolean noMatch = destinationMacAddressMatch(ace.getDestinationMacAddress(), request);
104             noMatch &= sourceMacAddressMatch(ace.getSourceMacAddress(), request);
105
106             final AceIpVersion aceIpVersion = ace.getAceIpVersion();
107             checkArgument(aceIpVersion != null, "AceIpAndEth have to define IpVersion");
108
109             final int baseOffset = getVlanTagsLen(vlanTags);
110             if (aceIpVersion instanceof AceIpv4) {
111                 final AceIpv4 ipVersion = (AceIpv4) aceIpVersion;
112                 noMatch &= ip4Match(baseOffset, mode, ace, ipVersion, pair.getSrc(), pair.getDst(), request);
113             } else if (aceIpVersion instanceof AceIpv6) {
114                 final AceIpv6 ipVersion = (AceIpv6) aceIpVersion;
115                 noMatch &= ip6Match(baseOffset, mode, ace, ipVersion, pair.getSrc(), pair.getDst(), request);
116             } else {
117                 throw new IllegalArgumentException(String.format("Unsupported IP version %s", aceIpVersion));
118             }
119
120             if (noMatch) {
121                 throw new IllegalArgumentException(
122                     String.format("Ace %s does not define packet field match values", ace.toString()));
123             }
124             LOG.debug("ACE action={}, rule={} translated to session={}.", action, ace, request);
125             requests.add(request);
126         }
127         return requests;
128     }
129 }