2 * Copyright (c) 2015 Inocybe 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.controller.packetcable.provider;
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.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class MdsalUtils {
25 private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
26 private DataBroker databroker = null;
29 * Class constructor setting the data broker.
31 * @param dataBroker the {@link DataBroker}
33 public MdsalUtils(DataBroker dataBroker) {
34 this.databroker = dataBroker;
38 * Executes delete as a blocking transaction.
40 * @param store {@link LogicalDatastoreType} which should be modified
41 * @param path {@link InstanceIdentifier} to read from
42 * @param <D> the data object type
43 * @return the result of the request
45 public <D extends DataObject> boolean delete(
46 final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
47 boolean result = false;
48 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
49 transaction.delete(store, path);
50 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
54 } catch (TransactionCommitFailedException e) {
55 LOG.warn("Failed to delete {} ", path, e);
61 * Executes merge as a blocking transaction.
63 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
64 * @param path {@link InstanceIdentifier} for path to read
65 * @param <D> the data object type
66 * @return the result of the request
68 public <D extends DataObject> boolean merge(
69 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
70 boolean result = false;
71 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
72 transaction.merge(logicalDatastoreType, path, data, true);
73 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
77 } catch (TransactionCommitFailedException e) {
78 LOG.warn("Failed to merge {} ", path, e);
84 * Execures a read as a blocking transaction.
86 * @param logicalDatastoreType which datastore to read from
87 * @param path The path to read
88 * @param <D> the DataObject type
89 * @return an Optional containing the object.
91 public <D extends DataObject> Optional<D> read(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path) {
92 final ReadOnlyTransaction readOnlyTransaction = databroker.newReadOnlyTransaction();
94 CheckedFuture<Optional<D>, ReadFailedException> future = readOnlyTransaction.read(logicalDatastoreType, path);
97 return future.checkedGet();
98 } catch (ReadFailedException e) {
99 LOG.error("Failed to read {} at path {}", logicalDatastoreType, path, e);
102 return Optional.absent();
106 * Executes put as a blocking transaction.
108 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
109 * @param path {@link InstanceIdentifier} for path to read
110 * @param <D> the data object type
111 * @return the result of the request
113 public <D extends DataObject> boolean put(
114 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
115 boolean result = false;
116 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
117 transaction.put(logicalDatastoreType, path, data, true);
118 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
122 } catch (TransactionCommitFailedException e) {
123 LOG.warn("Failed to merge {} ", path, e);