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