b7da14ecf9bd4e3dcffe640a95bda4a8109fc55e
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / ingress / IngressIetfAclWriter.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 import static com.google.common.base.Preconditions.checkNotNull;
21 import static com.google.common.base.Preconditions.checkState;
22
23 import com.google.common.base.Optional;
24 import io.fd.honeycomb.translate.MappingContext;
25 import io.fd.honeycomb.translate.v3po.interfaces.acl.common.AbstractIetfAclWriter;
26 import io.fd.honeycomb.translate.v3po.interfaces.acl.common.AclTableContextManager;
27 import io.fd.honeycomb.translate.write.WriteContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
30 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
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.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.acl.mapping.entry.context.mapping.table.MappingEntry;
39 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;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.AccessLists;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.access.lists.Acl;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 public final class IngressIetfAclWriter extends AbstractIetfAclWriter {
46     private final AclTableContextManager aclCtx;
47
48     public IngressIetfAclWriter(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull AclTableContextManager aclCtx) {
49         super(futureJVppCore);
50         this.aclCtx = checkNotNull(aclCtx, "aclCtx should not be null");
51     }
52
53     @Override
54     public void deleteAcl(@Nonnull final InstanceIdentifier<?> id, final int swIfIndex,
55                           @Nonnull final MappingContext mappingContext)
56         throws WriteFailedException {
57         Optional<MappingEntry> optional = aclCtx.getEntry(swIfIndex, mappingContext);
58         checkState(optional.isPresent(), "Removing ACL id=%s, but acl mapping entry is not present", id);
59         final MappingEntry entry = optional.get();
60         unassignClassifyTables(id, entry);
61         removeClassifyTables(id, entry);
62         aclCtx.removeEntry(swIfIndex, mappingContext);
63     }
64
65     private void unassignClassifyTables(@Nonnull final InstanceIdentifier<?> id,
66                                         @Nonnull final MappingEntry entry)
67         throws WriteFailedException {
68         final InputAclSetInterface request = new InputAclSetInterface();
69         request.isAdd = 0;
70         request.swIfIndex = entry.getIndex();
71         request.l2TableIndex = entry.getL2TableId();
72         request.ip4TableIndex = entry.getIp4TableId();
73         request.ip6TableIndex = entry.getIp6TableId();
74         final CompletionStage<InputAclSetInterfaceReply> inputAclSetInterfaceReplyCompletionStage =
75             jvpp.inputAclSetInterface(request);
76         getReplyForDelete(inputAclSetInterfaceReplyCompletionStage.toCompletableFuture(), id);
77     }
78
79     @Override
80     public void write(@Nonnull final InstanceIdentifier<?> id, int swIfIndex, @Nonnull final List<Acl> acls,
81                       @Nonnull final AccessLists.DefaultAction defaultAction, @Nullable final 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
87         final InputAclSetInterface request = new InputAclSetInterface();
88         request.isAdd = 1;
89         request.swIfIndex = swIfIndex;
90         request.l2TableIndex = NOT_DEFINED;
91         request.ip4TableIndex = NOT_DEFINED;
92         request.ip6TableIndex = NOT_DEFINED;
93
94         if (InterfaceMode.L2.equals(mode)) {
95             final List<Ace> aces = getACEs(acls, writeContext, ace -> true);
96             request.l2TableIndex = writeAces(id, aces, defaultAction, mode, numberOfTags);
97         } else {
98             final List<Ace> ip4Aces = getACEs(acls, writeContext, (AbstractIetfAclWriter::appliesToIp4Path));
99             request.ip4TableIndex = writeAces(id, ip4Aces, defaultAction, mode, numberOfTags);
100             final List<Ace> ip6Aces = getACEs(acls, writeContext, (AbstractIetfAclWriter::appliesToIp6Path));
101             request.ip6TableIndex = writeAces(id, ip6Aces, defaultAction, mode, numberOfTags);
102         }
103
104         final MappingEntry entry = new MappingEntryBuilder().setIndex(swIfIndex)
105             .setIp4TableId(request.ip4TableIndex)
106             .setIp6TableId(request.ip6TableIndex)
107             .setL2TableId(request.l2TableIndex)
108             .build();
109         aclCtx.addEntry(entry, mappingContext);
110
111         try {
112             getReplyForWrite(jvpp.inputAclSetInterface(request).toCompletableFuture(), id);
113         } catch (WriteFailedException e) {
114             removeClassifyTables(id, entry);
115             throw e;
116         }
117     }
118 }