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.core.test;
19 import static java.util.Objects.requireNonNull;
21 import org.openvpp.jvpp.JVpp;
22 import org.openvpp.jvpp.JVppRegistry;
23 import org.openvpp.jvpp.JVppRegistryImpl;
24 import org.openvpp.jvpp.core.JVppCoreImpl;
25 import org.openvpp.jvpp.core.dto.CreateSubif;
26 import org.openvpp.jvpp.core.dto.CreateSubifReply;
27 import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
28 import org.openvpp.jvpp.core.dto.SwInterfaceDump;
29 import org.openvpp.jvpp.core.future.FutureJVppCoreFacade;
32 * <p>Tests sub-interface creation.<br> Equivalent to:<br>
35 * vppctl create sub GigabitEthernet0/9/0 1 dot1q 100 inner-dot1q any
39 * To verify invoke:<br>
42 * vat# sw_interface_dump
45 public class CreateSubInterfaceTest {
47 private static SwInterfaceDump createSwInterfaceDumpRequest(final String ifaceName) {
48 SwInterfaceDump request = new SwInterfaceDump();
49 request.nameFilter = ifaceName.getBytes();
50 request.nameFilterValid = 1;
54 private static void requireSingleIface(final SwInterfaceDetailsReplyDump response, final String ifaceName) {
55 if (response.swInterfaceDetails.size() != 1) {
56 throw new IllegalStateException(
57 String.format("Expected one interface matching filter %s but was %d", ifaceName,
58 response.swInterfaceDetails.size()));
62 private static CreateSubif createSubifRequest(final int swIfIndex, final int subId) {
63 CreateSubif request = new CreateSubif();
64 request.swIfIndex = swIfIndex; // super interface id
65 request.subId = subId;
70 request.exactMatch = 1;
71 request.defaultSub = 0;
72 request.outerVlanIdAny = 0;
73 request.innerVlanIdAny = 1;
74 request.outerVlanId = 100;
75 request.innerVlanId = 0;
79 private static void print(CreateSubifReply reply) {
80 System.out.printf("CreateSubifReply: context=%d, swIfIndex=%d\n", reply.context, reply.swIfIndex);
83 private static void testCreateSubInterface() throws Exception {
84 System.out.println("Testing sub-interface creation using Java callback API");
85 final JVppRegistry registry = new JVppRegistryImpl("CreateSubInterface");
86 final JVpp jvpp = new JVppCoreImpl();
87 final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, jvpp);
89 System.out.println("Successfully connected to VPP");
92 final String ifaceName = "GigabitEthernet0/8/0";
94 final SwInterfaceDetailsReplyDump swInterfaceDetails =
95 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(ifaceName)).toCompletableFuture().get();
97 requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
98 requireNonNull(swInterfaceDetails.swInterfaceDetails, "swInterfaceDetails is null");
99 requireSingleIface(swInterfaceDetails, ifaceName);
101 final int swIfIndex = swInterfaceDetails.swInterfaceDetails.get(0).swIfIndex;
104 final CreateSubifReply createSubifReply =
105 jvppFacade.createSubif(createSubifRequest(swIfIndex, subId)).toCompletableFuture().get();
106 print(createSubifReply);
108 final String subIfaceName = "GigabitEthernet0/8/0." + subId;
109 final SwInterfaceDetailsReplyDump subIface =
110 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(subIfaceName)).toCompletableFuture().get();
111 requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
112 requireNonNull(subIface.swInterfaceDetails, "swInterfaceDump returned null");
113 requireSingleIface(swInterfaceDetails, ifaceName);
115 System.out.println("Disconnecting...");
120 public static void main(String[] args) throws Exception {
121 testCreateSubInterface();