2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 package org.openvpp.vppjapi;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.nio.file.Files;
21 import java.nio.file.Path;
22 import java.nio.file.StandardCopyOption;
23 import java.nio.file.attribute.PosixFilePermission;
24 import java.nio.file.attribute.PosixFilePermissions;
27 import org.openvpp.vppjapi.vppVersion;
28 import org.openvpp.vppjapi.vppInterfaceDetails;
29 import org.openvpp.vppjapi.vppInterfaceCounters;
30 import org.openvpp.vppjapi.vppBridgeDomainDetails;
31 import org.openvpp.vppjapi.vppIPv4Address;
32 import org.openvpp.vppjapi.vppIPv6Address;
33 import org.openvpp.vppjapi.vppVxlanTunnelDetails;
35 public class vppConn implements AutoCloseable {
36 private static final String LIBNAME = "libvppjni.so.0.0.0";
41 } catch (Exception e) {
42 System.out.printf("Can't find vpp jni library: %s\n", LIBNAME);
43 throw new ExceptionInInitializerError(e);
47 private static void loadStream(final InputStream is) throws IOException {
48 final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
49 final Path p = Files.createTempFile(LIBNAME, null, PosixFilePermissions.asFileAttribute(perms));
51 Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
54 Runtime.getRuntime().load(p.toString());
55 } catch (UnsatisfiedLinkError e) {
56 throw new IOException(String.format("Failed to load library %s", p), e);
60 Files.deleteIfExists(p);
61 } catch (IOException e) {
66 private static void loadLibrary() throws IOException {
67 try (final InputStream is = vppConn.class.getResourceAsStream('/' + LIBNAME)) {
69 throw new IOException(String.format("Failed to open library resource %s",
76 private static vppConn currentConnection = null;
77 private final String clientName;
78 private volatile boolean disconnected = false;
80 // Hidden on purpose to prevent external instantiation
81 vppConn(final String clientName) throws IOException {
82 this.clientName = clientName;
84 synchronized (vppConn.class) {
85 if (currentConnection != null) {
86 throw new IOException("Already connected as " + currentConnection.clientName);
89 final int ret = clientConnect(clientName);
91 throw new IOException("Connection returned error " + ret);
94 currentConnection = this;
99 public synchronized final void close() {
103 synchronized (vppConn.class) {
105 currentConnection = null;
111 * Check if this instance is connected.
113 * @throws IllegalStateException if this instance was disconnected.
115 protected final void checkConnected() {
117 throw new IllegalStateException("Disconnected client " + clientName);
121 public final int getRetval(int context, int release) {
123 return getRetval0(context, release);
126 public final String getInterfaceList (String nameFilter) {
128 return getInterfaceList0(nameFilter);
131 public final int swIfIndexFromName (String interfaceName) {
133 return swIfIndexFromName0(interfaceName);
136 public final String interfaceNameFromSwIfIndex (int swIfIndex) {
138 return interfaceNameFromSwIfIndex0(swIfIndex);
141 public final void clearInterfaceTable () {
143 clearInterfaceTable0();
146 public final vppInterfaceDetails[] swInterfaceDump (byte nameFilterValid, byte [] nameFilter) {
148 return swInterfaceDump0(nameFilterValid, nameFilter);
151 public final int bridgeDomainIdFromName(String bridgeDomain) {
153 return bridgeDomainIdFromName0(bridgeDomain);
156 public final int findOrAddBridgeDomainId(String bridgeDomain) {
158 return findOrAddBridgeDomainId0(bridgeDomain);
161 public final vppVersion getVppVersion() {
163 return getVppVersion0();
166 public final vppInterfaceCounters getInterfaceCounters(int swIfIndex) {
168 return getInterfaceCounters0(swIfIndex);
171 public final int[] bridgeDomainDump(int bdId) {
173 return bridgeDomainDump0(bdId);
176 public final vppBridgeDomainDetails getBridgeDomainDetails(int bdId) {
178 return getBridgeDomainDetails0(bdId);
181 public final vppL2Fib[] l2FibTableDump(int bdId) {
183 return l2FibTableDump0(bdId);
186 public final int bridgeDomainIdFromInterfaceName(String interfaceName) {
188 return bridgeDomainIdFromInterfaceName0(interfaceName);
191 public final vppIPv4Address[] ipv4AddressDump(String interfaceName) {
193 return ipv4AddressDump0(interfaceName);
196 public final vppIPv6Address[] ipv6AddressDump(String interfaceName) {
198 return ipv6AddressDump0(interfaceName);
201 public final vppVxlanTunnelDetails[] vxlanTunnelDump(int swIfIndex) {
203 return vxlanTunnelDump0(swIfIndex);
206 public final int setInterfaceDescription(String ifName, String ifDesc) {
208 return setInterfaceDescription0(ifName, ifDesc);
211 public final String getInterfaceDescription(String ifName) {
213 return getInterfaceDescription0(ifName);
216 private static native int clientConnect(String clientName);
217 private static native void clientDisconnect();
218 private static native int getRetval0(int context, int release);
219 private static native String getInterfaceList0(String nameFilter);
220 private static native int swIfIndexFromName0(String interfaceName);
221 private static native String interfaceNameFromSwIfIndex0(int swIfIndex);
222 private static native void clearInterfaceTable0();
223 private static native vppInterfaceDetails[] swInterfaceDump0(byte nameFilterValid, byte [] nameFilter);
224 private static native int bridgeDomainIdFromName0(String bridgeDomain);
225 private static native int findOrAddBridgeDomainId0(String bridgeDomain);
226 private static native vppVersion getVppVersion0();
227 private static native vppInterfaceCounters getInterfaceCounters0(int swIfIndex);
228 private static native int[] bridgeDomainDump0(int bdId);
229 private static native vppBridgeDomainDetails getBridgeDomainDetails0(int bdId);
230 private static native vppL2Fib[] l2FibTableDump0(int bdId);
231 private static native int bridgeDomainIdFromInterfaceName0(String interfaceName);
232 private static native vppIPv4Address[] ipv4AddressDump0(String interfaceName);
233 private static native vppIPv6Address[] ipv6AddressDump0(String interfaceName);
234 private static native vppVxlanTunnelDetails[] vxlanTunnelDump0(int swIfIndex);
235 private static native int setInterfaceDescription0(String ifName, String ifDesc);
236 private static native String getInterfaceDescription0(String ifName);