2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openvpp.jvpp.test;
19 import static java.util.Objects.requireNonNull;
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;
30 * <p>Tests sub-interface creation.<br> Equivalent to:<br>
33 * vppctl create sub GigabitEthernet0/9/0 1 dot1q 100 inner-dot1q any
37 * To verify invoke:<br>
40 * vat# sw_interface_dump
43 public class CreateSubInterfaceTest {
46 private static SwInterfaceDump createSwInterfaceDumpRequest(final String ifaceName) {
47 SwInterfaceDump request = new SwInterfaceDump();
48 request.nameFilter = ifaceName.getBytes();
49 request.nameFilterValid = 1;
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()));
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;
69 request.exactMatch = 1;
70 request.defaultSub = 0;
71 request.outerVlanIdAny = 0;
72 request.innerVlanIdAny = 1;
73 request.outerVlanId = 100;
74 request.innerVlanId = 0;
78 private static void print(CreateSubifReply reply) {
79 System.out.printf("CreateSubifReply: context=%d, retval=%d, swIfIndex=%d\n",
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);
90 System.out.println("Successfully connected to VPP");
93 final String ifaceName = "GigabitEthernet0/9/0";
95 final SwInterfaceDetailsReplyDump swInterfaceDetails =
96 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(ifaceName)).toCompletableFuture().get();
98 requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
99 requireNonNull(swInterfaceDetails.swInterfaceDetails, "swInterfaceDetails is null");
100 requireSingleIface(swInterfaceDetails, ifaceName);
102 final int swIfIndex = swInterfaceDetails.swInterfaceDetails.get(0).swIfIndex;
105 final CreateSubifReply createSubifReply =
106 jvppFacade.createSubif(createSubifRequest(swIfIndex, subId)).toCompletableFuture().get();
107 print(createSubifReply);
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);
116 System.out.println("Disconnecting...");
121 public static void main(String[] args) throws Exception {
122 testCreateSubInterface();