2 * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.connection;
10 import io.netty.util.concurrent.Future;
11 import io.netty.util.concurrent.GenericFutureListener;
13 import java.util.Collections;
15 import org.opendaylight.controller.sal.common.util.Rpcs;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
18 import org.opendaylight.yangtools.yang.common.RpcResult;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.common.base.Preconditions;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import com.google.common.util.concurrent.SettableFuture;
27 * This class holds all the context we need for sending a single message down the tube.
28 * A MessageHolder (used in queue) and the actual listener. It is not a thing of beauty,
29 * but it keeps us from allocating unnecessary objects in the egress path.
31 abstract class AbstractRpcListener<T> implements GenericFutureListener<Future<Void>>, ChannelOutboundQueue.MessageHolder<Object> {
32 private static final Logger LOG = LoggerFactory.getLogger(AbstractRpcListener.class);
33 private final SettableFuture<RpcResult<T>> result = SettableFuture.create();
34 private final String failureInfo;
35 private Object message;
37 AbstractRpcListener(final Object message, final String failureInfo) {
38 this.failureInfo = Preconditions.checkNotNull(failureInfo);
39 this.message = Preconditions.checkNotNull(message);
42 public final ListenableFuture<RpcResult<T>> getResult() {
47 public final void operationComplete(final Future<Void> future) {
48 if (!future.isSuccess()) {
49 LOG.debug("operation failed");
50 failedRpc(future.cause());
52 LOG.debug("operation complete");
53 operationSuccessful();
58 public final Object takeMessage() {
59 final Object ret = message;
60 Preconditions.checkState(ret != null, "Message has already been taken");
66 public final GenericFutureListener<Future<Void>> takeListener() {
70 protected abstract void operationSuccessful();
72 protected final void failedRpc(final Throwable cause) {
73 final RpcError rpcError = ConnectionAdapterImpl.buildRpcError(
74 failureInfo, ErrorSeverity.ERROR, "check switch connection", cause);
75 result.set(Rpcs.getRpcResult(
78 Collections.singletonList(rpcError)));
81 protected final void successfulRpc(final T value) {
82 result.set(Rpcs.getRpcResult(
85 Collections.<RpcError>emptyList()));