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.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.binding.api.ReadTransaction;
15 import org.opendaylight.mdsal.binding.api.WriteTransaction;
16 import org.opendaylight.mdsal.common.api.CommitInfo;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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 static int MDSAL_MAX_READ_TRIALS = Integer.getInteger("mdsalutil.max.tries", 30);
26 private static int MDSAL_READ_SLEEP_INTERVAL_MS = Integer.getInteger("mdsalutil.sleep.between.mdsal.reads", 1000);
28 private final DataBroker databroker;
31 * Class constructor setting the data broker.
33 * @param dataBroker the {@link DataBroker}
35 public MdsalUtils(DataBroker dataBroker) {
36 this.databroker = dataBroker;
40 * Executes delete as a blocking transaction.
42 * @param store {@link LogicalDatastoreType} which should be modified
43 * @param path {@link InstanceIdentifier} to read from
44 * @param <D> the data object type
45 * @return the result of the request
47 public <D extends DataObject> boolean delete(
48 final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
49 boolean result = false;
50 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
51 transaction.delete(store, path);
52 FluentFuture<? extends CommitInfo> future = transaction.commit();
56 } catch (InterruptedException | ExecutionException e) {
57 LOG.warn("Failed to delete {} ", path, e);
63 * Executes merge as a blocking transaction.
65 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
66 * @param path {@link InstanceIdentifier} for path to read
67 * @param <D> the data object type
68 * @return the result of the request
70 public <D extends DataObject> boolean merge(
71 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
72 boolean result = false;
73 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
74 transaction.mergeParentStructureMerge(logicalDatastoreType, path, data);
75 FluentFuture<? extends CommitInfo> future = transaction.commit();
79 } catch (InterruptedException | ExecutionException e) {
80 LOG.warn("Failed to merge {} ", path, e);
86 * Executes put as a blocking transaction.
88 * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
89 * @param path {@link InstanceIdentifier} for path to read
90 * @param <D> the data object type
91 * @return the result of the request
93 public <D extends DataObject> boolean put(
94 final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data) {
95 boolean result = false;
96 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
97 transaction.mergeParentStructurePut(logicalDatastoreType, path, data);
98 FluentFuture<? extends CommitInfo> future = transaction.commit();
102 } catch (InterruptedException | ExecutionException e) {
103 LOG.warn("Failed to put {} ", path, e);
109 * Executes read as a blocking transaction.
111 * @param store {@link LogicalDatastoreType} to read
112 * @param path {@link InstanceIdentifier} for path to read
113 * @param <D> the data object type
114 * @return the result as the data object requested
116 public <D extends DataObject> D read(
117 final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path) {
118 Optional<D> optionalDataObject = readOptional(store, path);
119 if (optionalDataObject.isPresent()) {
120 return optionalDataObject.get();
122 LOG.debug("{}: Failed to read {}",
123 Thread.currentThread().getStackTrace()[1], path);
127 public <D extends DataObject> Optional<D> readOptional(
128 final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path) {
130 ReadTransaction transaction = databroker.newReadOnlyTransaction();
133 Optional<D> result = transaction.read(store, (InstanceIdentifier<D>)path).get();
136 } catch (InterruptedException | ExecutionException e) {
138 logReadFailureError(path, " mdsal Read failed exception retrying the read after sleep");
142 Thread.sleep(MDSAL_READ_SLEEP_INTERVAL_MS);
143 transaction = databroker.newReadOnlyTransaction();
144 } catch (InterruptedException e1) {
145 logReadFailureError(path, " Sleep interrupted");
148 } while (trialNo++ < MDSAL_MAX_READ_TRIALS);
149 logReadFailureError(path, " All read trials exceeded");
150 return Optional.empty();
154 public boolean exists(
155 final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path) {
157 ReadTransaction transaction = databroker.newReadOnlyTransaction();
160 FluentFuture<Boolean> result = transaction.exists(store, path);
162 return result.get().booleanValue();
163 } catch (InterruptedException | ExecutionException e) {
165 logReadFailureError(path, " mdsal Read failed exception retrying the read after sleep");
169 Thread.sleep(MDSAL_READ_SLEEP_INTERVAL_MS);
170 transaction = databroker.newReadOnlyTransaction();
171 } catch (InterruptedException e1) {
172 logReadFailureError(path, " Sleep interrupted");
175 } while (trialNo++ < MDSAL_MAX_READ_TRIALS);
176 logReadFailureError(path, " All read trials exceeded");
180 private <D extends DataObject> void logReadFailureError(
181 InstanceIdentifier<D> path, String cause) {
182 LOG.error("{}: Failed to read {} Cause : {}", Thread.currentThread().getStackTrace()[2], path, cause);