e04371ec58f4263601359a325b5154873d1f5265
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / AbstractAceWriter.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;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import com.google.common.annotations.VisibleForTesting;
23 import io.fd.honeycomb.translate.util.RWUtils;
24 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
25 import io.fd.honeycomb.translate.vpp.util.WriteTimeoutException;
26 import java.util.List;
27 import java.util.concurrent.CompletionStage;
28 import java.util.stream.Collector;
29 import javax.annotation.Nonnegative;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace;
32 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;
33 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.packet.handling.Permit;
34 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;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import io.fd.vpp.jvpp.VppBaseCallException;
37 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
38 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
39 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
40 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
41 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
42 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
43
44 /**
45  * Base writer for translation of ietf-acl model ACEs to VPP's classify tables and sessions.
46  * <p/>
47  * Creates one classify table with single session per ACE.
48  *
49  * @param <T> type of access control list entry
50  */
51 abstract class AbstractAceWriter<T extends AceType> implements AceWriter, JvppReplyConsumer {
52
53     // TODO: HONEYCOMB-181 minimise memory used by classify tables (we create a lot of them to make ietf-acl model
54     // mapping more convenient):
55     // according to https://wiki.fd.io/view/VPP/Introduction_To_N-tuple_Classifiers#Creating_a_classifier_table,
56     // classify table needs 16*(1 + match_n_vectors) bytes, but this does not quite work, so setting 8K for now
57     protected static final int TABLE_MEM_SIZE = 8 * 1024;
58
59     @VisibleForTesting
60     static final int VLAN_TAG_LEN = 4;
61
62     private static final Collector<PacketHandling, ?, PacketHandling> SINGLE_ITEM_COLLECTOR =
63             RWUtils.singleItemCollector();
64
65     private final FutureJVppCore futureJVppCore;
66
67     public AbstractAceWriter(@Nonnull final FutureJVppCore futureJVppCore) {
68         this.futureJVppCore = checkNotNull(futureJVppCore, "futureJVppCore should not be null");
69     }
70
71     /**
72      * Creates classify table for given ACE.
73      *
74      * @param action         packet handling action (permit/deny)
75      * @param ace            ACE to be translated
76      * @param nextTableIndex classify table index
77      * @param vlanTags       number of vlan tags
78      * @return classify table that represents given ACE
79      */
80     protected abstract ClassifyAddDelTable createClassifyTable(@Nonnull final PacketHandling action,
81                                                                @Nonnull final T ace,
82                                                                final int nextTableIndex,
83                                                                final int vlanTags);
84
85     /**
86      * Creates classify session for given ACE.
87      *
88      * @param action     packet handling action (permit/deny)
89      * @param ace        ACE to be translated
90      * @param tableIndex classify table index for the given session
91      * @param vlanTags   number of vlan tags
92      * @return classify session that represents given ACE
93      */
94     protected abstract ClassifyAddDelSession createClassifySession(@Nonnull final PacketHandling action,
95                                                                    @Nonnull final T ace,
96                                                                    final int tableIndex,
97                                                                    final int vlanTags);
98
99     /**
100      * Sets classify table index for input_acl_set_interface request.
101      *
102      * @param request    request DTO
103      * @param tableIndex pointer to a chain of classify tables
104      */
105     protected abstract void setClassifyTable(@Nonnull final InputAclSetInterface request, final int tableIndex);
106
107     @Override
108     public final void write(@Nonnull final InstanceIdentifier<?> id, @Nonnull final List<Ace> aces,
109                             @Nonnull final InputAclSetInterface request, @Nonnegative final int vlanTags)
110             throws VppBaseCallException, WriteTimeoutException {
111         final PacketHandling action = aces.stream().map(ace -> ace.getActions().getPacketHandling()).distinct()
112                 .collect(SINGLE_ITEM_COLLECTOR);
113
114         checkArgument(vlanTags >= 0 && vlanTags <= 2, "Number of vlan tags %s is not in [0,2] range");
115
116         int nextTableIndex = -1;
117         for (final Ace ace : aces) {
118             // Create table + session per entry
119
120             final ClassifyAddDelTable ctRequest =
121                     createClassifyTable(action, (T) ace.getMatches().getAceType(), nextTableIndex, vlanTags);
122             nextTableIndex = createClassifyTable(id, ctRequest);
123             createClassifySession(id,
124                     createClassifySession(action, (T) ace.getMatches().getAceType(), nextTableIndex, vlanTags));
125         }
126         setClassifyTable(request, nextTableIndex);
127     }
128
129     private int createClassifyTable(@Nonnull final InstanceIdentifier<?> id,
130                                     @Nonnull final ClassifyAddDelTable request)
131             throws VppBaseCallException, WriteTimeoutException {
132         final CompletionStage<ClassifyAddDelTableReply> cs = futureJVppCore.classifyAddDelTable(request);
133
134         final ClassifyAddDelTableReply reply = getReplyForWrite(cs.toCompletableFuture(), id);
135         return reply.newTableIndex;
136     }
137
138     private void createClassifySession(@Nonnull final InstanceIdentifier<?> id,
139                                        @Nonnull final ClassifyAddDelSession request)
140             throws VppBaseCallException, WriteTimeoutException {
141         final CompletionStage<ClassifyAddDelSessionReply> cs = futureJVppCore.classifyAddDelSession(request);
142
143         getReplyForWrite(cs.toCompletableFuture(), id);
144     }
145
146     protected ClassifyAddDelTable createClassifyTable(@Nonnull final PacketHandling action, final int nextTableIndex) {
147         final ClassifyAddDelTable request = new ClassifyAddDelTable();
148         request.isAdd = 1;
149         request.tableIndex = -1; // value not present
150
151         request.nbuckets = 1; // we expect exactly one session per table
152
153         if (action instanceof Permit) {
154             request.missNextIndex = 0; // for list of permit rules, deny (0) should be default action
155         } else { // deny is default value
156             request.missNextIndex = -1; // for list of deny rules, permit (-1) should be default action
157         }
158
159         request.nextTableIndex = nextTableIndex;
160         request.memorySize = TABLE_MEM_SIZE;
161
162         return request;
163     }
164
165     protected ClassifyAddDelSession createClassifySession(@Nonnull final PacketHandling action, final int tableIndex) {
166         final ClassifyAddDelSession request = new ClassifyAddDelSession();
167         request.isAdd = 1;
168         request.tableIndex = tableIndex;
169         request.opaqueIndex = ~0; // value not used
170
171         if (action instanceof Permit) {
172             request.hitNextIndex = -1;
173         } // deny (0) is default value
174
175         return request;
176     }
177
178     protected int getVlanTagsLen(final int vlanTags) {
179         return vlanTags * VLAN_TAG_LEN;
180     }
181 }