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.utils.mdsal.utils;
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;
22 public class MdsalUtils {
23 private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
24 private DataBroker databroker = null;
27 * Class constructor setting the data broker.
29 * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
31 public MdsalUtils(DataBroker dataBroker) {
32 this.databroker = dataBroker;
36 * Executes delete as a blocking transaction.
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
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();
52 } catch (TransactionCommitFailedException e) {
53 LOG.warn("Failed to delete {} ", path, e);
59 * Executes merge as a blocking transaction.
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
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();
75 } catch (TransactionCommitFailedException e) {
76 LOG.warn("Failed to merge {} ", path, e);
82 * Executes put as a blocking transaction.
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
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();
98 } catch (TransactionCommitFailedException e) {
99 LOG.warn("Failed to put {} ", path, e);
105 * Executes read as a blocking transaction.
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
112 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
113 final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
115 final ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
116 Optional<D> optionalDataObject;
117 CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
119 optionalDataObject = future.checkedGet();
120 if (optionalDataObject.isPresent()) {
121 result = optionalDataObject.get();
123 LOG.debug("{}: Failed to read {}",
124 Thread.currentThread().getStackTrace()[1], path);
126 } catch (ReadFailedException e) {
127 LOG.warn("Failed to read {} ", path, e);