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