aboutsummaryrefslogtreecommitdiffstats
path: root/vpp-api/java/jvpp/org/openvpp/jvpp/test/L2AclTest.java
blob: 8bfa27144631c0db5a8cfb9944508ccbdb9df7c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openvpp.jvpp.test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.openvpp.jvpp.JVppImpl;
import org.openvpp.jvpp.VppJNIConnection;
import org.openvpp.jvpp.dto.ClassifyAddDelSession;
import org.openvpp.jvpp.dto.ClassifyAddDelSessionReply;
import org.openvpp.jvpp.dto.ClassifyAddDelTable;
import org.openvpp.jvpp.dto.ClassifyAddDelTableReply;
import org.openvpp.jvpp.dto.InputAclSetInterface;
import org.openvpp.jvpp.dto.InputAclSetInterfaceReply;
import org.openvpp.jvpp.dto.JVppReply;
import org.openvpp.jvpp.future.FutureJVppFacade;
import org.openvpp.jvpp.future.FutureJVppFacadeCallback;

/**
 * <p>Tests L2 ACL creation.<br>
 * Equivalent to the following vppctl commands:<br>
 *
 * <pre>{@code
 * vppctl classify table mask l2 src
 * vppctl classify session acl-hit-next deny opaque-index 0 table-index 0 match l2 src 01:02:03:04:05:06
 * vppctl vppctl set int input acl intfc local0 l2-table 0
 * }
 * </pre>
 *
 * To verify invoke:<br>
 * {@code vppctl sh class table verbose}
 */
public class L2AclTest {

    private static ClassifyAddDelTable createClassifyTable() {
        ClassifyAddDelTable request = new ClassifyAddDelTable();
        request.isAdd = 1;
        request.tableIndex = ~0; // default
        request.nbuckets = 2;
        request.memorySize = 2 << 20;
        request.nextTableIndex = ~0; // default
        request.missNextIndex = ~0; // default
        request.skipNVectors = 0;
        request.matchNVectors = 1;
        request.mask =
                new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
                        (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00};
        return request;
    }

    private static ClassifyAddDelSession createClassifySession(final int tableIndex) {
        ClassifyAddDelSession request = new ClassifyAddDelSession();
        request.isAdd = 1;
        request.tableIndex = tableIndex;
        request.hitNextIndex = 0; // deny
        request.opaqueIndex = 0;
        request.advance = 0; // default
        // match 01:02:03:04:05:06 mac address
        request.match =
                new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
                        (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
        return request;
    }

    private static InputAclSetInterface aclSetInterface() {
        InputAclSetInterface request = new InputAclSetInterface();
        request.isAdd = 1;
        request.swIfIndex = 0;
        request.ip4TableIndex = ~0; // skip
        request.ip6TableIndex = ~0; // skip
        request.l2TableIndex = 0;
        return request;
    }

    private static void print(ClassifyAddDelTableReply reply) {
        System.out.printf("ClassifyAddDelTableReply: context=%d, retval=%d, " +
                        "newTableIndex=%d, skipNVectors=%d, matchNVectors=%d\n",
                reply.context,
                reply.retval,
                reply.newTableIndex,
                reply.skipNVectors,
                reply.matchNVectors);
    }

    private static void print(ClassifyAddDelSessionReply reply) {
        System.out.printf("ClassifyAddDelSessionReply: context=%d, retval=%d\n",
                reply.context,
                reply.retval);
    }

    private static void print(final InputAclSetInterfaceReply reply) {
        System.out.printf("InputAclSetInterfaceReply: context=%d, retval=%d\n",
                reply.context,
                reply.retval);

    }

    private static void testL2Acl() throws Exception {
        System.out.println("Testing L2 ACLs using Java callback API");
        final Map<Integer, CompletableFuture<? extends JVppReply<?>>> map = new HashMap<>();
        final JVppImpl jvpp = new JVppImpl(VppJNIConnection.create("FutureApiTest", new FutureJVppFacadeCallback(map)));
        final FutureJVppFacade jvppFacade = new FutureJVppFacade(jvpp, map);
        System.out.println("Successfully connected to VPP");
        Thread.sleep(1000);

        final ClassifyAddDelTableReply classifyAddDelTableReply =
                jvppFacade.classifyAddDelTable(createClassifyTable()).toCompletableFuture().get();
        print(classifyAddDelTableReply);

        final ClassifyAddDelSessionReply classifyAddDelSessionReply =
                jvppFacade.classifyAddDelSession(createClassifySession(classifyAddDelTableReply.newTableIndex))
                        .toCompletableFuture().get();
        print(classifyAddDelSessionReply);

        final InputAclSetInterfaceReply inputAclSetInterfaceReply =
                jvppFacade.inputAclSetInterface(aclSetInterface()).toCompletableFuture().get();
        print(inputAclSetInterfaceReply);

        System.out.println("Disconnecting...");
        jvpp.close();
        Thread.sleep(1000);
    }

    public static void main(String[] args) throws Exception {
        testL2Acl();
    }
}