Bug-1190: Fix for select response 02/8602/6
authorSudheendra Murthy <sumurthy@ebay.com>
Thu, 3 Jul 2014 04:06:51 +0000 (21:06 -0700)
committerSudheendra Murthy <sumurthy@ebay.com>
Mon, 7 Jul 2014 21:50:50 +0000 (14:50 -0700)
Updated transact interface in OvsdbRPC to return JSON response and
deserializing to OperationResult being handled in OvsdbClientImpl

Change-Id: I061292435e68a638f83e69495710ffaa3a04479b
Signed-off-by: Sudheendra Murthy <sudhi.vm@gmail.com>
library/src/main/java/org/opendaylight/ovsdb/lib/impl/FutureTransformUtils.java [new file with mode: 0644]
library/src/main/java/org/opendaylight/ovsdb/lib/impl/OvsdbClientImpl.java
library/src/main/java/org/opendaylight/ovsdb/lib/message/OvsdbRPC.java
library/src/main/java/org/opendaylight/ovsdb/lib/schema/TableSchema.java
library/src/test/java/org/opendaylight/ovsdb/lib/OvsdbClientTestIT.java

diff --git a/library/src/main/java/org/opendaylight/ovsdb/lib/impl/FutureTransformUtils.java b/library/src/main/java/org/opendaylight/ovsdb/lib/impl/FutureTransformUtils.java
new file mode 100644 (file)
index 0000000..b027cb1
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2014 EBay Software Foundation
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Authors : Sudheendra Murthy
+ */
+
+package org.opendaylight.ovsdb.lib.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Function;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.opendaylight.ovsdb.lib.operations.Operation;
+import org.opendaylight.ovsdb.lib.operations.OperationResult;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class FutureTransformUtils {
+    private static final ObjectMapper objectMapper = new ObjectMapper();
+
+    public final static ListenableFuture<List<OperationResult>> transformTransactResponse
+            (ListenableFuture<List<JsonNode>> transactResponseFuture, final List<Operation> operations) {
+        return Futures.transform(transactResponseFuture, new Function<List<JsonNode>, List<OperationResult>>() {
+            @Override
+            public List<OperationResult> apply(List<JsonNode> jsonNodes) {
+                final List<OperationResult> operationResults = new ArrayList<>();
+                for (int i = 0; i < jsonNodes.size(); i++) {
+                    JsonNode jsonNode = jsonNodes.get(i);
+                    OperationResult or;
+                    if (jsonNode.size() > 0) {
+                        Operation op = operations.get(i);
+                        switch (op.getOp()) {
+                            case "select":
+                                or = new OperationResult();
+                                or.setRows(op.getTableSchema().createRows(jsonNode));
+
+                                break;
+
+                            default:
+                                or = objectMapper.convertValue(jsonNode, OperationResult.class);
+
+                                break;
+                        }
+                    } else {
+                        or = new OperationResult();
+                    }
+
+                    operationResults.add(or);
+                }
+
+                return operationResults;
+            }
+        });
+    }
+}
index c87f39f6287fab8d63dfb162a605ccb597a6e31c..c2afd024fa376a130e64ea33c6eb3e48bb8887e6 100644 (file)
@@ -9,17 +9,17 @@
  */
 package org.opendaylight.ovsdb.lib.impl;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.base.Function;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
 import io.netty.channel.Channel;
-
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Queue;
-import java.util.UUID;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-
 import org.opendaylight.ovsdb.lib.EchoServiceCallbackFilters;
 import org.opendaylight.ovsdb.lib.LockAquisitionCallback;
 import org.opendaylight.ovsdb.lib.LockStolenCallback;
