Move interface acls to separate yang module
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / ingress / IetfAClWriter.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.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.v3po.interfaces.acl.IetfAclWriter;
24 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
28 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
29 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterface;
30 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
31 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
32 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.concurrent.CompletionStage;
38 import java.util.stream.Collectors;
39 import java.util.stream.Stream;
40 import javax.annotation.Nonnegative;
41 import javax.annotation.Nonnull;
42 import javax.annotation.Nullable;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AclBase;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclKey;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntries;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace;
47 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;
48 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;
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.matches.ace.type.AceIp;
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.matches.ace.type.ace.ip.AceIpVersion;
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.ace.type.ace.ip.ace.ip.version.AceIpv4;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.access.lists.Acl;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.AccessLists;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public final class IetfAClWriter implements JvppReplyConsumer {
60
61     private static final Logger LOG = LoggerFactory.getLogger(IetfAClWriter.class);
62     private final FutureJVppCore jvpp;
63
64     private Map<AclType, AceWriter> aceWriters = new HashMap<>();
65
66     public IetfAClWriter(@Nonnull final FutureJVppCore futureJVppCore) {
67         this.jvpp = Preconditions.checkNotNull(futureJVppCore, "futureJVppCore should not be null");
68         aceWriters.put(AclType.ETH, new AceEthWriter(futureJVppCore));
69         aceWriters.put(AclType.IP4, new AceIp4Writer(futureJVppCore));
70         aceWriters.put(AclType.IP6, new AceIp6Writer(futureJVppCore));
71     }
72
73     private static Stream<Ace> aclToAceStream(@Nonnull final Acl assignedAcl,
74                                               @Nonnull final WriteContext writeContext) {
75         final String aclName = assignedAcl.getName();
76         final Class<? extends AclBase> aclType = assignedAcl.getType();
77
78         // ietf-acl updates are handled first, so we use writeContext.readAfter
79         final Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl>
80                 aclOptional = writeContext.readAfter(IetfAclWriter.ACL_ID.child(
81                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl.class,
82                 new AclKey(aclName, aclType)));
83         checkArgument(aclOptional.isPresent(), "Acl lists not configured");
84         final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl
85                 acl = aclOptional.get();
86
87         final AccessListEntries accessListEntries = acl.getAccessListEntries();
88         checkArgument(accessListEntries != null, "access list entries not configured");
89
90         return accessListEntries.getAce().stream();
91     }
92
93     void deleteAcl(@Nonnull final InstanceIdentifier<?> id, final int swIfIndex)
94             throws WriteFailedException {
95         final ClassifyTableByInterface request = new ClassifyTableByInterface();
96         request.swIfIndex = swIfIndex;
97
98         final CompletionStage<ClassifyTableByInterfaceReply> cs = jvpp.classifyTableByInterface(request);
99         final ClassifyTableByInterfaceReply reply = getReplyForDelete(cs.toCompletableFuture(), id);
100
101         // We unassign and remove all ACL-related classify tables for given interface (we assume we are the only
102         // classify table manager)
103
104         unassignClassifyTables(id, reply);
105
106         removeClassifyTable(id, reply.l2TableId);
107         removeClassifyTable(id, reply.ip4TableId);
108         removeClassifyTable(id, reply.ip6TableId);
109     }
110
111     private void unassignClassifyTables(@Nonnull final InstanceIdentifier<?> id,
112                                         final ClassifyTableByInterfaceReply currentState)
113             throws WriteFailedException {
114         final InputAclSetInterface request = new InputAclSetInterface();
115         request.isAdd = 0;
116         request.swIfIndex = currentState.swIfIndex;
117         request.l2TableIndex = currentState.l2TableId;
118         request.ip4TableIndex = currentState.ip4TableId;
119         request.ip6TableIndex = currentState.ip6TableId;
120         final CompletionStage<InputAclSetInterfaceReply> inputAclSetInterfaceReplyCompletionStage =
121                 jvpp.inputAclSetInterface(request);
122         getReplyForDelete(inputAclSetInterfaceReplyCompletionStage.toCompletableFuture(), id);
123     }
124
125     private void removeClassifyTable(@Nonnull final InstanceIdentifier<?> id, final int tableIndex)
126             throws WriteFailedException {
127
128         if (tableIndex == -1) {
129             return; // classify table id is absent
130         }
131         final ClassifyAddDelTable request = new ClassifyAddDelTable();
132         request.tableIndex = tableIndex;
133         final CompletionStage<ClassifyAddDelTableReply> cs = jvpp.classifyAddDelTable(request);
134         getReplyForDelete(cs.toCompletableFuture(), id);
135     }
136
137     void write(@Nonnull final InstanceIdentifier<?> id, final int swIfIndex, @Nonnull final List<Acl> acls,
138                final AccessLists.DefaultAction defaultAction, @Nullable final InterfaceMode mode, @Nonnull final WriteContext writeContext)
139             throws WriteFailedException {
140         write(id, swIfIndex, mode, acls, defaultAction, writeContext, 0);
141     }
142
143     void write(@Nonnull final InstanceIdentifier<?> id, final int swIfIndex, final InterfaceMode mode,
144                @Nonnull final List<Acl> acls, final AccessLists.DefaultAction defaultAction,
145                @Nonnull final WriteContext writeContext, @Nonnegative final int numberOfTags)
146             throws WriteFailedException {
147
148         // filter ACE entries and group by AceType
149         final Map<AclType, List<Ace>> acesByType = acls.stream()
150                 .flatMap(acl -> aclToAceStream(acl, writeContext))
151                 .collect(Collectors.groupingBy(AclType::fromAce));
152
153         final InputAclSetInterface request = new InputAclSetInterface();
154         request.isAdd = 1;
155         request.swIfIndex = swIfIndex;
156         request.l2TableIndex = -1;
157         request.ip4TableIndex = -1;
158         request.ip6TableIndex = -1;
159
160         // for each AceType:
161         for (Map.Entry<AclType, List<Ace>> entry : acesByType.entrySet()) {
162             final AclType aceType = entry.getKey();
163             final List<Ace> aces = entry.getValue();
164             LOG.trace("Processing ACEs of {} type: {}", aceType, aces);
165
166             final AceWriter aceWriter = aceWriters.get(aceType);
167             if (aceWriter == null) {
168                 LOG.warn("AceProcessor for {} not registered. Skipping ACE.", aceType);
169             } else {
170                 aceWriter.write(id, aces, mode, defaultAction, request, numberOfTags);
171             }
172         }
173
174         final CompletionStage<InputAclSetInterfaceReply> inputAclSetInterfaceReplyCompletionStage =
175                 jvpp.inputAclSetInterface(request);
176         getReplyForWrite(inputAclSetInterfaceReplyCompletionStage.toCompletableFuture(), id);
177     }
178
179     private enum AclType {
180         ETH, IP4, IP6;
181
182         @Nonnull
183         private static AclType fromAce(final Ace ace) {
184             AclType result = null;
185             final AceType aceType;
186             try {
187                 aceType = ace.getMatches().getAceType();
188                 if (aceType instanceof AceEth) {
189                     result = ETH;
190                 } else if (aceType instanceof AceIp) {
191                     final AceIpVersion aceIpVersion = ((AceIp) aceType).getAceIpVersion();
192                     if (aceIpVersion instanceof AceIpv4) {
193                         result = IP4;
194                     } else {
195                         result = IP6;
196                     }
197                 }
198             } catch (NullPointerException e) {
199                 throw new IllegalArgumentException("Incomplete ACE: " + ace, e);
200             }
201             if (result == null) {
202                 throw new IllegalArgumentException(String.format("Not supported ace type %s", aceType));
203             }
204             return result;
205         }
206     }
207 }