30d74d129bb68b94e46ec0e1b22de68ce8d3cd3f
[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                     connectionInstance.getDeviceInfo().clearInTransitData();
60                     printError(tb);
61                 }
62             } catch (Exception e) {
63                 LOG.warn("Transact execution exception: ", e);
64             }
65             LOG.trace("invoke exit command: {}, tb: {}", command, tb);
66         }
67     }
68
69     void printError(TransactionBuilder tb) {
70         StringBuffer sb = new StringBuffer();
71         for (Operation op : tb.getOperations()) {
72             if (op instanceof Insert) {
73                 Insert insert = (Insert)op;
74                 Map row = insert.getRow();
75                 sb.append("insert [");
76                 if (row != null) {
77                     for (Object key : row.keySet()) {
78                         sb.append(key).append(" : ").append(row.get(key)).append(" , ");
79                     }
80                 }
81                 sb.append("]   ");
82             } else if (op instanceof Delete) {
83                 Delete delete = (Delete)op;
84                 sb.append("delete from " );
85                 sb.append(delete.getTableSchema().getName());
86             } else if (op instanceof Update) {
87                 Update update = (Update)op;
88                 sb.append("update [" );
89                 Map row = update.getRow();
90                 if (row != null) {
91                     for (Object key : row.keySet()) {
92                         sb.append(key).append(" : ").append(row.get(key)).append(" , ");
93                     }
94                 }
95                 sb.append("]");
96             }
97         }
98         LOG.error("Failed transaction {}", sb);
99     }
100 }