VPP-205: jvpp plugin support.
[vpp.git] / vpp-api / java / jvpp-core / org / openvpp / jvpp / core / 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.core.test;
18
19 import static java.util.Objects.requireNonNull;
20
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;
30
31 /**
32  * <p>Tests sub-interface creation.<br> Equivalent to:<br>
33  *
34  * <pre>{@code
35  * vppctl create sub GigabitEthernet0/9/0 1 dot1q 100 inner-dot1q any
36  * }
37  * </pre>
38  *
39  * To verify invoke:<br>
40  * <pre>{@code
41  * vpp_api_test json
42  * vat# sw_interface_dump
43  * }
44  */
45 public class CreateSubInterfaceTest {
46
47     private static SwInterfaceDump createSwInterfaceDumpRequest(final String ifaceName) {
48         SwInterfaceDump request = new SwInterfaceDump();
49         request.nameFilter = ifaceName.getBytes();
50         request.nameFilterValid = 1;
51         return request;
52     }
53
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()));
59         }
60     }
61
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;
66         request.noTags = 0;
67         request.oneTag = 0;
68         request.twoTags = 1;
69         request.dot1Ad = 0;
70         request.exactMatch = 1;
71         request.defaultSub = 0;
72         request.outerVlanIdAny = 0;
73         request.innerVlanIdAny = 1;
74         request.outerVlanId = 100;
75         request.innerVlanId = 0;
76         return request;
77     }
78
79     private static void print(CreateSubifReply reply) {
80         System.out.printf("CreateSubifReply: context=%d, swIfIndex=%d\n", reply.context, reply.swIfIndex);
81     }
82
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);
88
89         System.out.println("Successfully connected to VPP");
90         Thread.sleep(1000);
91
92         final String ifaceName = "GigabitEthernet0/8/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/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);
114
115         System.out.println("Disconnecting...");
116         registry.close();
117         Thread.sleep(1000);
118     }
119
120     public static void main(String[] args) throws Exception {
121         testCreateSubInterface();
122     }
123 }