HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / AceIp4Writer.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 com.google.common.annotations.VisibleForTesting;
22 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
23 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.annotation.Nonnull;
27 import javax.annotation.Nullable;
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.actions.PacketHandling;
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.AceIp;
30 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.AceIpv4;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 final class AceIp4Writer implements AceWriter<AceIp>, AclTranslator, Ip4AclTranslator {
36
37     @VisibleForTesting
38     static final int MATCH_N_VECTORS = 3; // number of 16B vectors
39     private static final int TABLE_MASK_LENGTH = 48;
40     private static final Logger LOG = LoggerFactory.getLogger(AceIp4Writer.class);
41
42     @Override
43     public ClassifyAddDelTable createTable(@Nonnull final AceIp aceIp,
44                                            @Nullable final InterfaceMode mode,
45                                            final int nextTableIndex,
46                                            final int vlanTags) {
47         checkArgument(aceIp.getAceIpVersion() instanceof AceIpv4, "Expected AceIpv4 version, but was %", aceIp);
48         final AceIpv4 ipVersion = (AceIpv4) aceIp.getAceIpVersion();
49
50         final int numberOfSessions = PortPair.fromRange(aceIp.getSourcePortRange(), aceIp.getDestinationPortRange()).size();
51         final ClassifyAddDelTable request = createTable(nextTableIndex, numberOfSessions);
52         request.skipNVectors = 0; // match entire L2 and L3 header
53         request.matchNVectors = MATCH_N_VECTORS;
54         request.mask = new byte[TABLE_MASK_LENGTH];
55
56         final int baseOffset = getVlanTagsLen(vlanTags);
57         boolean aceIsEmpty = ip4Mask(baseOffset, mode, aceIp, ipVersion, request);
58         if (aceIsEmpty) {
59             throw new IllegalArgumentException(
60                 String.format("Ace %s does not define packet field match values", aceIp.toString()));
61         }
62
63         LOG.debug("ACE rule={} translated to table={}.", aceIp, request);
64         return request;
65     }
66
67     @Override
68     public List<ClassifyAddDelSession> createSession(@Nonnull final PacketHandling action,
69                                                      @Nonnull final AceIp aceIp,
70                                                      @Nullable final InterfaceMode mode,
71                                                      final int tableIndex,
72                                                      final int vlanTags) {
73         checkArgument(aceIp.getAceIpVersion() instanceof AceIpv4, "Expected AceIpv4 version, but was %", aceIp);
74         final AceIpv4 ipVersion = (AceIpv4) aceIp.getAceIpVersion();
75
76         final List<PortPair> portPairs = PortPair.fromRange(aceIp.getSourcePortRange(), aceIp.getDestinationPortRange());
77         final List<ClassifyAddDelSession> requests = new ArrayList<>(portPairs.size());
78         for (final PortPair pair : portPairs) {
79             final ClassifyAddDelSession request = createSession(action, tableIndex);
80             request.match = new byte[TABLE_MASK_LENGTH];
81
82             final int baseOffset = getVlanTagsLen(vlanTags);
83             boolean noMatch = ip4Match(baseOffset, mode, aceIp, ipVersion, pair.getSrc(), pair.getDst(), request);
84             if (noMatch) {
85                 throw new IllegalArgumentException(
86                     String.format("Ace %s does not define packet field match values", aceIp.toString()));
87             }
88
89             LOG.debug("ACE action={}, rule={} translated to session={}.", action, aceIp, request);
90             requests.add(request);
91         }
92         return requests;
93     }
94 }