58a72ab30c0970567ba84ff8733a5d838d1d3e57
[hc2vpp.git] /
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.ingress;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
27 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
28 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
29 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
30 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterface;
31 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
32 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
33 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.ListIterator;
38 import java.util.Map;
39 import java.util.concurrent.CompletionStage;
40 import java.util.function.Predicate;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
43 import javax.annotation.Nonnegative;
44 import javax.annotation.Nonnull;
45 import javax.annotation.Nullable;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AclBase;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclKey;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntries;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace;
50 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;
51 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.AceType;
52 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.AceEth;
53 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;
54 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.AceIpVersion;
55 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;
56 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;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
58 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;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.AccessLists;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.access.lists.Acl;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 public final class IetfAclWriter implements JvppReplyConsumer, AclTranslator {
66
67     private static final Logger LOG = LoggerFactory.getLogger(IetfAclWriter.class);
68     private static final int NOT_DEFINED = -1;
69     private final FutureJVppCore jvpp;
70
71     private Map<AclType, AceWriter<? extends AceType>> aceWriters = new HashMap<>();
72
73     public IetfAclWriter(@Nonnull final FutureJVppCore futureJVppCore) {
74         this.jvpp = Preconditions.checkNotNull(futureJVppCore, "futureJVppCore should not be null");
75         aceWriters.put(AclType.ETH, new AceEthWriter());
76         aceWriters.put(AclType.IP4, new AceIp4Writer());
77         aceWriters.put(AclType.IP6, new AceIp6Writer());
78         aceWriters.put(AclType.ETH_AND_IP, new AceIpAndEthWriter());
79     }
80
81     private static Stream<Ace> aclToAceStream(@Nonnull final Acl assignedAcl,
82                                               @Nonnull final WriteContext writeContext) {
83         final String aclName = assignedAcl.getName();
84         final Class<? extends AclBase> aclType = assignedAcl.getType();
85
86         // ietf-acl updates are handled first, so we use writeContext.readAfter
87         final Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl>
88             aclOptional =
89             writeContext.readAfter(io.fd.honeycomb.translate.v3po.interfaces.acl.IetfAclWriter.ACL_ID.child(
90                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl.class,
91                 new AclKey(aclName, aclType)));
92         checkArgument(aclOptional.isPresent(), "Acl lists not configured");
93         final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl
94             acl = aclOptional.get();
95
96         final AccessListEntries accessListEntries = acl.getAccessListEntries();
97         checkArgument(accessListEntries != null, "access list entries not configured");
98
99         return accessListEntries.getAce().stream();
100     }
101
102     void deleteAcl(@Nonnull final InstanceIdentifier<?> id, final int swIfIndex)
103         throws WriteFailedException {
104         final ClassifyTableByInterface request = new ClassifyTableByInterface();
105         request.swIfIndex = swIfIndex;
106
107         final CompletionStage<ClassifyTableByInterfaceReply> cs = jvpp.classifyTableByInterface(request);
108         final ClassifyTableByInterfaceReply reply = getReplyForDelete(cs.toCompletableFuture(), id);
109
110         // We unassign and remove all ACL-related classify tables for given interface (we assume we are the only
111         // classify table manager)
112
113         unassignClassifyTables(id, reply);
114
115         removeClassifyTable(id, reply.l2TableId);
116         removeClassifyTable(id, reply.ip4TableId);
117         removeClassifyTable(id, reply.ip6TableId);
118     }
119
120     private void unassignClassifyTables(@Nonnull final InstanceIdentifier<?> id,
121                                         final ClassifyTableByInterfaceReply currentState)
122         throws WriteFailedException {
123         final InputAclSetInterface request = new InputAclSetInterface();
124         request.isAdd = 0;
125         request.swIfIndex = currentState.swIfIndex;
126         request.l2TableIndex = currentState.l2TableId;
127         request.ip4TableIndex = currentState.ip4TableId;
128         request.ip6TableIndex = currentState.ip6TableId;
129         final CompletionStage<InputAclSetInterfaceReply> inputAclSetInterfaceReplyCompletionStage =
130             jvpp.inputAclSetInterface(request);
131         getReplyForDelete(inputAclSetInterfaceReplyCompletionStage.toCompletableFuture(), id);
132     }
133
134     private void removeClassifyTable(@Nonnull final InstanceIdentifier<?> id, final int tableIndex)
135         throws WriteFailedException {
136
137         if (tableIndex == -1) {
138             return; // classify table id is absent
139         }
140         final ClassifyAddDelTable request = new ClassifyAddDelTable();
141         request.tableIndex = tableIndex;
142         final CompletionStage<ClassifyAddDelTableReply> cs = jvpp.classifyAddDelTable(request);
143         getReplyForDelete(cs.toCompletableFuture(), id);
144     }
145
146     void write(@Nonnull final InstanceIdentifier<?> id, final int swIfIndex, @Nonnull final List<Acl> acls,
147                final AccessLists.DefaultAction defaultAction, @Nullable final InterfaceMode mode,
148                @Nonnull final WriteContext writeContext)
149         throws WriteFailedException {
150         write(id, swIfIndex, mode, acls, defaultAction, writeContext, 0);
151     }
152
153     private static boolean appliesToIp4Path(final Ace ace) {
154         final AceType aceType = ace.getMatches().getAceType();
155         if (aceType instanceof AceIp && ((AceIp) aceType).getAceIpVersion() instanceof AceIpv4) {
156             return true;
157         }
158         if (aceType instanceof AceEth) {
159             return true;  // L2 only rules are possible for IP4 traffic
160         }
161         if (aceType instanceof AceIpAndEth && ((AceIpAndEth) aceType)
162             .getAceIpVersion() instanceof 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) {
163             return true;
164         }
165         return false;
166     }
167
168     private static boolean appliesToIp6Path(final Ace ace) {
169         final AceType aceType = ace.getMatches().getAceType();
170         if (aceType instanceof AceIp && ((AceIp) aceType).getAceIpVersion() instanceof AceIpv6) {
171             return true;
172         }
173         if (aceType instanceof AceEth) {
174             return true; // L2 only rules are possible for IP6 traffic
175         }
176         if (aceType instanceof AceIpAndEth && ((AceIpAndEth) aceType)
177             .getAceIpVersion() instanceof 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) {
178             return true;
179         }
180         return false;
181     }
182
183     void write(@Nonnull final InstanceIdentifier<?> id, final int swIfIndex, final InterfaceMode mode,
184                @Nonnull final List<Acl> acls, final AccessLists.DefaultAction defaultAction,
185                @Nonnull final WriteContext writeContext, @Nonnegative final int numberOfTags)
186         throws WriteFailedException {
187         checkArgument(numberOfTags >= 0 && numberOfTags <= 2, "Number of vlan tags %s is not in [0,2] range");
188
189         final InputAclSetInterface request = new InputAclSetInterface();
190         request.isAdd = 1;
191         request.swIfIndex = swIfIndex;
192         request.l2TableIndex = NOT_DEFINED;
193         request.ip4TableIndex = NOT_DEFINED;
194         request.ip6TableIndex = NOT_DEFINED;
195
196         if (InterfaceMode.L2.equals(mode)) {
197             final List<Ace> aces = getACEs(acls, writeContext, ace -> true);
198             request.l2TableIndex = writeAces(id, aces, defaultAction, mode, numberOfTags);
199         } else {
200             final List<Ace> ip4Aces = getACEs(acls, writeContext, (IetfAclWriter::appliesToIp4Path));
201             request.ip4TableIndex = writeAces(id, ip4Aces, defaultAction, mode, numberOfTags);
202             final List<Ace> ip6Aces = getACEs(acls, writeContext, (IetfAclWriter::appliesToIp6Path));
203             request.ip6TableIndex = writeAces(id, ip6Aces, defaultAction, mode, numberOfTags);
204         }
205
206         final CompletionStage<InputAclSetInterfaceReply> inputAclSetInterfaceReplyCompletionStage =
207             jvpp.inputAclSetInterface(request);
208         getReplyForWrite(inputAclSetInterfaceReplyCompletionStage.toCompletableFuture(), id);
209     }
210
211     private static List<Ace> getACEs(@Nonnull final List<Acl> acls, @Nonnull final WriteContext writeContext,
212                                      final Predicate<? super Ace> filter) {
213         return acls.stream().flatMap(acl -> aclToAceStream(acl, writeContext)).filter(filter)
214             .collect(Collectors.toList());
215     }
216
217     private int writeAces(final InstanceIdentifier<?> id, final List<Ace> aces,
218                           final AccessLists.DefaultAction defaultAction, final InterfaceMode mode,
219                           final int vlanTags) throws WriteFailedException {
220         if (aces.isEmpty()) {
221             return NOT_DEFINED;
222         }
223
224         int nextTableIndex = configureDefaultAction(id, defaultAction);
225         final ListIterator<Ace> iterator = aces.listIterator(aces.size());
226         while (iterator.hasPrevious()) {
227             final Ace ace = iterator.previous();
228             LOG.trace("Processing ACE: {}", ace);
229
230             final AceWriter aceWriter =
231                 aceWriters.get(AclType.fromAce(ace));
232             if (aceWriter == null) {
233                 LOG.warn("AceProcessor for {} not registered. Skipping ACE.", ace.getClass());
234             } else {
235                 final AceType aceType = ace.getMatches().getAceType();
236                 final PacketHandling action = ace.getActions().getPacketHandling();
237                 final ClassifyAddDelTable ctRequest = aceWriter.createTable(aceType, mode, nextTableIndex, vlanTags);
238                 nextTableIndex = createClassifyTable(id, ctRequest);
239                 final List<ClassifyAddDelSession> sessionRequests =
240                     aceWriter.createSession(action, aceType, mode, nextTableIndex, vlanTags);
241                 for (ClassifyAddDelSession csRequest : sessionRequests) {
242                     createClassifySession(id, csRequest);
243                 }
244             }
245         }
246         return nextTableIndex;
247     }
248
249     private int configureDefaultAction(@Nonnull final InstanceIdentifier<?> id,
250                                        final AccessLists.DefaultAction defaultAction)
251         throws WriteFailedException {
252         ClassifyAddDelTable ctRequest = createTable(-1);
253         if (AccessLists.DefaultAction.Permit.equals(defaultAction)) {
254             ctRequest.missNextIndex = -1;
255         } else {
256             ctRequest.missNextIndex = 0;
257         }
258         ctRequest.mask = new byte[16];
259         ctRequest.skipNVectors = 0;
260         ctRequest.matchNVectors = 1;
261         return createClassifyTable(id, ctRequest);
262     }
263
264     private int createClassifyTable(@Nonnull final InstanceIdentifier<?> id,
265                                     @Nonnull final ClassifyAddDelTable request)
266         throws WriteFailedException {
267         final CompletionStage<ClassifyAddDelTableReply> cs = jvpp.classifyAddDelTable(request);
268
269         final ClassifyAddDelTableReply reply = getReplyForWrite(cs.toCompletableFuture(), id);
270         return reply.newTableIndex;
271     }
272
273     private void createClassifySession(@Nonnull final InstanceIdentifier<?> id,
274                                        @Nonnull final ClassifyAddDelSession request)
275         throws WriteFailedException {
276         final CompletionStage<ClassifyAddDelSessionReply> cs = jvpp.classifyAddDelSession(request);
277
278         getReplyForWrite(cs.toCompletableFuture(), id);
279     }
280
281     private enum AclType {
282         ETH, IP4, IP6, ETH_AND_IP;
283
284         @Nonnull
285         private static AclType fromAce(final Ace ace) {
286             AclType result = null;
287             final AceType aceType;
288             try {
289                 aceType = ace.getMatches().getAceType();
290                 if (aceType instanceof AceEth) {
291                     result = ETH;
292                 } else if (aceType instanceof AceIp) {
293                     final AceIpVersion aceIpVersion = ((AceIp) aceType).getAceIpVersion();
294                     if (aceIpVersion instanceof AceIpv4) {
295                         result = IP4;
296                     } else {
297                         result = IP6;
298                     }
299                 } else if (aceType instanceof AceIpAndEth) {
300                     result = ETH_AND_IP;
301                 }
302             } catch (NullPointerException e) {
303                 throw new IllegalArgumentException("Incomplete ACE: " + ace, e);
304             }
305             if (result == null) {
306                 throw new IllegalArgumentException(String.format("Not supported ace type %s", aceType));
307             }
308             return result;
309         }
310     }
311 }