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
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
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.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class MdsalUtils {
24 private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
25 private DataBroker databroker = null;
28 * Class constructor setting the data broker.
30 * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
32 public MdsalUtils(DataBroker dataBroker) {
33 this.databroker = dataBroker;
37 * Executes delete as a blocking transaction.
39 * @param store {@link LogicalDatastoreType} which should be modified
40 * @param path {@link InstanceIdentifier} to read from
41 * @param <D> the data object type
42 * @return the result of the request
44 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
45 final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
46 boolean result = false;
47 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
48 transaction.delete(store, path);
49 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
53 } catch (TransactionCommitFailedException e) {
54 LOG.warn("Failed to delete {} ", path, e);
60 * Executes merge as a blocking transaction.
62 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
63 * @param path {@link InstanceIdentifier} for path to read
64 * @param data object of type D
65 * @return the result of the request
67 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean merge(
68 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
69 boolean result = false;
70 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
71 transaction.merge(logicalDatastoreType, path, data, true);
72 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
76 } catch (TransactionCommitFailedException e) {
77 LOG.warn("Failed to merge {} ", path, e);
83 * Executes put as a blocking transaction.
85 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
86 * @param path {@link InstanceIdentifier} for path to read
87 * @param data object of type D
88 * @return the result of the request
90 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean put(
91 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
92 boolean result = false;
93 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
94 transaction.put(logicalDatastoreType, path, data, true);
95 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
99 } catch (TransactionCommitFailedException e) {
100 LOG.warn("Failed to put {} ", path, e);
106 * Executes read as a blocking transaction.
108 * @param store {@link LogicalDatastoreType} to read
109 * @param path {@link InstanceIdentifier} for path to read
110 * @param <D> the data object type
111 * @return the result as the data object requested
113 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
114 final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
116 final ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
117 Optional<D> optionalDataObject;
118 CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
120 optionalDataObject = future.checkedGet();
121 if (optionalDataObject.isPresent()) {
122 result = optionalDataObject.get();
124 LOG.debug("{}: Failed to read {}",
125 Thread.currentThread().getStackTrace()[1], path);
127 } catch (ReadFailedException e) {
128 LOG.warn("Failed to read {} ", path, e);