More reliable netconf transaction handling
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / util / GbpNetconfTransaction.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.util;
10
11 import java.util.concurrent.ExecutionException;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
17 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class GbpNetconfTransaction {
28
29     public static final byte RETRY_COUNT = 5;
30     private static final Logger LOG = LoggerFactory.getLogger(GbpNetconfTransaction.class);
31
32     /**
33      * Use {@link ConfigCommand} to put data into netconf transaction and submit. Transaction is restarted if failed
34      *
35      * @param mountpoint   to access remote device
36      * @param command      config command with data, datastore type and iid
37      * @param retryCounter number of attempts
38      * @return true if transaction is successful, false otherwise
39      */
40     public synchronized static boolean write(final DataBroker mountpoint, final ConfigCommand command,
41                                              byte retryCounter) {
42         LOG.trace("Netconf WRITE transaction started. RetryCounter: {}", retryCounter);
43         Preconditions.checkNotNull(mountpoint);
44         final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
45         try {
46             command.execute(rwTx);
47             final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
48             futureTask.get();
49             LOG.trace("Netconf WRITE transaction done. Retry counter: {}", retryCounter);
50             return true;
51         } catch (IllegalStateException e) {
52             // Retry
53             if (retryCounter > 0) {
54                 LOG.warn("Assuming that netconf write-transaction failed, restarting ...", e.getMessage());
55                 return write(mountpoint, command, --retryCounter);
56             } else {
57                 LOG.warn("Netconf write-transaction failed. Maximal number of attempts reached", e.getMessage());
58                 return false;
59             }
60         } catch (InterruptedException | ExecutionException e) {
61             LOG.warn("Exception while writing data ...", e.getMessage());
62             return false;
63         }
64     }
65
66     /**
67      * Write data to remote device. Transaction is restarted if failed
68      *
69      * @param mountpoint   to access remote device
70      * @param iid          data identifier
71      * @param data         to write
72      * @param retryCounter number of attempts
73      * @param <T>          generic data type. Has to be child of {@link DataObject}
74      * @return true if transaction is successful, false otherwise
75      */
76     public synchronized static <T extends DataObject> boolean write(final DataBroker mountpoint,
77                                                                     final InstanceIdentifier<T> iid,
78                                                                     final T data,
79                                                                     byte retryCounter) {
80         LOG.trace("Netconf WRITE transaction started. RetryCounter: {}", retryCounter);
81         Preconditions.checkNotNull(mountpoint);
82         final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
83         try {
84             rwTx.put(LogicalDatastoreType.CONFIGURATION, iid, data, true);
85             final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
86             futureTask.get();
87             LOG.trace("Netconf WRITE transaction done. Retry counter: {}", retryCounter);
88             return true;
89         } catch (IllegalStateException e) {
90             // Retry
91             if (retryCounter > 0) {
92                 LOG.warn("Assuming that netconf write-transaction failed, restarting ...", e.getMessage());
93                 return write(mountpoint, iid, data, --retryCounter);
94             } else {
95                 LOG.warn("Netconf write-transaction failed. Maximal number of attempts reached", e.getMessage());
96                 return false;
97             }
98         } catch (InterruptedException | ExecutionException e) {
99             LOG.warn("Exception while writing data ...", e.getMessage());
100             return false;
101         }
102     }
103
104     /**
105      * Read data from remote device. Transaction is restarted if failed.
106      *
107      * @param mountpoint    to access remote device
108      * @param datastoreType {@link LogicalDatastoreType}
109      * @param iid           data identifier
110      * @param retryCounter  number of attempts
111      * @param <T>           generic data type. Has to be child of {@link DataObject}
112      * @return optional data object if successful, {@link Optional#absent()} if failed
113      */
114     public synchronized static <T extends DataObject> Optional<T> read(final DataBroker mountpoint,
115                                                                        final LogicalDatastoreType datastoreType,
116                                                                        final InstanceIdentifier<T> iid,
117                                                                        byte retryCounter) {
118         LOG.trace("Netconf READ transaction started. RetryCounter: {}", retryCounter);
119         Preconditions.checkNotNull(mountpoint);
120         final ReadOnlyTransaction rTx = mountpoint.newReadOnlyTransaction();
121         Optional<T> data;
122         try {
123             final CheckedFuture<Optional<T>, ReadFailedException> futureData =
124                     rTx.read(datastoreType, iid);
125             data = futureData.get();
126             LOG.trace("Netconf READ transaction done. Data present: {}, Retry counter: {}",
127                     data.isPresent(), retryCounter);
128             return data;
129         } catch (IllegalStateException e) {
130             // Retry
131             if (retryCounter > 0) {
132                 LOG.warn("Assuming that netconf read-transaction failed, restarting ...", e.getMessage());
133                 rTx.close();
134                 return read(mountpoint, datastoreType, iid, --retryCounter);
135             } else {
136                 LOG.warn("Netconf read-transaction failed. Maximal number of attempts reached", e.getMessage());
137                 return Optional.absent();
138             }
139         } catch (InterruptedException | ExecutionException e) {
140             LOG.warn("Exception while reading data ...", e.getMessage());
141             return Optional.absent();
142         }
143     }
144
145     /**
146      * Remove data from remote device using {@link ConfigCommand}. Transaction is restarted if failed.
147      *
148      * @param mountpoint   to access remote device
149      * @param command      config command with data, datastore type and iid
150      * @param retryCounter number of attempts
151      * @return true if transaction is successful, false otherwise
152      */
153     public synchronized static boolean delete(final DataBroker mountpoint, final ConfigCommand command,
154                                               byte retryCounter) {
155         LOG.trace("Netconf DELETE transaction started. RetryCounter: {}", retryCounter);
156         Preconditions.checkNotNull(mountpoint);
157         final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
158         try {
159             command.execute(rwTx);
160             final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
161             futureTask.get();
162             LOG.trace("Netconf DELETE transaction done. Retry counter: {}", retryCounter);
163             return true;
164         } catch (IllegalStateException e) {
165             // Retry
166             if (retryCounter > 0) {
167                 LOG.warn("Assuming that netconf delete-transaction failed, restarting ...", e.getMessage());
168                 return delete(mountpoint, command, --retryCounter);
169             } else {
170                 LOG.warn("Netconf delete-transaction failed. Maximal number of attempts reached", e.getMessage());
171                 return false;
172             }
173         } catch (InterruptedException | ExecutionException e) {
174             LOG.warn("Exception while removing data ...", e.getMessage());
175             return false;
176         }
177     }
178
179     /**
180      * Remove data from remote device. Transaction is restarted if failed.
181      *
182      * @param mountpoint   to access remote device
183      * @param iid          data identifier
184      * @param retryCounter number of attempts
185      * @param <T>          generic data type. Has to be child of {@link DataObject}
186      * @return true if transaction is successful, false otherwise
187      */
188     public synchronized static <T extends DataObject> boolean delete(final DataBroker mountpoint,
189                                                                      final InstanceIdentifier<T> iid,
190                                                                      byte retryCounter) {
191         LOG.trace("Netconf DELETE transaction started. RetryCounter: {}", retryCounter);
192         Preconditions.checkNotNull(mountpoint);
193         final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
194         try {
195             rwTx.delete(LogicalDatastoreType.CONFIGURATION, iid);
196             final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
197             futureTask.get();
198             LOG.trace("Netconf DELETE transaction done. Retry counter: {}", retryCounter);
199             return true;
200         } catch (IllegalStateException e) {
201             // Retry
202             if (retryCounter > 0) {
203                 LOG.warn("Assuming that netconf delete-transaction failed, restarting ...", e.getMessage());
204                 return delete(mountpoint, iid, --retryCounter);
205             } else {
206                 LOG.warn("Netconf delete-transaction failed. Maximal number of attempts reached", e.getMessage());
207                 return false;
208             }
209         } catch (InterruptedException | ExecutionException e) {
210             LOG.warn("Exception while removing data ...", e.getMessage());
211             return false;
212         }
213     }
214
215 }