2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openvpp.jvpp.test;
19 import java.util.Arrays;
20 import javax.xml.bind.DatatypeConverter;
21 import org.openvpp.jvpp.JVppImpl;
22 import org.openvpp.jvpp.VppJNIConnection;
23 import org.openvpp.jvpp.dto.ClassifyAddDelSession;
24 import org.openvpp.jvpp.dto.ClassifyAddDelSessionReply;
25 import org.openvpp.jvpp.dto.ClassifyAddDelTable;
26 import org.openvpp.jvpp.dto.ClassifyAddDelTableReply;
27 import org.openvpp.jvpp.dto.ClassifySessionDetails;
28 import org.openvpp.jvpp.dto.ClassifySessionDetailsReplyDump;
29 import org.openvpp.jvpp.dto.ClassifySessionDump;
30 import org.openvpp.jvpp.dto.ClassifyTableByInterface;
31 import org.openvpp.jvpp.dto.ClassifyTableByInterfaceReply;
32 import org.openvpp.jvpp.dto.ClassifyTableIds;
33 import org.openvpp.jvpp.dto.ClassifyTableIdsReply;
34 import org.openvpp.jvpp.dto.ClassifyTableInfo;
35 import org.openvpp.jvpp.dto.ClassifyTableInfoReply;
36 import org.openvpp.jvpp.dto.InputAclSetInterface;
37 import org.openvpp.jvpp.dto.InputAclSetInterfaceReply;
38 import org.openvpp.jvpp.future.FutureJVppFacade;
41 * <p>Tests L2 ACL creation and read.<br> Equivalent to the following vppctl commands:<br>
44 * vppctl classify table mask l2 src
45 * vppctl classify session acl-hit-next deny opaque-index 0 table-index 0 match l2 src 01:02:03:04:05:06
46 * vppctl set int input acl intfc local0 l2-table 0
47 * vppctl sh class table verbose
51 public class L2AclTest {
53 private static final int LOCAL0_IFACE_ID = 0;
55 private static ClassifyAddDelTable createClassifyTable() {
56 ClassifyAddDelTable request = new ClassifyAddDelTable();
58 request.tableIndex = ~0; // default
60 request.memorySize = 2 << 20;
61 request.nextTableIndex = ~0; // default
62 request.missNextIndex = ~0; // default
63 request.skipNVectors = 0;
64 request.matchNVectors = 1;
66 new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
67 (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00};
71 private static ClassifyTableInfo createClassifyTableInfoRequest(final int tableId) {
72 ClassifyTableInfo request = new ClassifyTableInfo();
73 request.tableId = tableId;
77 private static ClassifyAddDelSession createClassifySession(final int tableIndex) {
78 ClassifyAddDelSession request = new ClassifyAddDelSession();
80 request.tableIndex = tableIndex;
81 request.hitNextIndex = 0; // deny
82 request.opaqueIndex = 0;
83 request.advance = 0; // default
84 // match 01:02:03:04:05:06 mac address
86 new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
87 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
91 private static ClassifySessionDump createClassifySessionDumpRequest(final int newTableIndex) {
92 ClassifySessionDump request = new ClassifySessionDump();
93 request.tableId = newTableIndex;
97 private static InputAclSetInterface aclSetInterface() {
98 InputAclSetInterface request = new InputAclSetInterface();
100 request.swIfIndex = LOCAL0_IFACE_ID;
101 request.ip4TableIndex = ~0; // skip
102 request.ip6TableIndex = ~0; // skip
103 request.l2TableIndex = 0;
107 private static ClassifyTableByInterface createClassifyTableByInterfaceRequest() {
108 ClassifyTableByInterface request = new ClassifyTableByInterface();
109 request.swIfIndex = LOCAL0_IFACE_ID;
113 private static void print(ClassifyAddDelTableReply reply) {
114 System.out.printf("ClassifyAddDelTableReply: context=%d, " +
115 "newTableIndex=%d, skipNVectors=%d, matchNVectors=%d\n",
116 reply.context, reply.newTableIndex, reply.skipNVectors, reply.matchNVectors);
119 private static void print(ClassifyTableIdsReply reply) {
120 System.out.printf("ClassifyTableIdsReply: context=%d, count=%d, ids.length=%d\n",
121 reply.context, reply.count, reply.ids.length);
122 Arrays.stream(reply.ids).forEach(System.out::println);
125 private static void print(final ClassifyTableInfoReply reply) {
126 final StringBuilder builder = new StringBuilder("ClassifyTableInfoReply:\n");
127 builder.append("context: ").append(reply.context).append('\n');
128 builder.append("tableId: ").append(reply.tableId).append('\n');
129 builder.append("nbuckets: ").append(reply.nbuckets).append('\n');
130 builder.append("matchNVectors: ").append(reply.matchNVectors).append('\n');
131 builder.append("skipNVectors: ").append(reply.skipNVectors).append('\n');
132 builder.append("activeSessions: ").append(reply.activeSessions).append('\n');
133 builder.append("nextTableIndex: ").append(reply.nextTableIndex).append('\n');
134 builder.append("missNextIndex: ").append(reply.missNextIndex).append('\n');
135 builder.append("maskLength: ").append(reply.maskLength).append('\n');
136 builder.append("mask: ").append(DatatypeConverter.printHexBinary(reply.mask)).append('\n');
137 System.out.println(builder.toString());
140 private static void print(ClassifyAddDelSessionReply reply) {
141 System.out.printf("ClassifyAddDelSessionReply: context=%d\n",
145 private static void print(final ClassifySessionDetailsReplyDump reply) {
146 if (reply.classifySessionDetails == null) {
147 System.out.println("ClassifySessionDetailsReplyDump: classifySessionDetails == NULL");
149 for (final ClassifySessionDetails details : reply.classifySessionDetails) {
150 final StringBuilder builder = new StringBuilder("ClassifySessionDetails:\n");
151 builder.append("context: ").append(details.context).append('\n');
152 builder.append("tableId: ").append(details.tableId).append('\n');
153 builder.append("hitNextIndex: ").append(details.hitNextIndex).append('\n');
154 builder.append("advance: ").append(details.advance).append('\n');
155 builder.append("opaqueIndex: ").append(details.opaqueIndex).append('\n');
156 builder.append("matchLength: ").append(details.matchLength).append('\n');
157 builder.append("match: ").append(DatatypeConverter.printHexBinary(details.match)).append('\n');
158 System.out.println(builder.toString());
162 private static void print(final InputAclSetInterfaceReply reply) {
163 System.out.printf("InputAclSetInterfaceReply: context=%d\n", reply.context);
166 private static void print(final ClassifyTableByInterfaceReply reply) {
167 System.out.printf("ClassifyAddDelTableReply: context=%d, swIfIndex=%d, l2TableId=%d, ip4TableId=%d," +
168 "ip6TableId=%d\n", reply.context, reply.swIfIndex, reply.l2TableId, reply.ip4TableId, reply.ip6TableId);
171 private static void testL2Acl() throws Exception {
172 System.out.println("Testing L2 ACLs using Java callback API");
173 final JVppImpl jvpp = new JVppImpl(new VppJNIConnection("L2AclTest"));
174 final FutureJVppFacade jvppFacade = new FutureJVppFacade(jvpp);
176 System.out.println("Successfully connected to VPP");
179 final ClassifyAddDelTableReply classifyAddDelTableReply =
180 jvppFacade.classifyAddDelTable(createClassifyTable()).toCompletableFuture().get();
181 print(classifyAddDelTableReply);
183 final ClassifyTableIdsReply classifyTableIdsReply =
184 jvppFacade.classifyTableIds(new ClassifyTableIds()).toCompletableFuture().get();
185 print(classifyTableIdsReply);
187 final ClassifyTableInfoReply classifyTableInfoReply =
188 jvppFacade.classifyTableInfo(createClassifyTableInfoRequest(classifyAddDelTableReply.newTableIndex))
189 .toCompletableFuture().get();
190 print(classifyTableInfoReply);
192 final ClassifyAddDelSessionReply classifyAddDelSessionReply =
193 jvppFacade.classifyAddDelSession(createClassifySession(classifyAddDelTableReply.newTableIndex))
194 .toCompletableFuture().get();
195 print(classifyAddDelSessionReply);
197 final ClassifySessionDetailsReplyDump classifySessionDetailsReplyDump =
198 jvppFacade.classifySessionDump(createClassifySessionDumpRequest(classifyAddDelTableReply.newTableIndex))
199 .toCompletableFuture().get();
200 print(classifySessionDetailsReplyDump);
202 final InputAclSetInterfaceReply inputAclSetInterfaceReply =
203 jvppFacade.inputAclSetInterface(aclSetInterface()).toCompletableFuture().get();
204 print(inputAclSetInterfaceReply);
206 final ClassifyTableByInterfaceReply classifyTableByInterfaceReply =
207 jvppFacade.classifyTableByInterface(createClassifyTableByInterfaceRequest()).toCompletableFuture().get();
208 print(classifyTableByInterfaceReply);
210 System.out.println("Disconnecting...");
215 public static void main(String[] args) throws Exception {