Update MRI projects for Aluminium
[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.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;
23
24 @Deprecated
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);
29
30     private final DataBroker databroker;
31
32     /**
33      * Class constructor setting the data broker.
34      *
35      * @param dataBroker the {@link org.opendaylight.mdsal.binding.api.DataBroker}
36      */
37     public ControllerMdsalUtils(DataBroker dataBroker) {
38         this.databroker = dataBroker;
39     }
40
41     /**
42      * Executes delete as a blocking transaction.
43      *
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
48      */
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();
55         try {
56             future.get();
57             result = true;
58         } catch (InterruptedException | ExecutionException e) {
59             LOG.warn("Failed to delete {} ", path, e);
60         }
61         return result;
62     }
63
64     /**
65      * Executes merge as a blocking transaction.
66      *
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
71      */
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();
78         try {
79             future.get();
80             result = true;
81         } catch (InterruptedException | ExecutionException e) {
82             LOG.warn("Failed to merge {} ", path, e);
83         }
84         return result;
85     }
86
87     /**
88      * Executes put as a blocking transaction.
89      *
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
94      */
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();
101         try {
102             future.get();
103             result = true;
104         } catch (InterruptedException | ExecutionException e) {
105             LOG.warn("Failed to put {} ", path, e);
106         }
107         return result;
108     }
109
110     /**
111      * Executes read as a blocking transaction.
112      *
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
117      */
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();
123         }
124         LOG.debug("{}: Failed to read {}",
125                 Thread.currentThread().getStackTrace()[1], path);
126         return null;
127     }
128
129     public <D extends DataObject> Optional<D> readOptional(
130             final LogicalDatastoreType store, final InstanceIdentifier<? extends DataObject> path)  {
131         int trialNo = 0;
132         ReadTransaction transaction = databroker.newReadOnlyTransaction();
133         do {
134             try {
135                 Optional<D> result = transaction.read(store, (InstanceIdentifier<D>)path).get();
136                 transaction.close();
137                 return result;
138             } catch (InterruptedException | ExecutionException e) {
139                 if (trialNo == 0) {
140                     logReadFailureError(path, " mdsal Read failed exception retrying the read after sleep");
141                 }
142                 try {
143                     transaction.close();
144                     Thread.sleep(MDSAL_READ_SLEEP_INTERVAL_MS);
145                     transaction = databroker.newReadOnlyTransaction();
146                 } catch (InterruptedException e1) {
147                     logReadFailureError(path, " Sleep interrupted");
148                 }
149             }
150         } while (trialNo++ < MDSAL_MAX_READ_TRIALS);
151         logReadFailureError(path, " All read trials exceeded");
152         return Optional.empty();
153     }
154
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);
158
159     }
160 }