Remove Objects.{is,non}Null abuse
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorFutureZipDecorator.java
index 5dd431aeab461cfbaa34f6f652fe956cc227744d..8f49b42a9dfb2ff21abd255115d1fd1f29b00a9c 100644 (file)
@@ -1,11 +1,10 @@
-/**
+/*
  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.applications.frsync.impl;
 
 import com.google.common.util.concurrent.Futures;
@@ -14,48 +13,52 @@ import com.google.common.util.concurrent.ListeningExecutorService;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.Semaphore;
-import javax.annotation.concurrent.GuardedBy;
+import org.opendaylight.openflowplugin.applications.frsync.SemaphoreKeeper;
 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
+import org.opendaylight.openflowplugin.applications.frsync.util.SemaphoreKeeperGuavaImpl;
 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Enriches {@link SyncReactorFutureDecorator} with state compression.
  */
 public class SyncReactorFutureZipDecorator extends SyncReactorFutureDecorator {
 
-    private static final Logger LOG = LoggerFactory.getLogger(SyncReactorFutureZipDecorator.class);
-
-    @GuardedBy("compressionGuard")
     private final Map<InstanceIdentifier<FlowCapableNode>, SyncupEntry> compressionQueue = new HashMap<>();
-    private final Semaphore compressionGuard = new Semaphore(1, false);
+    private final SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper =
+            new SemaphoreKeeperGuavaImpl<>(1, true);
 
-    public SyncReactorFutureZipDecorator(SyncReactor delegate, ListeningExecutorService executorService) {
+    public SyncReactorFutureZipDecorator(final SyncReactor delegate, final ListeningExecutorService executorService) {
         super(delegate, executorService);
     }
 
+    @Override
     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
-                                            final SyncupEntry syncupEntry) throws InterruptedException {
+                                            final SyncupEntry syncupEntry) {
+        Semaphore guard = null;
         try {
-            compressionGuard.acquire();
+            guard = semaphoreKeeper.summonGuardAndAcquire(flowcapableNodePath);
+            if (guard == null) {
+                return Futures.immediateFuture(Boolean.FALSE);
+            }
             final boolean newTaskNecessary = updateCompressionState(flowcapableNodePath, syncupEntry);
             if (newTaskNecessary) {
                 super.syncup(flowcapableNodePath, syncupEntry);
             }
-            return Futures.immediateFuture(true);
+            return Futures.immediateFuture(Boolean.TRUE);
         } finally {
-            compressionGuard.release();
+            semaphoreKeeper.releaseGuard(guard);
         }
     }
 
+    @Override
     protected ListenableFuture<Boolean> doSyncupInFuture(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
-                                                         final SyncupEntry syncupEntry) throws InterruptedException {
+                                                         final SyncupEntry syncupEntry) {
         final SyncupEntry lastCompressionState = removeLastCompressionState(flowcapableNodePath);
+
         if (lastCompressionState == null) {
-            return Futures.immediateFuture(true);
+            return Futures.immediateFuture(Boolean.TRUE);
         } else {
             return super.doSyncupInFuture(flowcapableNodePath, lastCompressionState);
         }
@@ -69,6 +72,7 @@ public class SyncReactorFutureZipDecorator extends SyncReactorFutureDecorator {
     private boolean updateCompressionState(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
                                            final SyncupEntry syncupEntry) {
         final SyncupEntry previousEntry = compressionQueue.get(flowcapableNodePath);
+
         if (previousEntry != null && syncupEntry.isOptimizedConfigDelta()) {
             updateOptimizedConfigDelta(flowcapableNodePath, syncupEntry, previousEntry);
         } else {
@@ -86,15 +90,12 @@ public class SyncReactorFutureZipDecorator extends SyncReactorFutureDecorator {
     }
 
     private SyncupEntry removeLastCompressionState(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath) {
+        Semaphore guard = null;
         try {
-            try {
-                compressionGuard.acquire();
-            } catch (InterruptedException e) {
-                return null;
-            }
-            return compressionQueue.remove(flowcapableNodePath);
+            guard = semaphoreKeeper.summonGuardAndAcquire(flowcapableNodePath);
+            return guard == null ? null : compressionQueue.remove(flowcapableNodePath);
         } finally {
-            compressionGuard.release();
+            semaphoreKeeper.releaseGuard(guard);
         }
     }
 }
\ No newline at end of file