Add implementation for flat L3 overlay
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / util / GbpNetconfTransaction.java
index b054a000dd29dce5d5e0782379083fa73d477725..ca2922d681e6b8527c5ba79599842f8f1b8d480d 100644 (file)
@@ -53,6 +53,23 @@ public class GbpNetconfTransaction {
         return result;
     }
 
+    /***
+     * Netconf wrapper for merge operation on a Netconf Device
+     * @param mountpoint    netconf device
+     * @param iid           path for Data to be merged to
+     * @param data          data to be merged
+     * @param retryCounter  retry counter, will repeat the operation for specified amount of times if transaction fails
+     * @param <T>           data type
+     * @return true if transaction is successful, false otherwise
+     */
+    public static <T extends DataObject> boolean netconfSyncedMerge(@Nonnull final DataBroker mountpoint,
+                                                                    @Nonnull final InstanceIdentifier<T> iid, @Nonnull final T data, byte retryCounter) {
+        VbdNetconfTransaction.REENTRANT_LOCK.lock();
+        boolean result = merge(mountpoint, iid, data, retryCounter);
+        VbdNetconfTransaction.REENTRANT_LOCK.unlock();
+        return result;
+    }
+
     /***
      * Netconf wrapper method for synced requests for write operation on a Netconf Device
      * @param mountpoint    netconf device
@@ -193,6 +210,39 @@ public class GbpNetconfTransaction {
         }
     }
 
+    /**
+     * Merge data to remote device. Transaction is restarted if failed
+     *
+     * @param mountpoint   to access remote device
+     * @param iid          data identifier
+     * @param data         to merge
+     * @param retryCounter number of attempts
+     * @param <T>          generic data type. Has to be child of {@link DataObject}
+     * @return true if transaction is successful, false otherwise
+     */
+    private static <T extends DataObject> boolean merge(final DataBroker mountpoint, final InstanceIdentifier<T> iid,
+                                                        final T data, byte retryCounter) {
+        LOG.trace("Netconf MERGE transaction started. RetryCounter: {}", retryCounter);
+        Preconditions.checkNotNull(mountpoint);
+        final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
+        try {
+            rwTx.merge(LogicalDatastoreType.CONFIGURATION, iid, data, true);
+            final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
+            futureTask.get();
+            LOG.trace("Netconf MERGE transaction done for {}", iid);
+            return true;
+        } catch (Exception e) {
+            // Retry
+            if (retryCounter > 0) {
+                LOG.warn("Netconf MERGE transaction failed to {}. Restarting transaction ... ", e.getMessage());
+                return write(mountpoint, iid, data, --retryCounter);
+            } else {
+                LOG.warn("Netconf MERGE transaction unsuccessful. Maximal number of attempts reached. Trace: {}", e);
+                return false;
+            }
+        }
+    }
+
     /**
      * Use {@link AbstractLispCommand} to put data into netconf transaction and submit. Transaction is restarted if failed
      *