Merge "Bug 6745 Improve compression queue locking and handle InterruptedException"
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorClusterDecorator.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.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
14 import org.opendaylight.openflowplugin.applications.frsync.impl.clustering.DeviceMastershipManager;
15 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
16 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Decorator for cluster related issues.
25  */
26 public class SyncReactorClusterDecorator implements SyncReactor {
27
28     private static final Logger LOG = LoggerFactory.getLogger(SyncReactorClusterDecorator.class);
29     private final SyncReactor delegate;
30     private final DeviceMastershipManager deviceMastershipManager;
31
32     public SyncReactorClusterDecorator(final SyncReactor delegate,
33                                        final DeviceMastershipManager deviceMastershipManager) {
34         this.delegate = delegate;
35         this.deviceMastershipManager = deviceMastershipManager;
36     }
37
38     @Override
39     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
40                                             final SyncupEntry syncupEntry) {
41         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
42         if (!deviceMastershipManager.isDeviceMastered(nodeId)) {
43             LOG.debug("Skip syncup since not master for: {}", nodeId.getValue());
44             return Futures.immediateFuture(Boolean.TRUE);
45         } else {
46             return delegate.syncup(flowcapableNodePath, syncupEntry);
47         }
48     }
49 }