cad80022e755611e83b4be53b7815e79aa9970ca
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / AceIp6Writer.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.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv6;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 final class AceIp6Writer implements AceWriter<AceIp>, AclTranslator, Ip6AclTranslator {
35
36     private static final Logger LOG = LoggerFactory.getLogger(AceIp6Writer.class);
37
38     @Override
39     public ClassifyAddDelTable createTable(@Nonnull final AceIp aceIp,
40                                            @Nullable final InterfaceMode mode,
41                                            final int nextTableIndex,
42                                            final int vlanTags) {
43         checkArgument(aceIp.getAceIpVersion() instanceof AceIpv6, "Expected AceIpv6 version, but was %", aceIp);
44         final AceIpv6 ipVersion = (AceIpv6) aceIp.getAceIpVersion();
45
46         final int numberOfSessions = PortPair.fromRange(aceIp.getSourcePortRange(), aceIp.getDestinationPortRange()).size();
47         final ClassifyAddDelTable request = createTable(nextTableIndex, numberOfSessions);
48         request.skipNVectors = 0; // match entire L2 and L3 header
49         request.mask = new byte[getTableMaskLength(vlanTags)];
50         request.matchNVectors = request.mask.length/16;
51
52         final int baseOffset = getVlanTagsLen(vlanTags);
53         boolean aceIsEmpty = ip6Mask(baseOffset, mode, aceIp, ipVersion, request);
54         if (aceIsEmpty) {
55             throw new IllegalArgumentException(
56                 String.format("Ace %s does not define packet field match values", aceIp.toString()));
57         }
58
59         LOG.debug("ACE rule={} translated to table={}.", aceIp, request);
60         return request;
61     }
62
63     private static int getTableMaskLength(final int vlanTags) {
64         if (vlanTags == 2) {
65             return 80;
66         } else {
67             return 64;
68         }
69     }
70
71     @Override
72     public List<ClassifyAddDelSession> createSession(@Nonnull final PacketHandling action,
73                                                      @Nonnull final AceIp aceIp,
74                                                      @Nullable final InterfaceMode mode,
75                                                      final int tableIndex,
76                                                      final int vlanTags) {
77         checkArgument(aceIp.getAceIpVersion() instanceof AceIpv6, "Expected AceIpv6 version, but was %", aceIp);
78         final AceIpv6 ipVersion = (AceIpv6) aceIp.getAceIpVersion();
79         final List<PortPair> portPairs =
80             PortPair.fromRange(aceIp.getSourcePortRange(), aceIp.getDestinationPortRange());
81
82         final List<ClassifyAddDelSession> requests = new ArrayList<>(portPairs.size());
83         for (final PortPair pair : portPairs) {
84             final ClassifyAddDelSession request = createSession(action, tableIndex);
85             request.match = new byte[getTableMaskLength(vlanTags)];
86
87             final int baseOffset = getVlanTagsLen(vlanTags);
88             boolean noMatch = ip6Match(baseOffset, mode, aceIp, ipVersion, pair.getSrc(), pair.getDst(), request);
89             if (noMatch) {
90                 throw new IllegalArgumentException(
91                     String.format("Ace %s does not define packet field match values", aceIp.toString()));
92             }
93
94             LOG.debug("ACE action={}, rule={} translated to session={}.", action, aceIp, request);
95             requests.add(request);
96         }
97         return requests;
98     }
99 }