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