Bug 6284 - Cancellation exceptions during operation execution
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TransactInvokerImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.ovsdb.lib.operations.OperationResult;
18 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
19 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
20 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class TransactInvokerImpl implements TransactInvoker {
28     private static final Logger LOG = LoggerFactory.getLogger(TransactInvokerImpl.class);
29     private OvsdbConnectionInstance connectionInstance;
30     private DatabaseSchema dbSchema;
31
32     public TransactInvokerImpl(OvsdbConnectionInstance connectionInstance, DatabaseSchema dbSchema) {
33         this.connectionInstance = connectionInstance;
34         this.dbSchema = dbSchema;
35     }
36
37     @Override
38     public void invoke(TransactCommand command, BridgeOperationalState state,
39                        AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
40         TransactionBuilder tb = new TransactionBuilder(connectionInstance.getOvsdbClient(), dbSchema);
41         command.execute(tb, state, events);
42         invoke(command, tb);
43     }
44
45     @Override
46     public void invoke(TransactCommand command, BridgeOperationalState state,
47                        Collection<DataTreeModification<Node>> modifications) {
48         TransactionBuilder tb = new TransactionBuilder(connectionInstance.getOvsdbClient(), dbSchema);
49         command.execute(tb, state, modifications);
50         invoke(command, tb);
51     }
52
53     private void invoke(TransactCommand command, TransactionBuilder tb) {
54         ListenableFuture<List<OperationResult>> result = tb.execute();
55         LOG.debug("invoke: command: {}, tb: {}", command, tb);
56         if (tb.getOperations().size() > 0) {
57             try {
58                 if (!result.isCancelled()) {
59                     List<OperationResult> got = result.get();
60                     LOG.debug("OVSDB transaction result: {}", got);
61                 } else {
62                     LOG.debug("Operation task cancelled for transaction : {}", tb);
63                 }
64             } catch (InterruptedException | ExecutionException e) {
65                 LOG.warn("Transact execution exception: ", e);
66             }
67             LOG.trace("invoke exit command: {}, tb: {}", command, tb);
68         }
69     }
70 }