2 * Copyright © 2015, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
11 import java.util.List;
14 import com.google.common.base.Strings;
15 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
16 import org.opendaylight.ovsdb.lib.operations.Delete;
17 import org.opendaylight.ovsdb.lib.operations.Insert;
18 import org.opendaylight.ovsdb.lib.operations.Operation;
19 import org.opendaylight.ovsdb.lib.operations.OperationResult;
20 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
21 import org.opendaylight.ovsdb.lib.operations.Update;
22 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.common.util.concurrent.ListenableFuture;
28 public class TransactInvokerImpl implements TransactInvoker {
29 private static final Logger LOG = LoggerFactory.getLogger(TransactInvokerImpl.class);
30 private HwvtepConnectionInstance connectionInstance;
31 private DatabaseSchema dbSchema;
33 public TransactInvokerImpl(HwvtepConnectionInstance connectionInstance, DatabaseSchema dbSchema) {
34 this.connectionInstance = connectionInstance;
35 this.dbSchema = dbSchema;
39 public void invoke(TransactCommand command) {
40 TransactionBuilder tb = new TransactionBuilder(connectionInstance.getOvsdbClient(), dbSchema);
42 ListenableFuture<List<OperationResult>> result = tb.execute();
43 LOG.debug("invoke: command: {}, tb: {}", command, tb);
44 if (tb.getOperations().size() > 0) {
46 List<OperationResult> got = result.get();
47 LOG.debug("HWVTEP transaction result: {}", got);
48 boolean errorOccured = false;
49 if (got != null && got.size() > 0) {
50 for (OperationResult opResult : got) {
51 if (!Strings.isNullOrEmpty(opResult.getError())) {
52 LOG.error("HWVTEP transaction operation failed {} {}",
53 opResult.getError(), opResult.getDetails());
60 command.onFailure(tb);
62 command.onSuccess(tb);
64 } catch (Exception e) {
65 LOG.warn("Transact execution exception: ", e);
67 LOG.trace("invoke exit command: {}, tb: {}", command, tb);
71 void printError(TransactionBuilder tb) {
72 StringBuffer sb = new StringBuffer();
73 for (Operation op : tb.getOperations()) {
74 if (op instanceof Insert) {
75 Insert insert = (Insert)op;
76 Map row = insert.getRow();
77 sb.append("insert [");
79 for (Object key : row.keySet()) {
80 sb.append(key).append(" : ").append(row.get(key)).append(" , ");
84 } else if (op instanceof Delete) {
85 Delete delete = (Delete)op;
86 sb.append("delete from " );
87 sb.append(delete.getTableSchema().getName());
88 } else if (op instanceof Update) {
89 Update update = (Update)op;
90 sb.append("update [" );
91 Map row = update.getRow();
93 for (Object key : row.keySet()) {
94 sb.append(key).append(" : ").append(row.get(key)).append(" , ");
100 LOG.error("Failed transaction {}", sb);