blob: 3e8c12a93f9fb8595d089114ee541a465898b570 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
16package org.openvpp.vppjapi;
17
18import java.io.IOException;
19import java.io.InputStream;
20import java.nio.file.Files;
21import java.nio.file.Path;
22import java.nio.file.StandardCopyOption;
23import java.nio.file.attribute.PosixFilePermission;
24import java.nio.file.attribute.PosixFilePermissions;
25import java.util.Set;
26
27import org.openvpp.vppjapi.vppVersion;
Dave Wallacebf8c15e2015-12-17 20:54:54 -050028import org.openvpp.vppjapi.vppInterfaceDetails;
Ed Warnickecb9cada2015-12-08 15:45:58 -070029import org.openvpp.vppjapi.vppInterfaceCounters;
30import org.openvpp.vppjapi.vppBridgeDomainDetails;
Dave Wallacebf8c15e2015-12-17 20:54:54 -050031import org.openvpp.vppjapi.vppIPv4Address;
32import org.openvpp.vppjapi.vppIPv6Address;
33import org.openvpp.vppjapi.vppVxlanTunnelDetails;
Ed Warnickecb9cada2015-12-08 15:45:58 -070034
Robert Varga67ba3be2016-01-31 12:20:47 +010035public class vppConn implements AutoCloseable {
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 private static final String LIBNAME = "libvppjni.so.0.0.0";
37
38 static {
39 try {
40 loadLibrary();
Robert Vargae1cfcbc2016-02-14 02:10:18 +010041 } catch (Exception e) {
42 System.out.printf("Can't find vpp jni library: %s\n", LIBNAME);
Robert Varga67ba3be2016-01-31 12:20:47 +010043 throw new ExceptionInInitializerError(e);
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 }
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));
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 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) {
Robert Vargae1cfcbc2016-02-14 02:10:18 +010069 throw new IOException(String.format("Failed to open library resource %s",
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 LIBNAME));
71 }
72 loadStream(is);
73 }
74 }
75
Robert Varga67ba3be2016-01-31 12:20:47 +010076 private static vppConn currentConnection = null;
Robert Varga67ba3be2016-01-31 12:20:47 +010077 private final String clientName;
Robert Vargae1cfcbc2016-02-14 02:10:18 +010078 private volatile boolean disconnected = false;
Robert Varga67ba3be2016-01-31 12:20:47 +010079
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
Robert Vargae1cfcbc2016-02-14 02:10:18 +010099 public synchronized final void close() {
100 if (!disconnected) {
101 disconnected = true;
102
Robert Varga67ba3be2016-01-31 12:20:47 +0100103 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() {
Robert Vargae1cfcbc2016-02-14 02:10:18 +0100116 if (disconnected) {
Robert Varga67ba3be2016-01-31 12:20:47 +0100117 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237}