2 * Copyright (c) 2015 Red Hat, Inc. 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
8 package org.opendaylight.ovsdb.southbound.it;
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;
23 * Utility class for mdsal transactions.
25 * @author Sam Hague (shague@redhat.com)
27 public class MdsalUtils {
28 private static final Logger LOG = LoggerFactory.getLogger(SouthboundIT.class);
29 private DataBroker databroker = null;
32 * Class constructor setting the data broker.
34 * @param dataBroker the {@link DataBroker}
36 public MdsalUtils(DataBroker dataBroker) {
37 this.databroker = dataBroker;
41 * Executes delete as a blocking transaction.
43 * @param store {@link LogicalDatastoreType} which should be modified
44 * @param path {@link InstanceIdentifier} to read from
45 * @param <D> the data object type
46 * @return the result of the request
48 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
49 final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
50 boolean result = false;
51 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
52 transaction.delete(store, path);
53 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
57 } catch (TransactionCommitFailedException e) {
58 LOG.warn("Failed to delete {} ", path, e);
64 * Executes merge as a blocking transaction.
66 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
67 * @param path {@link InstanceIdentifier} for path to read
68 * @param <D> the data object type
69 * @return the result of the request
71 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean merge(
72 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
73 boolean result = false;
74 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
75 transaction.merge(logicalDatastoreType, path, data, true);
76 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
80 } catch (TransactionCommitFailedException e) {
81 LOG.warn("Failed to merge {} ", path, e);
87 * Executes put as a blocking transaction.
89 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
90 * @param path {@link InstanceIdentifier} for path to read
91 * @param <D> the data object type
92 * @return the result of the request
94 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean put(
95 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
96 boolean result = false;
97 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
98 transaction.put(logicalDatastoreType, path, data, true);
99 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
103 } catch (TransactionCommitFailedException e) {
104 LOG.warn("Failed to put {} ", path, e);
110 * Executes read as a blocking transaction.
112 * @param store {@link LogicalDatastoreType} to read
113 * @param path {@link InstanceIdentifier} for path to read
114 * @param <D> the data object type
115 * @return the result as the data object requested
117 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
118 final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
120 final ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
121 Optional<D> optionalDataObject;
122 CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
124 optionalDataObject = future.checkedGet();
125 if (optionalDataObject.isPresent()) {
126 result = optionalDataObject.get();
128 LOG.debug("{}: Failed to read {}",
129 Thread.currentThread().getStackTrace()[1], path);
131 } catch (ReadFailedException e) {
132 LOG.warn("Failed to read {} ", path, e);