@@ -48,15 +48,14 @@ import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.google.common.base.Function;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.SettableFuture;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.UUID;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
 
 
 public class OvsdbClientImpl implements OvsdbClient {
@@ -70,6 +69,7 @@ public class OvsdbClientImpl implements OvsdbClient {
     private OvsdbRPC.Callback rpcCallback;
     private OvsdbConnectionInfo connectionInfo;
     private Channel channel;
+    private static final ObjectMapper objectMapper = new ObjectMapper();
 
     public OvsdbClientImpl(OvsdbRPC rpc, Channel channel, ConnectionType type, ExecutorService executorService) {
         this.rpc = rpc;
@@ -142,7 +142,7 @@ public class OvsdbClientImpl implements OvsdbClient {
             builder.addOperation(o);
         }
 
-        return rpc.transact(builder);
+        return FutureTransformUtils.transformTransactResponse(rpc.transact(builder), operations);
     }
 
     @Override
index cb50d6810577ffab6023859ee65a0b91b813eb04..c108ce5cf4920126e891c92be8988997bf6b4248 100644 (file)
@@ -9,15 +9,12 @@
  */
 package org.opendaylight.ovsdb.lib.message;
 
-import java.util.List;
-
-import org.opendaylight.ovsdb.lib.operations.OperationResult;
-
 import com.fasterxml.jackson.databind.JsonNode;
 import com.google.common.util.concurrent.ListenableFuture;
-
 import org.opendaylight.ovsdb.lib.jsonrpc.Params;
 
+import java.util.List;
+
 public interface OvsdbRPC {
     public static final String REGISTER_CALLBACK_METHOD = "registerCallback";
 
@@ -30,7 +27,7 @@ public interface OvsdbRPC {
 
     public ListenableFuture<List<String>> list_dbs();
 
-    public ListenableFuture<List<OperationResult>> transact(TransactBuilder transact);
+    public ListenableFuture<List<JsonNode>> transact(TransactBuilder transact);
 
     public ListenableFuture<Response> cancel(String id);
 
index 21ef290c6aab802a9fa85134bda2637ec15431a6..f34c4842aeef4836a70c465d8602e112a6f13f63 100644 (file)
@@ -12,6 +12,7 @@ package org.opendaylight.ovsdb.lib.schema;
 import java.lang.reflect.Constructor;
 import java.util.Iterator;
 import java.util.List;
+import java.util.ArrayList;
 import java.util.Map;
 import java.util.Set;
 
@@ -159,6 +160,15 @@ public abstract class TableSchema<E extends TableSchema<E>> {
         return new Row<>(this, columns);
     }
 
+    public ArrayList<Row<E>> createRows(JsonNode rowsNode) {
+        ArrayList<Row<E>> rows = Lists.newArrayList();
+        for (JsonNode rowNode : rowsNode.get("rows")) {
+            rows.add(createRow((ObjectNode)rowNode));
+        }
+
+        return rows;
+    }
+
     /*
      * RFC 7047 Section 3.2 specifies 2 internally generated columns in each table
      * namely _uuid and _version which are not exposed in get_schema call.
index aaa3d8ef3abb01a93a0eeebc5478058db47b4ef0..4f272bbb83f9283af95b8c46af503e0f2dd0bf49 100644 (file)
@@ -233,7 +233,7 @@ public class OvsdbClientTestIT extends OvsdbTestBase {
 
         String namedUuid = "br_test";
         int insertOperationIndex = 0;
-        UUID parentTable = getOpenVSwitchTableUuid();
+        UUID parentTable = selectOpenVSwitchTableUuid();
         TransactionBuilder transactionBuilder = ovs.transactBuilder()
                  /*
                   * Make sure that the position of insert operation matches the insertOperationIndex.
@@ -360,7 +360,7 @@ public class OvsdbClientTestIT extends OvsdbTestBase {
         GenericTableSchema ovsTable = dbSchema.table("Open_vSwitch", GenericTableSchema.class);
         ColumnSchema<GenericTableSchema, Set<UUID>> bridges = ovsTable.multiValuedColumn("bridges", UUID.class);
         ColumnSchema<GenericTableSchema, UUID> _uuid = ovsTable.column("_uuid", UUID.class);
-        UUID parentTable = getOpenVSwitchTableUuid();
+        UUID parentTable = selectOpenVSwitchTableUuid();
 
         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
                 .add(op.delete(bridge)