VPP-330 Track pending map-requests with a fifo
[vpp.git] / vpp-api / java / jvpp-core / org / openvpp / jvpp / core / test / L2AclTest.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 org.openvpp.jvpp.core.test;
18
19 import java.util.Arrays;
20 import javax.xml.bind.DatatypeConverter;
21 import org.openvpp.jvpp.JVpp;
22 import org.openvpp.jvpp.JVppRegistry;
23 import org.openvpp.jvpp.JVppRegistryImpl;
24 import org.openvpp.jvpp.core.JVppCoreImpl;
25 import org.openvpp.jvpp.core.dto.ClassifyAddDelSession;
26 import org.openvpp.jvpp.core.dto.ClassifyAddDelSessionReply;
27 import org.openvpp.jvpp.core.dto.ClassifyAddDelTable;
28 import org.openvpp.jvpp.core.dto.ClassifyAddDelTableReply;
29 import org.openvpp.jvpp.core.dto.ClassifySessionDetails;
30 import org.openvpp.jvpp.core.dto.ClassifySessionDetailsReplyDump;
31 import org.openvpp.jvpp.core.dto.ClassifySessionDump;
32 import org.openvpp.jvpp.core.dto.ClassifyTableByInterface;
33 import org.openvpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
34 import org.openvpp.jvpp.core.dto.ClassifyTableIds;
35 import org.openvpp.jvpp.core.dto.ClassifyTableIdsReply;
36 import org.openvpp.jvpp.core.dto.ClassifyTableInfo;
37 import org.openvpp.jvpp.core.dto.ClassifyTableInfoReply;
38 import org.openvpp.jvpp.core.dto.InputAclSetInterface;
39 import org.openvpp.jvpp.core.dto.InputAclSetInterfaceReply;
40 import org.openvpp.jvpp.core.future.FutureJVppCoreFacade;
41
42 /**
43  * <p>Tests L2 ACL creation and read.<br> Equivalent to the following vppctl commands:<br>
44  *
45  * <pre>{@code
46  * vppctl classify table mask l2 src
47  * vppctl classify session acl-hit-next deny opaque-index 0 table-index 0 match l2 src 01:02:03:04:05:06
48  * vppctl set int input acl intfc local0 l2-table 0
49  * vppctl sh class table verbose
50  * }
51  * </pre>
52  */
53 public class L2AclTest {
54
55     private static final int LOCAL0_IFACE_ID = 0;
56
57     private static ClassifyAddDelTable createClassifyTable() {
58         ClassifyAddDelTable request = new ClassifyAddDelTable();
59         request.isAdd = 1;
60         request.tableIndex = ~0; // default
61         request.nbuckets = 2;
62         request.memorySize = 2 << 20;
63         request.nextTableIndex = ~0; // default
64         request.missNextIndex = ~0; // default
65         request.skipNVectors = 0;
66         request.matchNVectors = 1;
67         request.mask =
68             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
69                 (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00};
70         return request;
71     }
72
73     private static ClassifyTableInfo createClassifyTableInfoRequest(final int tableId) {
74         ClassifyTableInfo request = new ClassifyTableInfo();
75         request.tableId = tableId;
76         return request;
77     }
78
79     private static ClassifyAddDelSession createClassifySession(final int tableIndex) {
80         ClassifyAddDelSession request = new ClassifyAddDelSession();
81         request.isAdd = 1;
82         request.tableIndex = tableIndex;
83         request.hitNextIndex = 0; // deny
84         request.opaqueIndex = 0;
85         request.advance = 0; // default
86         // match 01:02:03:04:05:06 mac address
87         request.match =
88             new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
89                 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
90         return request;
91     }
92
93     private static ClassifySessionDump createClassifySessionDumpRequest(final int newTableIndex) {
94         ClassifySessionDump request = new ClassifySessionDump();
95         request.tableId = newTableIndex;
96         return request;
97     }
98
99     private static InputAclSetInterface aclSetInterface() {
100         InputAclSetInterface request = new InputAclSetInterface();
101         request.isAdd = 1;
102         request.swIfIndex = LOCAL0_IFACE_ID;
103         request.ip4TableIndex = ~0; // skip
104         request.ip6TableIndex = ~0; // skip
105         request.l2TableIndex = 0;
106         return request;
107     }
108
109     private static ClassifyTableByInterface createClassifyTableByInterfaceRequest() {
110         ClassifyTableByInterface request = new ClassifyTableByInterface();
111         request.swIfIndex = LOCAL0_IFACE_ID;
112         return request;
113     }
114
115     private static void print(ClassifyAddDelTableReply reply) {
116         System.out.printf("ClassifyAddDelTableReply: context=%d, "
117                 + "newTableIndex=%d, skipNVectors=%d, matchNVectors=%d\n",
118             reply.context, reply.newTableIndex, reply.skipNVectors, reply.matchNVectors);
119     }
120
121     private static void print(ClassifyTableIdsReply reply) {
122         System.out.printf("ClassifyTableIdsReply: context=%d, count=%d, ids.length=%d\n",
123             reply.context, reply.count, reply.ids.length);
124         Arrays.stream(reply.ids).forEach(System.out::println);
125     }
126
127     private static void print(final ClassifyTableInfoReply reply) {
128         final StringBuilder builder = new StringBuilder("ClassifyTableInfoReply:\n");
129         builder.append("context: ").append(reply.context).append('\n');
130         builder.append("tableId: ").append(reply.tableId).append('\n');
131         builder.append("nbuckets: ").append(reply.nbuckets).append('\n');
132         builder.append("matchNVectors: ").append(reply.matchNVectors).append('\n');
133         builder.append("skipNVectors: ").append(reply.skipNVectors).append('\n');
134         builder.append("activeSessions: ").append(reply.activeSessions).append('\n');
135         builder.append("nextTableIndex: ").append(reply.nextTableIndex).append('\n');
136         builder.append("missNextIndex: ").append(reply.missNextIndex).append('\n');
137         builder.append("maskLength: ").append(reply.maskLength).append('\n');
138         builder.append("mask: ").append(DatatypeConverter.printHexBinary(reply.mask)).append('\n');
139         System.out.println(builder.toString());
140     }
141
142     private static void print(ClassifyAddDelSessionReply reply) {
143         System.out.printf("ClassifyAddDelSessionReply: context=%d\n",
144             reply.context);
145     }
146
147     private static void print(final ClassifySessionDetailsReplyDump reply) {
148         if (reply.classifySessionDetails == null) {
149             System.out.println("ClassifySessionDetailsReplyDump: classifySessionDetails == NULL");
150         }
151         for (final ClassifySessionDetails details : reply.classifySessionDetails) {
152             final StringBuilder builder = new StringBuilder("ClassifySessionDetails:\n");
153             builder.append("context: ").append(details.context).append('\n');
154             builder.append("tableId: ").append(details.tableId).append('\n');
155             builder.append("hitNextIndex: ").append(details.hitNextIndex).append('\n');
156             builder.append("advance: ").append(details.advance).append('\n');
157             builder.append("opaqueIndex: ").append(details.opaqueIndex).append('\n');
158             builder.append("matchLength: ").append(details.matchLength).append('\n');
159             builder.append("match: ").append(DatatypeConverter.printHexBinary(details.match)).append('\n');
160             System.out.println(builder.toString());
161         }
162     }
163
164     private static void print(final InputAclSetInterfaceReply reply) {
165         System.out.printf("InputAclSetInterfaceReply: context=%d\n", reply.context);
166     }
167
168     private static void print(final ClassifyTableByInterfaceReply reply) {
169         System.out.printf("ClassifyAddDelTableReply: context=%d, swIfIndex=%d, l2TableId=%d, ip4TableId=%d,"
170             + "ip6TableId=%d\n", reply.context, reply.swIfIndex, reply.l2TableId, reply.ip4TableId, reply.ip6TableId);
171     }
172
173     private static void testL2Acl() throws Exception {
174         System.out.println("Testing L2 ACLs using Java callback API");
175         final JVppRegistry registry = new JVppRegistryImpl("L2AclTest");
176         final JVpp jvpp = new JVppCoreImpl();
177         final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
178
179         System.out.println("Successfully connected to VPP");
180         Thread.sleep(1000);
181
182         final ClassifyAddDelTableReply classifyAddDelTableReply =
183             jvppFacade.classifyAddDelTable(createClassifyTable()).toCompletableFuture().get();
184         print(classifyAddDelTableReply);
185
186         final ClassifyTableIdsReply classifyTableIdsReply =
187             jvppFacade.classifyTableIds(new ClassifyTableIds()).toCompletableFuture().get();
188         print(classifyTableIdsReply);
189
190         final ClassifyTableInfoReply classifyTableInfoReply =
191             jvppFacade.classifyTableInfo(createClassifyTableInfoRequest(classifyAddDelTableReply.newTableIndex))
192                 .toCompletableFuture().get();
193         print(classifyTableInfoReply);
194
195         final ClassifyAddDelSessionReply classifyAddDelSessionReply =
196             jvppFacade.classifyAddDelSession(createClassifySession(classifyAddDelTableReply.newTableIndex))
197                 .toCompletableFuture().get();
198         print(classifyAddDelSessionReply);
199
200         final ClassifySessionDetailsReplyDump classifySessionDetailsReplyDump =
201             jvppFacade.classifySessionDump(createClassifySessionDumpRequest(classifyAddDelTableReply.newTableIndex))
202                 .toCompletableFuture().get();
203         print(classifySessionDetailsReplyDump);
204
205         final InputAclSetInterfaceReply inputAclSetInterfaceReply =
206             jvppFacade.inputAclSetInterface(aclSetInterface()).toCompletableFuture().get();
207         print(inputAclSetInterfaceReply);
208
209         final ClassifyTableByInterfaceReply classifyTableByInterfaceReply =
210             jvppFacade.classifyTableByInterface(createClassifyTableByInterfaceRequest()).toCompletableFuture().get();
211         print(classifyTableByInterfaceReply);
212
213         System.out.println("Disconnecting...");
214         registry.close();
215         Thread.sleep(1000);
216     }
217
218     public static void main(String[] args) throws Exception {
219         testL2Acl();
220     }
221 }