bug 6578 added mdsal read retry
[ovsdb.git] / utils / mdsal-utils / src / main / java / org / opendaylight / ovsdb / utils / mdsal / utils / MdsalUtils.java
1 /*
2  * Copyright (c) 2015 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.utils.mdsal.utils;
9
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;
22
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);
28     /**
29      * Class constructor setting the data broker.
30      *
31      * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
32      */
33     public MdsalUtils(DataBroker dataBroker) {
34         this.databroker = dataBroker;
35     }
36
37     /**
38      * Executes delete as a blocking transaction.
39      *
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
44      */
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();
51         try {
52             future.checkedGet();
53             result = true;
54         } catch (TransactionCommitFailedException e) {
55             LOG.warn("Failed to delete {} ", path, e);
56         }
57         return result;
58     }
59
60     /**
61      * Executes merge as a blocking transaction.
62      *
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
67      */
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();
74         try {
75             future.checkedGet();
76             result = true;
77         } catch (TransactionCommitFailedException e) {
78             LOG.warn("Failed to merge {} ", path, e);
79         }
80         return result;
81     }
82
83     /**
84      * Executes put as a blocking transaction.
85      *
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
90      */
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();
97         try {
98             future.checkedGet();
99             result = true;
100         } catch (TransactionCommitFailedException e) {
101             LOG.warn("Failed to put {} ", path, e);
102         }
103         return result;
104     }
105
106     /**
107      * Executes read as a blocking transaction.
108      *
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
113      */
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();
119         }
120         LOG.debug("{}: Failed to read {}",
121                 Thread.currentThread().getStackTrace()[1], path);
122         return null;
123     }
124
125     public <D extends DataObject> Optional<D> readOptional(
126             final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path)  {
127         int trialNo = 0;
128         ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
129         do {
130             try {
131                 Optional<D> result = transaction.read(store, (InstanceIdentifier<D>)path).checkedGet();
132                 transaction.close();
133                 return result;
134             } catch (ReadFailedException e) {
135                 if (trialNo == 0) {
136                     logReadFailureError(path, " mdsal Read failed exception retrying the read after sleep");
137                 }
138                 try {
139                     transaction.close();
140                     Thread.sleep(MDSAL_READ_SLEEP_INTERVAL_MS);
141                     transaction = databroker.newReadOnlyTransaction();
142                 } catch (InterruptedException e1) {
143                     logReadFailureError(path, " Sleep interrupted");
144                 }
145             }
146         } while (trialNo++ < MDSAL_MAX_READ_TRIALS);
147         logReadFailureError(path, " All read trials exceeded");
148         return Optional.absent();
149     }
150
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);
154
155     }
156 }