Fix u16 type handling in jvpp
[vpp.git] / vpp-api / java / jvpp / org / openvpp / jvpp / test / CreateSubInterfaceTest.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 org.openvpp.jvpp.test;
18
19 import static java.util.Objects.requireNonNull;
20
21 import org.openvpp.jvpp.JVppImpl;
22 import org.openvpp.jvpp.VppJNIConnection;
23 import org.openvpp.jvpp.dto.CreateSubif;
24 import org.openvpp.jvpp.dto.CreateSubifReply;
25 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
26 import org.openvpp.jvpp.dto.SwInterfaceDump;
27 import org.openvpp.jvpp.future.FutureJVppFacade;
28
29 /**
30  * <p>Tests sub-interface creation.<br> Equivalent to:<br>
31  *
32  * <pre>{@code
33  * vppctl create sub GigabitEthernet0/9/0 1 dot1q 100 inner-dot1q any
34  * }
35  * </pre>
36  *
37  * To verify invoke:<br>
38  * <pre>{@code
39  * vpp_api_test json
40  * vat# sw_interface_dump
41  * }
42  */
43 public class CreateSubInterfaceTest {
44
45
46     private static SwInterfaceDump createSwInterfaceDumpRequest(final String ifaceName) {
47         SwInterfaceDump request = new SwInterfaceDump();
48         request.nameFilter = ifaceName.getBytes();
49         request.nameFilterValid = 1;
50         return request;
51     }
52
53     private static void requireSingleIface(final SwInterfaceDetailsReplyDump response, final String ifaceName) {
54         if (response.swInterfaceDetails.size() != 1) {
55             throw new IllegalStateException(
56                     String.format("Expected one interface matching filter %s but was %d", ifaceName,
57                             response.swInterfaceDetails.size()));
58         }
59     }
60
61     private static CreateSubif createSubifRequest(final int swIfIndex, final int subId) {
62         CreateSubif request = new CreateSubif();
63         request.swIfIndex = swIfIndex; // super interface id
64         request.subId = subId;
65         request.noTags = 0;
66         request.oneTag = 0;
67         request.twoTags = 1;
68         request.dot1Ad = 0;
69         request.exactMatch = 1;
70         request.defaultSub = 0;
71         request.outerVlanIdAny = 0;
72         request.innerVlanIdAny = 1;
73         request.outerVlanId = 100;
74         request.innerVlanId = 0;
75         return request;
76     }
77
78     private static void print(CreateSubifReply reply) {
79         System.out.printf("CreateSubifReply: context=%d, retval=%d, swIfIndex=%d\n",
80                 reply.context,
81                 reply.retval,
82                 reply.swIfIndex);
83     }
84
85     private static void testCreateSubInterface() throws Exception {
86         System.out.println("Testing sub-interface creation using Java callback API");
87         final JVppImpl jvpp = new JVppImpl(new VppJNIConnection("SubIfaceTest"));
88         final FutureJVppFacade jvppFacade = new FutureJVppFacade(jvpp);
89
90         System.out.println("Successfully connected to VPP");
91         Thread.sleep(1000);
92
93         final String ifaceName = "GigabitEthernet0/9/0";
94
95         final SwInterfaceDetailsReplyDump swInterfaceDetails =
96                 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(ifaceName)).toCompletableFuture().get();
97
98         requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
99         requireNonNull(swInterfaceDetails.swInterfaceDetails, "swInterfaceDetails is null");
100         requireSingleIface(swInterfaceDetails, ifaceName);
101
102         final int swIfIndex = swInterfaceDetails.swInterfaceDetails.get(0).swIfIndex;
103         final int subId = 1;
104
105         final CreateSubifReply createSubifReply =
106                 jvppFacade.createSubif(createSubifRequest(swIfIndex, subId)).toCompletableFuture().get();
107         print(createSubifReply);
108
109         final String subIfaceName = "GigabitEthernet0/9/0." + subId;
110         final SwInterfaceDetailsReplyDump subIface =
111                 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(subIfaceName)).toCompletableFuture().get();
112         requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
113         requireNonNull(subIface.swInterfaceDetails, "swInterfaceDump returned null");
114         requireSingleIface(swInterfaceDetails, ifaceName);
115
116         System.out.println("Disconnecting...");
117         jvpp.close();
118         Thread.sleep(1000);
119     }
120
121     public static void main(String[] args) throws Exception {
122         testCreateSubInterface();
123     }
124 }