Ditch blueprint from frm-sync
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorFutureDecorator.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.concurrent.Executor;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
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.SyncupEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Decorator for running delegate syncup in Future.
27  */
28 public class SyncReactorFutureDecorator implements SyncReactor {
29     private static final Logger LOG = LoggerFactory.getLogger(SyncReactorFutureDecorator.class);
30
31     private final SyncReactor delegate;
32     private final Executor executor;
33
34     public SyncReactorFutureDecorator(final SyncReactor delegate, final Executor executor) {
35         this.delegate = requireNonNull(delegate);
36         this.executor = requireNonNull(executor);
37     }
38
39     @Override
40     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
41             final SyncupEntry syncupEntry) {
42         final var nodeId = PathUtil.digNodeId(flowcapableNodePath);
43         return Futures.submit(() -> {
44             try {
45                 return doSyncupInFuture(flowcapableNodePath, syncupEntry).get(10000, TimeUnit.MILLISECONDS);
46             } catch (TimeoutException e) {
47                 LOG.warn("Syncup future timeout occured {}", nodeId.getValue());
48                 return Boolean.FALSE;
49             }
50         }, executor);
51     }
52
53     protected ListenableFuture<Boolean> doSyncupInFuture(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
54             final SyncupEntry syncupEntry) {
55         return delegate.syncup(flowcapableNodePath, syncupEntry);
56     }
57 }