2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.core.sal;
10 import java.util.concurrent.Future;
11 import java.util.concurrent.TimeUnit;
13 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
14 import org.opendaylight.controller.sal.common.util.Rpcs;
15 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
16 import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService;
17 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
21 import com.google.common.base.Objects;
22 import com.google.common.util.concurrent.FutureCallback;
23 import com.google.common.util.concurrent.Futures;
24 import com.google.common.util.concurrent.JdkFutureAdapters;
25 import com.google.common.util.concurrent.SettableFuture;
30 public class OFRpcTaskHelper {
32 private IMessageDispatchService messageService;
33 private SessionContext session;
34 private NotificationProviderService rpcNotificationProviderService;
37 * @param messageService
39 * @param rpcNotificationProviderService
41 public OFRpcTaskHelper(IMessageDispatchService messageService, SessionContext session,
42 NotificationProviderService rpcNotificationProviderService) {
43 this.messageService = messageService;
44 this.session = session;
45 this.rpcNotificationProviderService = rpcNotificationProviderService;
55 public <T, K> OFRpcTask<T, K> initTask(OFRpcTask<T, K> task, T input, SwitchConnectionDistinguisher cookie) {
56 task.setMessageService(messageService);
57 task.setSession(session);
58 task.setRpcNotificationProviderService(rpcNotificationProviderService);
59 task.setResult(SettableFuture.<K>create());
60 task.setCookie(cookie);
69 public static <K> void chainFutures(final Future<K> intern, final SettableFuture<K> wrapper) {
71 JdkFutureAdapters.listenInPoolThread(intern),
72 new FutureCallback<K>() {
75 public void onSuccess(
81 public void onFailure(Throwable t) {
82 wrapper.setException(t);
90 * @param maxTimeoutUnit
95 public <T> void rawBarrierSend(final long maxTimeout, final TimeUnit maxTimeoutUnit,
96 Boolean isBarrier, SwitchConnectionDistinguisher cookie, SettableFuture<RpcResult<T>> result) {
97 if (Objects.firstNonNull(isBarrier, Boolean.FALSE)) {
98 Future<RpcResult<BarrierOutput>> barrierFuture = ModelDrivenSwitchImpl.sendBarrier(cookie, session, messageService);
100 RpcResult<BarrierOutput> barrierResult = barrierFuture.get(maxTimeout, maxTimeoutUnit);
101 if (!barrierResult.isSuccessful()) {
102 result.set(Rpcs.<T>getRpcResult(false, barrierResult.getErrors()));
104 } catch (Exception e) {
105 result.setException(e);