HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / common / AclTranslator.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.common;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
22 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
23 import javax.annotation.Nonnegative;
24 import javax.annotation.Nonnull;
25 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;
26 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;
27
28 /**
29  * Utility that helps translating of ietf-acl model ACEs to VPP's classify tables and sessions.
30  */
31 interface AclTranslator {
32     int TABLE_MEM_SIZE = 8 * 1024;
33     int VLAN_TAG_LEN = 4;
34
35     default ClassifyAddDelTable createTable(final int nextTableIndex) {
36         return createTable(nextTableIndex, 1);
37     }
38
39     default ClassifyAddDelTable createTable(final int nextTableIndex, @Nonnegative final int numberOfSessions) {
40         final ClassifyAddDelTable request = new ClassifyAddDelTable();
41         request.isAdd = 1;
42         request.tableIndex = -1; // value not present
43         request.nbuckets = numberOfSessions;
44         request.nextTableIndex = nextTableIndex;
45
46
47         // TODO: HONEYCOMB-181 minimise memory used by classify tables (we create a lot of them to make ietf-acl model
48         // mapping more convenient):
49         // according to https://wiki.fd.io/view/VPP/Introduction_To_N-tuple_Classifiers#Creating_a_classifier_table,
50         // classify table needs 16*(1 + match_n_vectors) bytes, but this does not quite work,
51         // so setting 8K +1k*numberOfSessions for now
52         checkArgument(numberOfSessions>0, "negative numberOfSessions %s", numberOfSessions);
53         request.memorySize = TABLE_MEM_SIZE+1024*(numberOfSessions-1);
54         request.missNextIndex = -1; // value not set, but anyway it is ignored for tables in chain
55         return request;
56     }
57
58     default ClassifyAddDelSession createSession(@Nonnull final PacketHandling action, final int tableIndex) {
59         final ClassifyAddDelSession request = new ClassifyAddDelSession();
60         request.isAdd = 1;
61         request.tableIndex = tableIndex;
62         request.opaqueIndex = ~0; // value not used
63
64         if (action instanceof Permit) {
65             request.hitNextIndex = -1;
66         } // deny (0) is default value
67
68         return request;
69     }
70
71     default int getVlanTagsLen(final int vlanTags) {
72         return vlanTags * VLAN_TAG_LEN;
73     }
74 }