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.core.test;
19 import javax.xml.bind.DatatypeConverter;
20 import org.openvpp.jvpp.JVpp;
21 import org.openvpp.jvpp.JVppRegistry;
22 import org.openvpp.jvpp.JVppRegistryImpl;
23 import org.openvpp.jvpp.core.JVppCoreImpl;
24 import org.openvpp.jvpp.core.dto.ClassifyAddDelSession;
25 import org.openvpp.jvpp.core.dto.ClassifyAddDelSessionReply;
26 import org.openvpp.jvpp.core.dto.ClassifyAddDelTable;
27 import org.openvpp.jvpp.core.dto.ClassifyAddDelTableReply;
28 import org.openvpp.jvpp.core.dto.ClassifySessionDetailsReplyDump;
29 import org.openvpp.jvpp.core.dto.ClassifySessionDump;
30 import org.openvpp.jvpp.core.dto.ClassifyTableByInterface;
31 import org.openvpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
32 import org.openvpp.jvpp.core.dto.ClassifyTableIds;
33 import org.openvpp.jvpp.core.dto.ClassifyTableIdsReply;
34 import org.openvpp.jvpp.core.dto.ClassifyTableInfo;
35 import org.openvpp.jvpp.core.dto.ClassifyTableInfoReply;
36 import org.openvpp.jvpp.core.dto.InputAclSetInterface;
37 import org.openvpp.jvpp.core.dto.InputAclSetInterfaceReply;
38 import org.openvpp.jvpp.core.future.FutureJVppCoreFacade;
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: %s\n", reply);
117 private static void print(ClassifyTableIdsReply reply) {
118 System.out.printf("ClassifyTableIdsReply: %s\n", reply);
121 private static void print(final ClassifyTableInfoReply reply) {
122 System.out.println(reply);
124 System.out.println("Mask hex: " + DatatypeConverter.printHexBinary(reply.mask));
128 private static void print(ClassifyAddDelSessionReply reply) {
129 System.out.printf("ClassifyAddDelSessionReply: context=%s\n", reply);
132 private static void print(final ClassifySessionDetailsReplyDump reply) {
133 System.out.println(reply);
134 reply.classifySessionDetails.forEach(detail -> {
135 System.out.println(detail);
136 System.out.println("Match hex: " + DatatypeConverter.printHexBinary(detail.match));
140 private static void print(final InputAclSetInterfaceReply reply) {
141 System.out.printf("InputAclSetInterfaceReply: context=%s\n", reply);
144 private static void print(final ClassifyTableByInterfaceReply reply) {
145 System.out.printf("ClassifyAddDelTableReply: %s\n", reply);
148 private static void testL2Acl() throws Exception {
149 System.out.println("Testing L2 ACLs using Java callback API");
150 final JVppRegistry registry = new JVppRegistryImpl("L2AclTest");
151 final JVpp jvpp = new JVppCoreImpl();
152 final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
154 System.out.println("Successfully connected to VPP");
157 final ClassifyAddDelTableReply classifyAddDelTableReply =
158 jvppFacade.classifyAddDelTable(createClassifyTable()).toCompletableFuture().get();
159 print(classifyAddDelTableReply);
161 final ClassifyTableIdsReply classifyTableIdsReply =
162 jvppFacade.classifyTableIds(new ClassifyTableIds()).toCompletableFuture().get();
163 print(classifyTableIdsReply);
165 final ClassifyTableInfoReply classifyTableInfoReply =
166 jvppFacade.classifyTableInfo(createClassifyTableInfoRequest(classifyAddDelTableReply.newTableIndex))
167 .toCompletableFuture().get();
168 print(classifyTableInfoReply);
170 final ClassifyAddDelSessionReply classifyAddDelSessionReply =
171 jvppFacade.classifyAddDelSession(createClassifySession(classifyAddDelTableReply.newTableIndex))
172 .toCompletableFuture().get();
173 print(classifyAddDelSessionReply);
175 final ClassifySessionDetailsReplyDump classifySessionDetailsReplyDump =
176 jvppFacade.classifySessionDump(createClassifySessionDumpRequest(classifyAddDelTableReply.newTableIndex))
177 .toCompletableFuture().get();
178 print(classifySessionDetailsReplyDump);
180 final InputAclSetInterfaceReply inputAclSetInterfaceReply =
181 jvppFacade.inputAclSetInterface(aclSetInterface()).toCompletableFuture().get();
182 print(inputAclSetInterfaceReply);
184 final ClassifyTableByInterfaceReply classifyTableByInterfaceReply =
185 jvppFacade.classifyTableByInterface(createClassifyTableByInterfaceRequest()).toCompletableFuture().get();
186 print(classifyTableByInterfaceReply);
188 System.out.println("Disconnecting...");
193 public static void main(String[] args) throws Exception {