2 * Copyright (c) 2017 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.hc2vpp.vpp.classifier.write.acl.egress;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static com.google.common.base.Preconditions.checkState;
23 import com.google.common.base.Optional;
24 import io.fd.hc2vpp.vpp.classifier.write.acl.common.AbstractIetfAclWriter;
25 import io.fd.hc2vpp.vpp.classifier.write.acl.common.AclTableContextManager;
26 import io.fd.honeycomb.translate.MappingContext;
27 import io.fd.honeycomb.translate.write.WriteContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.vpp.jvpp.core.dto.ClassifySetInterfaceL2Tables;
30 import io.fd.vpp.jvpp.core.dto.ClassifySetInterfaceL2TablesReply;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import java.util.List;
33 import java.util.concurrent.CompletionStage;
34 import javax.annotation.Nonnegative;
35 import javax.annotation.Nonnull;
36 import javax.annotation.Nullable;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace;
38 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;
39 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;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.acl.mapping.entry.context.mapping.table.MappingEntry;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.acl.mapping.entry.context.mapping.table.MappingEntryBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.InterfaceMode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.ietf.acl.base.attributes.AccessLists;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.ietf.acl.base.attributes.access.lists.Acl;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 public final class EgressIetfAclWriter extends AbstractIetfAclWriter {
48 private final AclTableContextManager aclCtx;
50 public EgressIetfAclWriter(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull AclTableContextManager aclCtx) {
51 super(futureJVppCore);
52 this.aclCtx = checkNotNull(aclCtx, "aclCtx should not be null");
56 public void deleteAcl(@Nonnull final InstanceIdentifier<?> id, final int swIfIndex,
57 @Nonnull final MappingContext mappingContext)
58 throws WriteFailedException {
59 Optional<MappingEntry> optional = aclCtx.getEntry(swIfIndex, mappingContext);
60 checkState(optional.isPresent(), "Removing ACL id=%s, but acl mapping entry is not present", id);
61 final MappingEntry entry = optional.get();
62 unassignClassifyTables(id, swIfIndex);
63 removeClassifyTables(id, entry);
64 aclCtx.removeEntry(swIfIndex, mappingContext);
67 private void unassignClassifyTables(@Nonnull final InstanceIdentifier<?> id, final int swIfIndex)
68 throws WriteFailedException {
69 final ClassifySetInterfaceL2Tables request = new ClassifySetInterfaceL2Tables();
70 request.swIfIndex = swIfIndex;
71 request.ip4TableIndex = NOT_DEFINED;
72 request.ip6TableIndex = NOT_DEFINED;
73 request.otherTableIndex = NOT_DEFINED;
74 request.isInput = 0; // egress
75 final CompletionStage<ClassifySetInterfaceL2TablesReply> cs = jvpp.classifySetInterfaceL2Tables(request);
76 getReplyForDelete(cs.toCompletableFuture(), id);
80 public void write(@Nonnull final InstanceIdentifier<?> id, int swIfIndex, @Nonnull final List<Acl> acls,
81 @Nonnull final AccessLists.DefaultAction defaultAction, @Nullable InterfaceMode mode,
82 @Nonnull final WriteContext writeContext, @Nonnegative final int numberOfTags,
83 @Nonnull final MappingContext mappingContext)
84 throws WriteFailedException {
85 checkArgument(numberOfTags >= 0 && numberOfTags <= 2, "Number of vlan tags %s is not in [0,2] range");
86 checkArgument(InterfaceMode.L2.equals(mode), "Writing egress Acls is supported only in L2 mode");
88 final ClassifySetInterfaceL2Tables request = new ClassifySetInterfaceL2Tables();
89 request.isInput = 0; // egress
90 request.swIfIndex = swIfIndex;
92 // applied to packets according to their ether type
93 final List<Ace> ip4Aces = getACEs(acls, writeContext, (AbstractIetfAclWriter::appliesToIp4Path));
94 request.ip4TableIndex = writeAces(id, ip4Aces, defaultAction, mode, numberOfTags);
95 final List<Ace> ip6Aces = getACEs(acls, writeContext, (AbstractIetfAclWriter::appliesToIp6Path));
96 request.ip6TableIndex = writeAces(id, ip6Aces, defaultAction, mode, numberOfTags);
97 final List<Ace> aces = getACEs(acls, writeContext, EgressIetfAclWriter::isNotIpRule);
98 request.otherTableIndex = writeAces(id, aces, defaultAction, mode, numberOfTags);
100 final MappingEntry entry = new MappingEntryBuilder().setIndex(swIfIndex)
101 .setIp4TableId(request.ip4TableIndex)
102 .setIp6TableId(request.ip6TableIndex)
103 .setL2TableId(request.otherTableIndex)
105 aclCtx.addEntry(entry, mappingContext);
108 getReplyForWrite(jvpp.classifySetInterfaceL2Tables(request).toCompletableFuture(), id);
109 } catch (WriteFailedException e) {
110 removeClassifyTables(id, entry);
115 private static boolean isNotIpRule(final Ace ace) {
116 final Matches matches = ace.getMatches();
117 checkArgument(matches != null, "Incomplete ACE: %s", ace);
118 return matches.getAceType() instanceof AceEth;