f32df0408221b0d9a6545adb1257092e1846e13e
[unimgr.git] / netvirt / src / main / java / org / opendaylight / unimgr / mef / netvirt / MdsalUtils.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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.unimgr.mef.netvirt;
10
11 import java.util.concurrent.ExecutionException;
12
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.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.common.base.Optional;
25 import com.google.common.util.concurrent.CheckedFuture;
26
27 public class MdsalUtils {
28
29     private static final Logger logger = LoggerFactory.getLogger(MdsalUtils.class);
30
31     public static <T extends DataObject> void syncWrite(DataBroker broker, LogicalDatastoreType datastoreType,
32             InstanceIdentifier<T> path, T data) {
33         CheckedFuture<Void, TransactionCommitFailedException> futures = write(broker, datastoreType, path, data);
34         try {
35             futures.get();
36         } catch (InterruptedException | ExecutionException e) {
37             logger.error("Error writing to datastore (path, data) : ({}, {})", path, data);
38             throw new RuntimeException(e.getMessage());
39         }
40     }
41
42     public static <T extends DataObject> void syncUpdate(DataBroker broker, LogicalDatastoreType datastoreType,
43             InstanceIdentifier<T> path, T data) {
44         CheckedFuture<Void, TransactionCommitFailedException> futures = update(broker, datastoreType, path, data);
45         try {
46             futures.get();
47         } catch (InterruptedException | ExecutionException e) {
48             logger.error("Error writing to datastore (path, data) : ({}, {})", path, data);
49             throw new RuntimeException(e.getMessage());
50         }
51     }
52
53     public static <T extends DataObject> void syncDelete(DataBroker broker, LogicalDatastoreType datastoreType,
54             InstanceIdentifier<T> obj) {
55         CheckedFuture<Void, TransactionCommitFailedException> futures = delete(broker, datastoreType, obj);
56         try {
57             futures.get();
58         } catch (InterruptedException | ExecutionException e) {
59             logger.error("Error deleting from datastore (path) : ({})", obj);
60             throw new RuntimeException(e.getMessage());
61         }
62     }
63
64     public static <T extends DataObject> CheckedFuture<Void, TransactionCommitFailedException> write(DataBroker broker,
65             LogicalDatastoreType datastoreType, InstanceIdentifier<T> path, T data) {
66         WriteTransaction tx = broker.newWriteOnlyTransaction();
67         tx.put(datastoreType, path, data, true);
68         CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
69         return futures;
70     }
71
72     public static <T extends DataObject> CheckedFuture<Void, TransactionCommitFailedException> update(DataBroker broker,
73             LogicalDatastoreType datastoreType, InstanceIdentifier<T> path, T data) {
74         WriteTransaction tx = broker.newWriteOnlyTransaction();
75         tx.merge(datastoreType, path, data, true);
76         CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
77         return futures;
78     }
79
80     public static <T extends DataObject> CheckedFuture<Void, TransactionCommitFailedException> delete(DataBroker broker,
81             LogicalDatastoreType datastoreType, InstanceIdentifier<T> obj) {
82         WriteTransaction tx = broker.newWriteOnlyTransaction();
83         tx.delete(datastoreType, obj);
84         CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
85         return futures;
86     }
87
88     public static <T extends DataObject> Optional<T> read(DataBroker broker, LogicalDatastoreType datastoreType,
89             InstanceIdentifier<T> path) {
90         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
91         Optional<T> result = Optional.absent();
92         try {
93             CheckedFuture<Optional<T>, ReadFailedException> checkedFuture = tx.read(datastoreType, path);
94             result = checkedFuture.get();
95         } catch (Exception e) {
96             throw new RuntimeException(e);
97         }
98
99         return result;
100     }
101 }