utils: migrate to the mdsal DataBroker
[ovsdb.git] / utils / mdsal-utils / src / main / java / org / opendaylight / ovsdb / utils / mdsal / utils / ControllerMdsalUtils.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 @Deprecated
24 public class ControllerMdsalUtils {
25     private static final Logger LOG = LoggerFactory.getLogger(ControllerMdsalUtils.class);
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     private final DataBroker databroker;
30
31     /**
32      * Class constructor setting the data broker.
33      *
34      * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
35      */
36     public ControllerMdsalUtils(DataBroker dataBroker) {
37         this.databroker = dataBroker;
38     }
39
40     /**
41      * Executes delete as a blocking transaction.
42      *
43      * @param store {@link LogicalDatastoreType} which should be modified
44      * @param path {@link InstanceIdentifier} to read from
45      * @param <D> the data object type
46      * @return the result of the request
47      */
48     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
49             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
50         boolean result = false;
51         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
52         transaction.delete(store, path);
53         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
54         try {
55             future.checkedGet();
56             result = true;
57         } catch (TransactionCommitFailedException e) {
58             LOG.warn("Failed to delete {} ", path, e);
59         }
60         return result;
61     }
62
63     /**
64      * Executes merge as a blocking transaction.
65      *
66      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
67      * @param path {@link InstanceIdentifier} for path to read
68      * @param <D> the data object type
69      * @return the result of the request
70      */
71     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean merge(
72             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
73         boolean result = false;
74         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
75         transaction.merge(logicalDatastoreType, path, data, true);
76         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
77         try {
78             future.checkedGet();
79             result = true;
80         } catch (TransactionCommitFailedException e) {
81             LOG.warn("Failed to merge {} ", path, e);
82         }
83         return result;
84     }
85
86     /**
87      * Executes put as a blocking transaction.
88      *
89      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
90      * @param path {@link InstanceIdentifier} for path to read
91      * @param <D> the data object type
92      * @return the result of the request
93      */
94     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean put(
95             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
96         boolean result = false;
97         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
98         transaction.put(logicalDatastoreType, path, data, true);
99         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
100         try {
101             future.checkedGet();
102             result = true;
103         } catch (TransactionCommitFailedException e) {
104             LOG.warn("Failed to put {} ", path, e);
105         }
106         return result;
107     }
108
109     /**
110      * Executes read as a blocking transaction.
111      *
112      * @param store {@link LogicalDatastoreType} to read
113      * @param path {@link InstanceIdentifier} for path to read
114      * @param <D> the data object type
115      * @return the result as the data object requested
116      */
117     public <D extends DataObject> D read(
118             final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path) {
119         Optional<D> optionalDataObject = readOptional(store, path);
120         if (optionalDataObject.isPresent()) {
121             return optionalDataObject.get();
122         }
123         LOG.debug("{}: Failed to read {}",
124                 Thread.currentThread().getStackTrace()[1], path);
125         return null;
126     }
127
128     public <D extends DataObject> Optional<D> readOptional(
129             final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path)  {
130         int trialNo = 0;
131         ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
132         do {
133             try {
134                 Optional<D> result = transaction.read(store, (InstanceIdentifier<D>)path).checkedGet();
135                 transaction.close();
136                 return result;
137             } catch (ReadFailedException e) {
138                 if (trialNo == 0) {
139                     logReadFailureError(path, " mdsal Read failed exception retrying the read after sleep");
140                 }
141                 try {
142                     transaction.close();
143                     Thread.sleep(MDSAL_READ_SLEEP_INTERVAL_MS);
144                     transaction = databroker.newReadOnlyTransaction();
145                 } catch (InterruptedException e1) {
146                     logReadFailureError(path, " Sleep interrupted");
147                 }
148             }
149         } while (trialNo++ < MDSAL_MAX_READ_TRIALS);
150         logReadFailureError(path, " All read trials exceeded");
151         return Optional.absent();
152     }
153
154     private <D extends org.opendaylight.yangtools.yang.binding.DataObject> void logReadFailureError(
155             InstanceIdentifier<D> path, String cause) {
156         LOG.error("{}: Failed to read {} Cause : {}", Thread.currentThread().getStackTrace()[2], path, cause);
157
158     }
159 }