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