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 io.fd.vpp.jvpp.core.test;
19 import static java.util.Objects.requireNonNull;
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.CreateSubif;
25 import io.fd.vpp.jvpp.core.dto.CreateSubifReply;
26 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetailsReplyDump;
27 import io.fd.vpp.jvpp.core.dto.SwInterfaceDump;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
31 * <p>Tests sub-interface creation.<br> Equivalent to:<br>
34 * vppctl create sub GigabitEthernet0/9/0 1 dot1q 100 inner-dot1q any
38 * To verify invoke:<br>
41 * vat# sw_interface_dump
44 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: %s%n", reply);
82 private static void testCreateSubInterface() throws Exception {
83 System.out.println("Testing sub-interface creation using Java callback API");
84 try (final JVppRegistry registry = new JVppRegistryImpl("CreateSubInterface");
85 final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
86 System.out.println("Successfully connected to VPP");
89 final String ifaceName = "GigabitEthernet0/8/0";
91 final SwInterfaceDetailsReplyDump swInterfaceDetails =
92 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(ifaceName)).toCompletableFuture().get();
94 requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
95 requireNonNull(swInterfaceDetails.swInterfaceDetails, "swInterfaceDetails is null");
96 requireSingleIface(swInterfaceDetails, ifaceName);
98 final int swIfIndex = swInterfaceDetails.swInterfaceDetails.get(0).swIfIndex;
101 final CreateSubifReply createSubifReply =
102 jvppFacade.createSubif(createSubifRequest(swIfIndex, subId)).toCompletableFuture().get();
103 print(createSubifReply);
105 final String subIfaceName = "GigabitEthernet0/8/0." + subId;
106 final SwInterfaceDetailsReplyDump subIface =
107 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(subIfaceName)).toCompletableFuture().get();
108 requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
109 requireNonNull(subIface.swInterfaceDetails, "swInterfaceDump returned null");
110 requireSingleIface(swInterfaceDetails, ifaceName);
112 System.out.println("Disconnecting...");
117 public static void main(String[] args) throws Exception {
118 testCreateSubInterface();