VPP-330 Track pending map-requests with a fifo
[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 org.openvpp.jvpp.JVppImpl;
20 import org.openvpp.jvpp.VppJNIConnection;
21 import org.openvpp.jvpp.dto.CreateSubif;
22 import org.openvpp.jvpp.dto.CreateSubifReply;
23 import org.openvpp.jvpp.dto.SwInterfaceDetailsReplyDump;
24 import org.openvpp.jvpp.dto.SwInterfaceDump;
25 import org.openvpp.jvpp.future.FutureJVppFacade;
26
27 import static java.util.Objects.requireNonNull;
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, swIfIndex=%d\n",
80                 reply.context,
81                 reply.swIfIndex);
82     }
83
84     private static void testCreateSubInterface() throws Exception {
85         System.out.println("Testing sub-interface creation using Java callback API");
86         final JVppImpl jvpp = new JVppImpl(new VppJNIConnection("SubIfaceTest"));
87         final FutureJVppFacade jvppFacade = new FutureJVppFacade(jvpp);
88
89         System.out.println("Successfully connected to VPP");
90         Thread.sleep(1000);
91
92         final String ifaceName = "GigabitEthernet0/9/0";
93
94         final SwInterfaceDetailsReplyDump swInterfaceDetails =
95                 jvppFacade.swInterfaceDump(createSwInterfaceDumpRequest(ifaceName)).toCompletableFuture().get();
96
97         requireNonNull(swInterfaceDetails, "swInterfaceDump returned null");
98         requireNonNull(swInterfaceDetails.swInterfaceDetails, "swInterfaceDetails is null");
99         requireSingleIface(swInterfaceDetails, ifaceName);
100
101         final int swIfIndex = swInterfaceDetails.swInterfaceDetails.get(0).swIfIndex;
102         final int subId = 1;
103
104         final CreateSubifReply createSubifReply =
105                 jvppFacade.createSubif(createSubifRequest(swIfIndex, subId)).toCompletableFuture().get();
106         print(createSubifReply);
107
108         final String subIfaceName = "GigabitEthernet0/9/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);
114
115         System.out.println("Disconnecting...");
116         jvpp.close();
117         Thread.sleep(1000);
118     }
119
120     public static void main(String[] args) throws Exception {
121         testCreateSubInterface();
122     }
123 }