bug 8029 added ref counts for physical locators.
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / TransactInvokerImpl.java
1 /*
2  * Copyright © 2015, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10
11 import java.util.List;
12 import java.util.Map;
13
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;
25
26 import com.google.common.util.concurrent.ListenableFuture;
27
28 public class TransactInvokerImpl implements TransactInvoker {
29     private static final Logger LOG = LoggerFactory.getLogger(TransactInvokerImpl.class);
30     private HwvtepConnectionInstance connectionInstance;
31     private DatabaseSchema dbSchema;
32
33     public TransactInvokerImpl(HwvtepConnectionInstance connectionInstance, DatabaseSchema dbSchema) {
34         this.connectionInstance = connectionInstance;
35         this.dbSchema = dbSchema;
36     }
37
38     @Override
39     public void invoke(TransactCommand command) {
40         TransactionBuilder tb = new TransactionBuilder(connectionInstance.getOvsdbClient(), dbSchema);
41         command.execute(tb);
42         ListenableFuture<List<OperationResult>> result = tb.execute();
43         LOG.debug("invoke: command: {}, tb: {}", command, tb);
44         if (tb.getOperations().size() > 0) {
45             try {
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());
54                             errorOccured = true;
55                         }
56                     }
57                 }
58                 if (errorOccured) {
59                     printError(tb);
60                     command.onFailure(tb);
61                 } else {
62                     command.onSuccess(tb);
63                 }
64             } catch (Exception e) {
65                 LOG.warn("Transact execution exception: ", e);
66             }
67             LOG.trace("invoke exit command: {}, tb: {}", command, tb);
68         }
69     }
70
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 [");
78                 if (row != null) {
79                     for (Object key : row.keySet()) {
80                         sb.append(key).append(" : ").append(row.get(key)).append(" , ");
81                     }
82                 }
83                 sb.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();
92                 if (row != null) {
93                     for (Object key : row.keySet()) {
94                         sb.append(key).append(" : ").append(row.get(key)).append(" , ");
95                     }
96                 }
97                 sb.append("]");
98             }
99         }
100         LOG.error("Failed transaction {}", sb);
101     }
102 }