Initial commit of vpp code.
[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.vppInterfaceCounters;
29 import org.openvpp.vppjapi.vppBridgeDomainDetails;
30 import org.openvpp.vppjapi.vppL2Fib;
31
32 public class vppConn {
33     private static final String LIBNAME = "libvppjni.so.0.0.0";
34
35     static {
36         try {
37             loadLibrary();
38         } catch (IOException | RuntimeException e) {
39             System.out.printf ("Can't find vpp jni library: %s\n", LIBNAME);
40         }
41     }
42
43     private static void loadStream(final InputStream is) throws IOException {
44         final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
45         final Path p = Files.createTempFile(LIBNAME, null, PosixFilePermissions.asFileAttribute(perms));
46
47         try {
48             Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
49
50             try {
51                 Runtime.getRuntime().load(p.toString());
52             } catch (UnsatisfiedLinkError e) {
53                 throw new IOException(String.format("Failed to load library %s", p), e);
54             }
55         } finally {
56             try {
57                 Files.deleteIfExists(p);
58             } catch (IOException e) {
59             }
60         }
61     }
62
63     private static void loadLibrary() throws IOException {
64       try (final InputStream is = vppConn.class.getResourceAsStream('/' + LIBNAME)) {
65           if (is == null) {
66             throw new IOException(String.format("Failed to open library resource %s",
67                                                 LIBNAME));
68           }
69           loadStream(is);
70         }
71     }
72
73     public native int clientConnect(String clientName);
74     public native void clientDisconnect();
75     public native int getRetval(int context, int release);
76     public native String getInterfaceList (String nameFilter);
77     public native int swIfIndexFromName (String interfaceName);
78     public native String interfaceNameFromSwIfIndex (int swIfIndex);
79     public native void clearInterfaceTable ();
80     public native int swInterfaceDump (byte nameFilterValid, byte [] nameFilter);
81     public native int bridgeDomainIdFromName(String bridgeDomain);
82     public native int findOrAddBridgeDomainId(String bridgeDomain);
83     public native vppVersion getVppVersion();
84     public native vppInterfaceCounters getInterfaceCounters(int swIfIndex);
85     public native int[] bridgeDomainDump(int bdId);
86     public native vppBridgeDomainDetails getBridgeDomainDetails(int bdId);
87     public native vppL2Fib[] l2FibTableDump(int bdId);
88     public native int bridgeDomainIdFromInterfaceName(String interfaceName);
89 }