Fix coverity issues in jvpp (newlines + CompletableDumpFuture.ctxId)
[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 javax.xml.bind.DatatypeConverter;
20 import io.fd.vpp.jvpp.JVpp;
21 import io.fd.vpp.jvpp.JVppRegistry;
22 import io.fd.vpp.jvpp.JVppRegistryImpl;
23 import io.fd.vpp.jvpp.core.JVppCoreImpl;
24 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
25 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
26 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
27 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
28 import io.fd.vpp.jvpp.core.dto.ClassifySessionDetailsReplyDump;
29 import io.fd.vpp.jvpp.core.dto.ClassifySessionDump;
30 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterface;
31 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
32 import io.fd.vpp.jvpp.core.dto.ClassifyTableIds;
33 import io.fd.vpp.jvpp.core.dto.ClassifyTableIdsReply;
34 import io.fd.vpp.jvpp.core.dto.ClassifyTableInfo;
35 import io.fd.vpp.jvpp.core.dto.ClassifyTableInfoReply;
36 import io.fd.vpp.jvpp.core.dto.InputAclSetInterface;
37 import io.fd.vpp.jvpp.core.dto.InputAclSetInterfaceReply;
38 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
39
40 /**
41  * <p>Tests L2 ACL creation and read.<br> Equivalent to the following vppctl commands:<br>
42  *
43  * <pre>{@code
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
48  * }
49  * </pre>
50  */
51 public class L2AclTest {
52
53     private static final int LOCAL0_IFACE_ID = 0;
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 ClassifyTableInfo createClassifyTableInfoRequest(final int tableId) {
72         ClassifyTableInfo request = new ClassifyTableInfo();
73         request.tableId = tableId;
74         return request;
75     }
76
77     private static ClassifyAddDelSession createClassifySession(final int tableIndex) {
78         ClassifyAddDelSession request = new ClassifyAddDelSession();
79         request.isAdd = 1;
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
85         request.match =
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};
88         return request;
89     }
90
91     private static ClassifySessionDump createClassifySessionDumpRequest(final int newTableIndex) {
92         ClassifySessionDump request = new ClassifySessionDump();
93         request.tableId = newTableIndex;
94         return request;
95     }
96
97     private static InputAclSetInterface aclSetInterface() {
98         InputAclSetInterface request = new InputAclSetInterface();
99         request.isAdd = 1;
100         request.swIfIndex = LOCAL0_IFACE_ID;
101         request.ip4TableIndex = ~0; // skip
102         request.ip6TableIndex = ~0; // skip
103         request.l2TableIndex = 0;
104         return request;
105     }
106
107     private static ClassifyTableByInterface createClassifyTableByInterfaceRequest() {
108         ClassifyTableByInterface request = new ClassifyTableByInterface();
109         request.swIfIndex = LOCAL0_IFACE_ID;
110         return request;
111     }
112
113     private static void print(ClassifyAddDelTableReply reply) {
114         System.out.printf("ClassifyAddDelTableReply: %s%n", reply);
115     }
116
117     private static void print(ClassifyTableIdsReply reply) {
118         System.out.printf("ClassifyTableIdsReply: %s%n", reply);
119     }
120
121     private static void print(final ClassifyTableInfoReply reply) {
122         System.out.println(reply);
123         if (reply != null) {
124             System.out.println("Mask hex: " + DatatypeConverter.printHexBinary(reply.mask));
125         }
126     }
127
128     private static void print(ClassifyAddDelSessionReply reply) {
129         System.out.printf("ClassifyAddDelSessionReply: context=%s%n", reply);
130     }
131
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));
137         });
138     }
139
140     private static void print(final InputAclSetInterfaceReply reply) {
141         System.out.printf("InputAclSetInterfaceReply: context=%s%n", reply);
142     }
143
144     private static void print(final ClassifyTableByInterfaceReply reply) {
145         System.out.printf("ClassifyAddDelTableReply: %s%n", reply);
146     }
147
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);
153
154         System.out.println("Successfully connected to VPP");
155         Thread.sleep(1000);
156
157         final ClassifyAddDelTableReply classifyAddDelTableReply =
158                 jvppFacade.classifyAddDelTable(createClassifyTable()).toCompletableFuture().get();
159         print(classifyAddDelTableReply);
160
161         final ClassifyTableIdsReply classifyTableIdsReply =
162                 jvppFacade.classifyTableIds(new ClassifyTableIds()).toCompletableFuture().get();
163         print(classifyTableIdsReply);
164
165         final ClassifyTableInfoReply classifyTableInfoReply =
166                 jvppFacade.classifyTableInfo(createClassifyTableInfoRequest(classifyAddDelTableReply.newTableIndex))
167                         .toCompletableFuture().get();
168         print(classifyTableInfoReply);
169
170         final ClassifyAddDelSessionReply classifyAddDelSessionReply =
171                 jvppFacade.classifyAddDelSession(createClassifySession(classifyAddDelTableReply.newTableIndex))
172                         .toCompletableFuture().get();
173         print(classifyAddDelSessionReply);
174
175         final ClassifySessionDetailsReplyDump classifySessionDetailsReplyDump =
176                 jvppFacade.classifySessionDump(createClassifySessionDumpRequest(classifyAddDelTableReply.newTableIndex))
177                         .toCompletableFuture().get();
178         print(classifySessionDetailsReplyDump);
179
180         final InputAclSetInterfaceReply inputAclSetInterfaceReply =
181                 jvppFacade.inputAclSetInterface(aclSetInterface()).toCompletableFuture().get();
182         print(inputAclSetInterfaceReply);
183
184         final ClassifyTableByInterfaceReply classifyTableByInterfaceReply =
185                 jvppFacade.classifyTableByInterface(createClassifyTableByInterfaceRequest()).toCompletableFuture().get();
186         print(classifyTableByInterfaceReply);
187
188         System.out.println("Disconnecting...");
189         registry.close();
190         Thread.sleep(1000);
191     }
192
193     public static void main(String[] args) throws Exception {
194         testL2Acl();
195     }
196 }