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.DataObject;
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;
26 private static int MDSAL_MAX_READ_TRIALS = Integer.getInteger("mdsalutil.max.tries", 30);
27 private static int MDSAL_READ_SLEEP_INTERVAL_MS = Integer.getInteger("mdsalutil.sleep.between.mdsal.reads", 1000);
29 * Class constructor setting the data broker.
31 * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.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 org.opendaylight.yangtools.yang.binding.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 org.opendaylight.yangtools.yang.binding.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 * Executes put as a blocking transaction.
86 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
87 * @param path {@link InstanceIdentifier} for path to read
88 * @param <D> the data object type
89 * @return the result of the request
91 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean put(
92 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
93 boolean result = false;
94 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
95 transaction.put(logicalDatastoreType, path, data, true);
96 CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
100 } catch (TransactionCommitFailedException e) {
101 LOG.warn("Failed to put {} ", path, e);
107 * Executes read as a blocking transaction.
109 * @param store {@link LogicalDatastoreType} to read
110 * @param path {@link InstanceIdentifier} for path to read
111 * @param <D> the data object type
112 * @return the result as the data object requested
114 public <D extends DataObject> D read(
115 final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path) {
116 Optional<D> optionalDataObject = readOptional(store, path);
117 if (optionalDataObject.isPresent()) {
118 return optionalDataObject.get();
120 LOG.debug("{}: Failed to read {}",
121 Thread.currentThread().getStackTrace()[1], path);
125 public <D extends DataObject> Optional<D> readOptional(
126 final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path) {
128 ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
131 Optional<D> result = transaction.read(store, (InstanceIdentifier<D>)path).checkedGet();
134 } catch (ReadFailedException e) {
136 logReadFailureError(path, " mdsal Read failed exception retrying the read after sleep");
140 Thread.sleep(MDSAL_READ_SLEEP_INTERVAL_MS);
141 transaction = databroker.newReadOnlyTransaction();
142 } catch (InterruptedException e1) {
143 logReadFailureError(path, " Sleep interrupted");
146 } while (trialNo++ < MDSAL_MAX_READ_TRIALS);
147 logReadFailureError(path, " All read trials exceeded");
148 return Optional.absent();
151 private <D extends org.opendaylight.yangtools.yang.binding.DataObject> void logReadFailureError(
152 InstanceIdentifier<D> path, String cause) {
153 LOG.error("{}: Failed to read {} Cause : {}", Thread.currentThread().getStackTrace()[2], path, cause);