3e8c12a93f9fb8595d089114ee541a465898b570
[vpp.git] / vpp-api / java / 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 implements AutoCloseable {
36     private static final String LIBNAME = "libvppjni.so.0.0.0";
37
38     static {
39         try {
40             loadLibrary();
41         } catch (Exception e) {
42             System.out.printf("Can't find vpp jni library: %s\n", LIBNAME);
43             throw new ExceptionInInitializerError(e);
44         }
45     }
46
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));
50         try {
51             Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
52
53             try {
54                 Runtime.getRuntime().load(p.toString());
55             } catch (UnsatisfiedLinkError e) {
56                 throw new IOException(String.format("Failed to load library %s", p), e);
57             }
58         } finally {
59             try {
60                 Files.deleteIfExists(p);
61             } catch (IOException e) {
62             }
63         }
64     }
65
66     private static void loadLibrary() throws IOException {
67       try (final InputStream is = vppConn.class.getResourceAsStream('/' + LIBNAME)) {
68           if (is == null) {
69               throw new IOException(String.format("Failed to open library resource %s",
70                                                 LIBNAME));
71           }
72           loadStream(is);
73         }
74     }
75
76     private static vppConn currentConnection = null;
77     private final String clientName;
78     private volatile boolean disconnected = false;
79
80     // Hidden on purpose to prevent external instantiation
81     vppConn(final String clientName) throws IOException {
82         this.clientName = clientName;
83
84         synchronized (vppConn.class) {
85             if (currentConnection != null) {
86                 throw new IOException("Already connected as " + currentConnection.clientName);
87             }
88
89             final int ret = clientConnect(clientName);
90             if (ret != 0) {
91                 throw new IOException("Connection returned error " + ret);
92             }
93
94             currentConnection = this;
95         }
96     }
97
98     @Override
99     public synchronized final void close() {
100         if (!disconnected) {
101             disconnected = true;
102
103             synchronized (vppConn.class) {
104                 clientDisconnect();
105                 currentConnection = null;
106             }
107         }
108     }
109
110     /**
111      * Check if this instance is connected.
112      *
113      * @throws IllegalStateException if this instance was disconnected.
114      */
115     protected final void checkConnected() {
116         if (disconnected) {
117             throw new IllegalStateException("Disconnected client " + clientName);
118         }
119     }
120
121     public final int getRetval(int context, int release) {
122         checkConnected();
123         return getRetval0(context, release);
124     }
125
126     public final String getInterfaceList (String nameFilter) {
127         checkConnected();
128         return getInterfaceList0(nameFilter);
129     }
130
131     public final int swIfIndexFromName (String interfaceName) {
132         checkConnected();
133         return swIfIndexFromName0(interfaceName);
134     }
135
136     public final String interfaceNameFromSwIfIndex (int swIfIndex) {
137         checkConnected();
138         return interfaceNameFromSwIfIndex0(swIfIndex);
139     }
140
141     public final void clearInterfaceTable () {
142         checkConnected();
143         clearInterfaceTable0();
144     }
145
146     public final vppInterfaceDetails[] swInterfaceDump (byte nameFilterValid, byte [] nameFilter) {
147         checkConnected();
148         return swInterfaceDump0(nameFilterValid, nameFilter);
149     }
150
151     public final int bridgeDomainIdFromName(String bridgeDomain) {
152         checkConnected();
153         return bridgeDomainIdFromName0(bridgeDomain);
154     }
155
156     public final int findOrAddBridgeDomainId(String bridgeDomain) {
157         checkConnected();
158         return findOrAddBridgeDomainId0(bridgeDomain);
159     }
160
161     public final vppVersion getVppVersion() {
162         checkConnected();
163         return getVppVersion0();
164     }
165
166     public final vppInterfaceCounters getInterfaceCounters(int swIfIndex) {
167         checkConnected();
168         return getInterfaceCounters0(swIfIndex);
169     }
170
171     public final int[] bridgeDomainDump(int bdId) {
172         checkConnected();
173         return bridgeDomainDump0(bdId);
174     }
175
176     public final vppBridgeDomainDetails getBridgeDomainDetails(int bdId) {
177         checkConnected();
178         return getBridgeDomainDetails0(bdId);
179     }
180
181     public final vppL2Fib[] l2FibTableDump(int bdId) {
182         checkConnected();
183         return l2FibTableDump0(bdId);
184     }
185
186     public final int bridgeDomainIdFromInterfaceName(String interfaceName) {
187         checkConnected();
188         return bridgeDomainIdFromInterfaceName0(interfaceName);
189     }
190
191     public final vppIPv4Address[] ipv4AddressDump(String interfaceName) {
192         checkConnected();
193         return ipv4AddressDump0(interfaceName);
194     }
195
196     public final vppIPv6Address[] ipv6AddressDump(String interfaceName) {
197         checkConnected();
198         return ipv6AddressDump0(interfaceName);
199     }
200
201     public final vppVxlanTunnelDetails[] vxlanTunnelDump(int swIfIndex) {
202         checkConnected();
203         return vxlanTunnelDump0(swIfIndex);
204     }
205
206     public final int setInterfaceDescription(String ifName, String ifDesc) {
207         checkConnected();
208         return setInterfaceDescription0(ifName, ifDesc);
209     }
210
211     public final String getInterfaceDescription(String ifName) {
212         checkConnected();
213         return getInterfaceDescription0(ifName);
214     }
215
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);
237 }