Fix findbugs violations in hwvtepsouthbound-impl
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / TransactInvokerImpl.java
index 4402c889c136b4a84b5676294c62a39b59de4d7e..ae77d46689d1dc18b95b125c34adb68722ab0e49 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright © 2015, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,21 +8,27 @@
 
 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
 
+import com.google.common.base.Strings;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.util.List;
-
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ExecutionException;
 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
+import org.opendaylight.ovsdb.lib.operations.Delete;
+import org.opendaylight.ovsdb.lib.operations.Insert;
+import org.opendaylight.ovsdb.lib.operations.Operation;
 import org.opendaylight.ovsdb.lib.operations.OperationResult;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
+import org.opendaylight.ovsdb.lib.operations.Update;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.util.concurrent.ListenableFuture;
-
 public class TransactInvokerImpl implements TransactInvoker {
     private static final Logger LOG = LoggerFactory.getLogger(TransactInvokerImpl.class);
-    private HwvtepConnectionInstance connectionInstance;
-    private DatabaseSchema dbSchema;
+    private final HwvtepConnectionInstance connectionInstance;
+    private final DatabaseSchema dbSchema;
 
     public TransactInvokerImpl(HwvtepConnectionInstance connectionInstance, DatabaseSchema dbSchema) {
         this.connectionInstance = connectionInstance;
@@ -31,19 +37,66 @@ public class TransactInvokerImpl implements TransactInvoker {
 
     @Override
     public void invoke(TransactCommand command) {
-        TransactionBuilder tb = new TransactionBuilder(connectionInstance, dbSchema);
+        TransactionBuilder tb = new TransactionBuilder(connectionInstance.getOvsdbClient(), dbSchema);
         command.execute(tb);
         ListenableFuture<List<OperationResult>> result = tb.execute();
         LOG.debug("invoke: command: {}, tb: {}", command, tb);
         if (tb.getOperations().size() > 0) {
             try {
                 List<OperationResult> got = result.get();
-                LOG.debug("OVSDB transaction result: {}", got);
-            } catch (Exception e) {
+                LOG.debug("HWVTEP transaction result: {}", got);
+                boolean errorOccured = false;
+                if (got != null && got.size() > 0) {
+                    for (OperationResult opResult : got) {
+                        if (!Strings.isNullOrEmpty(opResult.getError())) {
+                            LOG.error("HWVTEP transaction operation failed {} {}",
+                                    opResult.getError(), opResult.getDetails());
+                            errorOccured = true;
+                        }
+                    }
+                }
+                if (errorOccured) {
+                    printError(tb);
+                    command.onFailure(tb);
+                } else {
+                    command.onSuccess(tb);
+                }
+            } catch (InterruptedException | ExecutionException e) {
                 LOG.warn("Transact execution exception: ", e);
             }
             LOG.trace("invoke exit command: {}, tb: {}", command, tb);
         }
     }
 
+    void printError(TransactionBuilder tb) {
+        StringBuffer sb = new StringBuffer();
+        for (Operation op : tb.getOperations()) {
+            if (op instanceof Insert) {
+                Insert insert = (Insert)op;
+                Map<String, Object> row = insert.getRow();
+                sb.append("insert [");
+                if (row != null) {
+                    for (Entry<String, Object> entry : row.entrySet()) {
+                        sb.append(entry.getKey()).append(" : ").append(entry.getValue()).append(" , ");
+                    }
+                }
+                sb.append("]   ");
+            } else if (op instanceof Delete) {
+                Delete delete = (Delete)op;
+                sb.append("delete from ");
+                sb.append(delete.getTableSchema().getName());
+            } else if (op instanceof Update) {
+                Update update = (Update)op;
+                sb.append("update [");
+                Map<String, Object> row = update.getRow();
+                if (row != null) {
+                    for (Entry<String, Object> entry : row.entrySet()) {
+                        sb.append(entry.getKey()).append(" : ").append(entry.getValue()).append(" , ");
+                    }
+                }
+                sb.append("]");
+            }
+        }
+        LOG.error("Failed transaction {}", sb);
+    }
 }