2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openvpp.jvpp;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.StandardCopyOption;
24 import java.nio.file.attribute.PosixFilePermission;
25 import java.nio.file.attribute.PosixFilePermissions;
26 import java.util.HashMap;
28 import java.util.Objects;
30 import java.util.logging.Logger;
31 import org.openvpp.jvpp.callback.JVppCallback;
34 * JNI based representation of a management connection to VPP
36 public final class VppJNIConnection implements VppConnection {
37 private final static Logger LOG = Logger.getLogger(VppJNIConnection.class.getName());
38 private static final String LIBNAME = "libjvpp.so.0.0.0";
43 } catch (Exception e) {
44 LOG.severe("Can't find vpp jni library: " + LIBNAME);
45 throw new ExceptionInInitializerError(e);
49 private static void loadStream(final InputStream is) throws IOException {
50 final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---");
51 final Path p = Files.createTempFile(LIBNAME, null, PosixFilePermissions.asFileAttribute(perms));
53 Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
56 Runtime.getRuntime().load(p.toString());
57 } catch (UnsatisfiedLinkError e) {
58 throw new IOException("Failed to load library " + p, e);
62 Files.deleteIfExists(p);
63 } catch (IOException e) {
68 private static void loadLibrary() throws IOException {
69 try (final InputStream is = VppJNIConnection.class.getResourceAsStream('/' + LIBNAME)) {
71 throw new IOException("Failed to open library resource " + LIBNAME);
77 private final String clientName;
78 private volatile boolean disconnected = false;
80 private VppJNIConnection(final String clientName) {
81 this.clientName = Objects.requireNonNull(clientName,"Null clientName");
85 * Guarded by VppJNIConnection.class
87 private static final Map<String, VppJNIConnection> connections = new HashMap<>();
90 * Create a new Vpp connection identified by clientName parameter.
92 * Multiple instances are allowed since this class is not a singleton
93 * (VPP allows multiple management connections).
95 * However only a single connection per clientName is allowed.
97 * @param clientName identifier of vpp connection
98 * @param callback global callback to receive response calls from vpp
100 * @return new Vpp connection
101 * @throws IOException in case the connection could not be established, or there already is a connection with the same name
103 public static VppJNIConnection create(final String clientName, final JVppCallback callback) throws IOException {
104 synchronized (VppJNIConnection.class) {
105 if(connections.containsKey(clientName)) {
106 throw new IOException("Client " + clientName + " already connected");
109 final VppJNIConnection vppJNIConnection = new VppJNIConnection(clientName);
110 final int ret = clientConnect(clientName, callback);
112 throw new IOException("Connection returned error " + ret);
114 connections.put(clientName, vppJNIConnection);
115 return vppJNIConnection;
120 public final void checkActive() {
122 throw new IllegalStateException("Disconnected client " + clientName);
127 public synchronized final void close() {
133 synchronized (VppJNIConnection.class) {
134 connections.remove(clientName);
140 private static native int clientConnect(String clientName, JVppCallback callback);
141 private static native void clientDisconnect();