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.util.concurrent.FluentFuture;
11 import java.util.Optional;
12 import java.util.concurrent.ExecutionException;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.ReadTransaction;
16 import org.opendaylight.mdsal.binding.api.WriteTransaction;
17 import org.opendaylight.mdsal.common.api.CommitInfo;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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;
25 public class ControllerMdsalUtils {
26 private static final Logger LOG = LoggerFactory.getLogger(ControllerMdsalUtils.class);
27 private static int MDSAL_MAX_READ_TRIALS = Integer.getInteger("mdsalutil.max.tries", 30);
28 private static int MDSAL_READ_SLEEP_INTERVAL_MS = Integer.getInteger("mdsalutil.sleep.between.mdsal.reads", 1000);
30 private final DataBroker databroker;
33 * Class constructor setting the data broker.
35 * @param dataBroker the {@link org.opendaylight.mdsal.binding.api.DataBroker}
37 public ControllerMdsalUtils(DataBroker dataBroker) {
38 this.databroker = dataBroker;
42 * Executes delete as a blocking transaction.
44 * @param store {@link LogicalDatastoreType} which should be modified
45 * @param path {@link InstanceIdentifier} to read from
46 * @param <D> the data object type
47 * @return the result of the request
49 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
50 final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
51 boolean result = false;
52 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
53 transaction.delete(store, path);
54 FluentFuture<? extends @NonNull CommitInfo> future = transaction.commit();
58 } catch (InterruptedException | ExecutionException e) {
59 LOG.warn("Failed to delete {} ", path, e);
65 * Executes merge as a blocking transaction.
67 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
68 * @param path {@link InstanceIdentifier} for path to read
69 * @param <D> the data object type
70 * @return the result of the request
72 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean merge(
73 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
74 boolean result = false;
75 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
76 transaction.mergeParentStructureMerge(logicalDatastoreType, path, data);
77 FluentFuture<? extends @NonNull CommitInfo> future = transaction.commit();
81 } catch (InterruptedException | ExecutionException e) {
82 LOG.warn("Failed to merge {} ", path, e);
88 * Executes put as a blocking transaction.
90 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
91 * @param path {@link InstanceIdentifier} for path to read
92 * @param <D> the data object type
93 * @return the result of the request
95 public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean put(
96 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
97 boolean result = false;
98 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
99 transaction.mergeParentStructurePut(logicalDatastoreType, path, data);
100 FluentFuture<? extends @NonNull CommitInfo> future = transaction.commit();
104 } catch (InterruptedException | ExecutionException e) {
105 LOG.warn("Failed to put {} ", path, e);
111 * Executes read as a blocking transaction.
113 * @param store {@link LogicalDatastoreType} to read
114 * @param path {@link InstanceIdentifier} for path to read
115 * @param <D> the data object type
116 * @return the result as the data object requested
118 public <D extends DataObject> D read(
119 final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path) {
120 Optional<D> optionalDataObject = readOptional(store, path);
121 if (optionalDataObject.isPresent()) {
122 return optionalDataObject.get();
124 LOG.debug("{}: Failed to read {}",
125 Thread.currentThread().getStackTrace()[1], path);
129 public <D extends DataObject> Optional<D> readOptional(
130 final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path) {
132 ReadTransaction transaction = databroker.newReadOnlyTransaction();
135 Optional<D> result = transaction.read(store, (InstanceIdentifier<D>)path).get();
138 } catch (InterruptedException | ExecutionException e) {
140 logReadFailureError(path, " mdsal Read failed exception retrying the read after sleep");
144 Thread.sleep(MDSAL_READ_SLEEP_INTERVAL_MS);
145 transaction = databroker.newReadOnlyTransaction();
146 } catch (InterruptedException e1) {
147 logReadFailureError(path, " Sleep interrupted");
150 } while (trialNo++ < MDSAL_MAX_READ_TRIALS);
151 logReadFailureError(path, " All read trials exceeded");
152 return Optional.empty();
155 private <D extends org.opendaylight.yangtools.yang.binding.DataObject> void logReadFailureError(
156 InstanceIdentifier<D> path, String cause) {
157 LOG.error("{}: Failed to read {} Cause : {}", Thread.currentThread().getStackTrace()[2], path, cause);