Merge "Skeleton for the ovsdb-ui feature"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / MdsalUtils.java
1 package org.opendaylight.ovsdb.openstack.netvirt.impl;
2
3 import com.google.common.base.Optional;
4 import com.google.common.util.concurrent.CheckedFuture;
5 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
6 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
7 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
8 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
9 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
10 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
11 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class MdsalUtils {
16     private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
17     private DataBroker databroker = null;
18
19     /**
20      * Class constructor setting the data broker.
21      *
22      * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
23      */
24     public MdsalUtils(DataBroker dataBroker) {
25         this.databroker = dataBroker;
26     }
27
28     /**
29      * Executes delete as a blocking transaction.
30      *
31      * @param store {@link LogicalDatastoreType} which should be modified
32      * @param path {@link InstanceIdentifier} to read from
33      * @param <D> the data object type
34      * @return the result of the request
35      */
36     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
37             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
38         boolean result = false;
39         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
40         transaction.delete(store, path);
41         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
42         try {
43             future.checkedGet();
44             result = true;
45         } catch (TransactionCommitFailedException e) {
46             LOG.warn("Failed to delete {} ", path, e);
47         }
48         return result;
49     }
50
51     /**
52      * Executes merge as a blocking transaction.
53      *
54      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
55      * @param path {@link InstanceIdentifier} for path to read
56      * @param <D> the data object type
57      * @return the result of the request
58      */
59     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean merge(
60             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
61         boolean result = false;
62         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
63         transaction.merge(logicalDatastoreType, path, data, true);
64         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
65         try {
66             future.checkedGet();
67             result = true;
68         } catch (TransactionCommitFailedException e) {
69             LOG.warn("Failed to merge {} ", path, e);
70         }
71         return result;
72     }
73
74     /**
75      * Executes put as a blocking transaction.
76      *
77      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
78      * @param path {@link InstanceIdentifier} for path to read
79      * @param <D> the data object type
80      * @return the result of the request
81      */
82     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean put(
83             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
84         boolean result = false;
85         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
86         transaction.put(logicalDatastoreType, path, data, true);
87         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
88         try {
89             future.checkedGet();
90             result = true;
91         } catch (TransactionCommitFailedException e) {
92             LOG.warn("Failed to put {} ", path, e);
93         }
94         return result;
95     }
96
97     /**
98      * Executes read as a blocking transaction.
99      *
100      * @param store {@link LogicalDatastoreType} to read
101      * @param path {@link InstanceIdentifier} for path to read
102      * @param <D> the data object type
103      * @return the result as the data object requested
104      */
105     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
106             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
107         D result = null;
108         final ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
109         Optional<D> optionalDataObject;
110         CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
111         try {
112             optionalDataObject = future.checkedGet();
113             if (optionalDataObject.isPresent()) {
114                 result = optionalDataObject.get();
115             } else {
116                 LOG.debug("{}: Failed to read {}",
117                         Thread.currentThread().getStackTrace()[1], path);
118             }
119         } catch (ReadFailedException e) {
120             LOG.warn("Failed to read {} ", path, e);
121         }
122         transaction.close();
123         return result;
124     }
125 }