cd9578086831e1c4d41eaf4007ab3c6cacf7fdad
[vpp.git] / vpp-japi / japi / org / openvpp / vppjapi / vppConn.java
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 package org.openvpp.vppjapi;
17
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;
25 import java.util.Set;
26
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;
34
35 public class vppConn {
36     private static final String LIBNAME = "libvppjni.so.0.0.0";
37
38     static {
39         try {
40             loadLibrary();
41         } catch (IOException | RuntimeException e) {
42             System.out.printf ("Can't find vpp jni library: %s\n", LIBNAME);
43         }
44     }
45
46     private static void loadStream(final InputStream is) throws IOException {
47         final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
48         final Path p = Files.createTempFile(LIBNAME, null, PosixFilePermissions.asFileAttribute(perms));
49         try {
50             Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
51
52             try {
53                 Runtime.getRuntime().load(p.toString());
54             } catch (UnsatisfiedLinkError e) {
55                 throw new IOException(String.format("Failed to load library %s", p), e);
56             }
57         } finally {
58             try {
59                 Files.deleteIfExists(p);
60             } catch (IOException e) {
61             }
62         }
63     }
64
65     private static void loadLibrary() throws IOException {
66       try (final InputStream is = vppConn.class.getResourceAsStream('/' + LIBNAME)) {
67           if (is == null) {
68             throw new IOException(String.format("Failed to open library resource %s",
69                                                 LIBNAME));
70           }
71           loadStream(is);
72         }
73     }
74
75     public native int clientConnect(String clientName);
76     public native void clientDisconnect();
77     public native int getRetval(int context, int release);
78     public native String getInterfaceList (String nameFilter);
79     public native int swIfIndexFromName (String interfaceName);
80     public native String interfaceNameFromSwIfIndex (int swIfIndex);
81     public native void clearInterfaceTable ();
82     public native vppInterfaceDetails[] swInterfaceDump (byte nameFilterValid, byte [] nameFilter);
83     public native int bridgeDomainIdFromName(String bridgeDomain);
84     public native int findOrAddBridgeDomainId(String bridgeDomain);
85     public native vppVersion getVppVersion();
86     public native vppInterfaceCounters getInterfaceCounters(int swIfIndex);
87     public native int[] bridgeDomainDump(int bdId);
88     public native vppBridgeDomainDetails getBridgeDomainDetails(int bdId);
89     public native vppL2Fib[] l2FibTableDump(int bdId);
90     public native int bridgeDomainIdFromInterfaceName(String interfaceName);
91     public native vppIPv4Address[] ipv4AddressDump(String interfaceName);
92     public native vppIPv6Address[] ipv6AddressDump(String interfaceName);
93     public native vppVxlanTunnelDetails[] vxlanTunnelDump(int swIfIndex);
94     public native int setInterfaceDescription (String ifName, String ifDesc);
95     public native String getInterfaceDescription (String ifName);
96 }