2 * Copyright (C) 2014 EBay Software Foundation
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Ashwin Raveendran
10 package org.opendaylight.ovsdb.lib;
12 import com.fasterxml.jackson.databind.JsonNode;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Maps;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.SettableFuture;
19 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
20 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
21 import org.opendaylight.ovsdb.lib.message.TransactBuilder;
22 import org.opendaylight.ovsdb.lib.operations.Operation;
23 import org.opendaylight.ovsdb.lib.operations.OperationResult;
24 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
25 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
27 import java.util.List;
29 import java.util.Queue;
30 import java.util.concurrent.ExecutorService;
33 public class OvsDBClientImpl implements OvsDBClient {
35 ExecutorService executorService;
38 Map<String, DatabaseSchema> schema = Maps.newHashMap();
39 Queue<Throwable> exceptions;
41 public OvsDBClientImpl(OvsdbRPC rpc, ExecutorService executorService) {
43 this.executorService = executorService;
46 public OvsDBClientImpl() {
50 public ListenableFuture<List<OperationResult>> transact(List<Operation> operations) {
52 //todo, we may not need transactionbuilder if we can have JSON objects
53 TransactBuilder builder = new TransactBuilder();
54 for (Operation o : operations) {
55 builder.addOperation(o);
58 ListenableFuture<List<OperationResult>> transact = rpc.transact(builder);
63 public void monitor(MonitorRequest monitorRequest, MonitorCallBack callback) {
64 throw new UnsupportedOperationException("not yet implemented");
68 public void cancelMonitor(MonitorHandle handler) {
69 throw new UnsupportedOperationException("not yet implemented");
73 public void lock(String lockId, LockAquisitionCallback lockedCallBack, LockStolenCallback stolenCallback) {
74 throw new UnsupportedOperationException("not yet implemented");
78 public ListenableFuture<Boolean> steal(String lockId) {
79 throw new UnsupportedOperationException("not yet implemented");
83 public ListenableFuture<Boolean> unLock(String lockId) {
84 throw new UnsupportedOperationException("not yet implemented");
88 public void startEchoService(EchoServiceCallbackFilters callbackFilters) {
89 throw new UnsupportedOperationException("not yet implemented");
93 public void stopEchoService() {
94 throw new UnsupportedOperationException("not yet implemented");
98 public TransactionBuilder transactBuilder() {
99 return new TransactionBuilder(this);
103 public boolean isReady(long timeout) {
104 //todo implement timeout
105 return null != schema;
109 public ListenableFuture<List<String>> getDatabases() {
110 return rpc.list_dbs();
114 public ListenableFuture<DatabaseSchema> getSchema(final String database, final boolean cacheResult) {
116 DatabaseSchema databaseSchema = schema.get(database);
118 if (databaseSchema == null) {
119 ListenableFuture<Map<String, DatabaseSchema>> schemaFromDevice = getSchemaFromDevice(Lists.newArrayList(database));
121 final SettableFuture<DatabaseSchema> future = SettableFuture.create();
122 Futures.addCallback(schemaFromDevice, new FutureCallback<Map<String, DatabaseSchema>>() {
124 public void onSuccess(Map<String, DatabaseSchema> result) {
125 if (result.containsKey(database)) {
126 DatabaseSchema s = result.get(database);
128 OvsDBClientImpl.this.schema.put(database, s);
137 public void onFailure(Throwable t) {
139 future.setException(t);
145 return Futures.immediateFuture(databaseSchema);
149 private ListenableFuture<Map<String, DatabaseSchema>> getSchemaFromDevice(final List<String> dbNames) {
150 Map<String, DatabaseSchema> schema = Maps.newHashMap();
151 SettableFuture<Map<String, DatabaseSchema>> future = SettableFuture.create();
152 _populateSchema(dbNames, schema, future);
156 private void _populateSchema(final List<String> dbNames,
157 final Map<String, DatabaseSchema> schema,
158 final SettableFuture<Map<String, DatabaseSchema>> sfuture) {
160 if (dbNames == null || dbNames.isEmpty()) {
164 Futures.transform(rpc.get_schema(Lists.newArrayList(dbNames.get(0))),
165 new com.google.common.base.Function<JsonNode, Void>() {
167 public Void apply(JsonNode jsonNode) {
169 schema.put(dbNames.get(0), DatabaseSchema.fromJson(jsonNode));
170 if (schema.size() > 1 && !sfuture.isCancelled()) {
171 _populateSchema(dbNames.subList(1, dbNames.size()), schema, sfuture);
172 } else if (schema.size() == 1) {
175 } catch (Throwable e) {
176 sfuture.setException(e);
182 public void setRpc(OvsdbRPC rpc) {
186 public Queue<Throwable> getExceptions() {