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