2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. and others. All rights reserved.
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
9 package org.opendaylight.unimgr.mef.netvirt;
11 import java.util.concurrent.ExecutionException;
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;
24 import com.google.common.base.Optional;
25 import com.google.common.util.concurrent.CheckedFuture;
27 public class MdsalUtils {
29 private static final Logger logger = LoggerFactory.getLogger(MdsalUtils.class);
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);
36 } catch (InterruptedException | ExecutionException e) {
37 logger.error("Error writing to datastore (path, data) : ({}, {})", path, data);
38 throw new RuntimeException(e.getMessage());
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);
47 } catch (InterruptedException | ExecutionException e) {
48 logger.error("Error writing to datastore (path, data) : ({}, {})", path, data);
49 throw new RuntimeException(e.getMessage());
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);
58 } catch (InterruptedException | ExecutionException e) {
59 logger.error("Error deleting from datastore (path) : ({})", obj);
60 throw new RuntimeException(e.getMessage());
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();
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();
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();
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();
93 CheckedFuture<Optional<T>, ReadFailedException> checkedFuture = tx.read(datastoreType, path);
94 result = checkedFuture.get();
95 } catch (Exception e) {
96 throw new RuntimeException(e);