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