HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / ingress / 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.ingress;
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.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.InputAclSetInterface;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import java.util.List;
33 import java.util.concurrent.CompletionStage;
34 import java.util.stream.Collector;
35 import javax.annotation.Nonnegative;
36 import javax.annotation.Nonnull;
37 import javax.annotation.Nullable;
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;
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.actions.PacketHandling;
40 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;
41 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;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.InterfaceMode;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 /**
46  * Base writer for translation of ietf-acl model ACEs to VPP's classify tables and sessions. <p/> Creates one classify
47  * 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 mode           interface mode
77      * @param nextTableIndex classify table index
78      * @param vlanTags       number of vlan tags
79      * @return classify table that represents given ACE
80      */
81     protected abstract ClassifyAddDelTable createClassifyTable(@Nonnull final PacketHandling action,
82                                                                @Nonnull final T ace,
83                                                                @Nullable final InterfaceMode mode,
84                                                                final int nextTableIndex,
85                                                                final int vlanTags);
86
87     /**
88      * Creates classify session for given ACE.
89      *
90      * @param action     packet handling action (permit/deny)
91      * @param ace        ACE to be translated
92      * @param mode           interface mode
93      * @param tableIndex classify table index for the given session
94      * @param vlanTags   number of vlan tags
95      * @return classify session that represents given ACE
96      */
97     protected abstract ClassifyAddDelSession createClassifySession(@Nonnull final PacketHandling action,
98                                                                    @Nonnull final T ace,
99                                                                    @Nullable final InterfaceMode mode,
100                                                                    final int tableIndex,
101                                                                    final int vlanTags);
102
103     /**
104      * Sets classify table index for input_acl_set_interface request.
105      *
106      * @param request    request DTO
107      * @param tableIndex pointer to a chain of classify tables
108      */
109     protected abstract void setClassifyTable(@Nonnull final InputAclSetInterface request, final int tableIndex);
110
111     @Override
112     public final void write(@Nonnull final InstanceIdentifier<?> id, @Nonnull final List<Ace> aces,
113                             final InterfaceMode mode, @Nonnull final InputAclSetInterface request,
114                             @Nonnegative final int vlanTags)
115         throws WriteFailedException {
116         final PacketHandling action = aces.stream().map(ace -> ace.getActions().getPacketHandling()).distinct()
117             .collect(SINGLE_ITEM_COLLECTOR);
118
119         checkArgument(vlanTags >= 0 && vlanTags <= 2, "Number of vlan tags %s is not in [0,2] range");
120         int nextTableIndex = -1;
121         for (final Ace ace : aces) {
122             // Create table + session per entry
123
124             final ClassifyAddDelTable ctRequest =
125                 createClassifyTable(action, (T) ace.getMatches().getAceType(), mode, nextTableIndex, vlanTags);
126             nextTableIndex = createClassifyTable(id, ctRequest);
127             createClassifySession(id,
128                 createClassifySession(action, (T) ace.getMatches().getAceType(), mode, nextTableIndex, vlanTags));
129         }
130         setClassifyTable(request, nextTableIndex);
131     }
132
133     private int createClassifyTable(@Nonnull final InstanceIdentifier<?> id,
134                                     @Nonnull final ClassifyAddDelTable request)
135         throws WriteFailedException {
136         final CompletionStage<ClassifyAddDelTableReply> cs = futureJVppCore.classifyAddDelTable(request);
137
138         final ClassifyAddDelTableReply reply = getReplyForWrite(cs.toCompletableFuture(), id);
139         return reply.newTableIndex;
140     }
141
142     private void createClassifySession(@Nonnull final InstanceIdentifier<?> id,
143                                        @Nonnull final ClassifyAddDelSession request)
144         throws WriteFailedException {
145         final CompletionStage<ClassifyAddDelSessionReply> cs = futureJVppCore.classifyAddDelSession(request);
146
147         getReplyForWrite(cs.toCompletableFuture(), id);
148     }
149
150     protected ClassifyAddDelTable createClassifyTable(@Nonnull final PacketHandling action, final int nextTableIndex) {
151         final ClassifyAddDelTable request = new ClassifyAddDelTable();
152         request.isAdd = 1;
153         request.tableIndex = -1; // value not present
154
155         request.nbuckets = 1; // we expect exactly one session per table
156
157         if (action instanceof Permit) {
158             request.missNextIndex = 0; // for list of permit rules, deny (0) should be default action
159         } else { // deny is default value
160             request.missNextIndex = -1; // for list of deny rules, permit (-1) should be default action
161         }
162
163         request.nextTableIndex = nextTableIndex;
164         request.memorySize = TABLE_MEM_SIZE;
165
166         return request;
167     }
168
169     protected ClassifyAddDelSession createClassifySession(@Nonnull final PacketHandling action, final int tableIndex) {
170         final ClassifyAddDelSession request = new ClassifyAddDelSession();
171         request.isAdd = 1;
172         request.tableIndex = tableIndex;
173         request.opaqueIndex = ~0; // value not used
174
175         if (action instanceof Permit) {
176             request.hitNextIndex = -1;
177         } // deny (0) is default value
178
179         return request;
180     }
181
182     protected int getVlanTagsLen(final int vlanTags) {
183         return vlanTags * VLAN_TAG_LEN;
184     }
185 }