Use DataTreeChangeListener instead of DataChangeListener
[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 java.util.Collection;
11 import java.util.List;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.ovsdb.lib.operations.OperationResult;
16 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
17 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
18 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.util.concurrent.ListenableFuture;
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, 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, 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                 List<OperationResult> got = result.get();
59                 LOG.debug("OVSDB transaction result: {}", got);
60             } catch (Exception e) {
61                 LOG.warn("Transact execution exception: ", e);
62             }
63             LOG.trace("invoke exit command: {}, tb: {}", command, tb);
64         }
65     }
66
67 }