Getting Started - vmware-archive/ovsdb-client-library GitHub Wiki
In order to use this library you have to add a dependency to the pom file:
<dependencies>
<dependency>
<groupId>com.vmware.ovsdb</groupId>
<artifactId>ovsdb-client</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
When the OVSDB client uses passive connection mode, it implies that the OVSDB server is running on active connection mode. In other words, the client listens on certain port (6640 by default) and waits for the server to connects. For more information, see ovsdb-server(1)
In the following example, the ovsdb-server is started by command:
$ ovsdb-server --remote=tcp:192.168.201.4:6640
Note: You can also configure it to read connection methods from a db table. For example, the manager table in hardware_vtep database.
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
OvsdbPassiveConnectionListener listener = new OvsdbPassiveConnectionListenerImpl(executorService); // (1)
CompletableFuture<OvsdbClient> ovsdbClientFuture = new CompletableFuture<>();
ConnectionCallback connectionCallback = new ConnectionCallback() { // (2)
public void connected(OvsdbClient ovsdbClient) {
System.out.println(ovsdbClient + " connected");
ovsdbClientFuture.complete(ovsdbClient);
}
public void disconnected(OvsdbClient ovsdbClient) {
System.out.println(ovsdbClient + " disconnected");
}
};
listener.startListening(6640, connectionCallback).join(); // (3)
OvsdbClient ovsdbClient = ovsdbClientFuture.get(3, TimeUnit.SECONDS); // (4)
CompletableFuture<String[]> f = ovsdbClient.listDatabases();
String[] dbs = f.get(3, TimeUnit.SECONDS);
System.out.println(Arrays.toString(dbs));
From above example we can see the steps of getting an OvsdbClient
object from a passive connection.
(1) Construct a OvsdbPassiveConnectionListener
. The OvsdbPassiveConnectionListenerImpl
takes a ScheduledExecutorService
for asynchronous operations.
(2) Implement the ConnectionCallback
interface and construct a callback object.
(3) Start listening on the port.
(4) Get the OvsdbClient
object from the callback and use it for operations on the OVSDB server.
Note:
- All the interfaces provided by
OvsdbClient
are asynchronous and return aCompletableFuture
. See OvsdbClient.java. - Exception handling is omitted in this example.
When the OVSDB client uses active connection mode, it implies that the OVSDB server is running on passive connection mode. In other words, the server listens on certain port (6640 by default) and waits for the client to connects. For more information, see ovsdb-server(1)
In the following example, the ovsdb-server is started by command:
$ ovsdb-server --remote=ptcp:6640
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
OvsdbActiveConnectionConnector connector = new OvsdbActiveConnectionConnectorImpl(executorService); // (1)
CompletableFuture<OvsdbClient> ovsdbClientFuture = connector.connect("192.168.33.74", 6640); // (2)
OvsdbClient ovsdbClient = ovsdbClientFuture.get(3, TimeUnit.SECONDS); // (3)
CompletableFuture<String[]> f = ovsdbClient.listDatabases();
String[] dbs = f.get(3, TimeUnit.SECONDS);
System.out.println(Arrays.toString(dbs));
From above example we can see the steps of getting an OvsdbClient
object from an active connection.
(1) Construct a OvsdbActiveConnectionConnector
. The OvsdbActiveConnectionConnectorImpl
takes a ScheduledExecutorService
for asynchronous operations.
(2) Connect to the host:port and get a CompletableFuture<OvsdbClient>
.
(3) Get the OvsdbClient
object from the CompletableFuture<OvsdbClient>
.