Remove Objects.{is,non}Null abuse
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorGuardDecorator.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 package org.opendaylight.openflowplugin.applications.frsync.impl;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.concurrent.Semaphore;
15 import java.util.concurrent.TimeUnit;
16 import javax.annotation.Nullable;
17 import org.opendaylight.openflowplugin.applications.frsync.SemaphoreKeeper;
18 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
19 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
20 import org.opendaylight.openflowplugin.applications.frsync.util.SemaphoreKeeperGuavaImpl;
21 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Decorator for NodeId level syncup locking.
30  */
31 public class SyncReactorGuardDecorator implements SyncReactor {
32
33     private static final Logger LOG = LoggerFactory.getLogger(SyncReactorGuardDecorator.class);
34     private final SyncReactor delegate;
35     private final SemaphoreKeeper<InstanceIdentifier<FlowCapableNode>> semaphoreKeeper =
36             new SemaphoreKeeperGuavaImpl<>(1, true);
37
38     public SyncReactorGuardDecorator(final SyncReactor delegate) {
39         this.delegate = delegate;
40     }
41
42     @Override
43     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
44                                             final SyncupEntry syncupEntry) {
45         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
46         final long stampBeforeGuard = System.nanoTime();
47         final Semaphore guard = semaphoreKeeper.summonGuardAndAcquire(flowcapableNodePath);
48         if (guard == null) {
49             return Futures.immediateFuture(Boolean.FALSE);
50         }
51         final long stampAfterGuard = System.nanoTime();
52
53         if (LOG.isDebugEnabled()) {
54             LOG.debug("Syncup guard acquired and running for {} ", nodeId.getValue());
55         }
56         final ListenableFuture<Boolean> endResult = delegate.syncup(flowcapableNodePath, syncupEntry);
57         Futures.addCallback(endResult, createSyncupCallback(guard, stampBeforeGuard, stampAfterGuard, nodeId),
58                 MoreExecutors.directExecutor());
59         return endResult;
60     }
61
62     private FutureCallback<Boolean> createSyncupCallback(final Semaphore guard,
63                                                          final long stampBeforeGuard,
64                                                          final long stampAfterGuard,
65                                                          final NodeId nodeId) {
66         return new FutureCallback<Boolean>() {
67             @Override
68             public void onSuccess(@Nullable final Boolean result) {
69                 if (LOG.isDebugEnabled()) {
70                     final long stampFinished = System.nanoTime();
71                     LOG.debug("Syncup finished {} took:{} rpc:{} wait:{}", nodeId.getValue(),
72                             formatNanos(stampFinished - stampBeforeGuard), formatNanos(stampFinished - stampAfterGuard),
73                             formatNanos(stampAfterGuard - stampBeforeGuard));
74                 }
75                 semaphoreKeeper.releaseGuard(guard);
76             }
77
78             @Override
79             public void onFailure(final Throwable failure) {
80                 final long stampFinished = System.nanoTime();
81                 LOG.warn("Syncup failed {} took:{} rpc:{} wait:{}", nodeId.getValue(),
82                         formatNanos(stampFinished - stampBeforeGuard), formatNanos(stampFinished - stampAfterGuard),
83                         formatNanos(stampAfterGuard - stampBeforeGuard));
84                 semaphoreKeeper.releaseGuard(guard);
85             }
86         };
87     }
88
89     private static String formatNanos(final long nanos) {
90         return "'" + TimeUnit.NANOSECONDS.toMillis(nanos) + " ms'";
91     }
92 }